| 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 |
|
| 15 |
package version |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"fmt" |
| 20 |
|
| 21 |
"github.com/containernetworking/cni/pkg/types" |
| 22 |
"github.com/containernetworking/cni/pkg/types/create" |
| 23 |
) |
| 24 |
|
| 25 |
// Current reports the version of the CNI spec implemented by this library |
| 26 |
func Current() string { |
| 27 |
return "1.1.0" |
| 28 |
} |
| 29 |
|
| 30 |
// Legacy PluginInfo describes a plugin that is backwards compatible with the |
| 31 |
// CNI spec version 0.1.0. In particular, a runtime compiled against the 0.1.0 |
| 32 |
// library ought to work correctly with a plugin that reports support for |
| 33 |
// Legacy versions. |
| 34 |
// |
| 35 |
// Any future CNI spec versions which meet this definition should be added to |
| 36 |
// this list. |
| 37 |
var ( |
| 38 |
Legacy = PluginSupports("0.1.0", "0.2.0") |
| 39 |
All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0", "1.1.0") |
| 40 |
) |
| 41 |
|
| 42 |
// VersionsFrom returns a list of versions starting from min, inclusive |
| 43 |
func VersionsStartingFrom(min string) PluginInfo { |
| 44 |
out := []string{} |
| 45 |
// cheat, just assume ordered |
| 46 |
ok := false |
| 47 |
for _, v := range All.SupportedVersions() { |
| 48 |
if !ok && v == min { |
| 49 |
ok = true |
| 50 |
} |
| 51 |
if ok { |
| 52 |
out = append(out, v) |
| 53 |
} |
| 54 |
} |
| 55 |
return PluginSupports(out...) |
| 56 |
} |
| 57 |
|
| 58 |
// Finds a Result object matching the requested version (if any) and asks |
| 59 |
// that object to parse the plugin result, returning an error if parsing failed. |
| 60 |
func NewResult(version string, resultBytes []byte) (types.Result, error) { |
| 61 |
return create.Create(version, resultBytes) |
| 62 |
} |
| 63 |
|
| 64 |
// ParsePrevResult parses a prevResult in a NetConf structure and sets |
| 65 |
// the NetConf's PrevResult member to the parsed Result object. |
| 66 |
func ParsePrevResult(conf *types.PluginConf) error { |
| 67 |
if conf.RawPrevResult == nil { |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
// Prior to 1.0.0, Result types may not marshal a CNIVersion. Since the |
| 72 |
// result version must match the config version, if the Result's version |
| 73 |
// is empty, inject the config version. |
| 74 |
if ver, ok := conf.RawPrevResult["CNIVersion"]; !ok || ver == "" { |
| 75 |
conf.RawPrevResult["CNIVersion"] = conf.CNIVersion |
| 76 |
} |
| 77 |
|
| 78 |
resultBytes, err := json.Marshal(conf.RawPrevResult) |
| 79 |
if err != nil { |
| 80 |
return fmt.Errorf("could not serialize prevResult: %w", err) |
| 81 |
} |
| 82 |
|
| 83 |
conf.RawPrevResult = nil |
| 84 |
conf.PrevResult, err = create.Create(conf.CNIVersion, resultBytes) |
| 85 |
if err != nil { |
| 86 |
return fmt.Errorf("could not parse prevResult: %w", err) |
| 87 |
} |
| 88 |
|
| 89 |
return nil |
| 90 |
} |