Isolate scalar quoting oddities to one test set.

The apimachinery code path, in its final marshalling
for output, calls Marshall

  https://github.com/go-yaml/yaml/blob/v2/yaml.go#L199

This code path (via apimachinery Unstructured types)
has no JSON schema tags

  https://yaml.org/spec/1.2/spec.html#id2803311

so it adds quotes to values that smell like
booleans and ints (e.g. `false` becomes `"false"`).

The kyaml code path, OTOH, uses such tags,
so generally does not quote ints and booleans.

This PR isolates this difference in behavior to
one set of tests (using data fields in configmaps
in api/krusty/configmaps_test.go) so that
they don't confuse other tests that cover
completely different behaviors.
This commit is contained in:
jregan
2020-12-23 16:23:45 -08:00
parent 1ffd790cfb
commit 1426137883
8 changed files with 172 additions and 76 deletions

View File

@@ -39,8 +39,8 @@ resources:
configMapGenerator: configMapGenerator:
- name: my-configmap - name: my-configmap
literals: literals:
- testValue=1 - testValue=purple
- otherValue=10 - otherValue=green
`) `)
th.WriteF("/app/base/deploy.yaml", ` th.WriteF("/app/base/deploy.yaml", `
apiVersion: v1 apiVersion: v1
@@ -64,8 +64,8 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- testValue=2 - testValue=blue
- compValue=5 - compValue=red
`) `)
th.WriteF("/app/comp/stub.yaml", ` th.WriteF("/app/comp/stub.yaml", `
apiVersion: v1 apiVersion: v1
@@ -125,12 +125,12 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
compValue: "5" compValue: red
otherValue: "10" otherValue: green
testValue: "2" testValue: blue
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: comp-my-configmap-kc6k2kmkh9 name: comp-my-configmap-97647ckcmg
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -154,7 +154,7 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- otherValue=9 - otherValue=orange
`), `),
writeK("/app/prod", ` writeK("/app/prod", `
resources: resources:
@@ -177,12 +177,12 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
compValue: "5" compValue: red
otherValue: "9" otherValue: orange
testValue: "2" testValue: blue
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: comp-my-configmap-55249mf5kb name: comp-my-configmap-g486mb229k
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -208,7 +208,7 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- otherValue=9 - otherValue=orange
`), `),
writeK("/app/prod", ` writeK("/app/prod", `
resources: resources:
@@ -230,12 +230,12 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
compValue: "5" compValue: red
otherValue: "9" otherValue: orange
testValue: "2" testValue: blue
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: comp-my-configmap-55249mf5kb name: comp-my-configmap-g486mb229k
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -273,11 +273,11 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
otherValue: "10" otherValue: green
testValue: "1" testValue: purple
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: my-configmap-2g9c94mhb8 name: my-configmap-9cd648hm8f
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -288,12 +288,12 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
compValue: "5" compValue: red
otherValue: "10" otherValue: green
testValue: "2" testValue: blue
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: comp-my-configmap-kc6k2kmkh9 name: comp-my-configmap-97647ckcmg
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -319,8 +319,8 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- compValue=5 - compValue=red
- testValue=2 - testValue=blue
`), `),
}, },
runPath: "/app/direct-component", runPath: "/app/direct-component",
@@ -334,12 +334,12 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
compValue: "5" compValue: red
otherValue: "10" otherValue: green
testValue: "2" testValue: blue
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: my-configmap-kc6k2kmkh9 name: my-configmap-97647ckcmg
`, `,
}, },
"missing-optional-component-api-version": { "missing-optional-component-api-version": {
@@ -350,7 +350,7 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- otherValue=9 - otherValue=orange
`), `),
}, },
runPath: "/app/prod", runPath: "/app/prod",
@@ -364,11 +364,11 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
otherValue: "9" otherValue: orange
testValue: "1" testValue: purple
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: my-configmap-5g7gh5mgt5 name: my-configmap-6hhdg8gkdg
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -411,11 +411,11 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
otherValue: "10" otherValue: green
testValue: "1" testValue: purple
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: my-configmap-a-b-2g9c94mhb8 name: my-configmap-a-b-9cd648hm8f
--- ---
apiVersion: v1 apiVersion: v1
kind: Deployment kind: Deployment
@@ -426,11 +426,11 @@ spec:
--- ---
apiVersion: v1 apiVersion: v1
data: data:
otherValue: "10" otherValue: green
testValue: "1" testValue: purple
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: my-configmap-b-2g9c94mhb8 name: my-configmap-b-9cd648hm8f
`, `,
}, },
@@ -562,7 +562,7 @@ configMapGenerator:
- name: my-configmap - name: my-configmap
behavior: merge behavior: merge
literals: literals:
- otherValue=9 - otherValue=orange
`), `),
}, },
runPath: "/app/prod", runPath: "/app/prod",

View File

@@ -7,10 +7,97 @@ import (
"fmt" "fmt"
"testing" "testing"
"sigs.k8s.io/kustomize/api/konfig"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
) )
// Numbers and booleans are quoted
func TestGeneratorIntVsStringNoMerge(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
resources:
- service.yaml
configMapGenerator:
- name: bob
literals:
- fruit=Indian Gooseberry
- year=2020
- crisis=true
`)
th.WriteF("service.yaml", `
apiVersion: v1
kind: Service
metadata:
annotations:
port: 8080
happy: true
color: green
name: demo
spec:
clusterIP: None
`)
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(
m, `
apiVersion: v1
kind: Service
metadata:
annotations:
color: green
happy: true
port: 8080
name: demo
spec:
clusterIP: None
---
apiVersion: v1
data:
crisis: "true"
fruit: Indian Gooseberry
year: "2020"
kind: ConfigMap
metadata:
name: bob-79t79mt227
`)
}
// Observation: Numbers no longer quoted
func TestGeneratorIntVsStringWithMerge(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK("base", `
configMapGenerator:
- name: bob
literals:
- fruit=Indian Gooseberry
- year=2020
- crisis=true
`)
th.WriteK("overlay", `
resources:
- ../base
configMapGenerator:
- name: bob
behavior: merge
literals:
- month=12
`)
opts := th.MakeDefaultOptions()
m := th.Run("overlay", opts)
expFmt := `apiVersion: v1
data:
crisis: %s
fruit: Indian Gooseberry
month: %s
year: %s
kind: ConfigMap
metadata:
name: bob-%s
`
th.AssertActualEqualsExpected(
m, opts.IfApiMachineryElseKyaml(
fmt.Sprintf(expFmt, `"true"`, `"12"`, `"2020"`, `bk46gm59c6`),
fmt.Sprintf(expFmt, `true`, `12`, `2020`, `bkmtk2t2fb`)))
}
// Generate a Secret and a ConfigMap from the same data // Generate a Secret and a ConfigMap from the same data
// to compare the result. // to compare the result.
func TestGeneratorBasics(t *testing.T) { func TestGeneratorBasics(t *testing.T) {
@@ -61,8 +148,8 @@ electromagnetic
strong nuclear strong nuclear
weak nuclear weak nuclear
`) `)
opts := th.MakeDefaultOptions()
m := th.Run("/app", th.MakeDefaultOptionsWithProperEnableKyaml()) m := th.Run("/app", opts)
expFmt := `apiVersion: v1 expFmt := `apiVersion: v1
data: data:
MOUNTAIN: everest MOUNTAIN: everest
@@ -102,19 +189,20 @@ data:
vegetable: YnJvY2NvbGk= vegetable: YnJvY2NvbGk=
kind: Secret kind: Secret
metadata: metadata:
name: blah-bob-9t25t44gg4 name: blah-bob-%s
type: Opaque type: Opaque
` `
th.AssertActualEqualsExpected( th.AssertActualEqualsExpected(
m, konfig.IfApiMachineryElseKyaml( m, opts.IfApiMachineryElseKyaml(
fmt.Sprintf(expFmt, fmt.Sprintf(expFmt,
`CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbnVjbGVhcgo`, `CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbnVjbGVhcgo=`,
`CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aGUgZXZpbCBkYXlzIGNvbWUgbm90Lgo`), `CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aGUgZXZpbCBkYXlzIGNvbWUgbm90Lgo=`,
`ftht6hfgmb`),
fmt.Sprintf(expFmt, `| fmt.Sprintf(expFmt, `|
CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbn CmdyYXZpdGF0aW9uYWwKZWxlY3Ryb21hZ25ldGljCnN0cm9uZyBudWNsZWFyCndlYWsgbn
VjbGVhcgo=`, `| VjbGVhcgo=`, `|
CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aG CkxpZmUgaXMgc2hvcnQuCkJ1dCB0aGUgeWVhcnMgYXJlIGxvbmcuCk5vdCB3aGlsZSB0aG
UgZXZpbCBkYXlzIGNvbWUgbm90Lgo=`))) UgZXZpbCBkYXlzIGNvbWUgbm90Lgo=`, `9t25t44gg4`)))
} }
// TODO: These should be errors instead. // TODO: These should be errors instead.

View File

@@ -1,6 +1,7 @@
package krusty_test package krusty_test
import ( import (
"fmt"
"os/exec" "os/exec"
"testing" "testing"
@@ -128,8 +129,9 @@ stringData:
bootcmd: bootcmd:
- mkdir /mnt/vda - mkdir /mnt/vda
`) `)
m := th.Run("/app", th.MakeOptionsPluginsEnabled()) opts := th.MakeOptionsPluginsEnabled()
th.AssertActualEqualsExpected(m, ` m := th.Run("/app", opts)
expFmt := `
apiVersion: v1 apiVersion: v1
kind: Secret kind: Secret
metadata: metadata:
@@ -152,7 +154,7 @@ metadata:
name: demo name: demo
name: demo-budget name: demo-budget
spec: spec:
minAvailable: 67% minAvailable: 67%%
selector: selector:
matchLabels: matchLabels:
app: cockroachdb app: cockroachdb
@@ -185,9 +187,7 @@ metadata:
annotations: annotations:
config.kubernetes.io/path: config/demo_service.yaml config.kubernetes.io/path: config/demo_service.yaml
prometheus.io/path: _status/vars prometheus.io/path: _status/vars
prometheus.io/port: "8080" %s
prometheus.io/scrape: "true"
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
labels: labels:
app: cockroachdb app: cockroachdb
name: demo name: demo
@@ -244,7 +244,7 @@ spec:
- /bin/bash - /bin/bash
- -ecx - -ecx
- | - |
# The use of qualified `+"`hostname -f`"+` is crucial: # The use of qualified ` + "`hostname -f`" + ` is crucial:
# Other nodes aren't able to look up the unqualified hostname. # Other nodes aren't able to look up the unqualified hostname.
CRARGS=("start" "--logtostderr" "--insecure" "--host" "$(hostname -f)" "--http-host" "0.0.0.0") CRARGS=("start" "--logtostderr" "--insecure" "--host" "$(hostname -f)" "--http-host" "0.0.0.0")
# We only want to initialize a new cluster (by omitting the join flag) # We only want to initialize a new cluster (by omitting the join flag)
@@ -302,7 +302,14 @@ spec:
resources: resources:
requests: requests:
storage: 1Gi storage: 1Gi
`) `
th.AssertActualEqualsExpected(m, opts.IfApiMachineryElseKyaml(
fmt.Sprintf(expFmt, ` prometheus.io/port: "8080"
prometheus.io/scrape: "true"
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"`),
fmt.Sprintf(expFmt, ` prometheus.io/port: 8080
prometheus.io/scrape: true
service.alpha.kubernetes.io/tolerate-unready-endpoints: true`)))
} }
func TestFnContainerTransformer(t *testing.T) { func TestFnContainerTransformer(t *testing.T) {

View File

@@ -38,7 +38,7 @@ configMapGenerator:
literals: literals:
- MYSQL_USER=default - MYSQL_USER=default
- MYSQL_DATABASE=default - MYSQL_DATABASE=default
- PORT=3306 - HOST=everest
`) `)
th.WriteK(".", ` th.WriteK(".", `
@@ -82,13 +82,13 @@ patches:
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
HOST: everest
MYSQL_DATABASE: db MYSQL_DATABASE: db
MYSQL_PASSWORD: correct horse battery staple MYSQL_PASSWORD: correct horse battery staple
MYSQL_USER: my-user MYSQL_USER: my-user
PORT: "3306"
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: mysql-9792mdchtg name: mysql-t7tt4cdbmf
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
@@ -102,10 +102,10 @@ spec:
- valueFrom: - valueFrom:
configMapKeyRef: configMapKeyRef:
key: MYSQL_DATABASE key: MYSQL_DATABASE
name: mysql-9792mdchtg name: mysql-t7tt4cdbmf
envFrom: envFrom:
- configMapRef: - configMapRef:
name: mysql-9792mdchtg name: mysql-t7tt4cdbmf
name: handler name: handler
`) `)
} }

View File

@@ -80,7 +80,7 @@ namespace: base
configMapGenerator: configMapGenerator:
- name: testCase - name: testCase
literals: literals:
- base=true - base=apple
`) `)
th.WriteK("/app/overlay", ` th.WriteK("/app/overlay", `
resources: resources:
@@ -92,17 +92,17 @@ configMapGenerator:
- name: testCase - name: testCase
behavior: merge behavior: merge
literals: literals:
- overlay=true - overlay=peach
`) `)
m := th.Run("/app/overlay", th.MakeDefaultOptions()) m := th.Run("/app/overlay", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, ` th.AssertActualEqualsExpected(m, `
apiVersion: v1 apiVersion: v1
data: data:
base: "true" base: apple
overlay: "true" overlay: peach
kind: ConfigMap kind: ConfigMap
metadata: metadata:
name: testCase-bcbmmg48hd name: testCase-gmfch8gkbt
namespace: overlay namespace: overlay
`) `)
} }

View File

@@ -55,3 +55,10 @@ func MakeDefaultOptions() *Options {
AllowResourceIdChanges: false, AllowResourceIdChanges: false,
} }
} }
func (o Options) IfApiMachineryElseKyaml(s1, s2 string) string {
if !o.UseKyaml {
return s1
}
return s2
}

View File

@@ -74,7 +74,7 @@ kind: ConfigMap
metadata: metadata:
name: configmap-a name: configmap-a
annotations: annotations:
kustomize.k8s.io/Generated: "false" fruit: peach
data: data:
foo: $FOO foo: $FOO
`) `)
@@ -87,7 +87,7 @@ data:
kind: ConfigMap kind: ConfigMap
metadata: metadata:
annotations: annotations:
kustomize.k8s.io/Generated: "false" fruit: peach
name: configmap-a name: configmap-a
--- ---
apiVersion: v1 apiVersion: v1

View File

@@ -70,12 +70,6 @@ func (th Harness) MakeDefaultOptions() krusty.Options {
return th.MakeOptionsPluginsDisabled() return th.MakeOptionsPluginsDisabled()
} }
func (th Harness) MakeDefaultOptionsWithProperEnableKyaml() krusty.Options {
o := th.MakeOptionsPluginsDisabled()
o.UseKyaml = konfig.FlagEnableKyamlDefaultValue
return o
}
// This has no impact on Builtin plugins, as they are always enabled. // This has no impact on Builtin plugins, as they are always enabled.
func (th Harness) MakeOptionsPluginsDisabled() krusty.Options { func (th Harness) MakeOptionsPluginsDisabled() krusty.Options {
return *krusty.MakeDefaultOptions() return *krusty.MakeDefaultOptions()