version.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 version
  15. import (
  16. "fmt"
  17. "github.com/containernetworking/cni/pkg/types"
  18. "github.com/containernetworking/cni/pkg/types/020"
  19. "github.com/containernetworking/cni/pkg/types/current"
  20. )
  21. // Current reports the version of the CNI spec implemented by this library
  22. func Current() string {
  23. return "0.3.1"
  24. }
  25. // Legacy PluginInfo describes a plugin that is backwards compatible with the
  26. // CNI spec version 0.1.0. In particular, a runtime compiled against the 0.1.0
  27. // library ought to work correctly with a plugin that reports support for
  28. // Legacy versions.
  29. //
  30. // Any future CNI spec versions which meet this definition should be added to
  31. // this list.
  32. var Legacy = PluginSupports("0.1.0", "0.2.0")
  33. var All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1")
  34. var resultFactories = []struct {
  35. supportedVersions []string
  36. newResult types.ResultFactoryFunc
  37. }{
  38. {current.SupportedVersions, current.NewResult},
  39. {types020.SupportedVersions, types020.NewResult},
  40. }
  41. // Finds a Result object matching the requested version (if any) and asks
  42. // that object to parse the plugin result, returning an error if parsing failed.
  43. func NewResult(version string, resultBytes []byte) (types.Result, error) {
  44. reconciler := &Reconciler{}
  45. for _, resultFactory := range resultFactories {
  46. err := reconciler.CheckRaw(version, resultFactory.supportedVersions)
  47. if err == nil {
  48. // Result supports this version
  49. return resultFactory.newResult(resultBytes)
  50. }
  51. }
  52. return nil, fmt.Errorf("unsupported CNI result version %q", version)
  53. }