Skip to content

Commit dc8757b

Browse files
committed
Update reconciler so that GitopsClusters are only Ready if ClusterConnected is Ready too
1 parent 7ac3571 commit dc8757b

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

api/v1alpha1/condition_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const (
1111
ControlPlaneReadyStatusReason string = "ControlPlaneReadyStatus"
1212
// WaitingForCAPIClusterReason signals that a given CAPI cluster has not been found.
1313
WaitingForCAPIClusterReason string = "WaitingForCAPICluster"
14+
// ClusterConnectedReason signals that a given cluster is connected.
15+
ClusterConnectedReason string = "ClusterConnected"
16+
// ClusterNotConnectedReason signals that a given cluster is not connected.
17+
ClusterNotConnectedReason string = "ClusterNotConnected"
1418

1519
// WaitingForCAPIClusterDeletionReason signals that this cluster has been
1620
// deleted, but the referenced CAPI Cluster still exists.

controllers/gitopscluster_controller.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/fluxcd/pkg/apis/meta"
2626
"github.com/fluxcd/pkg/runtime/conditions"
2727
corev1 "k8s.io/api/core/v1"
28-
v1 "k8s.io/api/core/v1"
2928
apierrors "k8s.io/apimachinery/pkg/api/errors"
3029
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3130
"k8s.io/apimachinery/pkg/runtime"
@@ -230,6 +229,21 @@ func (r *GitopsClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
230229
return ctrl.Result{}, err
231230
}
232231

232+
// Cluster is ready only if it has a secret and is connected
233+
connectedCondition := conditions.Get(cluster, gitopsv1alpha1.ClusterConnectivity)
234+
if connectedCondition == nil || !conditions.IsTrue(cluster, gitopsv1alpha1.ClusterConnectivity) {
235+
readyConditionReason := conditions.GetReason(cluster, meta.ReadyCondition)
236+
if readyConditionReason != gitopsv1alpha1.WaitingForControlPlaneReadyStatusReason {
237+
conditions.MarkFalse(cluster, meta.ReadyCondition, gitopsv1alpha1.ClusterNotConnectedReason, "No connectivity")
238+
if err := r.Status().Update(ctx, cluster); err != nil {
239+
log.Error(err, "failed to update Cluster status")
240+
return ctrl.Result{}, err
241+
}
242+
}
243+
} else {
244+
conditions.MarkTrue(cluster, meta.ReadyCondition, gitopsv1alpha1.ClusterConnectedReason, "")
245+
}
246+
233247
return ctrl.Result{RequeueAfter: r.Options.DefaultRequeueTime}, nil
234248
}
235249

@@ -443,7 +457,7 @@ func (r *GitopsClusterReconciler) restConfigFromSecret(ctx context.Context, clus
443457
Namespace: cluster.Namespace,
444458
}
445459

446-
var secret v1.Secret
460+
var secret corev1.Secret
447461
if err := r.Get(ctx, key, &secret); err != nil {
448462
log.Error(err, "unable to fetch secret for GitOps Cluster", "cluster", cluster.Name)
449463

controllers/gitopscluster_controller_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestReconcile(t *testing.T) {
7373
makeTestSecret(types.NamespacedName{
7474
Name: "dev",
7575
Namespace: testNamespace,
76-
}, map[string][]byte{"value": []byte("testing")}),
76+
}, map[string][]byte{"value": kubeConfig}),
7777
},
7878
obj: types.NamespacedName{Namespace: testNamespace, Name: testName},
7979
opts: controllers.Options{
@@ -282,6 +282,29 @@ func TestReconcile(t *testing.T) {
282282
wantStatus: "False",
283283
wantStatusMessage: `failed creating rest config from secret: invalid configuration: no server found for cluster "envtest"`,
284284
},
285+
{
286+
name: "No connectivity causes ready condition to be false",
287+
state: []runtime.Object{
288+
makeTestCluster(func(c *gitopsv1alpha1.GitopsCluster) {
289+
c.Spec.SecretRef = &meta.LocalObjectReference{
290+
Name: "dev",
291+
}
292+
}),
293+
makeTestSecret(types.NamespacedName{
294+
Name: "dev",
295+
Namespace: testNamespace,
296+
}, map[string][]byte{"value": kubeconfigWithError(t)}),
297+
},
298+
obj: types.NamespacedName{Namespace: testNamespace, Name: testName},
299+
opts: controllers.Options{
300+
CAPIEnabled: true,
301+
DefaultRequeueTime: defaultRequeueTime,
302+
},
303+
requeueAfter: defaultRequeueTime,
304+
wantCondition: meta.ReadyCondition,
305+
wantStatus: "False",
306+
wantStatusMessage: "No connectivity",
307+
},
285308
}
286309

287310
for _, tt := range tests {

0 commit comments

Comments
 (0)