Skip to content

Commit

Permalink
Changed operations order: setting status is the last operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Oct 24, 2023
1 parent b780cd7 commit 1d42b16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
25 changes: 19 additions & 6 deletions internal/controller/gardener_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ 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 {
return controller.resultWithoutRequeue(), err
}
}

err = controller.removeForceRotationAnnotation(ctx, &cluster)
if err != nil {
return controller.resultWithoutRequeue(), err
}

return controller.resultWithRequeue(), nil
}

Expand All @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/gardener_cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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"),
Expand All @@ -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
Expand All @@ -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())
})
})
})
Expand Down

0 comments on commit 1d42b16

Please sign in to comment.