Merge pull request #3366 from natasha41575/QuotedScalarValueWithColon

if setter value ends in colon, treat it as a string
This commit is contained in:
Kubernetes Prow Robot
2020-12-23 11:54:26 -08:00
committed by GitHub
2 changed files with 85 additions and 2 deletions

View File

@@ -266,7 +266,7 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
// document that we can use for validation.
var tmplText string
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
// output it with quotes in the yaml document to make sure it
// is later parsed as a string.
@@ -295,7 +295,7 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
var format string
// Only add quotes around the value is string is one of the
// types in the schema.
if sch.Type.Contains("string") {
if shouldQuoteSetterValue([]string{ext.Setter.Value}, sch.Type) {
format = "%s: \"%s\""
} else {
format = "%s: %s"
@@ -315,6 +315,22 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
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
// errors for the type field. This currently involves users using
// 'int' instead of 'integer' and 'bool' instead of 'boolean'. Early versions

View File

@@ -730,6 +730,73 @@ metadata:
name: nginx-deployment
annotations:
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:"
`,
},
}