delegate.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. package invoke
  15. import (
  16. "context"
  17. "os"
  18. "path/filepath"
  19. "github.com/containernetworking/cni/pkg/types"
  20. )
  21. func delegateCommon(delegatePlugin string, exec Exec) (string, Exec, error) {
  22. if exec == nil {
  23. exec = defaultExec
  24. }
  25. paths := filepath.SplitList(os.Getenv("CNI_PATH"))
  26. pluginPath, err := exec.FindInPath(delegatePlugin, paths)
  27. if err != nil {
  28. return "", nil, err
  29. }
  30. return pluginPath, exec, nil
  31. }
  32. // DelegateAdd calls the given delegate plugin with the CNI ADD action and
  33. // JSON configuration
  34. func DelegateAdd(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) (types.Result, error) {
  35. pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
  36. if err != nil {
  37. return nil, err
  38. }
  39. // DelegateAdd will override the original "CNI_COMMAND" env from process with ADD
  40. return ExecPluginWithResult(ctx, pluginPath, netconf, delegateArgs("ADD"), realExec)
  41. }
  42. // DelegateCheck calls the given delegate plugin with the CNI CHECK action and
  43. // JSON configuration
  44. func DelegateCheck(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error {
  45. pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
  46. if err != nil {
  47. return err
  48. }
  49. // DelegateCheck will override the original CNI_COMMAND env from process with CHECK
  50. return ExecPluginWithoutResult(ctx, pluginPath, netconf, delegateArgs("CHECK"), realExec)
  51. }
  52. // DelegateDel calls the given delegate plugin with the CNI DEL action and
  53. // JSON configuration
  54. func DelegateDel(ctx context.Context, delegatePlugin string, netconf []byte, exec Exec) error {
  55. pluginPath, realExec, err := delegateCommon(delegatePlugin, exec)
  56. if err != nil {
  57. return err
  58. }
  59. // DelegateDel will override the original CNI_COMMAND env from process with DEL
  60. return ExecPluginWithoutResult(ctx, pluginPath, netconf, delegateArgs("DEL"), realExec)
  61. }
  62. // return CNIArgs used by delegation
  63. func delegateArgs(action string) *DelegateArgs {
  64. return &DelegateArgs{
  65. Command: action,
  66. }
  67. }