Skip to content

Commit

Permalink
Fixed update problem
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Oct 20, 2023
1 parent 67125f0 commit cedd615
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
41 changes: 30 additions & 11 deletions internal/controller/gardener_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package controller
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/types"

Check failure on line 22 in internal/controller/gardener_cluster_controller.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -106,25 +107,22 @@ func (controller *GardenerClusterController) Reconcile(ctx context.Context, req
if err != nil {
controller.log.Error(err, "Failed to create, or rotate secret")
if controller.persistStatusChange(ctx, &cluster) != nil {
controller.log.Error(err, "Failed to set state for GardenerCluster", req.NamespacedName)
controller.log.Error(err, "Failed to set state for GardenerCluster")
}
return controller.resultWithoutRequeue(), err
}

cluster.UpdateConditionForReadyState(imv1.ConditionTypeKubeconfigManagement, imv1.ConditionReasonKubeconfigSecretReady, metav1.ConditionTrue)
annotations := cluster.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}

annotations[lastKubeconfigSyncAnnotation] = lastSyncTime.UTC().Format(time.RFC3339)
cluster.SetAnnotations(annotations)

controller.log.Info(fmt.Sprintf("Annotations: %v", cluster.Annotations))

err = controller.persistStatusChange(ctx, &cluster)
if err != nil {
controller.log.Error(err, "Failed to set state for GardenerCluster", req.NamespacedName)
controller.log.Error(err, "Failed to set state for GardenerCluster")
return controller.resultWithoutRequeue(), err
}

err = controller.persistLastSyncTime(ctx, &cluster, lastSyncTime)
if err != nil {
controller.log.Error(err, "Failed to set state for GardenerCluster")
return controller.resultWithoutRequeue(), err
}

Expand All @@ -148,6 +146,27 @@ func (controller *GardenerClusterController) persistStatusChange(ctx context.Con
return controller.Client.Status().Update(ctx, cluster)
}

func (controller *GardenerClusterController) persistLastSyncTime(ctx context.Context, cluster *imv1.GardenerCluster, lastSyncTime time.Time) error {
var clusterToUpgrade imv1.GardenerCluster

gardenerClusterKey := types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}

err := controller.Client.Get(ctx, gardenerClusterKey, &clusterToUpgrade)
if err != nil {
return err
}
annotations := clusterToUpgrade.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}

annotations[lastKubeconfigSyncAnnotation] = lastSyncTime.UTC().Format(time.RFC3339)

clusterToUpgrade.SetAnnotations(annotations)

return controller.Client.Update(ctx, &clusterToUpgrade)
}

func (controller *GardenerClusterController) deleteSecret(clusterCRName string) error {
selector := client.MatchingLabels(map[string]string{
clusterCRNameLabel: clusterCRName,
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/gardener_cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ var _ = Describe("Gardener Cluster controller", func() {

err = k8sClient.Get(context.Background(), gardenerClusterKey, &newGardenerCluster)
Expect(err).To(BeNil())
// _, err = parseLastSyncTime(newGardenerCluster.GetAnnotations()[lastKubeconfigSyncAnnotation])
// Expect(err).To(BeNil())
_, err = parseLastSyncTime(newGardenerCluster.GetAnnotations()[lastKubeconfigSyncAnnotation])
Expect(err).To(BeNil())

By("Delete Cluster CR")
Expect(k8sClient.Delete(context.Background(), &gardenerClusterCR)).To(Succeed())
Expand Down

0 comments on commit cedd615

Please sign in to comment.