| 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 |
return delegateNoResult(ctx, delegatePlugin, netconf, exec, "CHECK") |
| 55 |
} |
| 56 |
|
| 57 |
func delegateNoResult(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec, verb string) error { |
| 58 |
pluginPath, realExec, err := delegateCommon(delegatePlugin, exec) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
return ExecPluginWithoutResult(ctx, pluginPath, netconf, delegateArgs(verb), realExec) |
| 64 |
} |
| 65 |
|
| 66 |
// DelegateDel calls the given delegate plugin with the CNI DEL action and |
| 67 |
// JSON configuration |
| 68 |
func DelegateDel(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error { |
| 69 |
return delegateNoResult(ctx, delegatePlugin, netconf, exec, "DEL") |
| 70 |
} |
| 71 |
|
| 72 |
// DelegateStatus calls the given delegate plugin with the CNI STATUS action and |
| 73 |
// JSON configuration |
| 74 |
func DelegateStatus(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error { |
| 75 |
return delegateNoResult(ctx, delegatePlugin, netconf, exec, "STATUS") |
| 76 |
} |
| 77 |
|
| 78 |
// DelegateGC calls the given delegate plugin with the CNI GC action and |
| 79 |
// JSON configuration |
| 80 |
func DelegateGC(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error { |
| 81 |
return delegateNoResult(ctx, delegatePlugin, netconf, exec, "GC") |
| 82 |
} |
| 83 |
|
| 84 |
// return CNIArgs used by delegation |
| 85 |
func delegateArgs(action string) *DelegateArgs { |
| 86 |
return &DelegateArgs{ |
| 87 |
Command: action, |
| 88 |
} |
| 89 |
} |