Skip to content

Commit 5e0e74b

Browse files
authored
Merge pull request #146 from gianlucam76/expose-labels
Expose labels set on ClusterSummary
2 parents 95fef08 + 05e07d6 commit 5e0e74b

File tree

9 files changed

+74
-57
lines changed

9 files changed

+74
-57
lines changed

api/v1alpha1/clusterprofile_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ const (
3535
AdminLabel = "projectsveltos.io/admin-name"
3636
)
3737

38+
const (
39+
// ClusterNameLabel is the label set on:
40+
// - ClusterSummary instances created by a ClusterProfile instance for a given cluster;
41+
// - ClusterConfiguration instances created by a ClusterProfile instance for a given cluster;
42+
// - ClusterReport instances created by a ClusterProfile instance for a given cluster;
43+
ClusterNameLabel = "projectsveltos.io/cluster-name"
44+
45+
// ClusterTypeLabel is the label set on:
46+
// - ClusterSummary instances created by a ClusterProfile instance for a given cluster;
47+
// - ClusterConfiguration instances created by a ClusterProfile instance for a given cluster;
48+
// - ClusterReport instances created by a ClusterProfile instance for a given cluster;
49+
ClusterTypeLabel = "projectsveltos.io/cluster-type"
50+
)
51+
3852
type DryRunReconciliationError struct{}
3953

4054
func (m *DryRunReconciliationError) Error() string {

controllers/clusterprofile_controller.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,9 @@ func (r *ClusterProfileReconciler) createClusterReport(ctx context.Context, clus
460460
Namespace: cluster.Namespace,
461461
Name: getClusterReportName(clusterProfile.Name, cluster.Name, clusterType),
462462
Labels: map[string]string{
463-
ClusterProfileLabelName: clusterProfile.Name,
464-
ClusterLabelName: cluster.Name,
465-
ClusterTypeLabelName: string(getClusterType(cluster)),
463+
ClusterProfileLabelName: clusterProfile.Name,
464+
configv1alpha1.ClusterNameLabel: cluster.Name,
465+
configv1alpha1.ClusterTypeLabel: string(getClusterType(cluster)),
466466
},
467467
},
468468
Spec: configv1alpha1.ClusterReportSpec{
@@ -529,7 +529,7 @@ func (r *ClusterProfileReconciler) updateClusterSummaries(ctx context.Context, c
529529
// If a Cluster exists and it is a match, ClusterSummary is created (and ClusterSummary.Spec kept in sync if mode is
530530
// continuous).
531531
// ClusterSummary won't program cluster in paused state.
532-
_, err = getClusterSummary(ctx, r.Client, clusterProfileScope.Name(), cluster.Namespace, cluster.Name)
532+
_, err = getClusterSummary(ctx, r.Client, clusterProfileScope.Name(), cluster.Namespace, cluster.Name, getClusterType(&cluster))
533533
if err != nil {
534534
if apierrors.IsNotFound(err) {
535535
err = r.createClusterSummary(ctx, clusterProfileScope, &cluster)
@@ -754,9 +754,7 @@ func (r *ClusterProfileReconciler) createClusterSummary(ctx context.Context, clu
754754

755755
clusterSummary.Spec.ClusterType = getClusterType(cluster)
756756
clusterSummary.Labels = clusterProfileScope.ClusterProfile.Labels
757-
addLabel(clusterSummary, ClusterProfileLabelName, clusterProfileScope.Name())
758-
addLabel(clusterSummary, ClusterLabelNamespace, cluster.Namespace)
759-
addLabel(clusterSummary, ClusterLabelName, cluster.Name)
757+
r.addClusterSummaryLabels(clusterSummary, clusterProfileScope, cluster)
760758

761759
return r.Create(ctx, clusterSummary)
762760
}
@@ -770,7 +768,7 @@ func (r *ClusterProfileReconciler) updateClusterSummary(ctx context.Context, clu
770768
return nil
771769
}
772770

773-
clusterSummary, err := getClusterSummary(ctx, r.Client, clusterProfileScope.Name(), cluster.Namespace, cluster.Name)
771+
clusterSummary, err := getClusterSummary(ctx, r.Client, clusterProfileScope.Name(), cluster.Namespace, cluster.Name, getClusterType(cluster))
774772
if err != nil {
775773
return err
776774
}
@@ -784,14 +782,23 @@ func (r *ClusterProfileReconciler) updateClusterSummary(ctx context.Context, clu
784782
clusterSummary.Annotations = clusterProfileScope.ClusterProfile.Annotations
785783
clusterSummary.Spec.ClusterProfileSpec = clusterProfileScope.ClusterProfile.Spec
786784
clusterSummary.Spec.ClusterType = getClusterType(cluster)
787-
clusterSummary.Labels = clusterProfileScope.ClusterProfile.Labels
788-
addLabel(clusterSummary, ClusterProfileLabelName, clusterProfileScope.Name())
789-
addLabel(clusterSummary, ClusterLabelNamespace, cluster.Namespace)
790-
addLabel(clusterSummary, ClusterLabelName, cluster.Name)
785+
r.addClusterSummaryLabels(clusterSummary, clusterProfileScope, cluster)
791786

792787
return r.Update(ctx, clusterSummary)
793788
}
794789

790+
func (r *ClusterProfileReconciler) addClusterSummaryLabels(clusterSummary *configv1alpha1.ClusterSummary, clusterProfileScope *scope.ClusterProfileScope,
791+
cluster *corev1.ObjectReference) {
792+
793+
addLabel(clusterSummary, ClusterProfileLabelName, clusterProfileScope.Name())
794+
addLabel(clusterSummary, configv1alpha1.ClusterNameLabel, cluster.Name)
795+
if cluster.APIVersion == libsveltosv1alpha1.GroupVersion.String() {
796+
addLabel(clusterSummary, configv1alpha1.ClusterTypeLabel, string(libsveltosv1alpha1.ClusterTypeSveltos))
797+
} else {
798+
addLabel(clusterSummary, configv1alpha1.ClusterTypeLabel, string(libsveltosv1alpha1.ClusterTypeCapi))
799+
}
800+
}
801+
795802
// deleteClusterSummary deletes ClusterSummary given a ClusterProfile and a matching Sveltos/CAPI Cluster
796803
func (r *ClusterProfileReconciler) deleteClusterSummary(ctx context.Context,
797804
clusterSummary *configv1alpha1.ClusterSummary) error {
@@ -848,8 +855,8 @@ func (r *ClusterProfileReconciler) createClusterConfiguration(ctx context.Contex
848855
Namespace: cluster.Namespace,
849856
Name: getClusterConfigurationName(cluster.Name, getClusterType(cluster)),
850857
Labels: map[string]string{
851-
ClusterLabelName: cluster.Name,
852-
ClusterTypeLabelName: string(getClusterType(cluster)),
858+
configv1alpha1.ClusterNameLabel: cluster.Name,
859+
configv1alpha1.ClusterTypeLabel: string(getClusterType(cluster)),
853860
},
854861
},
855862
}

controllers/clusterprofile_controller_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -444,16 +444,24 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
444444
})
445445

446446
It("UpdateClusterSummary updates ClusterSummary with proper fields when ClusterProfile syncmode set to continuous", func() {
447-
clusterSummaryName := controllers.GetClusterSummaryName(clusterProfile.Name, matchingCluster.Name, false)
447+
sveltosCluster := &libsveltosv1alpha1.SveltosCluster{
448+
ObjectMeta: metav1.ObjectMeta{
449+
Name: randomString(),
450+
Namespace: randomString(),
451+
Labels: matchingCluster.Labels,
452+
},
453+
}
454+
455+
clusterSummaryName := controllers.GetClusterSummaryName(sveltosCluster.Name, sveltosCluster.Name, false)
448456
clusterSummary := &configv1alpha1.ClusterSummary{
449457
ObjectMeta: metav1.ObjectMeta{
450458
Name: clusterSummaryName,
451-
Namespace: matchingCluster.Namespace,
459+
Namespace: sveltosCluster.Namespace,
452460
},
453461
Spec: configv1alpha1.ClusterSummarySpec{
454-
ClusterNamespace: matchingCluster.Namespace,
455-
ClusterName: matchingCluster.Name,
456-
ClusterType: libsveltosv1alpha1.ClusterTypeCapi,
462+
ClusterNamespace: sveltosCluster.Namespace,
463+
ClusterName: sveltosCluster.Name,
464+
ClusterType: libsveltosv1alpha1.ClusterTypeSveltos,
457465
ClusterProfileSpec: configv1alpha1.ClusterProfileSpec{
458466
SyncMode: configv1alpha1.SyncModeOneTime,
459467
PolicyRefs: []libsveltosv1alpha1.PolicyRef{
@@ -466,7 +474,7 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
466474
},
467475
},
468476
}
469-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Namespace, matchingCluster.Name)
477+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, sveltosCluster.Name, libsveltosv1alpha1.ClusterTypeSveltos)
470478

471479
clusterProfile.Spec.SyncMode = configv1alpha1.SyncModeContinuous
472480
clusterProfile.Spec.PolicyRefs = []libsveltosv1alpha1.PolicyRef{
@@ -484,7 +492,7 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
484492

485493
initObjects := []client.Object{
486494
clusterProfile,
487-
matchingCluster,
495+
sveltosCluster,
488496
clusterSummary,
489497
}
490498

@@ -510,15 +518,15 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
510518

511519
err = controllers.UpdateClusterSummary(reconciler, context.TODO(),
512520
clusterProfileScope, &corev1.ObjectReference{
513-
Namespace: matchingCluster.Namespace, Name: matchingCluster.Name,
514-
Kind: clusterKind, APIVersion: clusterv1.GroupVersion.String()})
521+
Namespace: sveltosCluster.Namespace, Name: sveltosCluster.Name,
522+
Kind: libsveltosv1alpha1.SveltosClusterKind, APIVersion: libsveltosv1alpha1.GroupVersion.String()})
515523
Expect(err).To(BeNil())
516524

517525
clusterSummaryList := &configv1alpha1.ClusterSummaryList{}
518526
Expect(c.List(context.TODO(), clusterSummaryList)).To(BeNil())
519527
Expect(len(clusterSummaryList.Items)).To(Equal(1))
520-
Expect(clusterSummaryList.Items[0].Spec.ClusterName).To(Equal(matchingCluster.Name))
521-
Expect(clusterSummaryList.Items[0].Spec.ClusterNamespace).To(Equal(matchingCluster.Namespace))
528+
Expect(clusterSummaryList.Items[0].Spec.ClusterName).To(Equal(sveltosCluster.Name))
529+
Expect(clusterSummaryList.Items[0].Spec.ClusterNamespace).To(Equal(sveltosCluster.Namespace))
522530
Expect(reflect.DeepEqual(clusterSummaryList.Items[0].Spec.ClusterProfileSpec, clusterProfile.Spec)).To(BeTrue())
523531
})
524532

@@ -550,7 +558,7 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
550558
ClusterType: libsveltosv1alpha1.ClusterTypeCapi,
551559
},
552560
}
553-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Namespace, matchingCluster.Name)
561+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Name, libsveltosv1alpha1.ClusterTypeCapi)
554562

555563
initObjects := []client.Object{
556564
clusterProfile,
@@ -636,7 +644,7 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
636644
ClusterType: libsveltosv1alpha1.ClusterTypeCapi,
637645
},
638646
}
639-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Namespace, matchingCluster.Name)
647+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Name, libsveltosv1alpha1.ClusterTypeCapi)
640648

641649
initObjects := []client.Object{
642650
clusterProfile,
@@ -817,7 +825,7 @@ var _ = Describe("ClusterProfile: Reconciler", func() {
817825
},
818826
},
819827
}
820-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Namespace, matchingCluster.Name)
828+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, matchingCluster.Name, libsveltosv1alpha1.ClusterTypeCapi)
821829

822830
initObjects := []client.Object{
823831
clusterProfile,

controllers/clustersummary_deployer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var _ = Describe("ClustersummaryDeployer", func() {
6363
ClusterType: libsveltosv1alpha1.ClusterTypeCapi,
6464
},
6565
}
66-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, namespace, clusterName)
66+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, clusterName, libsveltosv1alpha1.ClusterTypeCapi)
6767

6868
cluster = &clusterv1.Cluster{
6969
ObjectMeta: metav1.ObjectMeta{

controllers/labels.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@ const (
2929
// by a ClusterProfile instance
3030
ClusterProfileLabelName = "projectsveltos.io/cluster-profile-name"
3131

32-
// clusterLabelNamespace is the label set on ClusterSummary instances created
33-
// by a ClusterProfile instance for a given cluster
34-
ClusterLabelNamespace = "projectsveltos.io/cluster-namespace"
35-
36-
// clusterLabelName is the label set on:
37-
// - ClusterSummary instances created by a ClusterProfile instance for a given cluster;
38-
// - ClusterConfiguration instances created by a ClusterProfile instance for a given cluster;
39-
// - ClusterReport instances created by a ClusterProfile instance for a given cluster;
40-
ClusterLabelName = "projectsveltos.io/cluster-name"
41-
42-
// ClusterTypeLabelName is the label set on:
43-
// - ClusterSummary instances created by a ClusterProfile instance for a given cluster;
44-
// - ClusterConfiguration instances created by a ClusterProfile instance for a given cluster;
45-
// - ClusterReport instances created by a ClusterProfile instance for a given cluster;
46-
ClusterTypeLabelName = "projectsveltos.io/cluster-type"
47-
4832
// PolicyTemplate is the annotation that must be set on a policy when the
4933
// policy is a template and needs variable sustitution.
5034
PolicyTemplate = "projectsveltos.io/template"

controllers/suite_helpers_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ func randomString() string {
146146
return util.RandomString(length)
147147
}
148148

149-
func addLabelsToClusterSummary(clusterSummary *configv1alpha1.ClusterSummary, clusterProfileName, clusterNamespace, clusterName string) {
149+
func addLabelsToClusterSummary(clusterSummary *configv1alpha1.ClusterSummary, clusterProfileName, clusterName string,
150+
clusterType libsveltosv1alpha1.ClusterType) {
151+
150152
labels := clusterSummary.Labels
151153
if labels == nil {
152154
labels = make(map[string]string)
153155
}
154156
labels[controllers.ClusterProfileLabelName] = clusterProfileName
155-
labels[controllers.ClusterLabelNamespace] = clusterNamespace
156-
labels[controllers.ClusterLabelName] = clusterName
157+
labels[configv1alpha1.ClusterTypeLabel] = string(clusterType)
158+
labels[configv1alpha1.ClusterNameLabel] = clusterName
157159

158160
clusterSummary.Labels = labels
159161
}
@@ -227,7 +229,7 @@ func prepareForDeployment(clusterProfile *configv1alpha1.ClusterProfile,
227229
cluster *clusterv1.Cluster) {
228230

229231
By("Add proper labels to ClusterSummary")
230-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, cluster.Namespace, cluster.Name)
232+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, cluster.Name, libsveltosv1alpha1.ClusterTypeCapi)
231233

232234
Expect(addTypeInformationToObject(testEnv.Scheme(), clusterProfile)).To(Succeed())
233235
Expect(addTypeInformationToObject(testEnv.Scheme(), clusterSummary)).To(Succeed())

controllers/utils.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ func GetClusterSummaryName(clusterProfileName, clusterName string, isSveltosClus
7575
// getClusterSummary returns the ClusterSummary instance created by a specific ClusterProfile for a specific
7676
// CAPI Cluster
7777
func getClusterSummary(ctx context.Context, c client.Client,
78-
clusterProfileName, clusterNamespace, clusterName string) (*configv1alpha1.ClusterSummary, error) {
78+
clusterProfileName, clusterNamespace, clusterName string,
79+
clusterType libsveltosv1alpha1.ClusterType) (*configv1alpha1.ClusterSummary, error) {
7980

8081
listOptions := []client.ListOption{
8182
client.InNamespace(clusterNamespace),
8283
client.MatchingLabels{
83-
ClusterProfileLabelName: clusterProfileName,
84-
ClusterLabelNamespace: clusterNamespace,
85-
ClusterLabelName: clusterName,
84+
ClusterProfileLabelName: clusterProfileName,
85+
configv1alpha1.ClusterNameLabel: clusterName,
86+
configv1alpha1.ClusterTypeLabel: string(clusterType),
8687
},
8788
}
8889

controllers/utils_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ var _ = Describe("getClusterProfileOwner ", func() {
172172
ClusterType: libsveltosv1alpha1.ClusterTypeCapi,
173173
},
174174
}
175-
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, cluster.Namespace, cluster.Name)
175+
addLabelsToClusterSummary(clusterSummary, clusterProfile.Name, cluster.Name, libsveltosv1alpha1.ClusterTypeCapi)
176176
})
177177

178178
It("getClusterProfileOwner returns ClusterProfile owner", func() {
@@ -255,7 +255,8 @@ var _ = Describe("getClusterProfileOwner ", func() {
255255

256256
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjects...).Build()
257257

258-
currentClusterSummary, err := controllers.GetClusterSummary(context.TODO(), c, clusterProfile.Name, cluster.Namespace, cluster.Name)
258+
currentClusterSummary, err := controllers.GetClusterSummary(context.TODO(), c, clusterProfile.Name, cluster.Namespace, cluster.Name,
259+
libsveltosv1alpha1.ClusterTypeCapi)
259260
Expect(err).To(BeNil())
260261
Expect(currentClusterSummary).ToNot(BeNil())
261262
Expect(currentClusterSummary.Name).To(Equal(clusterSummary.Name))

test/fv/utils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ func getClusterSummary(ctx context.Context,
177177
client.InNamespace(clusterNamespace),
178178
client.MatchingLabels{
179179
controllers.ClusterProfileLabelName: clusterProfileName,
180-
controllers.ClusterLabelNamespace: clusterNamespace,
181-
controllers.ClusterLabelName: clusterName,
180+
configv1alpha1.ClusterNameLabel: clusterName,
181+
configv1alpha1.ClusterTypeLabel: string(libsveltosv1alpha1.ClusterTypeCapi),
182182
},
183183
}
184184

0 commit comments

Comments
 (0)