-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathwalk.go
More file actions
233 lines (202 loc) · 5.79 KB
/
walk.go
File metadata and controls
233 lines (202 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package walk
import (
"fmt"
"os"
"strings"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/yaml/schema"
)
// Walker walks the Source RNode and modifies the RNode provided to GrepFilter.
type Walker struct {
// Visitor is invoked by GrepFilter
Visitor
Schema *openapi.ResourceSchema
// Source is the RNode to walk. All Source fields and associative list elements
// will be visited.
Sources Sources
// Path is the field path to the current Source Node.
Path []string
// InferAssociativeLists if set to true will infer merge strategies for
// 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
// MergeOptions is a struct to store options for merge
MergeOptions yaml.MergeOptions
}
// Kind returns the kind of the first non-null node in Sources.
func (l Walker) Kind() yaml.Kind {
for _, s := range l.Sources {
if !yaml.IsMissingOrNull(s) {
return s.YNode().Kind
}
}
return 0
}
// Walk will recursively traverse every item in the Sources and perform corresponding
// actions on them
func (l Walker) Walk() (*yaml.RNode, error) {
l.Schema = l.GetSchema()
// invoke the handler for the corresponding node type
switch l.Kind() {
case yaml.MappingNode:
if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.MappingNode, l.Sources...); err != nil {
// If AllowKindChange is set and there's a type mismatch, allow the
// origin (source) to replace dest entirely instead of erroring.
if l.MergeOptions.AllowKindChange {
if replaced := l.replaceOnKindMismatch(); replaced != nil {
return replaced, nil
}
}
return nil, err
}
return l.walkMap()
case yaml.SequenceNode:
if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.SequenceNode, l.Sources...); err != nil {
// If AllowKindChange is set and there's a type mismatch, allow the
// origin (source) to replace dest entirely instead of erroring.
if l.MergeOptions.AllowKindChange {
if replaced := l.replaceOnKindMismatch(); replaced != nil {
return replaced, nil
}
}
return nil, err
}
// AssociativeSequence means the items in the sequence are associative. They can be merged
// according to merge key.
if schema.IsAssociative(l.Schema, l.Sources, l.InferAssociativeLists) {
return l.walkAssociativeSequence()
}
return l.walkNonAssociativeSequence()
case yaml.ScalarNode:
if err := yaml.ErrorIfAnyInvalidAndNonNull(yaml.ScalarNode, l.Sources...); err != nil {
// If AllowKindChange is set and there's a type mismatch, allow the
// origin (source) to replace dest entirely instead of erroring.
if l.MergeOptions.AllowKindChange {
if replaced := l.replaceOnKindMismatch(); replaced != nil {
return replaced, nil
}
}
return nil, err
}
return l.walkScalar()
case 0:
// walk empty nodes as maps
return l.walkMap()
default:
return nil, nil
}
}
// replaceOnKindMismatch returns the origin node when there's a kind mismatch
// between dest and origin. This allows the origin to completely replace the
// dest when they have different types (e.g., map vs list, map vs scalar).
// Returns nil if no replacement should occur.
func (l Walker) replaceOnKindMismatch() *yaml.RNode {
dest := l.Sources.Dest()
origin := l.Sources.Origin()
// If origin is nil or null, we can't do a replacement
if yaml.IsMissingOrNull(origin) {
return nil
}
// If dest is nil or null, origin should be used directly
if yaml.IsMissingOrNull(dest) {
return origin
}
// Check if origin has a different kind than dest - if so, origin replaces dest
if origin.YNode().Kind != dest.YNode().Kind {
return origin
}
return nil
}
func (l Walker) GetSchema() *openapi.ResourceSchema {
for i := range l.Sources {
r := l.Sources[i]
if yaml.IsMissingOrNull(r) {
continue
}
fm := fieldmeta.FieldMeta{}
if err := fm.Read(r); err == nil && !fm.IsEmpty() {
// per-field schema, this is fine
if fm.Schema.Ref.String() != "" {
// resolve the reference
s, err := openapi.Resolve(&fm.Schema.Ref, openapi.Schema())
if err == nil && s != nil {
fm.Schema = *s
}
}
return &openapi.ResourceSchema{Schema: &fm.Schema}
}
}
if l.Schema != nil {
return l.Schema
}
for i := range l.Sources {
r := l.Sources[i]
if yaml.IsMissingOrNull(r) {
continue
}
m, _ := r.GetMeta()
if m.Kind == "" || m.APIVersion == "" {
continue
}
s := openapi.SchemaForResourceType(yaml.TypeMeta{Kind: m.Kind, APIVersion: m.APIVersion})
if s != nil {
return s
}
}
return nil
}
const (
DestIndex = iota
OriginIndex
UpdatedIndex
)
// Sources are a list of RNodes. First item is the dest node, followed by
// multiple source nodes.
type Sources []*yaml.RNode
// Dest returns the destination node
func (s Sources) Dest() *yaml.RNode {
if len(s) <= DestIndex {
return nil
}
return s[DestIndex]
}
// Origin returns the origin node
func (s Sources) Origin() *yaml.RNode {
if len(s) <= OriginIndex {
return nil
}
return s[OriginIndex]
}
// Updated returns the updated node
func (s Sources) Updated() *yaml.RNode {
if len(s) <= UpdatedIndex {
return nil
}
return s[UpdatedIndex]
}
func (s Sources) String() string {
var values []string
for i := range s {
str, err := s[i].String()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
values = append(values, str)
}
return strings.Join(values, "\n")
}
// setDestNode sets the destination source node
func (s Sources) setDestNode(node *yaml.RNode, err error) (*yaml.RNode, error) {
if err != nil {
return nil, err
}
s[0] = node
return node, nil
}