mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #2848 from phanimarupaka/UpdateArraySetterComments
Update setter comments correctly on updates
This commit is contained in:
@@ -447,6 +447,9 @@ type FieldSetter struct {
|
|||||||
// value on the ScalarNode.
|
// value on the ScalarNode.
|
||||||
Name string `yaml:"name,omitempty"`
|
Name string `yaml:"name,omitempty"`
|
||||||
|
|
||||||
|
// Comments for the field
|
||||||
|
Comments Comments `yaml:"comments,omitempty"`
|
||||||
|
|
||||||
// Value is the value to set.
|
// Value is the value to set.
|
||||||
// Optional if Kind is set.
|
// Optional if Kind is set.
|
||||||
Value *RNode `yaml:"value,omitempty"`
|
Value *RNode `yaml:"value,omitempty"`
|
||||||
@@ -504,7 +507,8 @@ func (s FieldSetter) Filter(rn *RNode) (*RNode, error) {
|
|||||||
|
|
||||||
// create the field
|
// create the field
|
||||||
rn.YNode().Content = append(rn.YNode().Content,
|
rn.YNode().Content = append(rn.YNode().Content,
|
||||||
&yaml.Node{Kind: yaml.ScalarNode, Value: s.Name},
|
&yaml.Node{Kind: yaml.ScalarNode, HeadComment: s.Comments.HeadComment,
|
||||||
|
LineComment: s.Comments.LineComment, FootComment: s.Comments.FootComment, Value: s.Name},
|
||||||
s.Value.YNode())
|
s.Value.YNode())
|
||||||
return s.Value, nil
|
return s.Value, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,12 +55,108 @@ list: # new value
|
|||||||
apiVersion: apps/v1`,
|
apiVersion: apps/v1`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
list:
|
list: # new value
|
||||||
- 2
|
- 2
|
||||||
- 3
|
- 3
|
||||||
- 4
|
- 4
|
||||||
`},
|
`},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Test Case
|
||||||
|
//
|
||||||
|
{
|
||||||
|
description: `Update comment`,
|
||||||
|
origin: `
|
||||||
|
list: # comment
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
update: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`,
|
||||||
|
local: `
|
||||||
|
list: # comment
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
expected: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Test Case
|
||||||
|
//
|
||||||
|
{
|
||||||
|
description: `Don't update local modified comment`,
|
||||||
|
origin: `
|
||||||
|
list: # origin comment
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
update: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`,
|
||||||
|
local: `
|
||||||
|
list: # local comment
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
expected: `
|
||||||
|
list: # local comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Test Case
|
||||||
|
//
|
||||||
|
{
|
||||||
|
description: `Don't add local deleted comment`,
|
||||||
|
origin: `
|
||||||
|
list: # origin comment
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
update: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`,
|
||||||
|
local: `
|
||||||
|
list:
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 3`,
|
||||||
|
expected: `
|
||||||
|
list:
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`},
|
||||||
|
|
||||||
|
{
|
||||||
|
description: `Add update with comment`,
|
||||||
|
origin: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
`,
|
||||||
|
update: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`,
|
||||||
|
local: `
|
||||||
|
apiVersion: apps/v1`,
|
||||||
|
expected: `
|
||||||
|
list: # updated comment
|
||||||
|
- 2
|
||||||
|
- 3
|
||||||
|
- 4`},
|
||||||
|
|
||||||
//
|
//
|
||||||
// Test Case
|
// Test Case
|
||||||
//
|
//
|
||||||
@@ -140,14 +236,14 @@ list: # ignore value
|
|||||||
- 3`,
|
- 3`,
|
||||||
local: `
|
local: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
list:
|
list: # local comment
|
||||||
- 2
|
- 2
|
||||||
- 3
|
- 3
|
||||||
- 4
|
- 4
|
||||||
`,
|
`,
|
||||||
expected: `
|
expected: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
list:
|
list: # local comment
|
||||||
- 2
|
- 2
|
||||||
- 3
|
- 3
|
||||||
- 4
|
- 4
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ func Merge(dest, original, update *yaml.RNode) (*yaml.RNode, error) {
|
|||||||
|
|
||||||
return walk.Walker{
|
return walk.Walker{
|
||||||
Visitor: Visitor{},
|
Visitor: Visitor{},
|
||||||
|
VisitKeysAsScalars: true,
|
||||||
Sources: []*yaml.RNode{dest, original, update}}.Walk()
|
Sources: []*yaml.RNode{dest, original, update}}.Walk()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ func MergeStrings(dest, original, update string, infer bool) (string, error) {
|
|||||||
result, err := walk.Walker{
|
result, err := walk.Walker{
|
||||||
InferAssociativeLists: infer,
|
InferAssociativeLists: infer,
|
||||||
Visitor: Visitor{},
|
Visitor: Visitor{},
|
||||||
|
VisitKeysAsScalars: true,
|
||||||
Sources: []*yaml.RNode{d, srcOriginal, srcUpdated}}.Walk()
|
Sources: []*yaml.RNode{d, srcOriginal, srcUpdated}}.Walk()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ kind: StatefulSet # new value`},
|
|||||||
// Test Case
|
// Test Case
|
||||||
//
|
//
|
||||||
{
|
{
|
||||||
description: `Ensure comments are updated`,
|
description: `Ensure comments are added`,
|
||||||
origin: `
|
origin: `
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -81,6 +81,58 @@ spec:
|
|||||||
replicas: 3 # {"$openapi":"replicas"}
|
replicas: 3 # {"$openapi":"replicas"}
|
||||||
`},
|
`},
|
||||||
|
|
||||||
|
//
|
||||||
|
// Test Case
|
||||||
|
//
|
||||||
|
{
|
||||||
|
description: `Ensure comments are updated`,
|
||||||
|
origin: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/merge-source: 'dest'
|
||||||
|
config.kubernetes.io/path: 'temp.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3 # {"$openapi":"replicas"}`,
|
||||||
|
update: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/merge-source: 'updated'
|
||||||
|
config.kubernetes.io/path: 'temp.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3 # {"$openapi":"replicas_new"}`,
|
||||||
|
local: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/merge-source: 'original'
|
||||||
|
config.kubernetes.io/path: 'temp.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3 # {"$openapi":"replicas"}
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
config.kubernetes.io/index: '0'
|
||||||
|
config.kubernetes.io/merge-source: 'updated'
|
||||||
|
config.kubernetes.io/path: 'temp.yaml'
|
||||||
|
spec:
|
||||||
|
replicas: 3 # {"$openapi":"replicas_new"}
|
||||||
|
`},
|
||||||
|
|
||||||
{
|
{
|
||||||
description: `Ensure deleted comments are not updated`,
|
description: `Ensure deleted comments are not updated`,
|
||||||
origin: `
|
origin: `
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
package merge3
|
package merge3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
|
||||||
"sigs.k8s.io/kustomize/kyaml/openapi"
|
"sigs.k8s.io/kustomize/kyaml/openapi"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||||
"sigs.k8s.io/kustomize/kyaml/yaml/walk"
|
"sigs.k8s.io/kustomize/kyaml/yaml/walk"
|
||||||
@@ -34,6 +33,7 @@ func (m Visitor) VisitMap(nodes walk.Sources, s *openapi.ResourceSchema) (*yaml.
|
|||||||
// initialize a new value that can be recursively merged
|
// initialize a new value that can be recursively merged
|
||||||
return yaml.NewRNode(&yaml.Node{Kind: yaml.MappingNode}), nil
|
return yaml.NewRNode(&yaml.Node{Kind: yaml.MappingNode}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// recursively merge the dest with the original and updated
|
// recursively merge the dest with the original and updated
|
||||||
return nodes.Dest(), nil
|
return nodes.Dest(), nil
|
||||||
}
|
}
|
||||||
@@ -67,17 +67,13 @@ func (m Visitor) VisitScalar(nodes walk.Sources, s *openapi.ResourceSchema) (*ya
|
|||||||
return nodes.Dest(), nil
|
return nodes.Dest(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedStr, err := nodes.Updated().String()
|
values, err := m.getStrValues(nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err)
|
return nil, err
|
||||||
}
|
|
||||||
originStr, err := nodes.Origin().String()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if updatedStr != originStr {
|
if (values.Dest == "" || values.Dest == values.Origin) && values.Origin != values.Update {
|
||||||
// change in update node
|
// if local is nil or is unchanged but there is new update
|
||||||
return nodes.Updated(), nil
|
return nodes.Updated(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,13 @@ type ResourceIdentifier struct {
|
|||||||
Kind string `yaml:"kind,omitempty"`
|
Kind string `yaml:"kind,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comments struct is comment yaml comment types
|
||||||
|
type Comments struct {
|
||||||
|
LineComment string `yaml:"lineComment,omitempty"`
|
||||||
|
HeadComment string `yaml:"headComment,omitempty"`
|
||||||
|
FootComment string `yaml:"footComment,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func (r *ResourceIdentifier) GetName() string {
|
func (r *ResourceIdentifier) GetName() string {
|
||||||
return r.Name
|
return r.Name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,11 +27,12 @@ func (l Walker) walkMap() (*yaml.RNode, error) {
|
|||||||
|
|
||||||
// recursively set the field values on the map
|
// recursively set the field values on the map
|
||||||
for _, key := range l.fieldNames() {
|
for _, key := range l.fieldNames() {
|
||||||
|
var res *yaml.RNode
|
||||||
|
var keys []*yaml.RNode
|
||||||
if l.VisitKeysAsScalars {
|
if l.VisitKeysAsScalars {
|
||||||
// visit the map keys as if they were scalars,
|
// visit the map keys as if they were scalars,
|
||||||
// this is necessary if doing things such as copying
|
// this is necessary if doing things such as copying
|
||||||
// comments
|
// comments
|
||||||
var keys []*yaml.RNode
|
|
||||||
for i := range l.Sources {
|
for i := range l.Sources {
|
||||||
// construct the sources from the keys
|
// construct the sources from the keys
|
||||||
if l.Sources[i] == nil {
|
if l.Sources[i] == nil {
|
||||||
@@ -47,7 +48,7 @@ func (l Walker) walkMap() (*yaml.RNode, error) {
|
|||||||
}
|
}
|
||||||
// visit the sources as a scalar
|
// visit the sources as a scalar
|
||||||
// keys don't have any schema --pass in nil
|
// keys don't have any schema --pass in nil
|
||||||
_, err := l.Visitor.VisitScalar(keys, nil)
|
res, err = l.Visitor.VisitScalar(keys, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -72,8 +73,23 @@ func (l Walker) walkMap() (*yaml.RNode, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// transfer the comments of res to dest node
|
||||||
|
var comments yaml.Comments
|
||||||
|
if !yaml.IsMissingOrNull(res) {
|
||||||
|
comments = yaml.Comments{
|
||||||
|
LineComment: res.YNode().LineComment,
|
||||||
|
HeadComment: res.YNode().HeadComment,
|
||||||
|
FootComment: res.YNode().FootComment,
|
||||||
|
}
|
||||||
|
if len(keys) > 0 && !yaml.IsMissingOrNull(keys[DestIndex]) {
|
||||||
|
keys[DestIndex].YNode().HeadComment = res.YNode().HeadComment
|
||||||
|
keys[DestIndex].YNode().LineComment = res.YNode().LineComment
|
||||||
|
keys[DestIndex].YNode().FootComment = res.YNode().FootComment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// this handles empty and non-empty values
|
// this handles empty and non-empty values
|
||||||
_, err = dest.Pipe(yaml.FieldSetter{Name: key, Value: val})
|
_, err = dest.Pipe(yaml.FieldSetter{Name: key, Comments: comments, Value: val})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user