diff --git a/cmd/config/internal/commands/cmdcreatesetter_test.go b/cmd/config/internal/commands/cmdcreatesetter_test.go index a0890fc67..7f3c58930 100644 --- a/cmd/config/internal/commands/cmdcreatesetter_test.go +++ b/cmd/config/internal/commands/cmdcreatesetter_test.go @@ -55,6 +55,7 @@ openAPI: name: replicas value: "3" setBy: me + count: 1 `, expectedResources: ` apiVersion: apps/v1 @@ -117,6 +118,7 @@ openAPI: name: replicas value: "3" setBy: me + count: 1 `, expectedResources: ` apiVersion: apps/v1 @@ -188,6 +190,7 @@ openAPI: - b - c setBy: me + count: 2 `, expectedResources: ` apiVersion: example.com/v1beta1 @@ -287,6 +290,7 @@ openAPI: - b - c setBy: me + count: 1 `, expectedResources: ` apiVersion: example.com/v1beta1 @@ -326,6 +330,7 @@ openAPI: name: replicas value: "3" setBy: me + count: 1 `, expectedResources: ` apiVersion: apps/v1 diff --git a/cmd/config/internal/commands/e2e/create_setter_test.go b/cmd/config/internal/commands/e2e/create_setter_test.go index 999ec0eeb..9cfeacc3a 100644 --- a/cmd/config/internal/commands/e2e/create_setter_test.go +++ b/cmd/config/internal/commands/e2e/create_setter_test.go @@ -49,6 +49,7 @@ openAPI: setter: name: replicas value: "3" + count: 1 `, }, }, diff --git a/kyaml/setters2/add.go b/kyaml/setters2/add.go index bd8055a60..9ed06bdc3 100644 --- a/kyaml/setters2/add.go +++ b/kyaml/setters2/add.go @@ -37,6 +37,9 @@ type Add struct { // Type is the type of the setter value Type string + + // Count is the number of fields the setter applies to + Count int } // Filter implements yaml.Filter @@ -85,6 +88,7 @@ func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSche "encountered different array values for specified field path: %s, %s", values, a.ListValues) } a.ListValues = values + a.Count++ return a.addRef(node.Key) } return nil @@ -104,6 +108,7 @@ func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchem if a.FieldValue != "" && a.FieldValue != object.YNode().Value { return nil } + a.Count++ return a.addRef(object) } diff --git a/kyaml/setters2/settersutil/settercreator.go b/kyaml/setters2/settersutil/settercreator.go index f49cf57ef..ba6d9cc67 100644 --- a/kyaml/setters2/settersutil/settercreator.go +++ b/kyaml/setters2/settersutil/settercreator.go @@ -6,6 +6,7 @@ package settersutil import ( "io/ioutil" + "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/fieldmeta" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/openapi" @@ -18,10 +19,10 @@ type SetterCreator struct { // Name is the name of the setter to create or update. Name string - Description string - SetBy string + Description string + Type string SchemaPath string @@ -49,19 +50,6 @@ func (c SetterCreator) Create(openAPIPath, resourcesPath string) error { if err != nil { return err } - // Update the OpenAPI definitions to hace the setter - sd := setters2.SetterDefinition{ - Name: c.Name, Value: c.FieldValue, Description: c.Description, SetBy: c.SetBy, - Type: c.Type, Schema: schema, Required: c.Required, - } - if err := sd.AddToFile(openAPIPath); err != nil { - return err - } - - // Load the updated definitions - if err := openapi.AddSchemaFromFile(openAPIPath); err != nil { - return err - } // Update the resources with the setter reference inout := &kio.LocalPackageReadWriter{PackagePath: resourcesPath} @@ -76,11 +64,26 @@ func (c SetterCreator) Create(openAPIPath, resourcesPath string) error { Filters: []kio.Filter{kio.FilterAll(a)}, Outputs: []kio.Writer{inout}, }.Execute() - + if a.Count == 0 { + return errors.Errorf("created setter doesn't match any fields") + } if err != nil { return err } + // Update the OpenAPI definitions to hace the setter + sd := setters2.SetterDefinition{ + Name: c.Name, Value: c.FieldValue, SetBy: c.SetBy, Description: c.Description, + Count: a.Count, Type: c.Type, Schema: schema, Required: c.Required, + } + if err := sd.AddToFile(openAPIPath); err != nil { + return err + } + + // Load the updated definitions + if err := openapi.AddSchemaFromFile(openAPIPath); 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 {