From 4908654c0932d72b4f63de8002f58d364f03e327 Mon Sep 17 00:00:00 2001 From: Natasha Sarkar Date: Wed, 16 Dec 2020 17:46:09 -0800 Subject: [PATCH] if setter value ends in colon, treat it as a string --- kyaml/setters2/set.go | 20 ++++++++++-- kyaml/setters2/set_test.go | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/kyaml/setters2/set.go b/kyaml/setters2/set.go index 067f40915..1dcaee4cf 100644 --- a/kyaml/setters2/set.go +++ b/kyaml/setters2/set.go @@ -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 diff --git a/kyaml/setters2/set_test.go b/kyaml/setters2/set_test.go index 726d4ec5b..107c6fc0c 100644 --- a/kyaml/setters2/set_test.go +++ b/kyaml/setters2/set_test.go @@ -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:" `, }, }