mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Make transformer configs array-aware
If path segments end with the special marker `[]` in transformer configs, this indicates that the respective path segment is supposed to be an array. That information may be used to suppress the meaningless creation of non-existent paths that should be arrays, not objects.
This commit is contained in:
57
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/expected.yaml
vendored
Normal file
57
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/expected.yaml
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
labels:
|
||||
notIn: arrays
|
||||
name: persisted-test
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: test
|
||||
notIn: arrays
|
||||
serviceName: test
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: test
|
||||
notIn: arrays
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx
|
||||
name: nginx
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
labels:
|
||||
notIn: arrays
|
||||
name: www
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
storageClassName: my-storage-class
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: StatefulSet
|
||||
metadata:
|
||||
labels:
|
||||
notIn: arrays
|
||||
name: test
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: test
|
||||
notIn: arrays
|
||||
serviceName: test
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: test
|
||||
notIn: arrays
|
||||
spec:
|
||||
containers:
|
||||
- image: nginx
|
||||
name: nginx
|
||||
6
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/in/kustomization.yaml
vendored
Normal file
6
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/in/kustomization.yaml
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
commonLabels:
|
||||
notIn: arrays
|
||||
|
||||
resources:
|
||||
- statefulset.yaml
|
||||
- statefulset-with-template.yaml
|
||||
@@ -0,0 +1,29 @@
|
||||
kind: StatefulSet
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: persisted-test
|
||||
labels:
|
||||
notIn: arrays
|
||||
spec:
|
||||
serviceName: "test"
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: test
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: test
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
volumeClaimTemplates:
|
||||
- metadata:
|
||||
name: www
|
||||
spec:
|
||||
accessModes: [ "ReadWriteOnce" ]
|
||||
storageClassName: my-storage-class
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
20
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/in/statefulset.yaml
vendored
Normal file
20
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/in/statefulset.yaml
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
kind: StatefulSet
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: test
|
||||
labels:
|
||||
notIn: arrays
|
||||
spec:
|
||||
serviceName: "test"
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: test
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: test
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
4
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/test.yaml
vendored
Normal file
4
pkg/commands/build/testdata/testcase-transformer-no-create-arrays/test.yaml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
description: arrays are not autocreated by transformers
|
||||
args: []
|
||||
filename: testdata/testcase-transformer-no-create-arrays/in
|
||||
expectedStdout: testdata/testcase-transformer-no-create-arrays/expected.yaml
|
||||
@@ -110,7 +110,7 @@ commonLabels:
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/volumeClaimTemplates/metadata/labels
|
||||
- path: spec/volumeClaimTemplates[]/metadata/labels
|
||||
create: true
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
@@ -33,18 +33,20 @@ func mutateField(
|
||||
return nil
|
||||
}
|
||||
|
||||
_, found := m[pathToField[0]]
|
||||
firstPathSegment, isArray := getFirstPathSegment(pathToField)
|
||||
|
||||
_, found := m[firstPathSegment]
|
||||
if !found {
|
||||
if !createIfNotPresent {
|
||||
if !createIfNotPresent || isArray {
|
||||
return nil
|
||||
}
|
||||
m[pathToField[0]] = map[string]interface{}{}
|
||||
m[firstPathSegment] = map[string]interface{}{}
|
||||
}
|
||||
|
||||
if len(pathToField) == 1 {
|
||||
var err error
|
||||
for _, fn := range fns {
|
||||
m[pathToField[0]], err = fn(m[pathToField[0]])
|
||||
m[firstPathSegment], err = fn(m[firstPathSegment])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -52,7 +54,7 @@ func mutateField(
|
||||
return nil
|
||||
}
|
||||
|
||||
v := m[pathToField[0]]
|
||||
v := m[firstPathSegment]
|
||||
newPathToField := pathToField[1:]
|
||||
switch typedV := v.(type) {
|
||||
case nil:
|
||||
@@ -79,3 +81,10 @@ func mutateField(
|
||||
return fmt.Errorf("%#v is not expected to be a primitive type", typedV)
|
||||
}
|
||||
}
|
||||
|
||||
func getFirstPathSegment(pathToField []string) (string, bool) {
|
||||
if strings.HasSuffix(pathToField[0], "[]") {
|
||||
return pathToField[0][:len(pathToField[0])-2], true
|
||||
}
|
||||
return pathToField[0], false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user