Skip to content

Commit 26b9af0

Browse files
committed
Allow setting every array element in replacements
1 parent c65ef48 commit 26b9af0

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

kyaml/yaml/fns.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,12 @@ func SplitIndexNameValue(p string) (string, string, error) {
796796
return parts[0], parts[1], nil
797797
}
798798

799+
// IsMatchEveryIndex returns true if p is matching every elements.
800+
// e.g. "*"
801+
func IsMatchEveryIndex(p string) bool {
802+
return p == "*"
803+
}
804+
799805
// IncrementFieldIndex increments i to point to the next field name element in
800806
// a slice of Contents.
801807
func IncrementFieldIndex(i int) int {

kyaml/yaml/match.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,42 @@ func (p *PathMatcher) filter(rn *RNode) (*RNode, error) {
8383
// match seq elements
8484
return p.doSeq(rn)
8585
}
86+
87+
if IsMatchEveryIndex(p.Path[0]) {
88+
// match every elements (*)
89+
return p.doMatchEvery(rn)
90+
}
8691
// match a field
8792
return p.doField(rn)
8893
}
8994

95+
func (p *PathMatcher) doMatchEvery(rn *RNode) (*RNode, error) {
96+
97+
if err := rn.VisitElements(p.visitEveryElem); err != nil {
98+
return nil, err
99+
}
100+
101+
// fmt.Println(p.val.String())
102+
return p.val, nil
103+
}
104+
105+
func (p *PathMatcher) visitEveryElem(elem *RNode) error {
106+
107+
fieldName := p.Path[0]
108+
// recurse on the matching element
109+
pm := &PathMatcher{Path: p.Path[1:]}
110+
add, err := pm.filter(elem)
111+
for k, v := range pm.Matches {
112+
p.Matches[k] = v
113+
}
114+
if err != nil || add == nil {
115+
return err
116+
}
117+
p.append(fieldName, add.Content()...)
118+
119+
return nil
120+
}
121+
90122
func (p *PathMatcher) doField(rn *RNode) (*RNode, error) {
91123
// lookup the field
92124
field, err := rn.Pipe(Get(p.Path[0]))

kyaml/yaml/match_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ spec:
7777
{[]string{
7878
"spec", "template", "spec", "containers", "[name=s.*]", "ports", "[containerPort=.*2]"},
7979
""},
80+
{[]string{
81+
"spec", "template", "spec", "containers", "*", "image"},
82+
"- nginx:1.7.9\n- sidecar:1.0.0\n"},
83+
{[]string{
84+
"spec", "template", "spec", "containers", "*", "ports", "*"},
85+
"- containerPort: 80\n- containerPort: 8081\n- containerPort: 9090\n"},
8086
}
8187
for i, u := range updates {
8288
result, err := node.Pipe(&PathMatcher{Path: u.path})

0 commit comments

Comments
 (0)