From c7f5f70554d026e7a3bc5a0ab52280576a2379d1 Mon Sep 17 00:00:00 2001 From: Casey Callendrello Date: Wed, 1 Sep 2021 18:18:47 +0200 Subject: [PATCH] pkg/version: add VersionsFrom function So plugins can easily declare support for "from version 0.3 and beyond". Signed-off-by: Casey Callendrello --- pkg/version/version.go | 18 +++++++++++++++++- pkg/version/version_test.go | 5 +++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/version/version.go b/pkg/version/version.go index 709a1fb3..1326f803 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -19,7 +19,7 @@ import ( "fmt" "github.com/containernetworking/cni/pkg/types" - "github.com/containernetworking/cni/pkg/types/100" + types100 "github.com/containernetworking/cni/pkg/types/100" "github.com/containernetworking/cni/pkg/types/create" ) @@ -38,6 +38,22 @@ func Current() string { var Legacy = PluginSupports("0.1.0", "0.2.0") var All = PluginSupports("0.1.0", "0.2.0", "0.3.0", "0.3.1", "0.4.0", "1.0.0") +// VersionsFrom returns a list of versions starting from min, inclusive +func VersionsStartingFrom(min string) PluginInfo { + out := []string{} + // cheat, just assume ordered + ok := false + for _, v := range All.SupportedVersions() { + if !ok && v == min { + ok = true + } + if ok { + out = append(out, v) + } + } + return PluginSupports(out...) +} + // Finds a Result object matching the requested version (if any) and asks // that object to parse the plugin result, returning an error if parsing failed. func NewResult(version string, resultBytes []byte) (types.Result, error) { diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go index 6d869c29..c273dfbc 100644 --- a/pkg/version/version_test.go +++ b/pkg/version/version_test.go @@ -28,6 +28,11 @@ import ( ) var _ = Describe("Version operations", func() { + It("computes a list of versions correctly", func() { + actual := version.VersionsStartingFrom("0.3.1") + Expect(actual.SupportedVersions()).To(Equal([]string{"0.3.1", "0.4.0", "1.0.0"})) + }) + Context("when a prevResult is available", func() { It("parses the prevResult", func() { rawBytes := []byte(`{