exec.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2015 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. "fmt"
  18. "os"
  19. "github.com/containernetworking/cni/pkg/types"
  20. "github.com/containernetworking/cni/pkg/types/create"
  21. "github.com/containernetworking/cni/pkg/version"
  22. )
  23. // Exec is an interface encapsulates all operations that deal with finding
  24. // and executing a CNI plugin. Tests may provide a fake implementation
  25. // to avoid writing fake plugins to temporary directories during the test.
  26. type Exec interface {
  27. ExecPlugin(ctx context.Context, pluginPath string, stdinData []byte, environ []string) ([]byte, error)
  28. FindInPath(plugin string, paths []string) (string, error)
  29. Decode(jsonBytes []byte) (version.PluginInfo, error)
  30. }
  31. // For example, a testcase could pass an instance of the following fakeExec
  32. // object to ExecPluginWithResult() to verify the incoming stdin and environment
  33. // and provide a tailored response:
  34. //
  35. //import (
  36. // "encoding/json"
  37. // "path"
  38. // "strings"
  39. //)
  40. //
  41. //type fakeExec struct {
  42. // version.PluginDecoder
  43. //}
  44. //
  45. //func (f *fakeExec) ExecPlugin(pluginPath string, stdinData []byte, environ []string) ([]byte, error) {
  46. // net := &types.NetConf{}
  47. // err := json.Unmarshal(stdinData, net)
  48. // if err != nil {
  49. // return nil, fmt.Errorf("failed to unmarshal configuration: %v", err)
  50. // }
  51. // pluginName := path.Base(pluginPath)
  52. // if pluginName != net.Type {
  53. // return nil, fmt.Errorf("plugin name %q did not match config type %q", pluginName, net.Type)
  54. // }
  55. // for _, e := range environ {
  56. // // Check environment for forced failure request
  57. // parts := strings.Split(e, "=")
  58. // if len(parts) > 0 && parts[0] == "FAIL" {
  59. // return nil, fmt.Errorf("failed to execute plugin %s", pluginName)
  60. // }
  61. // }
  62. // return []byte("{\"CNIVersion\":\"0.4.0\"}"), nil
  63. //}
  64. //
  65. //func (f *fakeExec) FindInPath(plugin string, paths []string) (string, error) {
  66. // if len(paths) > 0 {
  67. // return path.Join(paths[0], plugin), nil
  68. // }
  69. // return "", fmt.Errorf("failed to find plugin %s in paths %v", plugin, paths)
  70. //}
  71. func ExecPluginWithResult(ctx context.Context, pluginPath string, netconf []byte, args CNIArgs, exec Exec) (types.Result, error) {
  72. if exec == nil {
  73. exec = defaultExec
  74. }
  75. stdoutBytes, err := exec.ExecPlugin(ctx, pluginPath, netconf, args.AsEnv())
  76. if err != nil {
  77. return nil, err
  78. }
  79. return create.CreateFromBytes(stdoutBytes)
  80. }
  81. func ExecPluginWithoutResult(ctx context.Context, pluginPath string, netconf []byte, args CNIArgs, exec Exec) error {
  82. if exec == nil {
  83. exec = defaultExec
  84. }
  85. _, err := exec.ExecPlugin(ctx, pluginPath, netconf, args.AsEnv())
  86. return err
  87. }
  88. // GetVersionInfo returns the version information available about the plugin.
  89. // For recent-enough plugins, it uses the information returned by the VERSION
  90. // command. For older plugins which do not recognize that command, it reports
  91. // version 0.1.0
  92. func GetVersionInfo(ctx context.Context, pluginPath string, exec Exec) (version.PluginInfo, error) {
  93. if exec == nil {
  94. exec = defaultExec
  95. }
  96. args := &Args{
  97. Command: "VERSION",
  98. // set fake values required by plugins built against an older version of skel
  99. NetNS: "dummy",
  100. IfName: "dummy",
  101. Path: "dummy",
  102. }
  103. stdin := []byte(fmt.Sprintf(`{"cniVersion":%q}`, version.Current()))
  104. stdoutBytes, err := exec.ExecPlugin(ctx, pluginPath, stdin, args.AsEnv())
  105. if err != nil {
  106. if err.Error() == "unknown CNI_COMMAND: VERSION" {
  107. return version.PluginSupports("0.1.0"), nil
  108. }
  109. return nil, err
  110. }
  111. return exec.Decode(stdoutBytes)
  112. }
  113. // DefaultExec is an object that implements the Exec interface which looks
  114. // for and executes plugins from disk.
  115. type DefaultExec struct {
  116. *RawExec
  117. version.PluginDecoder
  118. }
  119. // DefaultExec implements the Exec interface
  120. var _ Exec = &DefaultExec{}
  121. var defaultExec = &DefaultExec{
  122. RawExec: &RawExec{Stderr: os.Stderr},
  123. }