mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 09:02:53 +00:00
Suggested changes
This commit is contained in:
@@ -128,9 +128,9 @@ func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
|||||||
r.CreateSetter.Type = r.Set.SetPartialField.Type
|
r.CreateSetter.Type = r.Set.SetPartialField.Type
|
||||||
|
|
||||||
if r.CreateSetter.Type == "array" {
|
if r.CreateSetter.Type == "array" {
|
||||||
// this is just a place holder value, we derive the actual value at
|
if !c.Flag("field").Changed {
|
||||||
// later point from the field path
|
return errors.Errorf("field flag must be set for array type setters")
|
||||||
r.CreateSetter.FieldValue = "listValues"
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -129,8 +129,101 @@ spec:
|
|||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "add replicas with schema list values",
|
name: "list values with schema",
|
||||||
args: []string{"list", "a", "--description", "hello world", "--set-by", "me", "--type", "array", "--field", "spec.list"},
|
args: []string{"list", "--description", "hello world", "--set-by", "me", "--type", "array", "--field", "spec.list"},
|
||||||
|
schema: `{"maxItems": 3, "type": "array", "items": {"type": "string"}}`,
|
||||||
|
input: `
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example1
|
||||||
|
spec:
|
||||||
|
list:
|
||||||
|
- "a"
|
||||||
|
- "b"
|
||||||
|
- "c"
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example2
|
||||||
|
spec:
|
||||||
|
list:
|
||||||
|
- "a"
|
||||||
|
- "b"
|
||||||
|
- "c"
|
||||||
|
`,
|
||||||
|
inputOpenAPI: `
|
||||||
|
apiVersion: v1alpha1
|
||||||
|
kind: Example
|
||||||
|
`,
|
||||||
|
expectedOpenAPI: `
|
||||||
|
apiVersion: v1alpha1
|
||||||
|
kind: Example
|
||||||
|
openAPI:
|
||||||
|
definitions:
|
||||||
|
io.k8s.cli.setters.list:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
maxItems: 3
|
||||||
|
type: array
|
||||||
|
description: hello world
|
||||||
|
x-k8s-cli:
|
||||||
|
setter:
|
||||||
|
name: list
|
||||||
|
value: ""
|
||||||
|
listValues:
|
||||||
|
- a
|
||||||
|
- b
|
||||||
|
- c
|
||||||
|
setBy: me
|
||||||
|
`,
|
||||||
|
expectedResources: `
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example1
|
||||||
|
spec:
|
||||||
|
list: # {"$openapi":"list"}
|
||||||
|
- "a"
|
||||||
|
- "b"
|
||||||
|
- "c"
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example2
|
||||||
|
spec:
|
||||||
|
list: # {"$openapi":"list"}
|
||||||
|
- "a"
|
||||||
|
- "b"
|
||||||
|
- "c"
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "error list path with different values",
|
||||||
|
args: []string{"list", "--description", "hello world", "--set-by", "me", "--type", "array", "--field", "spec.list"},
|
||||||
|
schema: `{"maxItems": 3, "type": "array", "items": {"type": "string"}}`,
|
||||||
|
input: `
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example
|
||||||
|
spec:
|
||||||
|
list:
|
||||||
|
- "a"
|
||||||
|
- "b"
|
||||||
|
- "c"
|
||||||
|
---
|
||||||
|
apiVersion: example.com/v1beta1
|
||||||
|
kind: Example
|
||||||
|
spec:
|
||||||
|
list:
|
||||||
|
- "c"
|
||||||
|
- "d"
|
||||||
|
`,
|
||||||
|
inputOpenAPI: `
|
||||||
|
apiVersion: v1alpha1
|
||||||
|
kind: Example
|
||||||
|
`,
|
||||||
|
err: `setters can only be created for fields with same values, encountered different ` +
|
||||||
|
`array values for specified field path: [c d], [a b c]`,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "list values error if field not set",
|
||||||
|
args: []string{"list", "a", "--description", "hello world", "--set-by", "me", "--type", "array"},
|
||||||
schema: `{"maxItems": 3, "type": "array", "items": {"type": "string"}}`,
|
schema: `{"maxItems": 3, "type": "array", "items": {"type": "string"}}`,
|
||||||
input: `
|
input: `
|
||||||
apiVersion: example.com/v1beta1
|
apiVersion: example.com/v1beta1
|
||||||
@@ -174,6 +267,7 @@ spec:
|
|||||||
- "b"
|
- "b"
|
||||||
- "c"
|
- "c"
|
||||||
`,
|
`,
|
||||||
|
err: `field flag must be set for array type setters`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "add replicas with value set by flag",
|
name: "add replicas with value set by flag",
|
||||||
|
|||||||
@@ -272,7 +272,6 @@ openAPI:
|
|||||||
x-k8s-cli:
|
x-k8s-cli:
|
||||||
setter:
|
setter:
|
||||||
name: list
|
name: list
|
||||||
value: List Values
|
|
||||||
listValues:
|
listValues:
|
||||||
- a
|
- a
|
||||||
- b
|
- b
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ spec:
|
|||||||
replicas: 4 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
replicas: 4 # {"$ref":"#/definitions/io.k8s.cli.setters.replicas"}
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "set image with value",
|
name: "set image with value",
|
||||||
args: []string{"tag", "1.8.1"},
|
args: []string{"tag", "1.8.1"},
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package setters2
|
package setters2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-openapi/spec"
|
"github.com/go-openapi/spec"
|
||||||
@@ -33,6 +34,9 @@ type Add struct {
|
|||||||
|
|
||||||
// ListValues are the value of a list setter.
|
// ListValues are the value of a list setter.
|
||||||
ListValues []string
|
ListValues []string
|
||||||
|
|
||||||
|
// Type is the type of the setter value
|
||||||
|
Type string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter implements yaml.Filter
|
// Filter implements yaml.Filter
|
||||||
@@ -70,6 +74,12 @@ func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSche
|
|||||||
for _, sc := range node.Value.Content() {
|
for _, sc := range node.Value.Content() {
|
||||||
values = append(values, sc.Value)
|
values = append(values, sc.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if there are different values for field path
|
||||||
|
if len(a.ListValues) > 0 && !reflect.DeepEqual(values, a.ListValues) {
|
||||||
|
return errors.Errorf("setters can only be created for fields with same values, "+
|
||||||
|
"encountered different array values for specified field path: %s, %s", values, a.ListValues)
|
||||||
|
}
|
||||||
a.ListValues = values
|
a.ListValues = values
|
||||||
|
|
||||||
// pathToKey refers to the path address of the key node ex: metadata.annotations
|
// pathToKey refers to the path address of the key node ex: metadata.annotations
|
||||||
@@ -86,6 +96,9 @@ func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSche
|
|||||||
// visitScalar will set the field metadata on each scalar field whose name + value match
|
// 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 {
|
func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error {
|
||||||
// check if the field matches
|
// check if the field matches
|
||||||
|
if a.Type == "array" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if a.FieldName != "" && !strings.HasSuffix(p, a.FieldName) {
|
if a.FieldName != "" && !strings.HasSuffix(p, a.FieldName) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -123,7 +136,7 @@ type SetterDefinition struct {
|
|||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
|
|
||||||
// Value is the value of the setter.
|
// Value is the value of the setter.
|
||||||
Value string `yaml:"value,omitempty"`
|
Value string `yaml:"value"`
|
||||||
|
|
||||||
// ListValues are the value of a list setter.
|
// ListValues are the value of a list setter.
|
||||||
ListValues []string `yaml:"listValues,omitempty"`
|
ListValues []string `yaml:"listValues,omitempty"`
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ func (c SetterCreator) Create(openAPIPath, resourcesPath string) error {
|
|||||||
FieldName: c.FieldName,
|
FieldName: c.FieldName,
|
||||||
FieldValue: c.FieldValue,
|
FieldValue: c.FieldValue,
|
||||||
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + c.Name,
|
Ref: fieldmeta.DefinitionsPrefix + fieldmeta.SetterDefinitionPrefix + c.Name,
|
||||||
|
Type: c.Type,
|
||||||
}
|
}
|
||||||
err = kio.Pipeline{
|
err = kio.Pipeline{
|
||||||
Inputs: []kio.Reader{inout},
|
Inputs: []kio.Reader{inout},
|
||||||
|
|||||||
Reference in New Issue
Block a user