Skip to content

Commit

Permalink
remove secret when IM CR is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Disper committed Sep 19, 2023
1 parent fb91a28 commit f953c8c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 30 additions & 3 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"
"github.com/pkg/errors"

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 @@ -78,10 +79,18 @@ func (r *GardenerClusterController) Reconcile(ctx context.Context, req ctrl.Requ
var cluster infrastructuremanagerv1.GardenerCluster

err := r.Client.Get(ctx, req.NamespacedName, &cluster)

if err != nil {
r.log.Error(err, "could not get the CR for "+req.NamespacedName.Name)
return r.ResultWithoutRequeue(), err
if k8serrors.IsNotFound(err) {
err = r.deleteSecret(req.NamespacedName.Name)
if err != nil {
r.log.Error(err, "failed to delete secret")
}
}

return ctrl.Result{
Requeue: true,
RequeueAfter: defaultRequeuInSeconds,
}, err
}

secret, err := r.getSecret(cluster.Spec.Shoot.Name)
Expand All @@ -106,6 +115,24 @@ func (r *GardenerClusterController) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, nil
}

func (r *GardenerClusterController) deleteSecret(clusterCRName string) error {
selector := client.MatchingLabels(map[string]string{
clusterCRNameLabel: clusterCRName,
})

var secretList corev1.SecretList
err := r.Client.List(context.TODO(), &secretList, selector)
if err != nil {
return err
}

if len(secretList.Items) != 1 {
return errors.Errorf("unexpected numer of secrets found for cluster CR `%s`", clusterCRName)
}

return r.Client.Delete(context.TODO(), &secretList.Items[0])
}

func (r *GardenerClusterController) ResultWithoutRequeue() ctrl.Result {
return ctrl.Result{
Requeue: false,
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/gardener_cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
k8serrors "k8s.io/apimachinery/pkg/api/errors"

Check failure on line 5 in internal/controller/gardener_cluster_controller_test.go

View workflow job for this annotation

GitHub Actions / lint

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

imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
Expand Down Expand Up @@ -39,6 +40,15 @@ var _ = Describe("Gardener Cluster controller", func() {
Expect(kubeconfigSecret.Labels).To(Equal(expectedSecret.Labels))
Expect(kubeconfigSecret.Data).To(Equal(expectedSecret.Data))
Expect(kubeconfigSecret.Annotations[lastKubeconfigSyncAnnotation]).To(Not(BeEmpty()))

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

By("Wait for secret deletion")
Eventually(func() bool {
err := k8sClient.Get(context.Background(), key, &kubeconfigSecret)
return err != nil && k8serrors.IsNotFound(err)
}, time.Second*30, time.Second*3).Should(BeTrue())
})
})
})
Expand Down

0 comments on commit f953c8c

Please sign in to comment.