exec.go 4.4 KB

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