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:
Tom Wieczorek
2018-12-09 01:02:44 +01:00
parent 63e4e5ccaa
commit c470982ce5
7 changed files with 131 additions and 6 deletions

View 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

View File

@@ -0,0 +1,6 @@
commonLabels:
notIn: arrays
resources:
- statefulset.yaml
- statefulset-with-template.yaml

View File

@@ -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

View 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

View 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

View File

@@ -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

View File

@@ -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
}