From d2fb2bf8367c17b60a3909a41e58679bf6daf2bc Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Fri, 30 Oct 2020 00:06:18 -0700 Subject: [PATCH] UpdateOpenAPI CleanUp Logic SetterInherit --- internal/util/setters/setters.go | 13 ++++--- internal/util/setters/setters_test.go | 51 ++++++++++++++++++++------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/internal/util/setters/setters.go b/internal/util/setters/setters.go index f6ac8fe1cc..b41effef37 100644 --- a/internal/util/setters/setters.go +++ b/internal/util/setters/setters.go @@ -312,7 +312,7 @@ func (a AutoSet) SetInheritedSetters() error { continue } - if err := openapi.AddSchemaFromFile(parentKptfilePath); err != nil { + if err := openapi.DeleteSchemaInFile(parentKptfilePath); err != nil { return err } @@ -324,6 +324,9 @@ func (a AutoSet) SetInheritedSetters() error { // for each setter in target path, derive the setter values from parent package // openAPI schema definitions and set them for _, ref := range targetPkgRefs { + if err := openapi.AddSchemaFromFile(parentKptfilePath); err != nil { + return err + } sch := openapi.Schema().Definitions[ref] cliExt, err := setters2.GetExtFromSchema(&sch) if cliExt == nil || cliExt.Setter == nil || err != nil { @@ -336,6 +339,7 @@ func (a AutoSet) SetInheritedSetters() error { // skip if the setter is already set on local continue } + fs := &settersutil.FieldSetter{ Name: cliExt.Setter.Name, Value: cliExt.Setter.Value, @@ -361,10 +365,9 @@ func (a AutoSet) SetInheritedSetters() error { fmt.Fprintf(a.Writer, format, count, cliExt.Setter.Name, cliExt.Setter.ListValues, targetPath, parentKptfilePath) } } - } - - if err := openapi.DeleteSchemaInFile(parentKptfilePath); err != nil { - return err + if err := openapi.DeleteSchemaInFile(parentKptfilePath); err != nil { + return err + } } } return nil diff --git a/internal/util/setters/setters_test.go b/internal/util/setters/setters_test.go index 23055c559b..5c78995d44 100644 --- a/internal/util/setters/setters_test.go +++ b/internal/util/setters/setters_test.go @@ -25,6 +25,7 @@ import ( "github.com/GoogleContainerTools/kpt/pkg/kptfile" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/copyutil" + "sigs.k8s.io/kustomize/kyaml/fieldmeta" "sigs.k8s.io/kustomize/kyaml/openapi" ) @@ -261,11 +262,17 @@ openAPI: setter: name: namespace value: parent_namespace - isSet: true`, + isSet: true + io.k8s.cli.setters.name: + x-k8s-cli: + setter: + name: name + value: parent_name`, nestedConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"}`, + namespace: child_namespace # {"$kpt-set":"namespace"} + name: child_name # {"$kpt-set":"name"}`, nestedKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile metadata: @@ -277,11 +284,17 @@ openAPI: setter: name: namespace value: child_namespace + io.k8s.cli.setters.name: + x-k8s-cli: + setter: + name: name + value: child_name `, childConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"}`, + namespace: child_namespace # {"$kpt-set":"namespace"} + name: child_name # {"$kpt-set":"name"}`, childKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile metadata: @@ -293,6 +306,11 @@ openAPI: setter: name: namespace value: child_namespace + io.k8s.cli.setters.name: + x-k8s-cli: + setter: + name: name + value: child_name `, expectedChildKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile @@ -306,14 +324,22 @@ openAPI: name: namespace value: parent_namespace isSet: true + io.k8s.cli.setters.name: + x-k8s-cli: + setter: + name: name + value: parent_name `, expectedChildConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: parent_namespace # {"$openapi":"namespace"} + namespace: parent_namespace # {"$kpt-set":"namespace"} + name: parent_name # {"$kpt-set":"name"} `, expectedOut: `automatically set 1 field(s) for setter "namespace" to value "parent_namespace" in package "${childPkg}" derived from parent "${parentPkgKptfile}" +automatically set 1 field(s) for setter "name" to value "parent_name" in package "${childPkg}" derived from parent "${parentPkgKptfile}" automatically set 1 field(s) for setter "namespace" to value "parent_namespace" in package "${nestedPkg}" derived from parent "${childPkg}/Kptfile" +automatically set 1 field(s) for setter "name" to value "parent_name" in package "${nestedPkg}" derived from parent "${childPkg}/Kptfile" `, }, { @@ -334,7 +360,7 @@ openAPI: childConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"} + namespace: child_namespace # {"$kpt-set":"namespace"} `, childKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile @@ -363,7 +389,7 @@ openAPI: expectedChildConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"} + namespace: child_namespace # {"$kpt-set":"namespace"} `, }, { @@ -384,7 +410,7 @@ openAPI: childConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"} + namespace: child_namespace # {"$kpt-set":"namespace"} `, childKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile @@ -415,7 +441,7 @@ openAPI: expectedChildConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"} + namespace: child_namespace # {"$kpt-set":"namespace"} `, expectedOut: `failed to set "namespace" automatically in package "${childPkg}" with error: ` + `The input value doesn't validate against provided OpenAPI schema: validation failure list: @@ -441,7 +467,7 @@ openAPI: childConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"}`, + namespace: child_namespace # {"$kpt-set":"namespace"}`, childKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile metadata: @@ -471,7 +497,7 @@ openAPI: expectedChildConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"}`, + namespace: child_namespace # {"$kpt-set":"namespace"}`, }, { name: "inherit-defalut-values-from-parent", @@ -491,7 +517,7 @@ openAPI: childConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: child_namespace # {"$openapi":"namespace"}`, + namespace: child_namespace # {"$kpt-set":"namespace"}`, childKptfile: `apiVersion: krm.dev/v1alpha1 kind: Kptfile metadata: @@ -519,7 +545,7 @@ openAPI: expectedChildConfigFile: `apiVersion: apps/v1 kind: Deployment metadata: - namespace: parent_namespace # {"$openapi":"namespace"} + namespace: parent_namespace # {"$kpt-set":"namespace"} `, expectedOut: `automatically set 1 field(s) for setter "namespace" to value "parent_namespace" in package "${childPkg}" derived from parent "${parentPkgKptfile}" `, @@ -527,6 +553,7 @@ metadata: } for i := range tests { test := tests[i] + fieldmeta.SetShortHandRef("$kpt-set") t.Run(test.name, func(t *testing.T) { // reset the openAPI afterward openapi.ResetOpenAPI()