From c419c1efc3b61519efda63cd3b201c72b8865fb9 Mon Sep 17 00:00:00 2001 From: Morten Torkildsen Date: Fri, 10 Jul 2020 17:09:43 -0700 Subject: [PATCH] Allow setters/substitutions with . in the name --- .../internal/commands/cmdcreatesetter_test.go | 35 +++++++++++++++++++ kyaml/fieldmeta/fieldmeta.go | 14 ++++++-- kyaml/kio/byteio_reader_test.go | 2 +- kyaml/setters2/add_test.go | 23 ++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/cmd/config/internal/commands/cmdcreatesetter_test.go b/cmd/config/internal/commands/cmdcreatesetter_test.go index 4669daa15..ad6f2be81 100644 --- a/cmd/config/internal/commands/cmdcreatesetter_test.go +++ b/cmd/config/internal/commands/cmdcreatesetter_test.go @@ -372,6 +372,41 @@ metadata: name: nginx-deployment spec: replicas: 3 # {"$openapi":"replicas"} + `, + }, + { + name: "add setter with . in the name", + args: []string{"foo.bar", "3"}, + 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.foo.bar: + x-k8s-cli: + setter: + name: foo.bar + value: "3" + `, + expectedResources: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment +spec: + replicas: 3 # {"$openapi":"foo.bar"} `, }, } diff --git a/kyaml/fieldmeta/fieldmeta.go b/kyaml/fieldmeta/fieldmeta.go index 30d48cc58..51ff0415a 100644 --- a/kyaml/fieldmeta/fieldmeta.go +++ b/kyaml/fieldmeta/fieldmeta.go @@ -155,8 +155,18 @@ func (fm *FieldMeta) Write(n *yaml.RNode) error { if fm.Schema.Ref.String() != "" { // Ex: {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} should be converted to // {"openAPI":"replicas"} and added to the line comment - arr := strings.Split(fm.Schema.Ref.String(), ".") - n.YNode().LineComment = fmt.Sprintf(`{"%s":"%s"}`, shortHandRef, arr[len(arr)-1]) + ref := fm.Schema.Ref.String() + var shortHandRefValue string + switch { + case strings.HasPrefix(ref, DefinitionsPrefix+SetterDefinitionPrefix): + shortHandRefValue = strings.TrimPrefix(ref, DefinitionsPrefix+SetterDefinitionPrefix) + case strings.HasPrefix(ref, DefinitionsPrefix+SubstitutionDefinitionPrefix): + shortHandRefValue = strings.TrimPrefix(ref, DefinitionsPrefix+SubstitutionDefinitionPrefix) + default: + return fmt.Errorf("unexpected ref format: %s", ref) + } + n.YNode().LineComment = fmt.Sprintf(`{"%s":"%s"}`, shortHandRef, + shortHandRefValue) } else { n.YNode().LineComment = "" } diff --git a/kyaml/kio/byteio_reader_test.go b/kyaml/kio/byteio_reader_test.go index c642affe6..9b13aa29f 100644 --- a/kyaml/kio/byteio_reader_test.go +++ b/kyaml/kio/byteio_reader_test.go @@ -303,7 +303,7 @@ metadata: // // { - name: "windows_line_ending", + name: "windows_line_ending", input: "\r\n---\r\na: b # first resource\r\nc: d\r\n---\r\n# second resource\r\ne: f\r\ng:\r\n- h\r\n---\r\n\r\n---\r\n i: j", expectedItems: []string{ `a: b # first resource diff --git a/kyaml/setters2/add_test.go b/kyaml/setters2/add_test.go index 9516612c3..49ed77db6 100644 --- a/kyaml/setters2/add_test.go +++ b/kyaml/setters2/add_test.go @@ -169,6 +169,29 @@ metadata: name: nginx-deployment spec: replicas: 3 + `, + }, + { + name: "ref has . in name of setter", + add: Add{ + FieldValue: "3", + Ref: "#/definitions/io.k8s.cli.setters.foo.bar", + }, + input: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment +spec: + replicas: 3 + `, + expected: ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-deployment +spec: + replicas: 3 # {"$openapi":"foo.bar"} `, }, }