version.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. "encoding/json"
  17. "fmt"
  18. "github.com/containernetworking/cni/pkg/types"
  19. "github.com/containernetworking/cni/pkg/types/020"
  20. "github.com/containernetworking/cni/pkg/types/current"
  21. )
  22. // Current reports the version of the CNI spec implemented by this library
  23. func Current() string {
  24. return "0.4.0"
  25. }
  26. // Legacy PluginInfo describes a plugin that is backwards compatible with the
  27. // CNI spec version 0.1.0. In particular, a runtime compiled against the 0.1.0
  28. // library ought to work correctly with a plugin that reports support for
  29. // Legacy versions.
  30. //
  31. // Any future CNI spec versions which meet this definition should be added to
  32. // this list.
  33. var Legacy = PluginSupports("0.1.0", "0.2.0")
  34. var All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0")
  35. var resultFactories = []struct {
  36. supportedVersions []string
  37. newResult types.ResultFactoryFunc
  38. }{
  39. {current.SupportedVersions, current.NewResult},
  40. {types020.SupportedVersions, types020.NewResult},
  41. }
  42. // Finds a Result object matching the requested version (if any) and asks
  43. // that object to parse the plugin result, returning an error if parsing failed.
  44. func NewResult(version string, resultBytes []byte) (types.Result, error) {
  45. reconciler := &Reconciler{}
  46. for _, resultFactory := range resultFactories {
  47. err := reconciler.CheckRaw(version, resultFactory.supportedVersions)
  48. if err == nil {
  49. // Result supports this version
  50. return resultFactory.newResult(resultBytes)
  51. }
  52. }
  53. return nil, fmt.Errorf("unsupported CNI result version %q", version)
  54. }
  55. // ParsePrevResult parses a prevResult in a NetConf structure and sets
  56. // the NetConf's PrevResult member to the parsed Result object.
  57. func ParsePrevResult(conf *types.NetConf) error {
  58. if conf.RawPrevResult == nil {
  59. return nil
  60. }
  61. resultBytes, err := json.Marshal(conf.RawPrevResult)
  62. if err != nil {
  63. return fmt.Errorf("could not serialize prevResult: %v", err)
  64. }
  65. conf.RawPrevResult = nil
  66. conf.PrevResult, err = NewResult(conf.CNIVersion, resultBytes)
  67. if err != nil {
  68. return fmt.Errorf("could not parse prevResult: %v", err)
  69. }
  70. return nil
  71. }