github.com/mccv1r0/cni@v0.7.0-alpha1/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  	"fmt"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/containernetworking/cni/pkg/types"
    23  )
    24  
    25  func delegateAddOrGet(command, delegatePlugin string, netconf []byte, exec Exec) (types.Result, 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 ExecPluginWithResult(pluginPath, netconf, ArgsFromEnv(), exec)
    37  }
    38  
    39  // DelegateAdd calls the given delegate plugin with the CNI ADD action and
    40  // JSON configuration
    41  func DelegateAdd(delegatePlugin string, netconf []byte, exec Exec) (types.Result, error) {
    42  	if os.Getenv("CNI_COMMAND") != "ADD" {
    43  		return nil, fmt.Errorf("CNI_COMMAND is not ADD")
    44  	}
    45  	return delegateAddOrGet("ADD", delegatePlugin, netconf, exec)
    46  }
    47  
    48  // DelegateGet calls the given delegate plugin with the CNI GET action and
    49  // JSON configuration
    50  func DelegateGet(delegatePlugin string, netconf []byte, exec Exec) (types.Result, error) {
    51  	if os.Getenv("CNI_COMMAND") != "GET" {
    52  		return nil, fmt.Errorf("CNI_COMMAND is not GET")
    53  	}
    54  	return delegateAddOrGet("GET", delegatePlugin, netconf, exec)
    55  }
    56  
    57  // DelegateDel calls the given delegate plugin with the CNI DEL action and
    58  // JSON configuration
    59  func DelegateDel(delegatePlugin string, netconf []byte, exec Exec) error {
    60  	if exec == nil {
    61  		exec = defaultExec
    62  	}
    63  
    64  	if os.Getenv("CNI_COMMAND") != "DEL" {
    65  		return fmt.Errorf("CNI_COMMAND is not DEL")
    66  	}
    67  
    68  	paths := filepath.SplitList(os.Getenv("CNI_PATH"))
    69  	pluginPath, err := exec.FindInPath(delegatePlugin, paths)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	return ExecPluginWithoutResult(pluginPath, netconf, ArgsFromEnv(), exec)
    75  }