This is an automated email from the ASF dual-hosted git repository.

janhoy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 439e6d83 Fix exporter cross namespace cloud watch (#879)
439e6d83 is described below

commit 439e6d83c4983da429c8c46998b3095b53f29f15
Author: Jan Høydahl <[email protected]>
AuthorDate: Thu Nov 19 02:13:59 2026 +0100

    Fix exporter cross namespace cloud watch (#879)
    
    * Reconcile exporters referencing a SolrCloud in another namespace
---
 controllers/solrprometheusexporter_controller.go   |  41 ++++---
 ...heusexporter_controller_cross_namespace_test.go | 133 +++++++++++++++++++++
 .../solr-prometheus-exporter/pages/index.adoc      |   7 +-
 helm/solr-operator/Chart.yaml                      |   5 +
 4 files changed, 169 insertions(+), 17 deletions(-)

diff --git a/controllers/solrprometheusexporter_controller.go 
b/controllers/solrprometheusexporter_controller.go
index 1251208c..8094c9ba 100644
--- a/controllers/solrprometheusexporter_controller.go
+++ b/controllers/solrprometheusexporter_controller.go
@@ -276,6 +276,22 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(ctx 
context.Context, req ct
        return requeueOrNot, err
 }
 
+// referencedSolrCloud returns the name and namespace of the SolrCloud that 
the given
+// SolrPrometheusExporter references, or nil if it does not reference a 
SolrCloud by name.
+// The namespace of the reference is normally filled in by WithDefaults(), but 
it is not guaranteed
+// to be set, e.g. when indexing an exporter that has not been defaulted yet.
+func referencedSolrCloud(prometheusExporter 
*solrv1beta1.SolrPrometheusExporter) *types.NamespacedName {
+       cloudRef := prometheusExporter.Spec.SolrReference.Cloud
+       if cloudRef == nil || cloudRef.Name == "" {
+               return nil
+       }
+       namespace := cloudRef.Namespace
+       if namespace == "" {
+               namespace = prometheusExporter.Namespace
+       }
+       return &types.NamespacedName{Name: cloudRef.Name, Namespace: namespace}
+}
+
 func getSolrConnectionInfo(ctx context.Context, r 
*SolrPrometheusExporterReconciler, prometheusExporter 
*solrv1beta1.SolrPrometheusExporter) (solrConnectionInfo 
util.SolrConnectionInfo, solrCloudImage *solrv1beta1.ContainerImage, err error) 
{
        solrConnectionInfo = util.SolrConnectionInfo{}
 
@@ -286,13 +302,9 @@ func getSolrConnectionInfo(ctx context.Context, r 
*SolrPrometheusExporterReconci
                cloudRef := prometheusExporter.Spec.SolrReference.Cloud
                if cloudRef.ZookeeperConnectionInfo != nil {
                        solrConnectionInfo.CloudZkConnnectionInfo = 
cloudRef.ZookeeperConnectionInfo
-               } else if cloudRef.Name != "" {
+               } else if cloudName := referencedSolrCloud(prometheusExporter); 
cloudName != nil {
                        solrCloud := &solrv1beta1.SolrCloud{}
-                       solrNamespace := 
prometheusExporter.Spec.SolrReference.Cloud.Namespace
-                       if solrNamespace == "" {
-                               solrNamespace = prometheusExporter.Namespace
-                       }
-                       err = r.Get(ctx, types.NamespacedName{Name: 
prometheusExporter.Spec.SolrReference.Cloud.Name, Namespace: solrNamespace}, 
solrCloud)
+                       err = r.Get(ctx, *cloudName, solrCloud)
                        if err == nil {
                                solrConnectionInfo.CloudZkConnnectionInfo = 
&solrCloud.Status.ZookeeperConnectionInfo
                                solrCloudImage = solrCloud.Spec.SolrImage
@@ -391,19 +403,18 @@ func (r *SolrPrometheusExporterReconciler) 
SetupWithManager(mgr ctrl.Manager) er
 }
 
 func (r *SolrPrometheusExporterReconciler) indexAndWatchForSolrClouds(mgr 
ctrl.Manager, ctrlBuilder *builder.Builder) (*builder.Builder, error) {
-       solrCloudField := ".spec.solrReference.cloud.name"
+       // An exporter can reference a SolrCloud in any namespace, so index the 
full namespaced name of the
+       // referenced cloud. Indexing the name alone would only ever match 
clouds in the exporter's own namespace.
+       solrCloudField := ".spec.solrReference.cloud"
 
        if err := mgr.GetFieldIndexer().IndexField(context.Background(), 
&solrv1beta1.SolrPrometheusExporter{}, solrCloudField, func(rawObj 
client.Object) []string {
                // grab the SolrPrometheusExporter object, extract the used 
SolrCloud...
-               exporter := rawObj.(*solrv1beta1.SolrPrometheusExporter)
-               if exporter.Spec.SolrReference.Cloud == nil {
-                       return nil
-               }
-               if exporter.Spec.SolrReference.Cloud.Name == "" {
+               cloudName := 
referencedSolrCloud(rawObj.(*solrv1beta1.SolrPrometheusExporter))
+               if cloudName == nil {
                        return nil
                }
                // ...and if so, return it
-               return []string{exporter.Spec.SolrReference.Cloud.Name}
+               return []string{cloudName.String()}
        }); err != nil {
                return ctrlBuilder, err
        }
@@ -412,9 +423,9 @@ func (r *SolrPrometheusExporterReconciler) 
indexAndWatchForSolrClouds(mgr ctrl.M
                &solrv1beta1.SolrCloud{},
                handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, 
obj client.Object) []reconcile.Request {
                        foundExporters := 
&solrv1beta1.SolrPrometheusExporterList{}
+                       // No namespace is given, since exporters in any 
namespace can reference this SolrCloud.
                        listOps := &client.ListOptions{
-                               FieldSelector: 
fields.OneTermEqualSelector(solrCloudField, obj.GetName()),
-                               Namespace:     obj.GetNamespace(),
+                               FieldSelector: 
fields.OneTermEqualSelector(solrCloudField, 
client.ObjectKeyFromObject(obj).String()),
                        }
                        err := r.List(ctx, foundExporters, listOps)
                        if err != nil {
diff --git 
a/controllers/solrprometheusexporter_controller_cross_namespace_test.go 
b/controllers/solrprometheusexporter_controller_cross_namespace_test.go
new file mode 100644
index 00000000..0eb2aad7
--- /dev/null
+++ b/controllers/solrprometheusexporter_controller_cross_namespace_test.go
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package controllers
+
+import (
+       "context"
+       "time"
+
+       appsv1 "k8s.io/api/apps/v1"
+       corev1 "k8s.io/api/core/v1"
+       "sigs.k8s.io/controller-runtime/pkg/client"
+
+       solrv1beta1 "github.com/apache/solr-operator/api/v1beta1"
+       . "github.com/onsi/ginkgo/v2"
+       . "github.com/onsi/gomega"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+)
+
+var _ = FDescribe("SolrPrometheusExporter controller - Cross-namespace 
SolrCloud reference", func() {
+
+       // Define utility constants for object names and testing 
timeouts/durations and intervals.
+       const (
+               timeout  = time.Second * 5
+               duration = time.Second * 1
+               interval = time.Millisecond * 250
+
+               // envtest has no namespace controller, so a created namespace 
can never be fully deleted.
+               // Therefore this namespace is created once and re-used by all 
specs in this suite.
+               solrCloudNamespace = "cross-namespace-solr"
+
+               imageBeforeChange = "before-change"
+               imageAfterChange  = "after-change"
+       )
+       SetDefaultConsistentlyDuration(duration)
+       SetDefaultConsistentlyPollingInterval(interval)
+       SetDefaultEventuallyTimeout(timeout)
+       SetDefaultEventuallyPollingInterval(interval)
+
+       var (
+               solrPrometheusExporter *solrv1beta1.SolrPrometheusExporter
+               solrRef                *solrv1beta1.SolrCloud
+       )
+
+       BeforeEach(func(ctx context.Context) {
+               By("creating the namespace that the referenced SolrCloud lives 
in")
+               namespace := &corev1.Namespace{ObjectMeta: 
metav1.ObjectMeta{Name: solrCloudNamespace}}
+               Expect(client.IgnoreAlreadyExists(k8sClient.Create(ctx, 
namespace))).To(Succeed(), "Creating the namespace for the referenced 
SolrCloud")
+
+               By("creating the SolrCloud in the other namespace")
+               solrRef = &solrv1beta1.SolrCloud{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test-solr",
+                               Namespace: solrCloudNamespace,
+                       },
+                       Spec: solrv1beta1.SolrCloudSpec{
+                               SolrImage: &solrv1beta1.ContainerImage{
+                                       Tag: imageBeforeChange,
+                               },
+                               ZookeeperRef: &solrv1beta1.ZookeeperRef{
+                                       ConnectionInfo: 
&solrv1beta1.ZookeeperConnectionInfo{
+                                               InternalConnectionString: 
"host-from-solr:2181",
+                                               ChRoot:                   
"/this/path",
+                                       },
+                               },
+                       },
+               }
+               Expect(k8sClient.Create(ctx, solrRef)).To(Succeed(), "Creating 
test SolrCloud for Prometheus Exporter to connect to")
+
+               By("creating the SolrPrometheusExporter that references it")
+               solrPrometheusExporter = &solrv1beta1.SolrPrometheusExporter{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "foo",
+                               Namespace: "default",
+                       },
+                       Spec: solrv1beta1.SolrPrometheusExporterSpec{
+                               SolrReference: solrv1beta1.SolrReference{
+                                       Cloud: &solrv1beta1.SolrCloudReference{
+                                               Name:      solrRef.Name,
+                                               Namespace: solrRef.Namespace,
+                                       },
+                               },
+                       },
+               }
+               Expect(k8sClient.Create(ctx, 
solrPrometheusExporter)).To(Succeed())
+       })
+
+       AfterEach(func(ctx context.Context) {
+               cleanupTest(ctx, solrPrometheusExporter)
+               cleanupTest(ctx, solrRef)
+       })
+
+       FIt("reconciles the exporter when the referenced SolrCloud changes", 
func(ctx context.Context) {
+               By("using the Solr image of the referenced SolrCloud")
+               expectExporterSolrImageTag(ctx, solrPrometheusExporter, 
imageBeforeChange)
+
+               By("changing the Solr image of the SolrCloud, leaving the 
exporter untouched")
+               foundSolrCloud := &solrv1beta1.SolrCloud{}
+               Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(solrRef), 
foundSolrCloud)).To(Succeed(), "Fetching the SolrCloud to update")
+               foundSolrCloud.Spec.SolrImage.Tag = imageAfterChange
+               Expect(k8sClient.Update(ctx, foundSolrCloud)).To(Succeed(), 
"Updating the Solr image of the SolrCloud")
+
+               By("picking up the new Solr image, which requires watching 
SolrClouds in other namespaces")
+               expectExporterSolrImageTag(ctx, solrPrometheusExporter, 
imageAfterChange)
+       })
+})
+
+// expectExporterSolrImageTag waits for the Deployment of the given 
SolrPrometheusExporter to use the
+// given Solr image tag. Unlike expectDeployment(), it leaves the Deployment 
alone, since deleting it
+// would trigger a reconcile of the exporter through the Deployment watch, 
which is exactly what must
+// not be relied on here.
+func expectExporterSolrImageTag(ctx context.Context, solrPrometheusExporter 
*solrv1beta1.SolrPrometheusExporter, imageTag string, additionalOffset ...int) {
+       deploymentName := solrPrometheusExporter.MetricsDeploymentName()
+       EventuallyWithOffset(resolveOffset(additionalOffset), func(g Gomega) {
+               deployment := &appsv1.Deployment{}
+               g.Expect(k8sClient.Get(ctx, resourceKey(solrPrometheusExporter, 
deploymentName), deployment)).To(Succeed(), "Expected Deployment does not 
exist")
+               
g.Expect(deployment.Spec.Template.Spec.Containers[0].Image).To(Equal(solrv1beta1.DefaultSolrRepo+":"+imageTag),
 "Incorrect image, should be taken from the referenced SolrCloud")
+       }).Should(Succeed())
+}
diff --git a/docs/modules/solr-prometheus-exporter/pages/index.adoc 
b/docs/modules/solr-prometheus-exporter/pages/index.adoc
index 0979fddd..d0690bc5 100644
--- a/docs/modules/solr-prometheus-exporter/pages/index.adoc
+++ b/docs/modules/solr-prometheus-exporter/pages/index.adoc
@@ -36,7 +36,9 @@ You have two options for the prometheus exporter to find the 
zookeeper connectio
 
 * Provide the name of a `SolrCloud` object in the same Kubernetes cluster, and 
optional namespace.
 The Solr Operator will keep the ZK Connection info up to date from the 
SolrCloud object.
-This name can be provided at: `SolrPrometheusExporter.spec.solrRef.cloud.name`
+This name can be provided at: 
`SolrPrometheusExporter.spec.solrReference.cloud.name`, and the namespace at: 
`SolrPrometheusExporter.spec.solrReference.cloud.namespace`
+The namespace defaults to the namespace of the `SolrPrometheusExporter`.
+A `SolrCloud` in another namespace is supported, but only if the Solr Operator 
is watching that namespace.
 * Provide explicit Zookeeper Connection info for the prometheus exporter to 
use.
   This info can be provided at: 
`SolrPrometheusExporter.spec.solrRef.cloud.zkConnectionInfo`, with keys 
`internalConnectionString` and `chroot`
 
@@ -172,7 +174,8 @@ kubectl get pods -n monitoring
 
 === Deploy Prometheus Exporter for Solr Metrics
 
-Next, deploy a Solr Prometheus exporter for the SolrCloud you want to capture 
metrics from in the namespace where you're running SolrCloud, not in the 
`monitoring` namespace.
+Next, deploy a Solr Prometheus exporter for the SolrCloud you want to capture 
metrics from.
+Deploy it in the namespace where you're running SolrCloud, not in the 
`monitoring` namespace.
 For instance, the following example creates a Prometheus exporter named 
`dev-prom-exporter` for a SolrCloud named `dev` deployed in the `dev` namespace:
 
 [source,yaml]
diff --git a/helm/solr-operator/Chart.yaml b/helm/solr-operator/Chart.yaml
index 14bd4317..abecbbe3 100644
--- a/helm/solr-operator/Chart.yaml
+++ b/helm/solr-operator/Chart.yaml
@@ -160,6 +160,11 @@ annotations:
           url: https://solr.apache.org/docs/9_11_0/changes/Changes.html
         - name: Solr Security News
           url: https://solr.apache.org/security-news.html
+    - kind: fixed
+      description: A SolrPrometheusExporter that references a SolrCloud in 
another namespace is now reconciled when that SolrCloud changes, instead of 
only when a SolrCloud of the same name in the exporter's own namespace changes
+      links:
+        - name: Github PR
+          url: https://github.com/apache/solr-operator/pull/879
     - kind: changed
       description: The `solr.xml` is now mounted into the Solr home directory 
from the ConfigMap, via a subPath volumeMount, instead of being copied there by 
the `cp-solr-xml` initContainer. That initContainer is only created for 
`volume` backupRepositories now, renamed to `volume-prep`
       links:

Reply via email to