From 8043f4d7f81b28b02cb01849e562d6f13b7d5d91 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Wed, 4 Dec 2024 09:50:59 -0500 Subject: [PATCH 1/3] Define and reuse constants in servicediscovery module Signed-off-by: Tom Pantelis --- controllers/servicediscovery/cleanup.go | 2 +- .../servicediscovery_controller.go | 35 ++++++++++--------- .../servicediscovery_controller_test.go | 12 ++++--- .../servicediscovery_suite_test.go | 17 +++++---- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/controllers/servicediscovery/cleanup.go b/controllers/servicediscovery/cleanup.go index b56cd2c69..eb3967a8c 100644 --- a/controllers/servicediscovery/cleanup.go +++ b/controllers/servicediscovery/cleanup.go @@ -53,7 +53,7 @@ func (r *Reconciler) doCleanup(ctx context.Context, instance *operatorv1alpha1.S if instance.Spec.CoreDNSCustomConfig != nil && instance.Spec.CoreDNSCustomConfig.ConfigMapName != "" { err = r.removeLighthouseConfigFromCustomDNSConfigMap(ctx, instance.Spec.CoreDNSCustomConfig) } else { - err = r.updateLighthouseConfigInConfigMap(ctx, instance, defaultCoreDNSNamespace, coreDNSName, "") + err = r.updateLighthouseConfigInConfigMap(ctx, instance, DefaultCoreDNSNamespace, CoreDNSName, "") } if apierrors.IsNotFound(err) { diff --git a/controllers/servicediscovery/servicediscovery_controller.go b/controllers/servicediscovery/servicediscovery_controller.go index a3a6390bc..02acaeee0 100644 --- a/controllers/servicediscovery/servicediscovery_controller.go +++ b/controllers/servicediscovery/servicediscovery_controller.go @@ -63,12 +63,13 @@ var log = logf.Log.WithName("controller_servicediscovery") const ( componentName = "submariner-lighthouse" - defaultOpenShiftDNSController = "default" - lighthouseForwardPluginName = "lighthouse" - defaultCoreDNSNamespace = "kube-system" - coreDNSName = "coredns" - microshiftDNSNamespace = "openshift-dns" - microshiftDNSConfigMap = "dns-default" + Corefile = "Corefile" + DefaultOpenShiftDNSController = "default" + LighthouseForwardPluginName = "lighthouse" + DefaultCoreDNSNamespace = "kube-system" + CoreDNSName = "coredns" + MicroshiftDNSNamespace = "openshift-dns" + MicroshiftDNSConfigMap = "dns-default" coreDNSDefaultPort = "53" ) @@ -150,7 +151,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) ( return reconcile.Result{}, err } } else { - err = r.configureDNSConfigMap(ctx, instance, defaultCoreDNSNamespace, coreDNSName) + err = r.configureDNSConfigMap(ctx, instance, DefaultCoreDNSNamespace, CoreDNSName) } if apierrors.IsNotFound(err) { @@ -290,7 +291,7 @@ prometheus :9153 Labels: labels, }, Data: map[string]string{ - "Corefile": expectedCorefile, + Corefile: expectedCorefile, }, } } @@ -363,7 +364,7 @@ func newLighthouseCoreDNSDeployment(cr *submarinerv1alpha1.ServiceDiscovery) *ap {Name: "config-volume", VolumeSource: corev1.VolumeSource{ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{Name: names.LighthouseCoreDNSComponent}, Items: []corev1.KeyToPath{ - {Key: "Corefile", Path: "Corefile"}, + {Key: Corefile, Path: Corefile}, }, DefaultMode: ptr.To(int32(0o644)), }}}, @@ -420,7 +421,7 @@ func getCustomCoreDNSNamespace(config *submarinerv1alpha1.CoreDNSCustomConfig) s return config.Namespace } - return defaultCoreDNSNamespace + return DefaultCoreDNSNamespace } func (r *Reconciler) updateDNSCustomConfigMap(ctx context.Context, cr *submarinerv1alpha1.ServiceDiscovery, @@ -486,7 +487,7 @@ func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr * configMap := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Namespace: configMapNamespace, Name: configMapName}} err := util.MustUpdate[*corev1.ConfigMap](ctx, resource.ForControllerClient(r.GeneralClient, configMap.Namespace, configMap), configMap, func(existing *corev1.ConfigMap) (*corev1.ConfigMap, error) { - coreFile := existing.Data["Corefile"] + coreFile := existing.Data[Corefile] if strings.Contains(coreFile, "lighthouse-start") { // Assume this means we've already set the ConfigMap up, first remove existing lighthouse config newCoreStr := "" @@ -528,7 +529,7 @@ func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr * } log.Info("Updated coredns ConfigMap " + coreFile) - existing.Data["Corefile"] = coreFile + existing.Data[Corefile] = coreFile return existing, nil }) @@ -569,12 +570,12 @@ func (r *Reconciler) updateLighthouseConfigInOpenshiftDNSOperator(ctx context.Co ) error { retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { dnsOperator := &operatorv1.DNS{} - if err := r.GeneralClient.Get(ctx, types.NamespacedName{Name: defaultOpenShiftDNSController}, dnsOperator); err != nil { + if err := r.GeneralClient.Get(ctx, types.NamespacedName{Name: DefaultOpenShiftDNSController}, dnsOperator); err != nil { // microshift uses the coredns image, but the DNS operator and CRDs are off if resource.IsNotFoundErr(err) { - err = r.configureDNSConfigMap(ctx, instance, microshiftDNSNamespace, microshiftDNSConfigMap) + err = r.configureDNSConfigMap(ctx, instance, MicroshiftDNSNamespace, MicroshiftDNSConfigMap) return errors.Wrapf(err, "error trying to update microshift coredns configmap %q in namespace %q", - microshiftDNSNamespace, microshiftDNSNamespace) + MicroshiftDNSNamespace, MicroshiftDNSNamespace) } return err @@ -618,7 +619,7 @@ func getUpdatedForwardServers(instance *submarinerv1alpha1.ServiceDiscovery, dns lighthouseDomains := buildDomains(instance) for _, forwardServer := range dnsOperator.Spec.Servers { - if forwardServer.Name == lighthouseForwardPluginName { + if forwardServer.Name == LighthouseForwardPluginName { containsLighthouse = true existingDomains = append(existingDomains, forwardServer.Zones...) @@ -655,7 +656,7 @@ func getUpdatedForwardServers(instance *submarinerv1alpha1.ServiceDiscovery, dns for _, domain := range lighthouseDomains { lighthouseServer := operatorv1.Server{ - Name: lighthouseForwardPluginName, + Name: LighthouseForwardPluginName, Zones: []string{domain}, ForwardPlugin: operatorv1.ForwardPlugin{ Upstreams: []string{clusterIP}, diff --git a/controllers/servicediscovery/servicediscovery_controller_test.go b/controllers/servicediscovery/servicediscovery_controller_test.go index ec00da070..a6daaaff8 100644 --- a/controllers/servicediscovery/servicediscovery_controller_test.go +++ b/controllers/servicediscovery/servicediscovery_controller_test.go @@ -25,6 +25,7 @@ import ( . "github.com/onsi/gomega" "github.com/submariner-io/admiral/pkg/names" submariner_v1 "github.com/submariner-io/submariner-operator/api/v1alpha1" + "github.com/submariner-io/submariner-operator/controllers/servicediscovery" opnames "github.com/submariner-io/submariner-operator/pkg/names" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -102,7 +103,7 @@ func testReconciliation() { It("should add it", func(ctx SpecContext) { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data["Corefile"])).To(Equal(coreDNSCorefileData(clusterIP))) + Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To(Equal(coreDNSCorefileData(clusterIP))) }) }) @@ -117,7 +118,8 @@ func testReconciliation() { It("should update the lighthouse config", func(ctx SpecContext) { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data["Corefile"])).To(Equal(coreDNSCorefileData(updatedClusterIP))) + Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To( + Equal(coreDNSCorefileData(updatedClusterIP))) }) }) @@ -133,7 +135,8 @@ func testReconciliation() { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data["Corefile"])).To(Equal(coreDNSCorefileData(clusterIP))) + Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])). + To(Equal(coreDNSCorefileData(clusterIP))) }) }) }) @@ -226,7 +229,8 @@ func testCoreDNSCleanup() { }) It("should remove the lighthouse config section", func(ctx SpecContext) { - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data["Corefile"])).To(Equal(coreDNSCorefileData(""))) + Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To( + Equal(coreDNSCorefileData(""))) }) t.testServiceDiscoveryDeleted() diff --git a/controllers/servicediscovery/servicediscovery_suite_test.go b/controllers/servicediscovery/servicediscovery_suite_test.go index 8ce333a1a..3bf600665 100644 --- a/controllers/servicediscovery/servicediscovery_suite_test.go +++ b/controllers/servicediscovery/servicediscovery_suite_test.go @@ -44,7 +44,6 @@ import ( const ( serviceDiscoveryName = "test-service-discovery" submarinerNamespace = "test-ns" - openShiftDNSConfigName = "default" clusterIP = "10.10.10.10" lighthouseDNSServiceName = "submariner-lighthouse-coredns" @@ -123,7 +122,7 @@ func (t *testDriver) assertUninstallServiceDiscoveryDeployment(ctx context.Conte func (t *testDriver) getDNSConfig(ctx context.Context) (*operatorv1.DNS, error) { foundDNSConfig := &operatorv1.DNS{} - err := t.GeneralClient.Get(ctx, types.NamespacedName{Name: openShiftDNSConfigName}, foundDNSConfig) + err := t.GeneralClient.Get(ctx, types.NamespacedName{Name: servicediscovery.DefaultOpenShiftDNSController}, foundDNSConfig) return foundDNSConfig, err } @@ -136,7 +135,7 @@ func (t *testDriver) assertDNSConfig(ctx context.Context) *operatorv1.DNS { } func (t *testDriver) assertCoreDNSConfigMap(ctx context.Context) *corev1.ConfigMap { - return t.assertConfigMap(ctx, "coredns", "kube-system") + return t.assertConfigMap(ctx, servicediscovery.CoreDNSName, servicediscovery.DefaultCoreDNSNamespace) } func (t *testDriver) assertConfigMap(ctx context.Context, name, namespace string) *corev1.ConfigMap { @@ -150,7 +149,7 @@ func (t *testDriver) assertConfigMap(ctx context.Context, name, namespace string func newDNSConfig(clusterIP string) *operatorv1.DNS { dns := &operatorv1.DNS{ ObjectMeta: metav1.ObjectMeta{ - Name: openShiftDNSConfigName, + Name: servicediscovery.DefaultOpenShiftDNSController, }, Spec: operatorv1.DNSSpec{ Servers: []operatorv1.Server{ @@ -168,14 +167,14 @@ func newDNSConfig(clusterIP string) *operatorv1.DNS { if clusterIP != "" { dns.Spec.Servers = append(dns.Spec.Servers, operatorv1.Server{ - Name: "lighthouse", + Name: servicediscovery.LighthouseForwardPluginName, Zones: []string{"clusterset.local"}, ForwardPlugin: operatorv1.ForwardPlugin{ Upstreams: []string{clusterIP}, }, }, operatorv1.Server{ - Name: "lighthouse", + Name: servicediscovery.LighthouseForwardPluginName, Zones: []string{"supercluster.local"}, ForwardPlugin: operatorv1.ForwardPlugin{ Upstreams: []string{clusterIP}, @@ -309,11 +308,11 @@ func coreDNSCorefileData(clusterIP string) string { func newCoreDNSConfigMap(corefile string) *corev1.ConfigMap { return &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: "coredns", - Namespace: "kube-system", + Name: servicediscovery.CoreDNSName, + Namespace: servicediscovery.DefaultCoreDNSNamespace, }, Data: map[string]string{ - "Corefile": corefile, + servicediscovery.Corefile: corefile, }, } } From d045c3e14e968cf6c330d508ed7da3d2a256ee9a Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Wed, 4 Dec 2024 10:27:47 -0500 Subject: [PATCH 2/3] Add unit test to cover microshift DNS ConfigMap Signed-off-by: Tom Pantelis --- controllers/servicediscovery/cleanup.go | 2 +- .../servicediscovery_controller.go | 11 +++++--- .../servicediscovery_controller_test.go | 26 ++++++++++++++----- .../servicediscovery_suite_test.go | 12 +++++++-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/controllers/servicediscovery/cleanup.go b/controllers/servicediscovery/cleanup.go index eb3967a8c..9fdd81d93 100644 --- a/controllers/servicediscovery/cleanup.go +++ b/controllers/servicediscovery/cleanup.go @@ -81,7 +81,7 @@ func (r *Reconciler) doCleanup(ctx context.Context, instance *operatorv1alpha1.S Client: r.ScopedClient, Components: components, StartTime: instance.DeletionTimestamp.Time, - Log: log, + Log: log.Logger, GetImageInfo: func(imageName, componentName string) (string, corev1.PullPolicy) { return getImagePath(instance, imageName, componentName), images.GetPullPolicy(instance.Spec.Version, instance.Spec.ImageOverrides[componentName]) diff --git a/controllers/servicediscovery/servicediscovery_controller.go b/controllers/servicediscovery/servicediscovery_controller.go index 02acaeee0..d0e1dfaa4 100644 --- a/controllers/servicediscovery/servicediscovery_controller.go +++ b/controllers/servicediscovery/servicediscovery_controller.go @@ -32,6 +32,7 @@ import ( operatorv1 "github.com/openshift/api/operator/v1" "github.com/pkg/errors" "github.com/submariner-io/admiral/pkg/finalizer" + logw "github.com/submariner-io/admiral/pkg/log" "github.com/submariner-io/admiral/pkg/names" "github.com/submariner-io/admiral/pkg/resource" "github.com/submariner-io/admiral/pkg/syncer/broker" @@ -59,7 +60,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" ) -var log = logf.Log.WithName("controller_servicediscovery") +var log = logw.Logger{Logger: logf.Log.WithName("controller_servicediscovery")} const ( componentName = "submariner-lighthouse" @@ -493,7 +494,7 @@ func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr * newCoreStr := "" skip := false - log.Info("coredns configmap has lighthouse configuration hence updating") + log.Infof("Coredns ConfigMap \"%s/%s\" has lighthouse configuration - updating it", configMapNamespace, configMapName) lines := strings.Split(coreFile, "\n") for _, line := range lines { @@ -513,7 +514,8 @@ func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr * coreFile = newCoreStr } else { - log.Info("coredns configmap does not have lighthouse configuration hence creating") + log.Infof("Coredns ConfigMap \"%s/%s\" does not have lighthouse configuration - adding it", + configMapNamespace, configMapName) } if clusterIP != "" { @@ -528,7 +530,8 @@ func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr * coreFile = expectedCorefile + "#lighthouse-end\n" + coreFile } - log.Info("Updated coredns ConfigMap " + coreFile) + log.Infof("Updated coredns ConfigMap \"%s/%s\": %s", configMapNamespace, configMapName, coreFile) + existing.Data[Corefile] = coreFile return existing, nil diff --git a/controllers/servicediscovery/servicediscovery_controller_test.go b/controllers/servicediscovery/servicediscovery_controller_test.go index a6daaaff8..8f3d701ef 100644 --- a/controllers/servicediscovery/servicediscovery_controller_test.go +++ b/controllers/servicediscovery/servicediscovery_controller_test.go @@ -103,7 +103,7 @@ func testReconciliation() { It("should add it", func(ctx SpecContext) { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To(Equal(coreDNSCorefileData(clusterIP))) + Expect(getCorefileData(t.assertCoreDNSConfigMap(ctx))).To(Equal(coreDNSCorefileData(clusterIP))) }) }) @@ -118,8 +118,7 @@ func testReconciliation() { It("should update the lighthouse config", func(ctx SpecContext) { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To( - Equal(coreDNSCorefileData(updatedClusterIP))) + Expect(getCorefileData(t.assertCoreDNSConfigMap(ctx))).To(Equal(coreDNSCorefileData(updatedClusterIP))) }) }) @@ -135,8 +134,7 @@ func testReconciliation() { t.AssertReconcileSuccess(ctx) - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])). - To(Equal(coreDNSCorefileData(clusterIP))) + Expect(getCorefileData(t.assertCoreDNSConfigMap(ctx))).To(Equal(coreDNSCorefileData(clusterIP))) }) }) }) @@ -199,6 +197,21 @@ func testReconciliation() { }) }) }) + + When("the microshift DNS ConfigMap exists", func() { + BeforeEach(func() { + t.InitScopedClientObjs = append(t.InitScopedClientObjs, newDNSService(clusterIP)) + t.InitGeneralClientObjs = append(t.InitGeneralClientObjs, newDNSConfigMap( + servicediscovery.MicroshiftDNSConfigMap, servicediscovery.MicroshiftDNSNamespace, coreDNSCorefileData(""))) + }) + + It("should update it with the lighthouse config", func(ctx SpecContext) { + t.AssertReconcileSuccess(ctx) + + Expect(getCorefileData(t.assertConfigMap(ctx, servicediscovery.MicroshiftDNSConfigMap, + servicediscovery.MicroshiftDNSNamespace))).To(Equal(coreDNSCorefileData(clusterIP))) + }) + }) } func testCoreDNSCleanup() { @@ -229,8 +242,7 @@ func testCoreDNSCleanup() { }) It("should remove the lighthouse config section", func(ctx SpecContext) { - Expect(strings.TrimSpace(t.assertCoreDNSConfigMap(ctx).Data[servicediscovery.Corefile])).To( - Equal(coreDNSCorefileData(""))) + Expect(getCorefileData(t.assertCoreDNSConfigMap(ctx))).To(Equal(coreDNSCorefileData(""))) }) t.testServiceDiscoveryDeleted() diff --git a/controllers/servicediscovery/servicediscovery_suite_test.go b/controllers/servicediscovery/servicediscovery_suite_test.go index 3bf600665..06caf3661 100644 --- a/controllers/servicediscovery/servicediscovery_suite_test.go +++ b/controllers/servicediscovery/servicediscovery_suite_test.go @@ -146,6 +146,10 @@ func (t *testDriver) assertConfigMap(ctx context.Context, name, namespace string return foundCoreMap } +func getCorefileData(from *corev1.ConfigMap) string { + return strings.TrimSpace(from.Data[servicediscovery.Corefile]) +} + func newDNSConfig(clusterIP string) *operatorv1.DNS { dns := &operatorv1.DNS{ ObjectMeta: metav1.ObjectMeta{ @@ -306,10 +310,14 @@ func coreDNSCorefileData(clusterIP string) string { } func newCoreDNSConfigMap(corefile string) *corev1.ConfigMap { + return newDNSConfigMap(servicediscovery.CoreDNSName, servicediscovery.DefaultCoreDNSNamespace, corefile) +} + +func newDNSConfigMap(name, namespace, corefile string) *corev1.ConfigMap { return &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: servicediscovery.CoreDNSName, - Namespace: servicediscovery.DefaultCoreDNSNamespace, + Name: name, + Namespace: namespace, }, Data: map[string]string{ servicediscovery.Corefile: corefile, From 9e5a83bb167fb9a9de17155896b17d1e9a3e563a Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 5 Dec 2024 07:41:15 -0500 Subject: [PATCH 3/3] Handle non-standard coredns ConfigMap name in SD controller Fixes https://github.com/submariner-io/lighthouse/issues/1602 Signed-off-by: Tom Pantelis --- .gitignore | 2 +- .../servicediscovery_controller.go | 59 ++++++++++--------- .../servicediscovery_controller_test.go | 19 +++++- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 0fbad6113..7872e34e1 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,7 @@ config/bundle/patches/submariner.csv.config.yaml package/.image.* output/ vendor/ -*.coverprofile +*coverprofile* .idea .shflags junit.xml diff --git a/controllers/servicediscovery/servicediscovery_controller.go b/controllers/servicediscovery/servicediscovery_controller.go index d0e1dfaa4..7daabf8e9 100644 --- a/controllers/servicediscovery/servicediscovery_controller.go +++ b/controllers/servicediscovery/servicediscovery_controller.go @@ -152,12 +152,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) ( return reconcile.Result{}, err } } else { - err = r.configureDNSConfigMap(ctx, instance, DefaultCoreDNSNamespace, CoreDNSName) - } - - if apierrors.IsNotFound(err) { - // Try to update Openshift-DNS - return reconcile.Result{}, r.configureOpenshiftClusterDNSOperator(ctx, instance) + err = r.updateDNSConfig(ctx, instance) } return reconcile.Result{}, err @@ -464,9 +459,7 @@ func (r *Reconciler) updateDNSCustomConfigMap(ctx context.Context, cr *submarine return errors.Wrap(err, "error updating DNS custom ConfigMap") } -func (r *Reconciler) configureDNSConfigMap(ctx context.Context, cr *submarinerv1alpha1.ServiceDiscovery, configMapNamespace, - configMapName string, -) error { +func (r *Reconciler) updateDNSConfig(ctx context.Context, cr *submarinerv1alpha1.ServiceDiscovery) error { lighthouseDNSService := &corev1.Service{} err := r.ScopedClient.Get(ctx, types.NamespacedName{Name: names.LighthouseCoreDNSComponent, Namespace: cr.Namespace}, @@ -479,7 +472,35 @@ func (r *Reconciler) configureDNSConfigMap(ctx context.Context, cr *submarinerv1 return goerrors.New("the lighthouse DNS Service ClusterIP is not set") } - return r.updateLighthouseConfigInConfigMap(ctx, cr, configMapNamespace, configMapName, lighthouseDNSService.Spec.ClusterIP) + err = r.updateLighthouseConfigInConfigMap(ctx, cr, DefaultCoreDNSNamespace, CoreDNSName, lighthouseDNSService.Spec.ClusterIP) + + if apierrors.IsNotFound(err) { + // Some providers may not use the exact "coredns" name but use it as a suffix, eg RKE "rke2-coredns". + configMaps := &corev1.ConfigMapList{} + + listErr := r.GeneralClient.List(ctx, configMaps, controllerClient.InNamespace(DefaultCoreDNSNamespace)) + if listErr != nil { + return errors.Wrapf(err, "error listing ConfigMaps in %q", DefaultCoreDNSNamespace) + } + + suffix := "-" + CoreDNSName + + for i := range configMaps.Items { + cm := &configMaps.Items[i] + + _, hasCorefile := cm.Data[Corefile] + if strings.HasSuffix(cm.Name, suffix) && hasCorefile { + return r.updateLighthouseConfigInConfigMap(ctx, cr, cm.Namespace, cm.Name, lighthouseDNSService.Spec.ClusterIP) + } + } + } + + if apierrors.IsNotFound(err) { + // Try to update Openshift-DNS + return r.updateLighthouseConfigInOpenshiftDNSOperator(ctx, cr, lighthouseDNSService.Spec.ClusterIP) + } + + return err } func (r *Reconciler) updateLighthouseConfigInConfigMap(ctx context.Context, cr *submarinerv1alpha1.ServiceDiscovery, @@ -552,22 +573,6 @@ func findCoreDNSListeningPort(coreFile string) string { return coreDNSPort } -func (r *Reconciler) configureOpenshiftClusterDNSOperator(ctx context.Context, instance *submarinerv1alpha1.ServiceDiscovery) error { - lighthouseDNSService := &corev1.Service{} - - err := r.ScopedClient.Get(ctx, types.NamespacedName{Name: names.LighthouseCoreDNSComponent, Namespace: instance.Namespace}, - lighthouseDNSService) - if err != nil { - return errors.Wrap(err, "error retrieving lighthouse DNS Service") - } - - if lighthouseDNSService.Spec.ClusterIP == "" { - return goerrors.New("the lighthouse DNS Service ClusterIP is not set") - } - - return r.updateLighthouseConfigInOpenshiftDNSOperator(ctx, instance, lighthouseDNSService.Spec.ClusterIP) -} - func (r *Reconciler) updateLighthouseConfigInOpenshiftDNSOperator(ctx context.Context, instance *submarinerv1alpha1.ServiceDiscovery, clusterIP string, ) error { @@ -576,7 +581,7 @@ func (r *Reconciler) updateLighthouseConfigInOpenshiftDNSOperator(ctx context.Co if err := r.GeneralClient.Get(ctx, types.NamespacedName{Name: DefaultOpenShiftDNSController}, dnsOperator); err != nil { // microshift uses the coredns image, but the DNS operator and CRDs are off if resource.IsNotFoundErr(err) { - err = r.configureDNSConfigMap(ctx, instance, MicroshiftDNSNamespace, MicroshiftDNSConfigMap) + err = r.updateLighthouseConfigInConfigMap(ctx, instance, MicroshiftDNSNamespace, MicroshiftDNSConfigMap, clusterIP) return errors.Wrapf(err, "error trying to update microshift coredns configmap %q in namespace %q", MicroshiftDNSNamespace, MicroshiftDNSNamespace) } diff --git a/controllers/servicediscovery/servicediscovery_controller_test.go b/controllers/servicediscovery/servicediscovery_controller_test.go index 8f3d701ef..81ce4e155 100644 --- a/controllers/servicediscovery/servicediscovery_controller_test.go +++ b/controllers/servicediscovery/servicediscovery_controller_test.go @@ -93,7 +93,7 @@ func testReconciliation() { }) }) - When("the coredns ConfigMap exists", func() { + When("the default coredns ConfigMap exists", func() { Context("and the lighthouse config isn't present", func() { BeforeEach(func() { t.InitScopedClientObjs = append(t.InitScopedClientObjs, newDNSService(clusterIP)) @@ -139,6 +139,23 @@ func testReconciliation() { }) }) + When("a ConfigMap exists with a non-standard coredns name", func() { + nonStandardName := "rke2-coredns-rke2-coredns" + + BeforeEach(func() { + t.InitScopedClientObjs = append(t.InitScopedClientObjs, newDNSService(clusterIP)) + t.InitGeneralClientObjs = append(t.InitGeneralClientObjs, newDNSConfigMap( + nonStandardName, servicediscovery.DefaultCoreDNSNamespace, coreDNSCorefileData(""))) + }) + + It("should update it with the lighthouse config", func(ctx SpecContext) { + t.AssertReconcileSuccess(ctx) + + Expect(getCorefileData(t.assertConfigMap(ctx, nonStandardName, + servicediscovery.DefaultCoreDNSNamespace))).To(Equal(coreDNSCorefileData(clusterIP))) + }) + }) + When("a custom coredns config is specified", func() { BeforeEach(func() { t.serviceDiscovery.Spec.CoreDNSCustomConfig = &submariner_v1.CoreDNSCustomConfig{