@@ -15,8 +15,10 @@ import (
1515)
1616
1717type 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
2022}
2123
2224// 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
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 commonLabels or labels in " +
2931 konfig .DefaultKustomizationFileName (),
3032 Example : `
3133 set label {labelKey1:labelValue1} {labelKey2:labelValue2}` ,
3234 RunE : func (cmd * cobra.Command , args []string ) error {
3335 return o .runE (args , fSys , o .setLabels )
3436 },
3537 }
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+ )
3644 return cmd
3745}
3846
@@ -62,6 +70,9 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
6270 if len (args ) < 1 {
6371 return fmt .Errorf ("must specify label" )
6472 }
73+ if ! o .labelsWithoutSelector && o .includeTemplates {
74+ return fmt .Errorf ("--without-selector flag must be specified for --include-templates to work" )
75+ }
6576 m , err := util .ConvertSliceToMap (args , "label" )
6677 if err != nil {
6778 return err
@@ -74,9 +85,36 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
7485}
7586
7687func (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+
77114 if m .CommonLabels == nil {
78115 m .CommonLabels = make (map [string ]string )
79116 }
117+
80118 return o .writeToMap (m .CommonLabels )
81119}
82120
@@ -86,3 +124,31 @@ func (o *setLabelOptions) writeToMap(m map[string]string) error {
86124 }
87125 return nil
88126}
127+
128+ // removeDuplicateLabels removes duplicate labels from commonLabels or labels
129+ func (o * setLabelOptions ) removeDuplicateLabels (m * types.Kustomization ) {
130+ for k := range o .metadata {
131+ // delete duplicate label from deprecated common labels
132+ delete (m .CommonLabels , k )
133+ for idx , label := range m .Labels {
134+ // delete label if it's already present in labels with mismatched includeTemplates value
135+ if label .IncludeTemplates != o .includeTemplates {
136+ m .Labels = deleteLabel (k , label , m .Labels , idx )
137+ }
138+ if label .IncludeSelectors {
139+ // delete label if it's already present in labels and includes selectors
140+ m .Labels = deleteLabel (k , label , m .Labels , idx )
141+ }
142+ }
143+ }
144+ }
145+
146+ // deleteLabel deletes label from types.Label
147+ func deleteLabel (key string , label types.Label , labels []types.Label , idx int ) []types.Label {
148+ delete (label .Pairs , key )
149+ if len (label .Pairs ) == 0 {
150+ // remove empty map label.Pairs from labels
151+ labels = append (labels [:idx ], labels [idx + 1 :]... )
152+ }
153+ return labels
154+ }
0 commit comments