From 1d42b16219bdefa9deff4c652bea1fa45a1530d4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Galwas Date: Tue, 24 Oct 2023 14:29:47 +0200 Subject: [PATCH] Changed operations order: setting status is the last operation. --- .../controller/gardener_cluster_controller.go | 25 ++++++++++++++----- .../gardener_cluster_controller_test.go | 8 +++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/controller/gardener_cluster_controller.go b/internal/controller/gardener_cluster_controller.go index dc87026a..835dceee 100644 --- a/internal/controller/gardener_cluster_controller.go +++ b/internal/controller/gardener_cluster_controller.go @@ -104,6 +104,11 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req return controller.resultWithoutRequeue(), err } + err = controller.removeForceRotationAnnotation(ctx, &cluster) + if err != nil { + return controller.resultWithoutRequeue(), err + } + if kubeconfigRotated { err = controller.persistStatusChange(ctx, &cluster) if err != nil { @@ -111,11 +116,6 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req } } - err = controller.removeForceRotationAnnotation(ctx, &cluster) - if err != nil { - return controller.resultWithoutRequeue(), err - } - return controller.resultWithRequeue(), nil } @@ -139,7 +139,20 @@ func (controller *GardenerClusterController) resultWithoutRequeue() ctrl.Result } func (controller *GardenerClusterController) persistStatusChange(ctx context.Context, cluster *imv1.GardenerCluster) error { - statusErr := controller.Client.Status().Update(ctx, cluster) + key := types.NamespacedName{ + Name: cluster.Name, + Namespace: cluster.Namespace, + } + var clusterToUpdate imv1.GardenerCluster + + err := controller.Client.Get(ctx, key, &clusterToUpdate) + if err != nil { + return err + } + + clusterToUpdate.Status = cluster.Status + + statusErr := controller.Client.Status().Update(ctx, &clusterToUpdate) if statusErr != nil { controller.log.Error(statusErr, "Failed to set state for GardenerCluster") } diff --git a/internal/controller/gardener_cluster_controller_test.go b/internal/controller/gardener_cluster_controller_test.go index ba006532..f60ae6fd 100644 --- a/internal/controller/gardener_cluster_controller_test.go +++ b/internal/controller/gardener_cluster_controller_test.go @@ -145,7 +145,7 @@ var _ = Describe("Gardener Cluster controller", func() { _, forceRotationAnnotationFound := newGardenerCluster.GetAnnotations()[forceKubeconfigRotationAnnotation] return readyState && !forceRotationAnnotationFound - }, time.Second*30, time.Second*3).Should(BeTrue()) + }, time.Second*45, time.Second*3).Should(BeTrue()) err := k8sClient.Get(context.Background(), secretKey, &kubeconfigSecret) Expect(err).To(BeNil()) @@ -158,7 +158,7 @@ var _ = Describe("Gardener Cluster controller", func() { fixGardenerClusterCR("kymaname4", namespace, "shootName4", "secret-name4"), fixNewSecret("secret-name4", namespace, "kymaname4", "shootName4", "kubeconfig4", "2023-10-09T23:00:00Z"), "kubeconfig4"), - Entry("Rotate dynamic kubeconfig", + Entry("Force rotation", fixGardenerClusterCRWithForceRotationAnnotation("kymaname5", namespace, "shootName5", "secret-name5"), fixNewSecret("secret-name5", namespace, "kymaname5", "shootName5", "kubeconfig5", time.Now().UTC().Format(time.RFC3339)), "kubeconfig5"), @@ -172,7 +172,7 @@ var _ = Describe("Gardener Cluster controller", func() { previousTimestamp := secret.Annotations[lastKubeconfigSyncAnnotation] By("Create Cluster CR") - gardenerClusterCR := fixGardenerClusterCRWithForceRotationAnnotation("kymaname6", namespace, "shootName6", "secret-name6") + gardenerClusterCR := fixGardenerClusterCR("kymaname6", namespace, "shootName6", "secret-name6") Expect(k8sClient.Create(context.Background(), &gardenerClusterCR)).To(Succeed()) var kubeconfigSecret corev1.Secret @@ -187,7 +187,7 @@ var _ = Describe("Gardener Cluster controller", func() { timestampAnnotation := kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation] return timestampAnnotation == previousTimestamp - }, time.Second*30, time.Second*3).Should(BeTrue()) + }, time.Second*45, time.Second*3).Should(BeTrue()) }) }) })