diff --git a/kyaml/yaml/walk/associative_sequence.go b/kyaml/yaml/walk/associative_sequence.go index 7d4a219f1..72e8e02e7 100644 --- a/kyaml/yaml/walk/associative_sequence.go +++ b/kyaml/yaml/walk/associative_sequence.go @@ -40,6 +40,7 @@ func (l *Walker) walkAssociativeSequence() (*yaml.RNode, error) { } for _, value := range values { val, err := Walker{ + VisitKeysAsScalars: l.VisitKeysAsScalars, InferAssociativeLists: l.InferAssociativeLists, Visitor: l, Schema: s, diff --git a/kyaml/yaml/walk/map.go b/kyaml/yaml/walk/map.go index 188aed9fa..4300205ac 100644 --- a/kyaml/yaml/walk/map.go +++ b/kyaml/yaml/walk/map.go @@ -27,6 +27,32 @@ func (l Walker) walkMap() (*yaml.RNode, error) { // recursively set the field values on the map for _, key := range l.fieldNames() { + if l.VisitKeysAsScalars { + // visit the map keys as if they were scalars, + // this is necessary if doing things such as copying + // comments + var keys []*yaml.RNode + for i := range l.Sources { + // construct the sources from the keys + if l.Sources[i] == nil { + keys = append(keys, nil) + continue + } + field := l.Sources[i].Field(key) + if field == nil || yaml.IsEmpty(field.Key) { + keys = append(keys, nil) + continue + } + keys = append(keys, field.Key) + } + // visit the sources as a scalar + // keys don't have any schema --pass in nil + _, err := l.Visitor.VisitScalar(keys, nil) + if err != nil { + return nil, err + } + } + var s *openapi.ResourceSchema if l.Schema != nil { s = l.Schema.Field(key) @@ -36,6 +62,7 @@ func (l Walker) walkMap() (*yaml.RNode, error) { s = commentSch } val, err := Walker{ + VisitKeysAsScalars: l.VisitKeysAsScalars, InferAssociativeLists: l.InferAssociativeLists, Visitor: l, Schema: s, diff --git a/kyaml/yaml/walk/walk.go b/kyaml/yaml/walk/walk.go index 2040aabd3..c615bc382 100644 --- a/kyaml/yaml/walk/walk.go +++ b/kyaml/yaml/walk/walk.go @@ -32,6 +32,10 @@ type Walker struct { // fields which it doesn't have the schema based on the fields in the // list elements. InferAssociativeLists bool + + // VisitKeysAsScalars if true will call VisitScalar on map entry keys, + // providing nil as the OpenAPI schema. + VisitKeysAsScalars bool } func (l Walker) Kind() yaml.Kind {