Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't delete coredns unless previously managed by kamaji #527

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion internal/resources/addons/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1"
"github.com/clastix/kamaji/internal/constants"
"github.com/clastix/kamaji/internal/kubeadm"
"github.com/clastix/kamaji/internal/resources"
"github.com/clastix/kamaji/internal/resources/utils"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *CoreDNS) Define(context.Context, *kamajiv1alpha1.TenantControlPlane) er
}

func (c *CoreDNS) ShouldCleanup(tcp *kamajiv1alpha1.TenantControlPlane) bool {
return tcp.Spec.Addons.CoreDNS == nil
return tcp.Spec.Addons.CoreDNS == nil && tcp.Status.Addons.CoreDNS.Enabled
}

func (c *CoreDNS) CleanUp(ctx context.Context, tcp *kamajiv1alpha1.TenantControlPlane) (bool, error) {
Expand All @@ -91,6 +92,25 @@ func (c *CoreDNS) CleanUp(ctx context.Context, tcp *kamajiv1alpha1.TenantControl
var deleted bool

for _, obj := range []client.Object{c.serviceAccount, c.clusterRoleBinding, c.clusterRole, c.service, c.configMap, c.deployment} {
objectKey := client.ObjectKeyFromObject(obj)

logger.Info("Checking if object exists to be removed ", "obj", objectKey.String(), "kind", obj.GetObjectKind().GroupVersionKind().String())
if err := tenantClient.Get(ctx, objectKey, obj); err != nil {
if k8serrors.IsNotFound(err) {
logger.Error(err, "object does not exist")
logger.Info("Object doest not exist", "obj", objectKey.String(), "kind", obj.GetObjectKind().GroupVersionKind().String())

continue
}
}

// Don't delete resource if it is not managed by Kamaji
if _, ok := obj.GetLabels()[constants.ProjectNameLabelKey]; !ok {
logger.Info("Object exists but not managed by Kamaji, not deleting", "obj", objectKey.String(), "kind", obj.GetObjectKind().GroupVersionKind().String())

continue
}
logger.Info("Deleting object managed by Kamaji", "obj", objectKey.String(), "kind", obj.GetObjectKind().GroupVersionKind().String())
if err = tenantClient.Delete(ctx, obj); err != nil {
if k8serrors.IsNotFound(err) {
continue
Expand Down Expand Up @@ -244,6 +264,9 @@ func (c *CoreDNS) decodeManifests(ctx context.Context, tcp *kamajiv1alpha1.Tenan
func (c *CoreDNS) mutateClusterRoleBinding(ctx context.Context, tenantClient client.Client) (controllerutil.OperationResult, error) {
crb := &rbacv1.ClusterRoleBinding{}
crb.SetName(c.clusterRoleBinding.GetName())
labels := crb.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
crb.SetLabels(labels)
Marlinc marked this conversation as resolved.
Show resolved Hide resolved

defer func() {
c.clusterRoleBinding.SetUID(crb.GetUID())
Expand All @@ -263,6 +286,9 @@ func (c *CoreDNS) mutateDeployment(ctx context.Context, tenantClient client.Clie
d := &appsv1.Deployment{}
d.SetName(c.deployment.GetName())
d.SetNamespace(c.deployment.GetNamespace())
labels := d.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
d.SetLabels(labels)

return utilities.CreateOrUpdateWithConflict(ctx, tenantClient, d, func() error {
d.SetLabels(c.deployment.GetLabels())
Expand Down Expand Up @@ -347,6 +373,9 @@ func (c *CoreDNS) mutateConfigMap(ctx context.Context, tenantClient client.Clien
cm := &corev1.ConfigMap{}
cm.SetName(c.configMap.GetName())
cm.SetNamespace(c.configMap.GetNamespace())
labels := cm.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
cm.SetLabels(labels)

return utilities.CreateOrUpdateWithConflict(ctx, tenantClient, cm, func() error {
cm.SetLabels(c.configMap.GetLabels())
Expand All @@ -361,6 +390,9 @@ func (c *CoreDNS) mutateService(ctx context.Context, tenantClient client.Client)
svc := &corev1.Service{}
svc.SetName(c.service.GetName())
svc.SetNamespace(c.service.GetNamespace())
labels := svc.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
svc.SetLabels(labels)

return utilities.CreateOrUpdateWithConflict(ctx, tenantClient, svc, func() error {
svc.SetLabels(c.service.GetLabels())
Expand All @@ -378,6 +410,9 @@ func (c *CoreDNS) mutateClusterRole(ctx context.Context, tenantClient client.Cli
cr := &rbacv1.ClusterRole{}
cr.SetName(c.clusterRole.GetName())
cr.SetNamespace(c.clusterRole.GetNamespace())
labels := cr.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
cr.SetLabels(labels)

return utilities.CreateOrUpdateWithConflict(ctx, tenantClient, cr, func() error {
cr.SetLabels(c.clusterRole.GetLabels())
Expand All @@ -392,6 +427,9 @@ func (c *CoreDNS) mutateServiceAccount(ctx context.Context, tenantClient client.
sa := &corev1.ServiceAccount{}
sa.SetName(c.serviceAccount.GetName())
sa.SetNamespace(c.serviceAccount.GetNamespace())
labels := sa.GetLabels()
labels[constants.ProjectNameLabelKey] = constants.ProjectNameLabelValue
sa.SetLabels(labels)

return utilities.CreateOrUpdateWithConflict(ctx, tenantClient, sa, func() error {
sa.SetLabels(c.serviceAccount.GetLabels())
Expand Down
Loading