@@ -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,37 @@ 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
+ label := label
94
+ if ! label .IncludeSelectors && label .IncludeTemplates == o .includeTemplates {
95
+ labelPairs = & label
96
+ break
97
+ }
98
+ }
99
+
100
+ if labelPairs != nil {
101
+ if labelPairs .Pairs == nil {
102
+ labelPairs .Pairs = make (map [string ]string )
103
+ }
104
+ return o .writeToMap (labelPairs .Pairs )
105
+ }
106
+
107
+ m .Labels = append (m .Labels , types.Label {
108
+ Pairs : make (map [string ]string ),
109
+ IncludeSelectors : false ,
110
+ IncludeTemplates : o .includeTemplates ,
111
+ })
112
+ return o .writeToMap (m .Labels [len (m .Labels )- 1 ].Pairs )
113
+ }
114
+
77
115
if m .CommonLabels == nil {
78
116
m .CommonLabels = make (map [string ]string )
79
117
}
118
+
80
119
return o .writeToMap (m .CommonLabels )
81
120
}
82
121
@@ -86,3 +125,31 @@ func (o *setLabelOptions) writeToMap(m map[string]string) error {
86
125
}
87
126
return nil
88
127
}
128
+
129
+ // removeDuplicateLabels removes duplicate labels from commonLabels or labels
130
+ func (o * setLabelOptions ) removeDuplicateLabels (m * types.Kustomization ) {
131
+ for k := range o .metadata {
132
+ // delete duplicate label from deprecated common labels
133
+ delete (m .CommonLabels , k )
134
+ for idx , label := range m .Labels {
135
+ // delete label if it's already present in labels with mismatched includeTemplates value
136
+ if label .IncludeTemplates != o .includeTemplates {
137
+ m .Labels = deleteLabel (k , label , m .Labels , idx )
138
+ }
139
+ if label .IncludeSelectors {
140
+ // delete label if it's already present in labels and includes selectors
141
+ m .Labels = deleteLabel (k , label , m .Labels , idx )
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ // deleteLabel deletes label from types.Label
148
+ func deleteLabel (key string , label types.Label , labels []types.Label , idx int ) []types.Label {
149
+ delete (label .Pairs , key )
150
+ if len (label .Pairs ) == 0 {
151
+ // remove empty map label.Pairs from labels
152
+ labels = append (labels [:idx ], labels [idx + 1 :]... )
153
+ }
154
+ return labels
155
+ }
0 commit comments