Skip to content

Commit 23fabe4

Browse files
committed
feat: support edit set label for labels and not only commonLabels
1 parent 8dab949 commit 23fabe4

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

kustomize/commands/edit/set/setlabel.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package set
55

66
import (
7+
"errors"
78
"fmt"
89

910
"github.com/spf13/cobra"
@@ -17,6 +18,7 @@ import (
1718
type setLabelOptions struct {
1819
metadata map[string]string
1920
mapValidator func(map[string]string) error
21+
fields string // TODO does enum type exists in Go?
2022
}
2123

2224
// newCmdSetLabel sets one or more commonLabels to the kustomization file.
@@ -25,14 +27,23 @@ func newCmdSetLabel(fSys filesys.FileSystem, v func(map[string]string) error) *c
2527
o.mapValidator = v
2628
cmd := &cobra.Command{
2729
Use: "label",
28-
Short: "Sets one or more commonLabels in " +
30+
Short: "Sets one or more labels/commonLabels in " +
2931
konfig.DefaultKustomizationFileName(),
3032
Example: `
31-
set label {labelKey1:labelValue1} {labelKey2:labelValue2}`,
33+
# Set commonLabels (default)
34+
set label {labelKey1:labelValue1} {labelKey2:labelValue2}
35+
36+
# Set commonLabels
37+
set label --fields=commonLabels {labelKey1:labelValue1} {labelKey2:labelValue2}
38+
39+
# Set labels
40+
set label --fields=labels {labelKey1:labelValue1} {labelKey2:labelValue2}
41+
`,
3242
RunE: func(cmd *cobra.Command, args []string) error {
3343
return o.runE(args, fSys, o.setLabels)
3444
},
3545
}
46+
cmd.Flags().StringVar(&o.fields, "fields", "commonLabels", "Name of the fields to set (commonLabels or labels)")
3647
return cmd
3748
}
3849

@@ -62,6 +73,9 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
6273
if len(args) < 1 {
6374
return fmt.Errorf("must specify label")
6475
}
76+
if o.fields != "labels" && o.fields != "commonLabels" {
77+
return errors.New("fields must be \"labels\" or \"commonLabels\"")
78+
}
6579
m, err := util.ConvertSliceToMap(args, "label")
6680
if err != nil {
6781
return err
@@ -74,10 +88,20 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
7488
}
7589

7690
func (o *setLabelOptions) setLabels(m *types.Kustomization) error {
77-
if m.CommonLabels == nil {
78-
m.CommonLabels = make(map[string]string)
91+
if o.fields == "commonLabels" {
92+
if m.CommonLabels == nil {
93+
m.CommonLabels = make(map[string]string)
94+
}
95+
return o.writeToMap(m.CommonLabels)
96+
} else if o.fields == "labels" {
97+
if m.Labels == nil {
98+
m.Labels = make([]types.Label, len(o.metadata))
99+
}
100+
m.Labels = o.addLabels(m.Labels)
101+
return nil
102+
} else {
103+
return nil
79104
}
80-
return o.writeToMap(m.CommonLabels)
81105
}
82106

83107
func (o *setLabelOptions) writeToMap(m map[string]string) error {
@@ -86,3 +110,8 @@ func (o *setLabelOptions) writeToMap(m map[string]string) error {
86110
}
87111
return nil
88112
}
113+
114+
func (o *setLabelOptions) addLabels(m []types.Label) []types.Label {
115+
newLabel := types.Label{Pairs: o.metadata, IncludeSelectors: false}
116+
return append(m, newLabel)
117+
}

kustomize/commands/edit/set/setlabel_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package set
55

66
import (
7+
"reflect"
78
"testing"
89

910
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
@@ -27,15 +28,57 @@ func makeKustomization(t *testing.T) *types.Kustomization {
2728
return m
2829
}
2930

30-
func TestRunSetLabel(t *testing.T) {
31+
func TestRunSetCommonLabels(t *testing.T) {
3132
var o setLabelOptions
33+
o.fields = "commonLabels"
3234
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
3335

3436
m := makeKustomization(t)
3537
err := o.setLabels(m)
3638
if err != nil {
3739
t.Errorf("unexpected error: could not write to kustomization file")
3840
}
41+
42+
// assert content
43+
expectedContent := map[string]string{"app": "helloworld", "owls": "cute", "otters": "adorable"}
44+
if !reflect.DeepEqual(m.CommonLabels, expectedContent) {
45+
t.Log("m.CommonLabels", m.CommonLabels)
46+
t.Log("expectedContent", expectedContent)
47+
t.Errorf("commonLabels does not contain expected content")
48+
}
49+
50+
// adding the same test input should work
51+
err = o.setLabels(m)
52+
if err != nil {
53+
t.Errorf("unexpected error: could not write to kustomization file")
54+
}
55+
// adding new labels should work
56+
o.metadata = map[string]string{"new": "label", "owls": "not cute"}
57+
err = o.setLabels(m)
58+
if err != nil {
59+
t.Errorf("unexpected error: could not write to kustomization file")
60+
}
61+
}
62+
63+
func TestRunSetLabels(t *testing.T) {
64+
var o setLabelOptions
65+
o.fields = "labels"
66+
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
67+
68+
m := makeKustomization(t)
69+
err := o.setLabels(m)
70+
if err != nil {
71+
t.Errorf("unexpected error: could not write to kustomization file")
72+
}
73+
74+
// assert content
75+
expectedContent := types.Label{Pairs: map[string]string{"owls": "cute", "otters": "adorable"}, IncludeSelectors: false}
76+
if !reflect.DeepEqual(m.Labels[2], expectedContent) {
77+
t.Log("m.Labels[2]", m.Labels[2])
78+
t.Log("expectedContent", expectedContent)
79+
t.Errorf("labels does not contain expected content")
80+
}
81+
3982
// adding the same test input should work
4083
err = o.setLabels(m)
4184
if err != nil {

0 commit comments

Comments
 (0)