Skip to content

Commit

Permalink
Merge pull request #39 from Disper/kubeconfig_secret_removal
Browse files Browse the repository at this point in the history
Infrastructure Manager removes secrets when Gardener Cluster CR is deleted
  • Loading branch information
kyma-bot authored Sep 25, 2023
2 parents 0d22ae0 + 7d09bd7 commit 7cf18b1
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 @@ -23,6 +23,7 @@ import (

"github.com/go-logr/logr"
infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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 Down Expand Up @@ -109,6 +118,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 @@ -8,6 +8,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
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 7cf18b1

Please sign in to comment.