version.go 2.8 KB

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