From 3feb55dd918520521cc9fdd833e1a1c431a5fdd3 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 | 12 ++++++++++-- kyaml/kio/kioutil/kioutil.go | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 9036b1739cf..c73ad8149bd 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -316,8 +316,16 @@ func (r *ByteReader) decode(originalYAML string, index int, decoder *yaml.Decode r.SetAnnotations = map[string]string{} } if !r.OmitReaderAnnotations { - r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) - r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) + err := kioutil.CopyLegacyAnnotations(n) + if err != nil { + // There are several places where we use ByteReader to read yaml without resource + // metadata, so we should ignore that error at this level. + return nil, err + } + if val, ok := n.GetAnnotations()[kioutil.IndexAnnotation]; !ok || val == "" { + r.SetAnnotations[kioutil.IndexAnnotation] = fmt.Sprintf("%d", index) + r.SetAnnotations[kioutil.LegacyIndexAnnotation] = fmt.Sprintf("%d", index) + } if r.PreserveSeqIndent { // derive and add the seqindent annotation diff --git a/kyaml/kio/kioutil/kioutil.go b/kyaml/kio/kioutil/kioutil.go index bcd97303007..542e28f1373 100644 --- a/kyaml/kio/kioutil/kioutil.go +++ b/kyaml/kio/kioutil/kioutil.go @@ -55,6 +55,9 @@ func GetFileAnnotations(rn *yaml.RNode) (string, string, error) { func CopyLegacyAnnotations(rn *yaml.RNode) error { meta, err := rn.GetMeta() if err != nil { + if err != yaml.ErrMissingMetadata { + return nil + } return err } if err := copyAnnotations(meta, rn, LegacyPathAnnotation, PathAnnotation); err != nil {