Skip to content

Commit

Permalink
Merge pull request #1172 from phanimarupaka/UpdateOpenAPICleanUpLogic…
Browse files Browse the repository at this point in the history
…ForSetterInherit

Update OpenAPI CleanUp Logic SetterInherit
  • Loading branch information
phanimarupaka authored Oct 30, 2020
2 parents 4b6af96 + d2fb2bf commit 2a7ae8f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
13 changes: 8 additions & 5 deletions internal/util/setters/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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
Expand Down
51 changes: 39 additions & 12 deletions internal/util/setters/setters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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"
`,
},
{
Expand All @@ -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
Expand Down Expand Up @@ -363,7 +389,7 @@ openAPI:
expectedChildConfigFile: `apiVersion: apps/v1
kind: Deployment
metadata:
namespace: child_namespace # {"$openapi":"namespace"}
namespace: child_namespace # {"$kpt-set":"namespace"}
`,
},
{
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -519,14 +545,15 @@ 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}"
`,
},
}
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()
Expand Down

0 comments on commit 2a7ae8f

Please sign in to comment.