From 31819e479648a6967758e054a824e45bfd5fa7c0 Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Fri, 29 Mar 2019 08:49:15 -0700 Subject: [PATCH] fix(pkg/chartutil): marshal capabilities VersionSet into slice Marshal capabilities VersionSet into readable value. Signed-off-by: Adam Reese --- pkg/chartutil/capabilities.go | 25 +++++++++++++++++++++- pkg/chartutil/capabilities_test.go | 33 +++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/pkg/chartutil/capabilities.go b/pkg/chartutil/capabilities.go index 00fc654e8..88d0530b9 100644 --- a/pkg/chartutil/capabilities.go +++ b/pkg/chartutil/capabilities.go @@ -16,10 +16,13 @@ limitations under the License. package chartutil import ( + "encoding/json" "fmt" "runtime" + "sort" "k8s.io/apimachinery/pkg/version" + "k8s.io/client-go/kubernetes/scheme" ) @@ -66,7 +69,7 @@ func NewVersionSet(apiVersions ...string) VersionSet { // Has returns true if the version string is in the set. // -// vs.Has("extensions/v1beta1") +// vs.Has("apps/v1") func (v VersionSet) Has(apiVersion string) bool { _, ok := v[apiVersion] return ok @@ -79,3 +82,23 @@ func allKnownVersions() VersionSet { } return vs } + +// MarshalJSON implements the encoding/json.Marshaler interface. +func (v VersionSet) MarshalJSON() ([]byte, error) { + out := make([]string, 0, len(v)) + for i := range v { + out = append(out, i) + } + sort.Strings(out) + return json.Marshal(out) +} + +// UnmarshalJSON implements the encoding/json.Unmarshaler interface. +func (v *VersionSet) UnmarshalJSON(data []byte) error { + var vs []string + if err := json.Unmarshal(data, &vs); err != nil { + return err + } + *v = NewVersionSet(vs...) + return nil +} diff --git a/pkg/chartutil/capabilities_test.go b/pkg/chartutil/capabilities_test.go index 8a43214b7..fd798ca0d 100644 --- a/pkg/chartutil/capabilities_test.go +++ b/pkg/chartutil/capabilities_test.go @@ -16,17 +16,18 @@ limitations under the License. package chartutil import ( + "encoding/json" "testing" ) func TestVersionSet(t *testing.T) { - vs := NewVersionSet("v1", "extensions/v1beta1") + vs := NewVersionSet("v1", "apps/v1") if d := len(vs); d != 2 { t.Errorf("Expected 2 versions, got %d", d) } - if !vs.Has("extensions/v1beta1") { - t.Error("Expected to find extensions/v1beta1") + if !vs.Has("apps/v1") { + t.Error("Expected to find apps/v1") } if vs.Has("Spanish/inquisition") { @@ -49,3 +50,29 @@ func TestCapabilities(t *testing.T) { t.Error("APIVersions should have v1") } } + +func TestCapabilitiesJSONMarshal(t *testing.T) { + vs := NewVersionSet("v1", "apps/v1") + b, err := json.Marshal(vs) + if err != nil { + t.Fatal(err) + } + + expect := `["apps/v1","v1"]` + if string(b) != expect { + t.Fatalf("JSON marshaled semantic version not equal: expected %q, got %q", expect, string(b)) + } +} + +func TestCapabilitiesJSONUnmarshal(t *testing.T) { + in := `["apps/v1","v1"]` + + var vs VersionSet + if err := json.Unmarshal([]byte(in), &vs); err != nil { + t.Fatal(err) + } + + if len(vs) != 2 { + t.Fatalf("JSON unmarshaled semantic version not equal: expected 2, got %d", len(vs)) + } +}