exec.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "fmt"
  17. "os"
  18. "github.com/containernetworking/cni/pkg/types"
  19. "github.com/containernetworking/cni/pkg/version"
  20. )
  21. func ExecPluginWithResult(pluginPath string, netconf []byte, args CNIArgs) (types.Result, error) {
  22. return defaultPluginExec.WithResult(pluginPath, netconf, args)
  23. }
  24. func ExecPluginWithoutResult(pluginPath string, netconf []byte, args CNIArgs) error {
  25. return defaultPluginExec.WithoutResult(pluginPath, netconf, args)
  26. }
  27. func GetVersionInfo(pluginPath string) (version.PluginInfo, error) {
  28. return defaultPluginExec.GetVersionInfo(pluginPath)
  29. }
  30. var defaultPluginExec = &PluginExec{
  31. RawExec: &RawExec{Stderr: os.Stderr},
  32. VersionDecoder: &version.PluginDecoder{},
  33. }
  34. type PluginExec struct {
  35. RawExec interface {
  36. ExecPlugin(pluginPath string, stdinData []byte, environ []string) ([]byte, error)
  37. }
  38. VersionDecoder interface {
  39. Decode(jsonBytes []byte) (version.PluginInfo, error)
  40. }
  41. }
  42. func (e *PluginExec) WithResult(pluginPath string, netconf []byte, args CNIArgs) (types.Result, error) {
  43. stdoutBytes, err := e.RawExec.ExecPlugin(pluginPath, netconf, args.AsEnv())
  44. if err != nil {
  45. return nil, err
  46. }
  47. // Plugin must return result in same version as specified in netconf
  48. versionDecoder := &version.ConfigDecoder{}
  49. confVersion, err := versionDecoder.Decode(netconf)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return version.NewResult(confVersion, stdoutBytes)
  54. }
  55. func (e *PluginExec) WithoutResult(pluginPath string, netconf []byte, args CNIArgs) error {
  56. _, err := e.RawExec.ExecPlugin(pluginPath, netconf, args.AsEnv())
  57. return err
  58. }
  59. // GetVersionInfo returns the version information available about the plugin.
  60. // For recent-enough plugins, it uses the information returned by the VERSION
  61. // command. For older plugins which do not recognize that command, it reports
  62. // version 0.1.0
  63. func (e *PluginExec) GetVersionInfo(pluginPath string) (version.PluginInfo, error) {
  64. args := &Args{
  65. Command: "VERSION",
  66. // set fake values required by plugins built against an older version of skel
  67. NetNS: "dummy",
  68. IfName: "dummy",
  69. Path: "dummy",
  70. }
  71. stdin := []byte(fmt.Sprintf(`{"cniVersion":%q}`, version.Current()))
  72. stdoutBytes, err := e.RawExec.ExecPlugin(pluginPath, stdin, args.AsEnv())
  73. if err != nil {
  74. if err.Error() == "unknown CNI_COMMAND: VERSION" {
  75. return version.PluginSupports("0.1.0"), nil
  76. }
  77. return nil, err
  78. }
  79. return e.VersionDecoder.Decode(stdoutBytes)
  80. }