mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
if setter value ends in colon, treat it as a string
This commit is contained in:
@@ -266,7 +266,7 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
|
|||||||
// document that we can use for validation.
|
// document that we can use for validation.
|
||||||
var tmplText string
|
var tmplText string
|
||||||
if sch.Items != nil && sch.Items.Schema != nil &&
|
if sch.Items != nil && sch.Items.Schema != nil &&
|
||||||
sch.Items.Schema.Type.Contains("string") {
|
shouldQuoteSetterValue(ext.Setter.ListValues, sch.Items.Schema.Type) {
|
||||||
// If string is one of the legal types for the value, we
|
// If string is one of the legal types for the value, we
|
||||||
// output it with quotes in the yaml document to make sure it
|
// output it with quotes in the yaml document to make sure it
|
||||||
// is later parsed as a string.
|
// is later parsed as a string.
|
||||||
@@ -295,7 +295,7 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
|
|||||||
var format string
|
var format string
|
||||||
// Only add quotes around the value is string is one of the
|
// Only add quotes around the value is string is one of the
|
||||||
// types in the schema.
|
// types in the schema.
|
||||||
if sch.Type.Contains("string") {
|
if shouldQuoteSetterValue([]string{ext.Setter.Value}, sch.Type) {
|
||||||
format = "%s: \"%s\""
|
format = "%s: \"%s\""
|
||||||
} else {
|
} else {
|
||||||
format = "%s: %s"
|
format = "%s: %s"
|
||||||
@@ -315,6 +315,22 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shouldQuoteSetterValue returns true if string is one of the types in the
|
||||||
|
// schema, or if the value ends in a ':' (the yaml parser gets confused by
|
||||||
|
// the ':' at the end unless the value is quoted)
|
||||||
|
func shouldQuoteSetterValue(a []string, schType spec.StringOrArray) bool {
|
||||||
|
if schType.Contains("string") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range a {
|
||||||
|
if strings.HasSuffix(s, ":") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// fixSchemaTypes traverses the schema and checks for some common
|
// fixSchemaTypes traverses the schema and checks for some common
|
||||||
// errors for the type field. This currently involves users using
|
// errors for the type field. This currently involves users using
|
||||||
// 'int' instead of 'integer' and 'bool' instead of 'boolean'. Early versions
|
// 'int' instead of 'integer' and 'bool' instead of 'boolean'. Early versions
|
||||||
|
|||||||
@@ -730,6 +730,73 @@ metadata:
|
|||||||
name: nginx-deployment
|
name: nginx-deployment
|
||||||
annotations:
|
annotations:
|
||||||
foo: true # {"$ref": "#/definitions/io.k8s.cli.setters.foo"}
|
foo: true # {"$ref": "#/definitions/io.k8s.cli.setters.foo"}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "set-quoted-value-with-colon",
|
||||||
|
description: "if a value ends in ':', we should accept it",
|
||||||
|
setter: "app",
|
||||||
|
openapi: `
|
||||||
|
openAPI:
|
||||||
|
definitions:
|
||||||
|
io.k8s.cli.setters.app:
|
||||||
|
x-k8s-cli:
|
||||||
|
setter:
|
||||||
|
name: app
|
||||||
|
value: "value:"
|
||||||
|
`,
|
||||||
|
input: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
app: nginx # {"$ref": "#/definitions/io.k8s.cli.setters.app"}
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
annotations:
|
||||||
|
app: "value:" # {"$ref": "#/definitions/io.k8s.cli.setters.app"}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "set-quoted-list-values-with-colon",
|
||||||
|
setter: "args",
|
||||||
|
openapi: `
|
||||||
|
openAPI:
|
||||||
|
definitions:
|
||||||
|
io.k8s.cli.setters.args:
|
||||||
|
x-k8s-cli:
|
||||||
|
type: array
|
||||||
|
setter:
|
||||||
|
name: args
|
||||||
|
listValues: ["1:", "2:", "3:"]
|
||||||
|
`,
|
||||||
|
input: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
spec:
|
||||||
|
# {"$ref": "#/definitions/io.k8s.cli.setters.args"}
|
||||||
|
replicas:
|
||||||
|
- 4
|
||||||
|
- 5
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: nginx-deployment
|
||||||
|
spec:
|
||||||
|
# {"$ref": "#/definitions/io.k8s.cli.setters.args"}
|
||||||
|
replicas:
|
||||||
|
- "1:"
|
||||||
|
- "2:"
|
||||||
|
- "3:"
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user