github.com/baraj55/containernetworking-cni@v0.7.2-0.20200219164625-56ace59a9e7f/pkg/invoke/delegate.go (about)

     1  // Copyright 2016 CNI authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package invoke
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/containernetworking/cni/pkg/types"
    23  )
    24  
    25  func delegateCommon(delegatePlugin string, exec Exec) (string, Exec, error) {
    26  	if exec == nil {
    27  		exec = defaultExec
    28  	}
    29  
    30  	paths := filepath.SplitList(os.Getenv("CNI_PATH"))
    31  	pluginPath, err := exec.FindInPath(delegatePlugin, paths)
    32  	if err != nil {
    33  		return "", nil, err
    34  	}
    35  
    36  	return pluginPath, exec, nil
    37  }
    38  
    39  // DelegateAdd calls the given delegate plugin with the CNI ADD action and
    40  // JSON configuration
    41  func DelegateAdd(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) (types.Result, error) {
    42  	pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	// DelegateAdd will override the original "CNI_COMMAND" env from process with ADD
    48  	return ExecPluginWithResult(ctx, pluginPath, netconf, delegateArgs("ADD"), realExec)
    49  }
    50  
    51  // DelegateCheck calls the given delegate plugin with the CNI CHECK action and
    52  // JSON configuration
    53  func DelegateCheck(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error {
    54  	pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	// DelegateCheck will override the original CNI_COMMAND env from process with CHECK
    60  	return ExecPluginWithoutResult(ctx, pluginPath, netconf, delegateArgs("CHECK"), realExec)
    61  }
    62  
    63  // DelegateDel calls the given delegate plugin with the CNI DEL action and
    64  // JSON configuration
    65  func DelegateDel(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error {
    66  	pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	// DelegateDel will override the original CNI_COMMAND env from process with DEL
    72  	return ExecPluginWithoutResult(ctx, pluginPath, netconf, delegateArgs("DEL"), realExec)
    73  }
    74  
    75  // return CNIArgs used by delegation
    76  func delegateArgs(action string) *DelegateArgs {
    77  	return &DelegateArgs{
    78  		Command: action,
    79  	}
    80  }