Skip to content

Commit 165868e

Browse files
committed
refactor(utils): export labels for reuse
On-behalf-of: @SAP [email protected] Signed-off-by: Christopher Junk <[email protected]>
1 parent a21f2ff commit 165868e

File tree

5 files changed

+43
-37
lines changed

5 files changed

+43
-37
lines changed

pkg/juggler/fluxcd/flux_reconciler_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import (
2222

2323
var errBoom = errors.New("boom")
2424

25-
const testLabelComponentName = "flux.juggler.test.io/component"
25+
const (
26+
testLabelComponentKey = "flux.juggler.test.io/component"
27+
testLabelManagedByKey = "flux.juggler.test.io/managedBy"
28+
testLabelManagedByValue = "flux.juggler.test.io/control-plane-operator"
29+
)
2630

2731
func TestNewFluxReconciler(t *testing.T) {
2832
fakeClient := fake.NewFakeClient()
@@ -371,7 +375,7 @@ func TestFluxReconciler_Observe(t *testing.T) {
371375
for _, tt := range tests {
372376
t.Run(tt.name, func(t *testing.T) {
373377

374-
r := NewFluxReconciler(logr.Logger{}, fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.localObjects...).Build(), nil, testLabelComponentName)
378+
r := NewFluxReconciler(logr.Logger{}, fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.localObjects...).Build(), nil, testLabelComponentKey)
375379
actualObservation, actualError := r.Observe(context.TODO(), tt.obj)
376380
if !assert.Equal(t, tt.expectedObservation, actualObservation) {
377381
t.Errorf("ObjectReconciler.Observe() = %v, want %v", actualObservation, tt.expectedObservation)
@@ -623,7 +627,7 @@ func TestFluxReconciler_Uninstall(t *testing.T) {
623627
}
624628
for _, tt := range tests {
625629
t.Run(tt.name, func(t *testing.T) {
626-
r := NewFluxReconciler(logr.Logger{}, fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.localObjects...).Build(), nil, testLabelComponentName)
630+
r := NewFluxReconciler(logr.Logger{}, fake.NewClientBuilder().WithScheme(scheme).WithObjects(tt.localObjects...).Build(), nil, testLabelComponentKey)
627631
actual := r.Uninstall(context.TODO(), tt.obj)
628632
if !errors.Is(actual, tt.expected) {
629633
t.Errorf("ObjectReconciler.Uninstall() = %v, want %v", actual, tt.expected)
@@ -683,7 +687,7 @@ func TestFluxReconciler_Install(t *testing.T) {
683687
}
684688
if !assert.Equal(t, helmRepo.GetLabels(), map[string]string{
685689
"app.kubernetes.io/managed-by": "control-plane-operator",
686-
testLabelComponentName: component.GetName(),
690+
testLabelComponentKey: component.GetName(),
687691
}) {
688692
return errors.New("labels not equal")
689693
}
@@ -728,7 +732,7 @@ func TestFluxReconciler_Install(t *testing.T) {
728732
}
729733
if !assert.Equal(t, helmRelease.GetLabels(), map[string]string{
730734
"app.kubernetes.io/managed-by": "control-plane-operator",
731-
testLabelComponentName: component.GetName(),
735+
testLabelComponentKey: component.GetName(),
732736
}) {
733737
return errors.New("labels not equal")
734738
}
@@ -738,11 +742,10 @@ func TestFluxReconciler_Install(t *testing.T) {
738742
},
739743
{
740744
name: "FluxReconciler with custom label func - creation successful",
741-
labelFunc: func(juggler.Component) map[string]string {
745+
labelFunc: func(comp juggler.Component) map[string]string {
742746
return map[string]string{
743-
testLabelComponentName: "custom-name",
744-
"app.kubernetes.io/managed-by": "service-provider-crossplane",
745-
"custom-label": "custom-value",
747+
testLabelComponentKey: comp.GetName(),
748+
testLabelManagedByKey: testLabelManagedByValue,
746749
}
747750
},
748751
obj: FakeFluxComponent{
@@ -774,9 +777,8 @@ func TestFluxReconciler_Install(t *testing.T) {
774777
},
775778
validateFunc: func(ctx context.Context, c client.Client, component juggler.Component) error {
776779
expectedLabels := map[string]string{
777-
"app.kubernetes.io/managed-by": "service-provider-crossplane",
778-
testLabelComponentName: "custom-name",
779-
"custom-label": "custom-value",
780+
testLabelComponentKey: component.GetName(),
781+
testLabelManagedByKey: testLabelManagedByValue,
780782
}
781783
helmRepository := &sourcev1.HelmRepository{}
782784
if err := c.Get(ctx, client.ObjectKey{Name: "test", Namespace: "default"}, helmRepository); err != nil {
@@ -800,7 +802,7 @@ func TestFluxReconciler_Install(t *testing.T) {
800802
for _, tt := range tests {
801803
t.Run(tt.name, func(t *testing.T) {
802804
fakeLocalClient := fake.NewClientBuilder().WithScheme(scheme).Build()
803-
r := NewFluxReconciler(logr.Logger{}, fakeLocalClient, nil, testLabelComponentName).
805+
r := NewFluxReconciler(logr.Logger{}, fakeLocalClient, nil, testLabelComponentKey).
804806
WithLabelFunc(tt.labelFunc)
805807
ctx := context.TODO()
806808
actual := r.Install(ctx, tt.obj)

pkg/juggler/object/object_reconciler_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import (
2727

2828
var errBoom = errors.New("boom")
2929

30-
const testLabelComponentName = "object.juggler.test.io/component"
30+
const (
31+
testLabelComponentKey = "object.juggler.test.io/component"
32+
testLabelManagedByKey = "object.juggler.test.io/managedBy"
33+
testLabelManagedByValue = "object.juggler.test.io/control-plane-operator"
34+
)
3135

3236
func TestObjectReconciler_Install(t *testing.T) {
3337
tests := []struct {
@@ -81,7 +85,7 @@ func TestObjectReconciler_Install(t *testing.T) {
8185
}
8286
if !assert.Equal(t, secret.GetLabels(), map[string]string{
8387
"app.kubernetes.io/managed-by": "control-plane-operator",
84-
testLabelComponentName: comp.GetName(),
88+
testLabelComponentKey: comp.GetName(),
8589
}) {
8690
return errors.New("labels not equal")
8791
}
@@ -121,7 +125,7 @@ func TestObjectReconciler_Install(t *testing.T) {
121125
}
122126
if !assert.Equal(t, secret.GetLabels(), map[string]string{
123127
"app.kubernetes.io/managed-by": "control-plane-operator",
124-
testLabelComponentName: comp.GetName(),
128+
testLabelComponentKey: comp.GetName(),
125129
}) {
126130
return errors.New("labels not equal")
127131
}
@@ -145,11 +149,10 @@ func TestObjectReconciler_Install(t *testing.T) {
145149
},
146150
name: "FakeObjectComponent",
147151
},
148-
labelFunc: func(juggler.Component) map[string]string {
152+
labelFunc: func(comp juggler.Component) map[string]string {
149153
return map[string]string{
150-
testLabelComponentName: "custom-name",
151-
"app.kubernetes.io/managed-by": "managed-by-value",
152-
"custom-label": "custom-value",
154+
testLabelComponentKey: comp.GetName(),
155+
testLabelManagedByKey: testLabelManagedByValue,
153156
}
154157
},
155158
validateFunc: func(ctx context.Context, c client.Client, comp juggler.Component) error {
@@ -158,9 +161,8 @@ func TestObjectReconciler_Install(t *testing.T) {
158161
return err
159162
}
160163
if !assert.Equal(t, secret.GetLabels(), map[string]string{
161-
testLabelComponentName: "custom-name",
162-
"app.kubernetes.io/managed-by": "managed-by-value",
163-
"custom-label": "custom-value",
164+
testLabelComponentKey: comp.GetName(),
165+
testLabelManagedByKey: testLabelManagedByValue,
164166
}) {
165167
return errors.New("labels not equal")
166168
}
@@ -171,7 +173,7 @@ func TestObjectReconciler_Install(t *testing.T) {
171173
for _, tt := range tests {
172174
t.Run(tt.name, func(t *testing.T) {
173175
fakeRemoteClient := fake.NewClientBuilder().WithObjects(tt.remoteObjects...).Build()
174-
r := NewReconciler(logr.Logger{}, fakeRemoteClient, testLabelComponentName).
176+
r := NewReconciler(logr.Logger{}, fakeRemoteClient, testLabelComponentKey).
175177
WithLabelFunc(tt.labelFunc)
176178
ctx := context.TODO()
177179
actual := r.Install(ctx, tt.obj)
@@ -409,7 +411,7 @@ func TestObjectReconciler_Uninstall(t *testing.T) {
409411
for _, tt := range tests {
410412
t.Run(tt.name, func(t *testing.T) {
411413
fakeClient := fake.NewClientBuilder().WithObjects(tt.remoteObjects...).Build()
412-
r := NewReconciler(logr.Logger{}, fakeClient, testLabelComponentName)
414+
r := NewReconciler(logr.Logger{}, fakeClient, testLabelComponentKey)
413415
ctx := context.TODO()
414416
actual := r.Uninstall(ctx, tt.obj)
415417
if !errors.Is(actual, tt.expected) {
@@ -575,7 +577,7 @@ func TestObjectReconciler_Observe(t *testing.T) {
575577
}
576578
for _, tt := range tests {
577579
t.Run(tt.name, func(t *testing.T) {
578-
r := NewReconciler(logr.Logger{}, fake.NewClientBuilder().WithObjects(tt.remoteObjects...).Build(), testLabelComponentName)
580+
r := NewReconciler(logr.Logger{}, fake.NewClientBuilder().WithObjects(tt.remoteObjects...).Build(), testLabelComponentKey)
579581
ctx := context.TODO()
580582
actualObservation, actualError := r.Observe(ctx, tt.obj)
581583
if !assert.Equal(t, tt.expectedObservation, actualObservation) {
@@ -669,7 +671,7 @@ func Test_ObjectReconciler_DetectOrphanedComponents(t *testing.T) {
669671
for _, tC := range testCases {
670672
t.Run(tC.desc, func(t *testing.T) {
671673
fakeClient := fake.NewClientBuilder().WithObjects(tC.initObjs...).WithInterceptorFuncs(tC.interceptorFuncs).Build()
672-
r := NewReconciler(logr.Logger{}, fakeClient, testLabelComponentName)
674+
r := NewReconciler(logr.Logger{}, fakeClient, testLabelComponentKey)
673675
for _, cc := range tC.configuredComponents {
674676
r.RegisterType(cc.(ObjectComponent))
675677
}

pkg/juggler/reconciler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package juggler
33
import (
44
"context"
55
"reflect"
6+
7+
"github.com/openmcp-project/control-plane-operator/pkg/utils"
68
)
79

810
type ComponentReconciler interface {
@@ -53,8 +55,8 @@ type LabelFunc func(comp Component) map[string]string
5355
func DefaultLabelFunc(componentLabel string) LabelFunc {
5456
return func(comp Component) map[string]string {
5557
return map[string]string{
56-
"app.kubernetes.io/managed-by": "control-plane-operator",
57-
componentLabel: comp.GetName(),
58+
utils.LabelManagedBy: utils.LabelManagedByValue,
59+
componentLabel: comp.GetName(),
5860
}
5961
}
6062
}

pkg/utils/meta.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
const (
9-
labelManagedBy = "app.kubernetes.io/managed-by"
10-
labelManagedByValue = "control-plane-operator"
9+
LabelManagedBy = "app.kubernetes.io/managed-by"
10+
LabelManagedByValue = "control-plane-operator"
1111
LabelComponentName = "controlplane.core.orchestrate.cloud.sap/component"
1212
)
1313

@@ -21,11 +21,11 @@ func SetLabel(obj v1.Object, label string, value string) {
2121
}
2222

2323
func SetManagedBy(obj v1.Object) {
24-
SetLabel(obj, labelManagedBy, labelManagedByValue)
24+
SetLabel(obj, LabelManagedBy, LabelManagedByValue)
2525
}
2626

2727
func IsManaged() client.MatchingLabels {
28-
return client.MatchingLabels{labelManagedBy: labelManagedByValue}
28+
return client.MatchingLabels{LabelManagedBy: LabelManagedByValue}
2929
}
3030

3131
func HasComponentLabel() client.ListOption {

pkg/utils/meta_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestSetManagedBy(t *testing.T) {
5757
{
5858
name: "set managed by label",
5959
obj: &v1beta1.ControlPlane{},
60-
want: map[string]string{labelManagedBy: labelManagedByValue},
60+
want: map[string]string{LabelManagedBy: LabelManagedByValue},
6161
},
6262
{
6363
name: "update existing label",
@@ -66,12 +66,12 @@ func TestSetManagedBy(t *testing.T) {
6666
Labels: map[string]string{"app.kubernetes.io/managed-by": "foo"},
6767
},
6868
},
69-
want: map[string]string{labelManagedBy: labelManagedByValue},
69+
want: map[string]string{LabelManagedBy: LabelManagedByValue},
7070
},
7171
{
7272
name: "add a second label to object",
7373
obj: &v1beta1.ControlPlane{ObjectMeta: v1.ObjectMeta{Labels: map[string]string{"foo": "bar"}}},
74-
want: map[string]string{"foo": "bar", labelManagedBy: labelManagedByValue},
74+
want: map[string]string{"foo": "bar", LabelManagedBy: LabelManagedByValue},
7575
},
7676
}
7777
for _, tt := range tests {
@@ -84,7 +84,7 @@ func TestSetManagedBy(t *testing.T) {
8484

8585
func TestIsManaged(t *testing.T) {
8686
got := IsManaged()
87-
assert.DeepEqual(t, got, client.MatchingLabels{labelManagedBy: labelManagedByValue})
87+
assert.DeepEqual(t, got, client.MatchingLabels{LabelManagedBy: LabelManagedByValue})
8888
}
8989

9090
func TestHasComponentLabel(t *testing.T) {

0 commit comments

Comments
 (0)