mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
Avoid manual step for creating array setters
This commit is contained in:
@@ -30,6 +30,9 @@ type Add struct {
|
||||
|
||||
// Ref is the OpenAPI reference to set on the matching fields as a comment.
|
||||
Ref string
|
||||
|
||||
// ListValues are the value of a list setter.
|
||||
ListValues []string
|
||||
}
|
||||
|
||||
// Filter implements yaml.Filter
|
||||
@@ -48,6 +51,37 @@ func (a *Add) visitSequence(_ *yaml.RNode, _ string, _ *openapi.ResourceSchema)
|
||||
return nil
|
||||
}
|
||||
|
||||
// visitMapping implements visitor
|
||||
// visitMapping visits the fields in input MappingNode and adds setter/subst ref
|
||||
// if the path path spec matches with input FiledName
|
||||
func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error {
|
||||
return object.VisitFields(func(node *yaml.MapNode) error {
|
||||
if node.Value.YNode().Kind != yaml.SequenceNode {
|
||||
return nil
|
||||
}
|
||||
|
||||
key, err := node.Key.String()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// derive the list values for the sequence node to write it to openAPI definitions
|
||||
var values []string
|
||||
for _, sc := range node.Value.Content() {
|
||||
values = append(values, sc.Value)
|
||||
}
|
||||
a.ListValues = values
|
||||
|
||||
// pathToKey refers to the path address of the key node ex: metadata.annotations
|
||||
// p is the path till parent node, pathToKey is obtained by appending child key
|
||||
pathToKey := p + "." + strings.Trim(key, "\n")
|
||||
if a.FieldName != "" && strings.HasSuffix(pathToKey, a.FieldName) {
|
||||
return a.addRef(node.Key)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// visitScalar implements visitor
|
||||
// visitScalar will set the field metadata on each scalar field whose name + value match
|
||||
func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error {
|
||||
@@ -58,7 +92,11 @@ func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchem
|
||||
if a.FieldValue != "" && a.FieldValue != object.YNode().Value {
|
||||
return nil
|
||||
}
|
||||
return a.addRef(object)
|
||||
}
|
||||
|
||||
// addRef adds the setter/subst ref to the object node as a line comment
|
||||
func (a *Add) addRef(object *yaml.RNode) error {
|
||||
// read the field metadata
|
||||
fm := fieldmeta.FieldMeta{}
|
||||
if err := fm.Read(object); err != nil {
|
||||
@@ -85,7 +123,7 @@ type SetterDefinition struct {
|
||||
Name string `yaml:"name"`
|
||||
|
||||
// Value is the value of the setter.
|
||||
Value string `yaml:"value"`
|
||||
Value string `yaml:"value,omitempty"`
|
||||
|
||||
// ListValues are the value of a list setter.
|
||||
ListValues []string `yaml:"listValues,omitempty"`
|
||||
|
||||
@@ -45,6 +45,10 @@ func (s *Set) isMatch(name string) bool {
|
||||
return s.SetAll || s.Name == name
|
||||
}
|
||||
|
||||
func (s *Set) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// visitSequence will perform setters for sequences
|
||||
func (s *Set) visitSequence(object *yaml.RNode, p string, schema *openapi.ResourceSchema) error {
|
||||
ext, err := getExtFromComment(schema)
|
||||
|
||||
@@ -60,16 +60,32 @@ func (c SetterCreator) Create(openAPIPath, resourcesPath string) error {
|
||||
|
||||
// Update the resources with the setter reference
|
||||
inout := &kio.LocalPackageReadWriter{PackagePath: resourcesPath}
|
||||
return kio.Pipeline{
|
||||
Inputs: []kio.Reader{inout},
|
||||
Filters: []kio.Filter{kio.FilterAll(
|
||||
&setters2.Add{
|
||||
FieldName: c.FieldName,
|
||||
FieldValue: c.FieldValue,
|
||||
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + c.Name,
|
||||
})},
|
||||
a := &setters2.Add{
|
||||
FieldName: c.FieldName,
|
||||
FieldValue: c.FieldValue,
|
||||
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + c.Name,
|
||||
}
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{inout},
|
||||
Filters: []kio.Filter{kio.FilterAll(a)},
|
||||
Outputs: []kio.Writer{inout},
|
||||
}.Execute()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if the setter is of array type write the derived list values back to
|
||||
// openAPI definitions
|
||||
if len(a.ListValues) > 0 {
|
||||
sd.ListValues = a.ListValues
|
||||
sd.Value = ""
|
||||
if err := sd.AddToFile(openAPIPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// schemaFromFile reads the contents from schemaPath and returns schema
|
||||
|
||||
@@ -23,6 +23,12 @@ type visitor interface {
|
||||
// path is the path to the field
|
||||
// oa is the OpenAPI schema for the field
|
||||
visitSequence(node *yaml.RNode, path string, oa *openapi.ResourceSchema) error
|
||||
|
||||
// visitMapping is called for each Mapping field value on a resource
|
||||
// node is the mapping field value
|
||||
// path is the path to the field
|
||||
// oa is the OpenAPI schema for the field
|
||||
visitMapping(node *yaml.RNode, path string, oa *openapi.ResourceSchema) error
|
||||
}
|
||||
|
||||
// accept invokes the appropriate function on v for each field in object
|
||||
@@ -39,6 +45,9 @@ func acceptImpl(v visitor, object *yaml.RNode, p string, oa *openapi.ResourceSch
|
||||
// Traverse the child of the document
|
||||
return accept(v, yaml.NewRNode(object.YNode()))
|
||||
case yaml.MappingNode:
|
||||
if err := v.visitMapping(object, p, oa); err != nil {
|
||||
return err
|
||||
}
|
||||
return object.VisitFields(func(node *yaml.MapNode) error {
|
||||
// get the schema for the field and propagate it
|
||||
oa = getSchema(node.Key, oa, node.Key.YNode().Value)
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestIsYaml1_1NonString(t *testing.T) {
|
||||
{val: "2", expected: true},
|
||||
{val: "true", expected: true},
|
||||
{val: "1.0\nhello", expected: false}, // multiline strings should always be false
|
||||
{val: "", expected: false}, // empty string should be considered a string
|
||||
{val: "", expected: false}, // empty string should be considered a string
|
||||
}
|
||||
|
||||
for k := range valueToTagMap {
|
||||
|
||||
Reference in New Issue
Block a user