From c3631389ec68ce1167e6610be5a02dc0e41e1fba Mon Sep 17 00:00:00 2001 From: Ricardo Weir Date: Thu, 2 Jan 2025 11:27:03 -0700 Subject: [PATCH] Delete deployment label selector is outdated Prior, if the coredns deployment was using the old label, "kube-dns", it would not be updated during the coredns manifests apply. This was because label selectors are immutable. Now, if the label is outdated on the deployment, the deployment is deleted prior to the coredns manifests apply. --- pkg/setup/controllers.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/setup/controllers.go b/pkg/setup/controllers.go index c7ebfea440..e34ce83eed 100644 --- a/pkg/setup/controllers.go +++ b/pkg/setup/controllers.go @@ -175,7 +175,23 @@ func StartControllers(controllerContext *synccontext.ControllerContext, syncers func ApplyCoreDNS(controllerContext *synccontext.ControllerContext) { _ = wait.ExponentialBackoffWithContext(controllerContext.Context, wait.Backoff{Duration: time.Second, Factor: 1.5, Cap: time.Minute, Steps: math.MaxInt32}, func(ctx context.Context) (bool, error) { - err := coredns.ApplyManifest(ctx, controllerContext.Config.ControlPlane.Advanced.DefaultImageRegistry, controllerContext.VirtualManager.GetConfig(), controllerContext.VirtualClusterVersion) + dnsDeployment := &appsv1.Deployment{} + err := controllerContext.VirtualManager.GetClient().Get(controllerContext.Context, types.NamespacedName{Namespace: "kube-system", Name: "coredns"}, dnsDeployment) + if err != nil && !kerrors.IsNotFound(err) { + return false, err + } + if err == nil { + // dns pod labels were changed to avoid conflict with apps running in the host cluster that select for the "kube-dns" label, e.g. cilium. + // If the deployment already exists with a label selector that is not "vcluster-kube-dns" then it needs to be deleted because the selector field is immutable. + // Otherwise, dns will break because the dns service will target the updated label but not match any deployments. + if dnsDeployment.Spec.Selector.MatchLabels["k8s-app"] != "vcluster-kube-dns" { + err = controllerContext.VirtualManager.GetClient().Delete(controllerContext.Context, dnsDeployment) + if err != nil && !kerrors.IsNotFound(err) { + return false, err + } + } + } + err = coredns.ApplyManifest(ctx, controllerContext.Config.ControlPlane.Advanced.DefaultImageRegistry, controllerContext.VirtualManager.GetConfig(), controllerContext.VirtualClusterVersion) if err != nil { if errors.Is(err, coredns.ErrNoCoreDNSManifests) { klog.Infof("No CoreDNS manifests found, skipping CoreDNS configuration")