Refactor openAPI

Refactor global openapi
This commit is contained in:
Phani Teja Marupaka
2020-11-10 12:50:24 -08:00
parent b2ba82a0bd
commit b6a4dd446a
27 changed files with 178 additions and 503 deletions

View File

@@ -6,8 +6,8 @@ package settersutil
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/go-openapi/spec"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/setters2"
@@ -40,6 +40,8 @@ type FieldSetter struct {
RecurseSubPackages bool
IsSet bool
SettersSchema *spec.Schema
}
func (fs *FieldSetter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
@@ -79,17 +81,17 @@ func (fs FieldSetter) Set() (int, error) {
}
// Load the updated definitions
clean, err := openapi.AddSchemaFromFile(fs.OpenAPIPath)
sc, err := openapi.SchemaFromFile(fs.OpenAPIPath)
if err != nil {
return 0, err
}
defer clean()
fs.SettersSchema = sc
// Update the resources with the new value
// Set NoDeleteFiles to true as SetAll will return only the nodes of files which should be updated and
// hence, rest of the files should not be deleted
inout := &kio.LocalPackageReadWriter{PackagePath: fs.ResourcesPath, NoDeleteFiles: true, PackageFileName: fs.OpenAPIFileName}
s := &setters2.Set{Name: fs.Name}
s := &setters2.Set{Name: fs.Name, SettersSchema: sc}
err = kio.Pipeline{
Inputs: []kio.Reader{inout},
Filters: []kio.Filter{setters2.SetAll(s)},
@@ -109,20 +111,12 @@ func (fs FieldSetter) Set() (int, error) {
// file and sets all values for the resource configs in the provided destination directories.
// If syncOpenAPI is true, the openAPI files in destination directories are also
// updated with the setter values in the input openAPI file
func SetAllSetterDefinitions(syncOpenAPI bool, openAPIPath string, dirs ...string) error {
clean, err := openapi.AddSchemaFromFile(openAPIPath)
func SetAllSetterDefinitions(openAPIPath string, dirs ...string) error {
sc, err := openapi.SchemaFromFile(openAPIPath)
if err != nil {
return err
}
defer clean()
for _, destDir := range dirs {
if syncOpenAPI {
openAPIFileName := filepath.Base(openAPIPath)
err := syncOpenAPIValuesWithSchema(filepath.Join(destDir, openAPIFileName))
if err != nil {
return err
}
}
rw := &kio.LocalPackageReadWriter{
PackagePath: destDir,
// set output won't include resources from files which
@@ -134,7 +128,7 @@ func SetAllSetterDefinitions(syncOpenAPI bool, openAPIPath string, dirs ...strin
err := kio.Pipeline{
Inputs: []kio.Reader{rw},
// Set all of the setters
Filters: []kio.Filter{setters2.SetAll(&setters2.Set{SetAll: true})},
Filters: []kio.Filter{setters2.SetAll(&setters2.Set{SetAll: true, SettersSchema: sc})},
Outputs: []kio.Writer{rw},
}.Execute()
if err != nil {
@@ -143,31 +137,3 @@ func SetAllSetterDefinitions(syncOpenAPI bool, openAPIPath string, dirs ...strin
}
return nil
}
// syncOpenAPIValuesWithSchema syncs the setter values in global openAPI schema
// with the ones in openAPIPath and writes them back
func syncOpenAPIValuesWithSchema(openAPIPath string) error {
refs, err := openapi.DefinitionRefs(openAPIPath)
if err != nil {
return err
}
for _, ref := range refs {
sch := openapi.Schema().Definitions[ref]
cliExt, err := setters2.GetExtFromSchema(&sch)
if cliExt == nil || cliExt.Setter == nil || err != nil {
// if the ref doesn't exist in global schema or if it is not a setter
// continue, as there might be setters which are not present global schema
continue
}
soa := setters2.SetOpenAPI{
Name: cliExt.Setter.Name,
Value: cliExt.Setter.Value,
ListValues: cliExt.Setter.ListValues,
IsSet: true,
}
if err := soa.UpdateFile(openAPIPath); err != nil {
return err
}
}
return nil
}