mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-18 02:01:29 +00:00
Merge pull request #4577 from koba1t/fix/replacement_wildcard_and_create_option_error_message
fix error message using replacement wildcard and create option
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
package replacement
|
package replacement
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -117,6 +118,14 @@ func applyToNode(node *yaml.RNode, value *yaml.RNode, target *types.TargetSelect
|
|||||||
var t *yaml.RNode
|
var t *yaml.RNode
|
||||||
var err error
|
var err error
|
||||||
if target.Options != nil && target.Options.Create {
|
if target.Options != nil && target.Options.Create {
|
||||||
|
// create option is not supported in a wildcard matching.
|
||||||
|
// Because, PathMatcher is not supported create option.
|
||||||
|
// So, if create option is set, we fallback to PathGetter.
|
||||||
|
for _, f := range fieldPath {
|
||||||
|
if f == "*" {
|
||||||
|
return errors.New("cannot support create option in a multi-value target") //nolint:goerr113
|
||||||
|
}
|
||||||
|
}
|
||||||
t, err = node.Pipe(yaml.LookupCreate(value.YNode().Kind, fieldPath...))
|
t, err = node.Pipe(yaml.LookupCreate(value.YNode().Kind, fieldPath...))
|
||||||
} else {
|
} else {
|
||||||
t, err = node.Pipe(&yaml.PathMatcher{Path: fieldPath})
|
t, err = node.Pipe(&yaml.PathMatcher{Path: fieldPath})
|
||||||
@@ -142,9 +151,6 @@ func applyToOneNode(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNod
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, scalarNode := range t.YNode().Content {
|
for _, scalarNode := range t.YNode().Content {
|
||||||
if options != nil && options.Create {
|
|
||||||
return fmt.Errorf("cannot use create option in a multi-value target")
|
|
||||||
}
|
|
||||||
rn := yaml.NewRNode(scalarNode)
|
rn := yaml.NewRNode(scalarNode)
|
||||||
if err := setTargetValue(options, rn, value); err != nil {
|
if err := setTargetValue(options, rn, value); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1634,6 +1634,45 @@ spec:
|
|||||||
- name: deployment-name
|
- name: deployment-name
|
||||||
value: sample-deploy`,
|
value: sample-deploy`,
|
||||||
},
|
},
|
||||||
|
"index contains '*' character and create options": {
|
||||||
|
input: `apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: sample-deploy
|
||||||
|
name: sample-deploy
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: sample-deploy
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: sample-deploy
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- image: nginx
|
||||||
|
name: main
|
||||||
|
env:
|
||||||
|
- name: other-env
|
||||||
|
value: YYYYY
|
||||||
|
`,
|
||||||
|
replacements: `replacements:
|
||||||
|
- source:
|
||||||
|
kind: Deployment
|
||||||
|
name: sample-deploy
|
||||||
|
fieldPath: metadata.name
|
||||||
|
targets:
|
||||||
|
- select:
|
||||||
|
kind: Deployment
|
||||||
|
fieldPaths:
|
||||||
|
- spec.template.spec.containers.*.env.[name=deployment-name].value
|
||||||
|
options:
|
||||||
|
create: true
|
||||||
|
`,
|
||||||
|
expectedErr: "cannot support create option in a multi-value target",
|
||||||
|
},
|
||||||
"multiple field paths in target": {
|
"multiple field paths in target": {
|
||||||
input: `apiVersion: v1
|
input: `apiVersion: v1
|
||||||
kind: ConfigMap
|
kind: ConfigMap
|
||||||
|
|||||||
@@ -541,6 +541,9 @@ func (l PathGetter) getFilter(part, nextPart string, fieldPath *[]string) (Filte
|
|||||||
case part == "-":
|
case part == "-":
|
||||||
// part is a hyphen
|
// part is a hyphen
|
||||||
return GetElementByIndex(-1), nil
|
return GetElementByIndex(-1), nil
|
||||||
|
case part == "*":
|
||||||
|
// PathGetter is not support for wildcard matching
|
||||||
|
return nil, errors.Errorf("wildcard is not supported in PathGetter")
|
||||||
case IsListIndex(part):
|
case IsListIndex(part):
|
||||||
// part is surrounded by brackets
|
// part is surrounded by brackets
|
||||||
return l.elemFilter(part)
|
return l.elemFilter(part)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ func TestElementSetter(t *testing.T) {
|
|||||||
|
|
||||||
node = MustParse(`
|
node = MustParse(`
|
||||||
- a: b
|
- a: b
|
||||||
- c: d
|
- c: d
|
||||||
`)
|
`)
|
||||||
// If given a key and no values, ElementSetter will
|
// If given a key and no values, ElementSetter will
|
||||||
// change node to be an empty list
|
// change node to be an empty list
|
||||||
@@ -154,7 +154,7 @@ func TestElementSetter(t *testing.T) {
|
|||||||
|
|
||||||
node = MustParse(`
|
node = MustParse(`
|
||||||
- a: b
|
- a: b
|
||||||
- c: d
|
- c: d
|
||||||
`)
|
`)
|
||||||
// Return error because ElementSetter will assume all elements are scalar when
|
// Return error because ElementSetter will assume all elements are scalar when
|
||||||
// there is only value provided.
|
// there is only value provided.
|
||||||
@@ -580,6 +580,13 @@ a: {}
|
|||||||
assert.Equal(t, "h\n", assertNoErrorString(t)(rn.String()))
|
assert.Equal(t, "h\n", assertNoErrorString(t)(rn.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLookup_Fn_create_with_wildcard_error(t *testing.T) {
|
||||||
|
node, err := Parse(s)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
_, err = node.Pipe(LookupCreate(yaml.MappingNode, "a", "b", "*", "t"))
|
||||||
|
assert.Error(t, err, "wildcard is not supported in PathGetter")
|
||||||
|
}
|
||||||
|
|
||||||
func TestLookup(t *testing.T) {
|
func TestLookup(t *testing.T) {
|
||||||
s := `n: o
|
s := `n: o
|
||||||
a:
|
a:
|
||||||
|
|||||||
Reference in New Issue
Block a user