@@ -15,8 +15,10 @@ import (
15
15
)
16
16
17
17
type setLabelOptions struct {
18
- metadata map [string ]string
19
- mapValidator func (map [string ]string ) error
18
+ metadata map [string ]string
19
+ mapValidator func (map [string ]string ) error
20
+ labelsWithoutSelector bool
21
+ includeTemplates bool
20
22
}
21
23
22
24
// newCmdSetLabel sets one or more commonLabels to the kustomization file.
@@ -25,14 +27,20 @@ func newCmdSetLabel(fSys filesys.FileSystem, v func(map[string]string) error) *c
25
27
o .mapValidator = v
26
28
cmd := & cobra.Command {
27
29
Use : "label" ,
28
- Short : "Sets one or more commonLabels in " +
30
+ Short : "Sets one or more commonLabels or labels in " +
29
31
konfig .DefaultKustomizationFileName (),
30
32
Example : `
31
33
set label {labelKey1:labelValue1} {labelKey2:labelValue2}` ,
32
34
RunE : func (cmd * cobra.Command , args []string ) error {
33
35
return o .runE (args , fSys , o .setLabels )
34
36
},
35
37
}
38
+ cmd .Flags ().BoolVar (& o .labelsWithoutSelector , "without-selector" , false ,
39
+ "using set labels without selector option" ,
40
+ )
41
+ cmd .Flags ().BoolVar (& o .includeTemplates , "include-templates" , false ,
42
+ "include labels in templates (requires --without-selector)" ,
43
+ )
36
44
return cmd
37
45
}
38
46
@@ -62,6 +70,9 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
62
70
if len (args ) < 1 {
63
71
return fmt .Errorf ("must specify label" )
64
72
}
73
+ if ! o .labelsWithoutSelector && o .includeTemplates {
74
+ return fmt .Errorf ("--without-selector flag must be specified for --include-templates to work" )
75
+ }
65
76
m , err := util .ConvertSliceToMap (args , "label" )
66
77
if err != nil {
67
78
return err
@@ -74,9 +85,36 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
74
85
}
75
86
76
87
func (o * setLabelOptions ) setLabels (m * types.Kustomization ) error {
88
+ if o .labelsWithoutSelector {
89
+ o .removeDuplicateLabels (m )
90
+
91
+ var labelPairs * types.Label
92
+ for _ , label := range m .Labels {
93
+ if ! label .IncludeSelectors && label .IncludeTemplates == o .includeTemplates {
94
+ labelPairs = & label
95
+ break
96
+ }
97
+ }
98
+
99
+ if labelPairs != nil {
100
+ if labelPairs .Pairs == nil {
101
+ labelPairs .Pairs = make (map [string ]string )
102
+ }
103
+ return o .writeToMap (labelPairs .Pairs )
104
+ }
105
+
106
+ m .Labels = append (m .Labels , types.Label {
107
+ Pairs : make (map [string ]string ),
108
+ IncludeSelectors : false ,
109
+ IncludeTemplates : o .includeTemplates ,
110
+ })
111
+ return o .writeToMap (m .Labels [len (m .Labels )- 1 ].Pairs )
112
+ }
113
+
77
114
if m .CommonLabels == nil {
78
115
m .CommonLabels = make (map [string ]string )
79
116
}
117
+
80
118
return o .writeToMap (m .CommonLabels )
81
119
}
82
120
@@ -86,3 +124,25 @@ func (o *setLabelOptions) writeToMap(m map[string]string) error {
86
124
}
87
125
return nil
88
126
}
127
+
128
+ func (o * setLabelOptions ) removeDuplicateLabels (m * types.Kustomization ) {
129
+ for k := range o .metadata {
130
+ delete (m .CommonLabels , k )
131
+ for idx , label := range m .Labels {
132
+ if label .IncludeTemplates != o .includeTemplates {
133
+ m .Labels = deleteLabel (k , label , m .Labels , idx )
134
+ }
135
+ if label .IncludeSelectors {
136
+ m .Labels = deleteLabel (k , label , m .Labels , idx )
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ func deleteLabel (key string , label types.Label , labels []types.Label , idx int ) []types.Label {
143
+ delete (label .Pairs , key )
144
+ if len (label .Pairs ) == 0 {
145
+ labels = append (labels [:idx ], labels [idx + 1 :]... )
146
+ }
147
+ return labels
148
+ }
0 commit comments