mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Avoid manual step for creating array setters
This commit is contained in:
@@ -91,7 +91,9 @@ func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if setterVersion == "" {
|
||||
if len(args) < 2 || !c.Flag("value").Changed && len(args) < 3 {
|
||||
if len(args) == 2 && r.Set.SetPartialField.Type == "array" && c.Flag("field").Changed {
|
||||
setterVersion = "v2"
|
||||
} else if len(args) < 2 || !c.Flag("value").Changed && len(args) < 3 {
|
||||
setterVersion = "v1"
|
||||
} else if err := initSetterVersion(c, args); err != nil {
|
||||
return err
|
||||
@@ -124,6 +126,12 @@ func (r *CreateSetterRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
r.CreateSetter.Description = r.Set.SetPartialField.Description
|
||||
r.CreateSetter.SetBy = r.Set.SetPartialField.SetBy
|
||||
r.CreateSetter.Type = r.Set.SetPartialField.Type
|
||||
|
||||
if r.CreateSetter.Type == "array" {
|
||||
// this is just a place holder value, we derive the actual value at
|
||||
// later point from the field path
|
||||
r.CreateSetter.FieldValue = "listValues"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -130,14 +130,16 @@ spec:
|
||||
|
||||
{
|
||||
name: "add replicas with schema list values",
|
||||
args: []string{"list", "a", "--description", "hello world", "--set-by", "me", "--type", "array"},
|
||||
schema: `{"maxItems": 2, "type": "array", "items": {"type": "string"}}`,
|
||||
args: []string{"list", "a", "--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"
|
||||
`,
|
||||
inputOpenAPI: `
|
||||
apiVersion: v1alpha1
|
||||
@@ -151,21 +153,26 @@ openAPI:
|
||||
io.k8s.cli.setters.list:
|
||||
items:
|
||||
type: string
|
||||
maxItems: 2
|
||||
maxItems: 3
|
||||
type: array
|
||||
description: hello world
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: list
|
||||
value: a
|
||||
listValues:
|
||||
- a
|
||||
- b
|
||||
- c
|
||||
setBy: me
|
||||
`,
|
||||
expectedResources: `
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Example
|
||||
spec:
|
||||
list:
|
||||
- "a" # {"$openapi":"list"}
|
||||
list: # {"$openapi":"list"}
|
||||
- "a"
|
||||
- "b"
|
||||
- "c"
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
||||
@@ -113,17 +113,20 @@ func (r *ListSettersRunner) ListSubstitutions(c *cobra.Command, args []string) e
|
||||
return err
|
||||
}
|
||||
table := newTable(c.OutOrStdout(), r.Markdown)
|
||||
table.SetHeader([]string{"SUBSTITUTION", "PATTERN", "SETTERS"})
|
||||
table.SetHeader([]string{"SUBSTITUTION", "PATTERN", "REFERENCES"})
|
||||
for i := range r.List.Substitutions {
|
||||
s := r.List.Substitutions[i]
|
||||
setters := ""
|
||||
refs := ""
|
||||
for _, value := range s.Values {
|
||||
setter := strings.TrimPrefix(value.Ref, fieldmeta.DefinitionsPrefix+fieldmeta.SetterDefinitionPrefix)
|
||||
setters = setters + "," + setter
|
||||
// trim setter and substitution prefixes
|
||||
ref := strings.TrimPrefix(
|
||||
strings.TrimPrefix(value.Ref, fieldmeta.DefinitionsPrefix+fieldmeta.SetterDefinitionPrefix),
|
||||
fieldmeta.DefinitionsPrefix+fieldmeta.SubstitutionDefinitionPrefix)
|
||||
refs = refs + "," + ref
|
||||
}
|
||||
setters = fmt.Sprintf("[%s]", strings.TrimPrefix(setters, ","))
|
||||
refs = fmt.Sprintf("[%s]", strings.TrimPrefix(refs, ","))
|
||||
table.Append([]string{
|
||||
s.Name, s.Pattern, setters})
|
||||
s.Name, s.Pattern, refs})
|
||||
}
|
||||
if len(r.List.Substitutions) == 0 {
|
||||
return nil
|
||||
|
||||
@@ -104,7 +104,7 @@ spec:
|
||||
image nginx me2 hello world 2 2
|
||||
replicas 3 me1 hello world 1 1
|
||||
tag 1.7.9 me3 hello world 3 1
|
||||
SUBSTITUTION PATTERN SETTERS
|
||||
SUBSTITUTION PATTERN REFERENCES
|
||||
image IMAGE:TAG [image,tag]
|
||||
`,
|
||||
},
|
||||
@@ -178,7 +178,7 @@ spec:
|
||||
image nginx me2 hello world 2 3
|
||||
replicas 3 me1 hello world 1 2
|
||||
tag 1.7.9 me3 hello world 3 2
|
||||
SUBSTITUTION PATTERN SETTERS
|
||||
SUBSTITUTION PATTERN REFERENCES
|
||||
image IMAGE:TAG [image,tag]
|
||||
`,
|
||||
},
|
||||
@@ -251,8 +251,116 @@ spec:
|
||||
`,
|
||||
expected: ` NAME VALUE SET BY DESCRIPTION COUNT
|
||||
image nginx me2 hello world 2 3
|
||||
SUBSTITUTION PATTERN SETTERS
|
||||
SUBSTITUTION PATTERN REFERENCES
|
||||
image IMAGE:TAG [image,tag]
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "list array setter",
|
||||
openapi: `
|
||||
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: List Values
|
||||
listValues:
|
||||
- a
|
||||
- b
|
||||
- c
|
||||
setBy: me
|
||||
`,
|
||||
input: `
|
||||
apiVersion: example.com/v1beta1
|
||||
kind: Example
|
||||
metadata:
|
||||
annotations:
|
||||
foo: bar
|
||||
spec:
|
||||
list: # {"$openapi":"list"}
|
||||
- "a"
|
||||
- "b"
|
||||
- "c"
|
||||
`,
|
||||
expected: ` NAME VALUE SET BY DESCRIPTION COUNT
|
||||
list [a,b,c] me hello world 1
|
||||
`,
|
||||
},
|
||||
|
||||
{
|
||||
name: "nested substitution",
|
||||
input: `
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx-deployment
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: something/nginx::1.7.9/nginxotherthing # {"$openapi":"my-nested-subst"}
|
||||
- name: sidecar
|
||||
image: nginx::1.7.9 # {"$openapi":"my-image-subst"}
|
||||
`,
|
||||
openapi: `
|
||||
apiVersion: v1alpha1
|
||||
kind: Example
|
||||
openAPI:
|
||||
definitions:
|
||||
io.k8s.cli.setters.my-image-setter:
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: my-image-setter
|
||||
value: nginx
|
||||
io.k8s.cli.setters.my-tag-setter:
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: my-tag-setter
|
||||
value: 1.7.9
|
||||
io.k8s.cli.substitutions.my-image-subst:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: my-image-subst
|
||||
pattern: ${my-image-setter}::${my-tag-setter}
|
||||
values:
|
||||
- marker: ${my-image-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-image-setter'
|
||||
- marker: ${my-tag-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-tag-setter'
|
||||
io.k8s.cli.substitutions.my-nested-subst:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: my-nested-subst
|
||||
pattern: something/${my-image-subst}/${my-other-setter}
|
||||
values:
|
||||
- marker: ${my-image-subst}
|
||||
ref: '#/definitions/io.k8s.cli.substitutions.my-image-subst'
|
||||
- marker: ${my-other-setter}
|
||||
ref: '#/definitions/io.k8s.cli.setters.my-other-setter'
|
||||
io.k8s.cli.setters.my-other-setter:
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: my-other-setter
|
||||
value: nginxotherthing
|
||||
`,
|
||||
expected: ` NAME VALUE SET BY DESCRIPTION COUNT
|
||||
my-image-setter nginx 2
|
||||
my-other-setter nginxotherthing 1
|
||||
my-tag-setter 1.7.9 2
|
||||
SUBSTITUTION PATTERN REFERENCES
|
||||
my-image-subst ${my-image-setter}::${my-tag-setter} [my-image-setter,my-tag-setter]
|
||||
my-nested-subst something/${my-image-subst}/${my-other-setter} [my-image-subst,my-other-setter]
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user