diff --git a/cmd/config/internal/commands/cmdcreatesetter_test.go b/cmd/config/internal/commands/cmdcreatesetter_test.go index ff6da4f31..001de307e 100644 --- a/cmd/config/internal/commands/cmdcreatesetter_test.go +++ b/cmd/config/internal/commands/cmdcreatesetter_test.go @@ -62,7 +62,7 @@ kind: Deployment metadata: name: nginx-deployment spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { @@ -124,7 +124,7 @@ kind: Deployment metadata: name: nginx-deployment spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, @@ -165,7 +165,7 @@ apiVersion: example.com/v1beta1 kind: Example spec: list: - - "a" # {"$ref":"#/definitions/io.k8s.cli.setters.list"} + - "a" # {"$openapi":"list"} `, }, } diff --git a/cmd/config/internal/commands/cmdcreatesubstitution_test.go b/cmd/config/internal/commands/cmdcreatesubstitution_test.go index 90ffc854b..505afbf39 100644 --- a/cmd/config/internal/commands/cmdcreatesubstitution_test.go +++ b/cmd/config/internal/commands/cmdcreatesubstitution_test.go @@ -99,7 +99,7 @@ spec: spec: containers: - name: nginx - image: nginx:1.7.9 # {"$ref":"#/definitions/io.k8s.cli.substitutions.my-image-subst"} + image: nginx:1.7.9 # {"$openapi":"my-image-subst"} - name: sidecar image: sidecar:1.7.9 `, @@ -181,7 +181,7 @@ spec: spec: containers: - name: nginx - image: something/nginx::1.7.9/nginxotherthing # {"$ref":"#/definitions/io.k8s.cli.substitutions.my-image-subst"} + image: something/nginx::1.7.9/nginxotherthing # {"$openapi":"my-image-subst"} - name: sidecar image: sidecar:1.7.9 `, diff --git a/cmd/config/internal/commands/cmdset_test.go b/cmd/config/internal/commands/cmdset_test.go index c10edcf08..5e253573c 100644 --- a/cmd/config/internal/commands/cmdset_test.go +++ b/cmd/config/internal/commands/cmdset_test.go @@ -50,7 +50,7 @@ kind: Deployment metadata: name: nginx-deployment spec: - replicas: 3 # {"$openAPI":"replicas"} + replicas: 3 # {"$openapi":"replicas"} `, expectedOpenAPI: ` apiVersion: v1alpha1 @@ -71,7 +71,7 @@ kind: Deployment metadata: name: nginx-deployment spec: - replicas: 4 # {"$openAPI":"replicas"} + replicas: 4 # {"$openapi":"replicas"} `, }, { @@ -161,7 +161,7 @@ spec: spec: containers: - name: nginx - image: nginx:1.7.9 # {"$openAPI":"image"} + image: nginx:1.7.9 # {"$openapi":"image"} - name: sidecar image: sidecar:1.7.9 `, @@ -203,7 +203,7 @@ spec: spec: containers: - name: nginx - image: nginx:1.8.1 # {"$openAPI":"image"} + image: nginx:1.8.1 # {"$openapi":"image"} - name: sidecar image: sidecar:1.7.9 `, diff --git a/kyaml/fieldmeta/fieldmeta.go b/kyaml/fieldmeta/fieldmeta.go index 663832efc..c8597a29e 100644 --- a/kyaml/fieldmeta/fieldmeta.go +++ b/kyaml/fieldmeta/fieldmeta.go @@ -5,6 +5,7 @@ package fieldmeta import ( "encoding/json" + "fmt" "reflect" "strconv" "strings" @@ -87,7 +88,7 @@ func (fm *FieldMeta) processShortHand(comment string) bool { if err != nil { return false } - name := input[ShortHandRef] + name := input[shortHandRef] if name == "" { return false } @@ -147,11 +148,11 @@ func (fm *FieldMeta) Write(n *yaml.RNode) error { } else { delete(fm.Schema.VendorExtensible.Extensions, "x-kustomize") } - b, err := json.Marshal(fm.Schema) - if err != nil { - return errors.Wrap(err) - } - n.YNode().LineComment = string(b) + + // 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]) return nil } @@ -230,7 +231,15 @@ const ( // DefinitionsPrefix is the prefix used to reference definitions in the OpenAPI DefinitionsPrefix = "#/definitions/" - - // ShortHandRef is the shorthand reference to setters and substitutions - ShortHandRef = "$openAPI" ) + +// shortHandRef is the shorthand reference to setters and substitutions +var shortHandRef = "$openapi" + +func SetShortHandRef(ref string) { + shortHandRef = ref +} + +func ShortHandRef() string { + return shortHandRef +} diff --git a/kyaml/setters2/add_test.go b/kyaml/setters2/add_test.go index ab5524287..9516612c3 100644 --- a/kyaml/setters2/add_test.go +++ b/kyaml/setters2/add_test.go @@ -42,7 +42,7 @@ kind: Deployment metadata: name: nginx-deployment spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { @@ -67,9 +67,9 @@ kind: Deployment metadata: name: nginx-deployment annotations: - something: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + something: 3 # {"$openapi":"replicas"} spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { @@ -97,7 +97,7 @@ metadata: annotations: something: 3 spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { @@ -123,9 +123,9 @@ kind: Deployment metadata: name: nginx-deployment annotations: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { @@ -153,7 +153,7 @@ metadata: annotations: replicas: 3 spec: - replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + replicas: 3 # {"$openapi":"replicas"} `, }, { diff --git a/kyaml/setters2/example_test.go b/kyaml/setters2/example_test.go index 10e3c918a..8bb36c485 100644 --- a/kyaml/setters2/example_test.go +++ b/kyaml/setters2/example_test.go @@ -162,7 +162,7 @@ spec: // annotations: // something: 3 // spec: - // replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + // replicas: 3 # {"$openapi":"replicas"} } // ExampleAdd demonstrates adding a setter reference to fields. @@ -196,7 +196,7 @@ spec: // metadata: // name: nginx-deployment // annotations: - // something: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + // something: 3 # {"$openapi":"replicas"} // spec: - // replicas: 3 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"} + // replicas: 3 # {"$openapi":"replicas"} }