Skip to content

Commit

Permalink
Preserve scalar types when using the replacement filter
Browse files Browse the repository at this point in the history
Erasing the scalar type tag leads to unfortunate circumstances, in that
the resulting yaml code is valid yaml, but will not meet the object
spec.

For example, using the replacement transformer to take a port number as
a string from a ConfigMap and set a Pod port would previously end up
with:
 - port: "8080"
when the spec requires that this is not a string:
 - port: 8080

Signed-off-by: Jim Ramsay <i.am@jimramsay.com>
  • Loading branch information
lack committed Feb 25, 2022
1 parent 6950a0d commit e76b84e
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/filters/replacement/replacement.go
Expand Up @@ -174,7 +174,12 @@ func setTargetValue(options *types.FieldOptions, t *yaml.RNode, value *yaml.RNod
value.YNode().Value = strings.Join(tv, options.Delimiter)
}

t.SetYNode(value.YNode())
if t.YNode().Kind == yaml.ScalarNode {
// For scalar, only copy the value (leave any type intact to auto-convert int->string or string->int)
t.YNode().Value = value.YNode().Value
} else {
t.SetYNode(value.YNode())
}

return nil
}
Expand Down
102 changes: 100 additions & 2 deletions api/filters/replacement/replacement_test.go
Expand Up @@ -205,13 +205,13 @@ spec:
command: ["printenv"]
args:
- example.com
- 8080
- "8080"
- name: busybox
image: busybox:latest
args:
- echo
- example.com
- 8080
- "8080"
---
apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -2011,6 +2011,104 @@ spec:
name: nginx-tagged
- image: postgres:1.8.0
name: postgresdb
`,
},
"string source -> integer target": {
input: `apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
PORT: "8080"
---
apiVersion: apps/v1
kind: Pod
metadata:
name: pod
spec:
containers:
- image: busybox
name: myapp-container
ports:
- port: 80
`,
replacements: `replacements:
- source:
kind: ConfigMap
name: config
fieldPath: data.PORT
targets:
- select:
kind: Pod
fieldPaths:
- spec.containers.0.ports.0.port
`,
expected: `apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
PORT: "8080"
---
apiVersion: apps/v1
kind: Pod
metadata:
name: pod
spec:
containers:
- image: busybox
name: myapp-container
ports:
- port: 8080
`,
},
"integer source -> string target": {
input: `apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
PORT: "8080"
---
apiVersion: apps/v1
kind: Pod
metadata:
name: pod
spec:
containers:
- image: busybox
name: myapp-container
ports:
- port: 80
`,
replacements: `replacements:
- source:
kind: Pod
name: pod
fieldPath: spec.containers.0.ports.0.port
targets:
- select:
kind: ConfigMap
fieldPaths:
- data.PORT
`,
expected: `apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
PORT: "80"
---
apiVersion: apps/v1
kind: Pod
metadata:
name: pod
spec:
containers:
- image: busybox
name: myapp-container
ports:
- port: 80
`,
},
}
Expand Down

0 comments on commit e76b84e

Please sign in to comment.