Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from luxas/fix_conversion
Browse files Browse the repository at this point in the history
Handle the case where `in` already is the desired version when encoding
  • Loading branch information
luxas authored Jul 6, 2020
2 parents 5ecac07 + 880e7e8 commit 9bca948
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/serializer/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ func (c *objectConvertor) ConvertToVersion(in runtime.Object, groupVersioner run
if err != nil {
return nil, fmt.Errorf("couldn't get GVK for hub: %w", err)
}

// If "in" already has the right, expected groupversion (here in the Encode CRD codepath),
// just return a deepcopy with its GVK set
if inGVK.GroupVersion().String() == gv.String() {
// Always make sure that the TypeMeta information is encoded.
// The Convert() call below also sets this information automatically
out := in.DeepCopyObject()
out.GetObjectKind().SetGroupVersionKind(inGVK)
return out, nil
}

// Assume the in and out (Hub and Convertible) kinds match (in encoded form)
outGVK := gv.WithKind(inGVK.Kind)

Expand Down
18 changes: 18 additions & 0 deletions pkg/serializer/serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ metadata:
creationTimestamp: null
# Preserve me please!
testString: foobar # Me too
`)

oldCRDNoComments = []byte(`apiVersion: foogroup/v1alpha1
kind: CRD
metadata:
creationTimestamp: null
testString: foobar
`)

newCRD = []byte(`# I'm a top comment
Expand All @@ -315,12 +322,21 @@ metadata:
creationTimestamp: null
# Preserve me please!
otherString: foobar # Me too
`)

newCRDNoComments = []byte(`apiVersion: foogroup/v1alpha2
kind: CRD
metadata:
creationTimestamp: null
otherString: foobar
`)
)

func TestEncode(t *testing.T) {
simpleObj := &runtimetest.InternalSimple{TestString: "foo"}
complexObj := &runtimetest.InternalComplex{String: "bar"}
oldCRDObj := &CRDOldVersion{TestString: "foobar"}
newCRDObj := &CRDNewVersion{OtherString: "foobar"}
tests := []struct {
name string
ct ContentType
Expand All @@ -333,6 +349,8 @@ func TestEncode(t *testing.T) {
{"both simple and complex yaml", ContentTypeYAML, []runtime.Object{simpleObj, complexObj}, simpleAndComplex, false},
{"simple json", ContentTypeJSON, []runtime.Object{simpleObj}, simpleJSON, false},
{"complex json", ContentTypeJSON, []runtime.Object{complexObj}, complexJSON, false},
{"old CRD yaml", ContentTypeYAML, []runtime.Object{oldCRDObj}, oldCRDNoComments, false},
{"new CRD yaml", ContentTypeYAML, []runtime.Object{newCRDObj}, newCRDNoComments, false},
//{"no-conversion simple", defaultEncoder, &runtimetest.ExternalSimple{TestString: "foo"}, simpleJSON, false},
//{"support internal", defaultEncoder, []runtime.Object{simpleObj}, []byte(`{"testString":"foo"}` + "\n"), false},
}
Expand Down

0 comments on commit 9bca948

Please sign in to comment.