Skip to content

Commit

Permalink
Merge pull request #278 from gianlucam76/exclude-non-ready-cluster
Browse files Browse the repository at this point in the history
Matching clusters: only cluster ready to be programmed can be a match
  • Loading branch information
gianlucam76 authored Mar 13, 2024
2 parents 864cbad + e9d3380 commit e1c8ba4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/clusterproxy/cluster_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ func getMatchingCAPIClusters(ctx context.Context, c client.Client, selector labe
continue
}

if !isCAPIControlPlaneReady(cluster) {
// Only ready cluster can match
continue
}

addTypeInformationToObject(c.Scheme(), cluster)
if selector.Matches(labels.Set(cluster.Labels)) {
matching = append(matching, corev1.ObjectReference{
Expand Down Expand Up @@ -467,6 +472,11 @@ func getMatchingSveltosClusters(ctx context.Context, c client.Client, selector l
continue
}

if !isSveltosClusterStatusReady(cluster) {
// Only ready cluster can match
continue
}

addTypeInformationToObject(c.Scheme(), cluster)
if selector.Matches(labels.Set(cluster.Labels)) {
matching = append(matching, corev1.ObjectReference{
Expand Down
14 changes: 13 additions & 1 deletion lib/clusterproxy/cluster_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ var _ = Describe("Cluster utils", func() {
Spec: clusterv1.ClusterSpec{
Paused: true,
},
Status: clusterv1.ClusterStatus{
ControlPlaneReady: true,
},
}

sveltosCluster = &libsveltosv1alpha1.SveltosCluster{
Expand All @@ -62,6 +65,9 @@ var _ = Describe("Cluster utils", func() {
Spec: libsveltosv1alpha1.SveltosClusterSpec{
Paused: true,
},
Status: libsveltosv1alpha1.SveltosClusterStatus{
Ready: true,
},
}
})

Expand Down Expand Up @@ -270,13 +276,19 @@ var _ = Describe("Cluster utils", func() {
Namespace: randomString(),
Labels: currentLabels,
},
Status: libsveltosv1alpha1.SveltosClusterStatus{
Ready: true,
},
}

nonMatchingSveltosCluster := &libsveltosv1alpha1.SveltosCluster{
ObjectMeta: metav1.ObjectMeta{
Name: randomString(),
Namespace: randomString(),
},
Status: libsveltosv1alpha1.SveltosClusterStatus{
Ready: true,
},
}

cluster.Labels = currentLabels
Expand All @@ -287,7 +299,7 @@ var _ = Describe("Cluster utils", func() {
nonMatchingSveltosCluster,
}

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

parsedSelector, _ := labels.Parse(string(selector))

Expand Down
14 changes: 11 additions & 3 deletions lib/clusterproxy/clusterproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ func isSveltosClusterReadyToBeConfigured(
return false, err
}

return sveltosCluster.Status.Ready, nil
return isSveltosClusterStatusReady(sveltosCluster), nil
}

func isSveltosClusterStatusReady(sveltosCluster *libsveltosv1alpha1.SveltosCluster) bool {
return sveltosCluster.Status.Ready
}

// isCAPIClusterReadyToBeConfigured checks whether Cluster:
Expand All @@ -228,16 +232,20 @@ func isCAPIClusterReadyToBeConfigured(
return false, err
}

return isCAPIControlPlaneReady(capiCluster), nil
}

func isCAPIControlPlaneReady(capiCluster *clusterv1.Cluster) bool {
for i := range capiCluster.Status.Conditions {
c := capiCluster.Status.Conditions[i]
if c.Type == clusterv1.ControlPlaneInitializedCondition &&
c.Status == corev1.ConditionTrue {

return true, nil
return true
}
}

return capiCluster.Status.ControlPlaneReady, nil
return capiCluster.Status.ControlPlaneReady
}

// GetMachinesForCluster find all Machines for a given CAPI Cluster.
Expand Down

0 comments on commit e1c8ba4

Please sign in to comment.