diff --git a/kyaml/kio/byteio_reader.go b/kyaml/kio/byteio_reader.go index 9036b1739cf..7cc96ba6cca 100644 --- a/kyaml/kio/byteio_reader.go +++ b/kyaml/kio/byteio_reader.go @@ -316,8 +316,14 @@ 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 { + 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 {