mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Refactor openAPI
Refactor global openapi
This commit is contained in:
@@ -53,6 +53,8 @@ type SubstitutionCreator struct {
|
||||
|
||||
// Path to resources folder
|
||||
ResourcesPath string
|
||||
|
||||
SettersSchema *spec.Schema
|
||||
}
|
||||
|
||||
func (c *SubstitutionCreator) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
@@ -64,7 +66,7 @@ func (c SubstitutionCreator) Create() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
values, err := markersAndRefs(c.Name, c.Pattern)
|
||||
values, err := c.markersAndRefs(c.Name, c.Pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -93,11 +95,11 @@ func (c SubstitutionCreator) Create() error {
|
||||
}
|
||||
|
||||
// Load the updated definitions
|
||||
clean, err := openapi.AddSchemaFromFile(c.OpenAPIPath)
|
||||
sc, err := openapi.SchemaFromFile(c.OpenAPIPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer clean()
|
||||
c.SettersSchema = sc
|
||||
|
||||
visited := sets.String{}
|
||||
ref, err := spec.NewRef(fieldmeta.DefinitionsPrefix + fieldmeta.SubstitutionDefinitionPrefix + c.Name)
|
||||
@@ -105,7 +107,7 @@ func (c SubstitutionCreator) Create() error {
|
||||
return err
|
||||
}
|
||||
|
||||
schema, err := openapi.Resolve(&ref)
|
||||
schema, err := openapi.Resolve(&ref, c.SettersSchema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -121,14 +123,14 @@ func (c SubstitutionCreator) Create() error {
|
||||
}
|
||||
|
||||
// Load the updated definitions after setters are created
|
||||
clean, err = openapi.AddSchemaFromFile(c.OpenAPIPath)
|
||||
sc, err = openapi.SchemaFromFile(c.OpenAPIPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer clean()
|
||||
c.SettersSchema = sc
|
||||
|
||||
// revert openAPI file if there are cycles detected in created input substitution
|
||||
if err := checkForCycles(ext, visited); err != nil {
|
||||
if err := c.checkForCycles(ext, visited); err != nil {
|
||||
if writeErr := ioutil.WriteFile(c.OpenAPIPath, curOpenAPI, stat.Mode().Perm()); writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
@@ -136,9 +138,10 @@ func (c SubstitutionCreator) Create() error {
|
||||
}
|
||||
|
||||
a := &setters2.Add{
|
||||
FieldName: c.FieldName,
|
||||
FieldValue: c.FieldValue,
|
||||
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SubstitutionDefinitionPrefix + c.Name,
|
||||
FieldName: c.FieldName,
|
||||
FieldValue: c.FieldValue,
|
||||
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SubstitutionDefinitionPrefix + c.Name,
|
||||
SettersSchema: c.SettersSchema,
|
||||
}
|
||||
|
||||
// Update the resources with the substitution reference
|
||||
@@ -158,7 +161,7 @@ func (c SubstitutionCreator) Create() error {
|
||||
|
||||
// createMarkersAndRefs takes the input pattern, creates setter/substitution markers
|
||||
// and corresponding openAPI refs
|
||||
func markersAndRefs(substName, pattern string) ([]setters2.Value, error) {
|
||||
func (c *SubstitutionCreator) markersAndRefs(substName, pattern string) ([]setters2.Value, error) {
|
||||
var values []setters2.Value
|
||||
// extract setter name tokens from pattern enclosed in ${}
|
||||
re := regexp.MustCompile(`\$\{([^}]*)\}`)
|
||||
@@ -180,7 +183,7 @@ func markersAndRefs(substName, pattern string) ([]setters2.Value, error) {
|
||||
}
|
||||
|
||||
var markerRef string
|
||||
subst, _ := openapi.Resolve(&ref)
|
||||
subst, _ := openapi.Resolve(&ref, c.SettersSchema)
|
||||
// check if the substitution exists with the marker name or fall back to creating setter
|
||||
// ref with the name
|
||||
if subst != nil {
|
||||
@@ -246,7 +249,7 @@ func (c SubstitutionCreator) CreateSettersForSubstitution(openAPIPath string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkForCycles(ext *setters2.CliExtension, visited sets.String) error {
|
||||
func (c SubstitutionCreator) checkForCycles(ext *setters2.CliExtension, visited sets.String) error {
|
||||
// check if the substitution has already been visited and throw error as cycles
|
||||
// are not allowed in nested substitutions
|
||||
if visited.Has(ext.Substitution.Name) {
|
||||
@@ -268,7 +271,7 @@ func checkForCycles(ext *setters2.CliExtension, visited sets.String) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
def, err := openapi.Resolve(&ref) // resolve the def to its openAPI def
|
||||
def, err := openapi.Resolve(&ref, c.SettersSchema) // resolve the def to its openAPI def
|
||||
if err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
@@ -279,7 +282,7 @@ func checkForCycles(ext *setters2.CliExtension, visited sets.String) error {
|
||||
|
||||
if defExt.Substitution != nil {
|
||||
// parse recursively if it reference is substitution
|
||||
err := checkForCycles(defExt, visited)
|
||||
err := c.checkForCycles(defExt, visited)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -440,7 +443,7 @@ func (c SubstitutionCreator) validateSubstitutionInfo() error {
|
||||
return err
|
||||
}
|
||||
|
||||
subst, _ := openapi.Resolve(&ref)
|
||||
subst, _ := openapi.Resolve(&ref, c.SettersSchema)
|
||||
// if substitution already exists with the input substitution name, throw error
|
||||
if subst != nil {
|
||||
return errors.Errorf("substitution with name %q already exists", c.Name)
|
||||
@@ -452,7 +455,7 @@ func (c SubstitutionCreator) validateSubstitutionInfo() error {
|
||||
return err
|
||||
}
|
||||
|
||||
setter, _ := openapi.Resolve(&ref)
|
||||
setter, _ := openapi.Resolve(&ref, c.SettersSchema)
|
||||
// if setter already exists with input substitution name, throw error
|
||||
if setter != nil {
|
||||
return errors.Errorf(fmt.Sprintf("setter with name %q already exists, "+
|
||||
|
||||
Reference in New Issue
Block a user