version.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. types100 "github.com/containernetworking/cni/pkg/types/100"
  20. "github.com/containernetworking/cni/pkg/types/create"
  21. )
  22. // Current reports the version of the CNI spec implemented by this library
  23. func Current() string {
  24. return types100.ImplementedSpecVersion
  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", "1.0.0")
  35. // VersionsFrom returns a list of versions starting from min, inclusive
  36. func VersionsStartingFrom(min string) PluginInfo {
  37. out := []string{}
  38. // cheat, just assume ordered
  39. ok := false
  40. for _, v := range All.SupportedVersions() {
  41. if !ok && v == min {
  42. ok = true
  43. }
  44. if ok {
  45. out = append(out, v)
  46. }
  47. }
  48. return PluginSupports(out...)
  49. }
  50. // Finds a Result object matching the requested version (if any) and asks
  51. // that object to parse the plugin result, returning an error if parsing failed.
  52. func NewResult(version string, resultBytes []byte) (types.Result, error) {
  53. return create.Create(version, resultBytes)
  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. // Prior to 1.0.0, Result types may not marshal a CNIVersion. Since the
  62. // result version must match the config version, if the Result's version
  63. // is empty, inject the config version.
  64. if ver, ok := conf.RawPrevResult["CNIVersion"]; !ok || ver == "" {
  65. conf.RawPrevResult["CNIVersion"] = conf.CNIVersion
  66. }
  67. resultBytes, err := json.Marshal(conf.RawPrevResult)
  68. if err != nil {
  69. return fmt.Errorf("could not serialize prevResult: %w", err)
  70. }
  71. conf.RawPrevResult = nil
  72. conf.PrevResult, err = create.Create(conf.CNIVersion, resultBytes)
  73. if err != nil {
  74. return fmt.Errorf("could not parse prevResult: %w", err)
  75. }
  76. return nil
  77. }