Merge pull request #2784 from mortent/HandleIncorrectTypes

Handle some incorrect type values like 'int' and 'bool' in setters
This commit is contained in:
Kubernetes Prow Robot
2020-07-30 12:19:06 -07:00
committed by GitHub
2 changed files with 90 additions and 0 deletions

View File

@@ -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

View File

@@ -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"}
`,
},
}