diff --git a/kyaml/setters2/set.go b/kyaml/setters2/set.go index 233d88ff5..af73f074b 100644 --- a/kyaml/setters2/set.go +++ b/kyaml/setters2/set.go @@ -242,6 +242,7 @@ func (s *Set) set(field *yaml.RNode, ext *CliExtension, sch *spec.Schema) (bool, // validateAgainstSchema validates the input setter value against user provided // openAI schema func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error { + fixSchemaTypes(sch) sc := spec.Schema{} sc.Properties = map[string]spec.Schema{} sc.Properties[ext.Setter.Name] = *sch @@ -301,6 +302,33 @@ func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error { return nil } +// 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 +// of setters didn't validate this, so there are users that have invalid +// types in their packages. +func fixSchemaTypes(sc *spec.Schema) { + for i := range sc.Type { + currentType := sc.Type[i] + if currentType == "int" { + sc.Type[i] = "integer" + } + if currentType == "bool" { + sc.Type[i] = "boolean" + } + } + + if items := sc.Items; items != nil { + if items.Schema != nil { + fixSchemaTypes(items.Schema) + } + for i := range items.Schemas { + schema := items.Schemas[i] + fixSchemaTypes(&schema) + } + } +} + // SetOpenAPI updates a setter value type SetOpenAPI struct { // Name is the name of the setter to add diff --git a/kyaml/setters2/set_test.go b/kyaml/setters2/set_test.go index 0352393e3..d1d7d289e 100644 --- a/kyaml/setters2/set_test.go +++ b/kyaml/setters2/set_test.go @@ -666,6 +666,68 @@ spec: - "1" - "2" - "3" + `, + }, + { + name: "set-with-invalid-type-int", + description: "if a type is set to int instead of integer, we accept it", + setter: "foo", + openapi: ` +openAPI: + definitions: + io.k8s.cli.setters.foo: + x-k8s-cli: + setter: + name: foo + value: "4" + type: int + `, + input: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + annotations: + foo: 3 # {"$ref": "#/definitions/io.k8s.cli.setters.foo"} + `, + expected: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + annotations: + foo: 4 # {"$ref": "#/definitions/io.k8s.cli.setters.foo"} + `, + }, + { + name: "set-with-invalid-type-bool", + description: "if a type is set to bool instead of boolean, we accept it", + setter: "foo", + openapi: ` +openAPI: + definitions: + io.k8s.cli.setters.foo: + x-k8s-cli: + setter: + name: foo + value: "true" + type: bool + `, + input: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + annotations: + foo: false # {"$ref": "#/definitions/io.k8s.cli.setters.foo"} + `, + expected: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment + annotations: + foo: true # {"$ref": "#/definitions/io.k8s.cli.setters.foo"} `, }, }