Merge remote-tracking branch 'upstream/master' into release-cmd/config-v0.1

This commit is contained in:
Morten Torkildsen
2020-05-21 15:34:04 -07:00
93 changed files with 3725 additions and 169 deletions

View File

@@ -128,10 +128,13 @@ github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=

View File

@@ -48,6 +48,9 @@ func NewCreateSetterRunner(parent string) *CreateSetterRunner {
set.Flags().MarkHidden("partial")
set.Flags().StringVar(&setterVersion, "version", "",
"use this version of the setter format")
set.Flags().StringVar(&r.CreateSetter.SchemaPath, "schema-path", "",
`openAPI schema file path for setter constraints -- file content `+
`e.g. {"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`)
set.Flags().MarkHidden("version")
fixDocs(parent, set)
r.Command = set

View File

@@ -21,6 +21,7 @@ func TestCreateSetterCommand(t *testing.T) {
name string
input string
args []string
schema string
out string
inputOpenAPI string
expectedOpenAPI string
@@ -85,6 +86,88 @@ openAPI:
`,
err: "substitution with name my-image already exists, substitution and setter can't have same name",
},
{
name: "add replicas with schema",
args: []string{"replicas", "3", "--description", "hello world", "--set-by", "me"},
schema: `{"maximum": 10, "type": "integer"}`,
input: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
`,
expectedOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.replicas:
maximum: 10
type: integer
description: hello world
x-k8s-cli:
setter:
name: replicas
value: "3"
setBy: me
`,
expectedResources: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
`,
},
{
name: "add replicas with schema list values",
args: []string{"list", "a", "--description", "hello world", "--set-by", "me", "--type", "array"},
schema: `{"maxItems": 2, "type": "array", "items": {"type": "string"}}`,
input: `
apiVersion: example.com/v1beta1
kind: Example
spec:
list:
- "a"
`,
inputOpenAPI: `
apiVersion: v1alpha1
kind: Example
`,
expectedOpenAPI: `
apiVersion: v1alpha1
kind: Example
openAPI:
definitions:
io.k8s.cli.setters.list:
items:
type: string
maxItems: 2
type: array
description: hello world
x-k8s-cli:
setter:
name: list
value: a
setBy: me
`,
expectedResources: `
apiVersion: example.com/v1beta1
kind: Example
spec:
list:
- "a" # {"$ref":"#/definitions/io.k8s.cli.setters.list"}
`,
},
}
for i := range tests {
test := tests[i]
@@ -98,10 +181,27 @@ openAPI:
t.FailNow()
}
defer os.Remove(f.Name())
err = ioutil.WriteFile(f.Name(), []byte(test.inputOpenAPI), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}
if test.schema != "" {
sch, err := ioutil.TempFile("", "schema.json")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.Remove(sch.Name())
err = ioutil.WriteFile(sch.Name(), []byte(test.schema), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}
test.args = append(test.args, "--schema-path", sch.Name())
}
old := ext.GetOpenAPIFile
defer func() { ext.GetOpenAPIFile = old }()
ext.GetOpenAPIFile = func(args []string) (s string, err error) {

View File

@@ -403,6 +403,58 @@ spec:
`,
errMsg: "tag in body should be at least 6 chars long",
},
{
name: "validate openAPI list values",
args: []string{"list", "10", "hi", "true"},
inputOpenAPI: `
kind: Kptfile
openAPI:
definitions:
io.k8s.cli.setters.list:
type: array
maxItems: 2
items:
type: integer
x-k8s-cli:
setter:
name: list
listValues:
- 0
`,
input: `
apiVersion: example.com/v1beta1
kind: Example
spec:
list: # {"$ref":"#/definitions/io.k8s.cli.setters.list"}
- 0
`,
expectedOpenAPI: `
kind: Kptfile
openAPI:
definitions:
io.k8s.cli.setters.list:
type: array
maxItems: 2
items:
type: integer
x-k8s-cli:
setter:
name: list
listValues:
- 0
`,
expectedResources: `
apiVersion: example.com/v1beta1
kind: Example
spec:
list: # {"$ref":"#/definitions/io.k8s.cli.setters.list"}
- 0
`,
errMsg: `list in body must be of type integer: "string"
list in body must be of type integer: "boolean"
list in body should have at most 2 items`,
},
}
for i := range tests {
test := tests[i]