From ab32c94900cb97f00ad4d2328ddd1cb0f4e83bdd Mon Sep 17 00:00:00 2001 From: natasha41575 Date: Tue, 12 Oct 2021 10:59:41 -0700 Subject: [PATCH] fix bug with migrating annotations --- kyaml/kio/byteio_reader.go | 4 ++ kyaml/kio/kioutil/kioutil.go | 14 ++++-- kyaml/kio/kioutil/kioutil_test.go | 77 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 9036b1739cf..fd0c45c6a24 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -316,6 +316,10 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode r.SetAnnotations = map[string]string{} } if !r.OmitReaderAnnotations { + err := kioutil.CopyLegacyAnnotations(n) + if err != nil { + return nil, err + } r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index bcd97303007..50abc187eb3 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -54,7 +54,11 @@ func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { func CopyLegacyAnnotations(rn *yaml.RNode) error { meta, err := rn.GetMeta() - if err != nil { + if err != nil { + if err == yaml.ErrMissingMetadata { + // resource has no metadata, this should be a no-op + return nil + } return err } if err := copyAnnotations(meta, rn, LegacyPathAnnotation, PathAnnotation); err != nil { @@ -71,12 +75,14 @@ func CopyLegacyAnnotations(rn *yaml.RNode) error { func copyAnnotations(meta yaml.ResourceMeta, rn *yaml.RNode, legacyKey string, newKey string) error { newValue := meta.Annotations[newKey] + legacyValue := meta.Annotations[legacyKey] if newValue != "" { - if err := rn.PipeE(yaml.SetAnnotation(legacyKey, newValue)); err != nil { - return err + if legacyValue == "" { + if err := rn.PipeE(yaml.SetAnnotation(legacyKey, newValue)); err != nil { + return err + } } } else { - legacyValue := meta.Annotations[legacyKey] if legacyValue != "" { if err := rn.PipeE(yaml.SetAnnotation(newKey, legacyValue)); err != nil { return err diff --git a/kyaml/kio/kioutil/kioutil_test.go b/kyaml/kio/kioutil/kioutil_test.go index 6fedf21fb5a..bf6c07b4eaa 100644 --- a/kyaml/kio/kioutil/kioutil_test.go +++ b/kyaml/kio/kioutil/kioutil_test.go @@ -374,3 +374,80 @@ func TestCreatePathAnnotationValue(t *testing.T) { } } } + +func TestCopyLegacyAnnotations(t *testing.T) { + var tests = []struct { + input string + expected string + }{ + { + input: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + config.kubernetes.io/path: 'a/b.yaml' + config.kubernetes.io/index: '5' +`, + expected: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + config.kubernetes.io/path: 'a/b.yaml' + config.kubernetes.io/index: '5' + internal.config.kubernetes.io/path: 'a/b.yaml' + internal.config.kubernetes.io/index: '5' +`, + }, + { + input: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + internal.config.kubernetes.io/path: 'a/b.yaml' + internal.config.kubernetes.io/index: '5' +`, + expected: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + internal.config.kubernetes.io/path: 'a/b.yaml' + internal.config.kubernetes.io/index: '5' + config.kubernetes.io/path: 'a/b.yaml' + config.kubernetes.io/index: '5' +`, + }, + { + input: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + internal.config.kubernetes.io/path: 'a/b.yaml' + config.kubernetes.io/path: 'c/d.yaml' +`, + expected: `apiVersion: v1 +kind: Foo +metadata: + name: foobar + annotations: + internal.config.kubernetes.io/path: 'a/b.yaml' + config.kubernetes.io/path: 'c/d.yaml' +`, + }, + } + + for _, tc := range tests { + rw := kio.ByteReadWriter{ + Reader: bytes.NewBufferString(tc.input), + OmitReaderAnnotations: true, + } + nodes, err := rw.Read() + assert.NoError(t, err) + assert.NoError(t, kioutil.CopyLegacyAnnotations(nodes[0])) + assert.Equal(t, tc.expected, nodes[0].MustString()) + } +}