From 0091ce6265223808d7eb6f93a6a15650b9b76918 Mon Sep 17 00:00:00 2001 From: Pankti Shah Date: Wed, 11 Oct 2023 22:58:39 -0700 Subject: [PATCH] Added an additional check to make sure etcdadm and capi clusters are available for workoad cluster --- pkg/api/v1alpha1/condition_consts.go | 3 ++ pkg/controller/clusters/status.go | 18 +++++-- pkg/controller/clusters/status_test.go | 72 +++++++++++++++++++++----- 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/pkg/api/v1alpha1/condition_consts.go b/pkg/api/v1alpha1/condition_consts.go index e3b27683a67b9..0418655c82ed8 100644 --- a/pkg/api/v1alpha1/condition_consts.go +++ b/pkg/api/v1alpha1/condition_consts.go @@ -48,6 +48,9 @@ const ( // RollingUpgradeInProgress reports the Cluster is executing a rolling upgrading to align the nodes to // a new desired machine spec. RollingUpgradeInProgress = "RollingUpgradeInProgress" + + // ExternalEtcdNotAvailable reports the Cluster status is waiting for Etcd to be available. + ExternalEtcdNotAvailable = "ExternalEtcdNotAvailable" ) const ( diff --git a/pkg/controller/clusters/status.go b/pkg/controller/clusters/status.go index 88f7ebb322400..af31607cc7edd 100644 --- a/pkg/controller/clusters/status.go +++ b/pkg/controller/clusters/status.go @@ -17,6 +17,8 @@ import ( // UpdateClusterStatusForControlPlane checks the current state of the Cluster's control plane and updates the // Cluster status information. +// There is a posibility that UpdateClusterStatusForControlPlane does not update the +// controleplane status specially in case where it still waiting for cluster objects to be created. func UpdateClusterStatusForControlPlane(ctx context.Context, client client.Client, cluster *anywherev1.Cluster) error { kcp, err := controller.GetKubeadmControlPlane(ctx, client, cluster) if err != nil { @@ -29,10 +31,11 @@ func UpdateClusterStatusForControlPlane(ctx context.Context, client client.Clien if err != nil { return errors.Wrap(err, "getting capi cluster") } - - etcdadmCluster, err = getEtcdadmCluster(ctx, client, capiCluster) - if err != nil { - return errors.Wrap(err, "reading etcdadm cluster") + if capiCluster != nil { + etcdadmCluster, err = getEtcdadmCluster(ctx, client, capiCluster) + if err != nil { + return errors.Wrap(err, "reading etcdadm cluster") + } } } @@ -85,6 +88,13 @@ func UpdateClusterStatusForCNI(ctx context.Context, cluster *anywherev1.Cluster) // updateConditionsForEtcdAndControlPlane updates the ControlPlaneReady condition if etcdadm cluster is not ready. func updateConditionsForEtcdAndControlPlane(cluster *anywherev1.Cluster, kcp *controlplanev1.KubeadmControlPlane, etcdadmCluster *etcdv1.EtcdadmCluster) { // Make sure etcd cluster is ready before marking ControlPlaneReady status to true + // This condition happens while creating a workload cluster from the management cluster using controller + // where it tries to get the etcdadm cluster for the first time before it generates the resources. + if cluster.Spec.ExternalEtcdConfiguration != nil && etcdadmCluster == nil { + conditions.MarkFalse(cluster, anywherev1.ControlPlaneReadyCondition, anywherev1.ExternalEtcdNotAvailable, clusterv1.ConditionSeverityInfo, "Etcd cluster is not available") + return + } + // Make sure etcd machine is ready before marking ControlPlaneReady status to true if cluster.Spec.ExternalEtcdConfiguration != nil && !etcdadmClusterReady(etcdadmCluster) { conditions.MarkFalse(cluster, anywherev1.ControlPlaneReadyCondition, anywherev1.RollingUpgradeInProgress, clusterv1.ConditionSeverityInfo, "Etcd is not ready") return diff --git a/pkg/controller/clusters/status_test.go b/pkg/controller/clusters/status_test.go index f49ab0186da8b..3307c1e3ce9db 100644 --- a/pkg/controller/clusters/status_test.go +++ b/pkg/controller/clusters/status_test.go @@ -38,6 +38,7 @@ func TestUpdateClusterStatusForControlPlane(t *testing.T) { wantCondition *anywherev1.Condition externalEtcdCount int externalEtcdCluster *etcdv1.EtcdadmCluster + capiCluster *clusterv1.Cluster }{ { name: "kcp is nil", @@ -429,6 +430,18 @@ func TestUpdateClusterStatusForControlPlane(t *testing.T) { Type: anywherev1.ControlPlaneReadyCondition, Status: "True", }, + capiCluster: &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: constants.EksaSystemNamespace, + }, + Spec: clusterv1.ClusterSpec{ + ManagedExternalEtcdRef: &corev1.ObjectReference{ + Kind: "EtcdadmCluster", + Name: fmt.Sprintf("%s-etcd", "test-cluster"), + }, + }, + }, }, { name: "with external etcd not ready", @@ -469,6 +482,50 @@ func TestUpdateClusterStatusForControlPlane(t *testing.T) { Message: "Etcd is not ready", Status: "False", }, + capiCluster: &clusterv1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + Namespace: constants.EksaSystemNamespace, + }, + Spec: clusterv1.ClusterSpec{ + ManagedExternalEtcdRef: &corev1.ObjectReference{ + Kind: "EtcdadmCluster", + Name: fmt.Sprintf("%s-etcd", "test-cluster"), + }, + }, + }, + }, + { + name: "with external etcd, etcd not reconciled", + kcp: test.KubeadmControlPlane(func(kcp *controlplanev1.KubeadmControlPlane) { + kcp.Status.Replicas = 3 + kcp.Status.ReadyReplicas = 3 + kcp.Status.UpdatedReplicas = 3 + + kcp.Status.Conditions = []clusterv1.Condition{ + { + Type: clusterv1.ReadyCondition, + Status: "True", + }, + } + }), + controlPlaneCount: 3, + conditions: []anywherev1.Condition{ + { + Type: anywherev1.ControlPlaneInitializedCondition, + Status: "True", + }, + }, + externalEtcdCount: 1, + externalEtcdCluster: &etcdv1.EtcdadmCluster{}, + wantCondition: &anywherev1.Condition{ + Type: anywherev1.ControlPlaneReadyCondition, + Reason: anywherev1.ExternalEtcdNotAvailable, + Severity: clusterv1.ConditionSeverityInfo, + Message: "Etcd cluster is not available yet", + Status: "False", + }, + capiCluster: &clusterv1.Cluster{}, }, } @@ -497,22 +554,11 @@ func TestUpdateClusterStatusForControlPlane(t *testing.T) { Name: fmt.Sprintf("%s-etcd", cluster.Name), }, } - capiCluster := &clusterv1.Cluster{ - ObjectMeta: metav1.ObjectMeta{ - Name: cluster.Name, - Namespace: constants.EksaSystemNamespace, - }, - Spec: clusterv1.ClusterSpec{ - ManagedExternalEtcdRef: &corev1.ObjectReference{ - Kind: "EtcdadmCluster", - Name: fmt.Sprintf("%s-etcd", cluster.Name), - }, - }, - } + tt.externalEtcdCluster.Name = fmt.Sprintf("%s-etcd", cluster.Name) tt.externalEtcdCluster.Namespace = cluster.Namespace - objs = append(objs, capiCluster) + objs = append(objs, tt.capiCluster) objs = append(objs, tt.externalEtcdCluster) }