Skip to content

Commit a9b6cf2

Browse files
Simon Emmsroboquat
authored andcommitted
[installer]: allow for customization of labels
1 parent 384091b commit a9b6cf2

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

install/installer/pkg/common/customize.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type CustomizationType string
1212

1313
const (
1414
CustomizationTypeAnnotation CustomizationType = "annotation"
15+
CustomizationTypeLabel CustomizationType = "label"
1516
)
1617

1718
func extractCustomizations(ctx *RenderContext, name string, typeMeta metav1.TypeMeta, customizationType CustomizationType) map[string]string {
@@ -30,6 +31,10 @@ func extractCustomizations(ctx *RenderContext, name string, typeMeta metav1.Type
3031
// Annotations
3132
customizations = mergeCustomizations(customizations, customization.Metadata.Annotations)
3233
}
34+
if customizationType == CustomizationTypeLabel {
35+
// Labels
36+
customizations = mergeCustomizations(customizations, customization.Metadata.Labels)
37+
}
3338
}
3439
}
3540
}
@@ -72,6 +77,12 @@ func CustomizeEnvvar(ctx *RenderContext, component string, existingEnvvars []cor
7277
func CustomizeLabel(ctx *RenderContext, component string, typeMeta metav1.TypeMeta, existingLabels ...func() map[string]string) map[string]string {
7378
labels := DefaultLabels(component)
7479

80+
// Apply the customizations
81+
for k, v := range extractCustomizations(ctx, component, typeMeta, CustomizationTypeLabel) {
82+
labels[k] = v
83+
}
84+
85+
// Always apply existing labels
7586
for _, e := range existingLabels {
7687
for k, v := range e() {
7788
labels[k] = v

install/installer/pkg/common/customize_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,189 @@ func TestCustomizeAnnotation(t *testing.T) {
193193
})
194194
}
195195
}
196+
197+
func TestCustomizeLabel(t *testing.T) {
198+
testCases := []struct {
199+
Name string
200+
Customization []config.Customization
201+
Component string
202+
TypeMeta metav1.TypeMeta
203+
ExistingLabels []func() map[string]string
204+
Expect map[string]string
205+
}{
206+
{
207+
Name: "no customization",
208+
Customization: nil,
209+
Component: "component",
210+
TypeMeta: common.TypeMetaDeployment,
211+
Expect: map[string]string{},
212+
},
213+
{
214+
Customization: []config.Customization{},
215+
Name: "empty customization",
216+
Component: "component",
217+
TypeMeta: common.TypeMetaDeployment,
218+
Expect: map[string]string{},
219+
},
220+
{
221+
Customization: []config.Customization{
222+
{
223+
TypeMeta: common.TypeMetaBatchJob,
224+
Metadata: metav1.ObjectMeta{
225+
Name: "component",
226+
Labels: map[string]string{
227+
"key1": "value1",
228+
},
229+
},
230+
},
231+
},
232+
Name: "ignore different typeMeta labels",
233+
Component: "component",
234+
TypeMeta: common.TypeMetaDeployment,
235+
Expect: map[string]string{},
236+
},
237+
{
238+
Customization: []config.Customization{
239+
{
240+
TypeMeta: common.TypeMetaDeployment,
241+
Metadata: metav1.ObjectMeta{
242+
Name: "component2",
243+
Labels: map[string]string{
244+
"key1": "value1",
245+
},
246+
},
247+
},
248+
},
249+
Name: "ignore same typeMeta, different name labels",
250+
Component: "component",
251+
TypeMeta: common.TypeMetaDeployment,
252+
Expect: map[string]string{},
253+
},
254+
{
255+
Customization: []config.Customization{
256+
{
257+
TypeMeta: common.TypeMetaDeployment,
258+
Metadata: metav1.ObjectMeta{
259+
Name: "component",
260+
Labels: map[string]string{
261+
"key1": "value1",
262+
},
263+
},
264+
},
265+
},
266+
Name: "single component labels",
267+
Component: "component",
268+
TypeMeta: common.TypeMetaDeployment,
269+
Expect: map[string]string{
270+
"key1": "value1",
271+
},
272+
},
273+
{
274+
Customization: []config.Customization{
275+
{
276+
TypeMeta: common.TypeMetaDeployment,
277+
Metadata: metav1.ObjectMeta{
278+
Name: "component",
279+
Labels: map[string]string{
280+
"key1": "value1",
281+
"key2": "value2",
282+
},
283+
},
284+
},
285+
{
286+
TypeMeta: common.TypeMetaDeployment,
287+
Metadata: metav1.ObjectMeta{
288+
Name: "component",
289+
Labels: map[string]string{
290+
"key3": "value3",
291+
},
292+
},
293+
},
294+
},
295+
Name: "multiple component labels",
296+
Component: "component",
297+
TypeMeta: common.TypeMetaDeployment,
298+
Expect: map[string]string{
299+
"key1": "value1",
300+
"key2": "value2",
301+
"key3": "value3",
302+
},
303+
},
304+
{
305+
Customization: []config.Customization{
306+
{
307+
TypeMeta: metav1.TypeMeta{
308+
APIVersion: "*",
309+
Kind: "*",
310+
},
311+
Metadata: metav1.ObjectMeta{
312+
Name: "*",
313+
Labels: map[string]string{
314+
"key1": "value1",
315+
},
316+
},
317+
},
318+
},
319+
Name: "wildcard labels",
320+
Component: "component",
321+
TypeMeta: common.TypeMetaDeployment,
322+
Expect: map[string]string{
323+
"key1": "value1",
324+
},
325+
},
326+
{
327+
Customization: []config.Customization{
328+
{
329+
TypeMeta: metav1.TypeMeta{
330+
APIVersion: "*",
331+
Kind: "*",
332+
},
333+
Metadata: metav1.ObjectMeta{
334+
Name: "*",
335+
Labels: map[string]string{
336+
"key1": "value1",
337+
"key2": "override",
338+
"key3": "",
339+
},
340+
},
341+
},
342+
},
343+
Name: "override input, do not override existing",
344+
Component: "component",
345+
TypeMeta: common.TypeMetaDeployment,
346+
ExistingLabels: []func() map[string]string{
347+
func() map[string]string {
348+
return map[string]string{
349+
"key2": "original",
350+
}
351+
},
352+
},
353+
Expect: map[string]string{
354+
"key1": "value1",
355+
"key2": "original",
356+
"key3": "",
357+
},
358+
},
359+
}
360+
361+
for _, testCase := range testCases {
362+
t.Run(testCase.Name, func(t *testing.T) {
363+
ctx, err := common.NewRenderContext(config.Config{
364+
Customization: &testCase.Customization,
365+
}, versions.Manifest{}, "test_namespace")
366+
require.NoError(t, err)
367+
368+
result := common.CustomizeLabel(ctx, testCase.Component, testCase.TypeMeta, testCase.ExistingLabels...)
369+
370+
// These all have the default labels - add these in
371+
expectation := common.DefaultLabels(testCase.Component)
372+
for k, v := range testCase.Expect {
373+
expectation[k] = v
374+
}
375+
376+
if !reflect.DeepEqual(expectation, result) {
377+
t.Errorf("expected %v but got %v", expectation, result)
378+
}
379+
})
380+
}
381+
}

0 commit comments

Comments
 (0)