This is an automated email from the ASF dual-hosted git repository.
HoustonPutman pushed a commit to branch release-0.10
in repository https://gitbox.apache.org/repos/asf/solr-operator.git
The following commit(s) were added to refs/heads/release-0.10 by this push:
new 0c0f2575 Don't keep ownership of SolrCloud and SolrPrometheusExporter
fields (#905)
0c0f2575 is described below
commit 0c0f257568e0c2021a99011988fe5a35ee5e1b03
Author: Jan Høydahl <[email protected]>
AuthorDate: Sat Nov 21 01:35:06 2026 +0100
Don't keep ownership of SolrCloud and SolrPrometheusExporter fields (#905)
The SolrCloud, SolrBackup and SolrPrometheusExporter reconcilers wrote
their defaults back with a full Update, and the storage finalizer was
added and removed the same way. The object goes through the Go types on
the way, which writes fields the client never set, such as an empty
resources block on every init container. The API server sees those as
changes, so the operator became the owner of them, and of the whole of
any atomic list they sit in. Helm 4 applies server-side by default, and
from the second helm upgrade on failed with a conflict on
.spec.customSolrKubeOptions.podOptions.initContainers.
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Houston Putman <[email protected]>
---
controllers/common.go | 38 ++++-
controllers/solrcloud_controller.go | 15 +-
.../solrcloud_controller_field_ownership_test.go | 175 +++++++++++++++++++++
controllers/solrprometheusexporter_controller.go | 6 +-
controllers/suite_test.go | 1 +
helm/solr-operator/Chart.yaml | 7 +
main.go | 2 +
7 files changed, 238 insertions(+), 6 deletions(-)
diff --git a/controllers/common.go b/controllers/common.go
index b86fb003..65d06229 100644
--- a/controllers/common.go
+++ b/controllers/common.go
@@ -18,10 +18,18 @@
package controllers
import (
- "sigs.k8s.io/controller-runtime/pkg/reconcile"
+ "context"
"time"
+
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "sigs.k8s.io/controller-runtime/pkg/client"
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
)
+// FieldManager is the field manager of all the operator's writes. It is set
as the default field owner of the
+// operator's client, so that its managedFields entries can be told apart from
those of other clients.
+const FieldManager = "solr-operator"
+
// Set the requeueAfter if it has not been set, or is greater than the new
time to requeue at
func updateRequeueAfter(requeueOrNot *reconcile.Result, newWait time.Duration)
{
if newWait <= 0 {
@@ -31,3 +39,31 @@ func updateRequeueAfter(requeueOrNot *reconcile.Result,
newWait time.Duration) {
requeueOrNot.RequeueAfter = newWait
}
}
+
+// patchResource writes only the changes made to obj since orig was read,
failing if obj was modified in between.
+// An Update would also write the fields that the Go types fill in, making the
operator their owner.
+func patchResource(ctx context.Context, c client.Client, obj client.Object,
orig client.Object) error {
+ return c.Patch(ctx, obj, client.MergeFromWithOptions(orig,
client.MergeFromWithOptimisticLock{}))
+}
+
+// releaseFieldOwnership drops the operator's ownership of the spec and
metadata fields it has written, such as defaults,
+// so that a server-side apply of another value, e.g. by Helm, does not
conflict with the operator.
+func releaseFieldOwnership(ctx context.Context, c client.Client, obj
client.Object) error {
+ managedFields := obj.GetManagedFields()
+ kept := make([]metav1.ManagedFieldsEntry, 0, len(managedFields))
+ for _, entry := range managedFields {
+ if entry.Manager != FieldManager || entry.Subresource != "" {
+ kept = append(kept, entry)
+ }
+ }
+ if len(kept) == len(managedFields) {
+ return nil
+ }
+ if len(kept) == 0 {
+ // An empty list leaves managedFields unchanged, a single empty
entry clears them
+ kept = []metav1.ManagedFieldsEntry{{}}
+ }
+ orig := obj.DeepCopyObject().(client.Object)
+ obj.SetManagedFields(kept)
+ return patchResource(ctx, c, obj, orig)
+}
diff --git a/controllers/solrcloud_controller.go
b/controllers/solrcloud_controller.go
index 89e9dda2..d8e3692f 100644
--- a/controllers/solrcloud_controller.go
+++ b/controllers/solrcloud_controller.go
@@ -108,14 +108,18 @@ func (r *SolrCloudReconciler) Reconcile(ctx
context.Context, req ctrl.Request) (
return reconcile.Result{}, err
}
+ unmodifiedInstance := instance.DeepCopy()
changed := instance.WithDefaults(logger)
if changed {
logger.Info("Setting default settings for SolrCloud")
- if err = r.Update(ctx, instance); err != nil {
+ if err = patchResource(ctx, r.Client, instance,
unmodifiedInstance); err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{Requeue: true}, nil
}
+ if err = releaseFieldOwnership(ctx, r.Client, instance); err != nil {
+ return reconcile.Result{}, err
+ }
// Owned resources are garbage collected, so a SolrCloud being deleted
only needs its storage finalizer handled.
// This keeps e.g. a deleted providedConfigMap or an unsupported Solr
version from blocking the deletion.
@@ -1243,8 +1247,9 @@ func (r *SolrCloudReconciler)
reconcileStorageFinalizer(ctx context.Context, clo
// The object is not being deleted, so if it does not
have our finalizer,
// then lets add the finalizer and update the object
if !util.ContainsString(cloud.ObjectMeta.Finalizers,
util.SolrStorageFinalizer) {
+ unmodifiedCloud := cloud.DeepCopy()
cloud.ObjectMeta.Finalizers =
append(cloud.ObjectMeta.Finalizers, util.SolrStorageFinalizer)
- if err := r.Update(ctx, cloud); err != nil {
+ if err := patchResource(ctx, r.Client, cloud,
unmodifiedCloud); err != nil {
return err
}
}
@@ -1260,16 +1265,18 @@ func (r *SolrCloudReconciler)
reconcileStorageFinalizer(ctx context.Context, clo
logger.Info("Deleted PVCs for SolrCloud")
// remove our finalizer from the list and update it.
+ unmodifiedCloud := cloud.DeepCopy()
cloud.ObjectMeta.Finalizers =
util.RemoveString(cloud.ObjectMeta.Finalizers, util.SolrStorageFinalizer)
- if err := r.Update(ctx, cloud); err != nil {
+ if err := patchResource(ctx, r.Client, cloud,
unmodifiedCloud); err != nil {
return err
}
}
} else if util.ContainsString(cloud.ObjectMeta.Finalizers,
util.SolrStorageFinalizer) {
// remove our finalizer from the list and update it, because
there is no longer a need to delete PVCs after the cloud is deleted.
logger.Info("Removing storage finalizer for SolrCloud")
+ unmodifiedCloud := cloud.DeepCopy()
cloud.ObjectMeta.Finalizers =
util.RemoveString(cloud.ObjectMeta.Finalizers, util.SolrStorageFinalizer)
- if err := r.Update(ctx, cloud); err != nil {
+ if err := patchResource(ctx, r.Client, cloud, unmodifiedCloud);
err != nil {
return err
}
}
diff --git a/controllers/solrcloud_controller_field_ownership_test.go
b/controllers/solrcloud_controller_field_ownership_test.go
new file mode 100644
index 00000000..ca1aa6b2
--- /dev/null
+++ b/controllers/solrcloud_controller_field_ownership_test.go
@@ -0,0 +1,175 @@
+/*
+ * 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"
+
+ solrv1beta1 "github.com/apache/solr-operator/api/v1beta1"
+ "github.com/apache/solr-operator/controllers/util"
+ . "github.com/onsi/ginkgo/v2"
+ . "github.com/onsi/gomega"
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+ "sigs.k8s.io/controller-runtime/pkg/client"
+)
+
+// Helm 4 applies server-side, so it fails on any field the operator has taken
ownership of
+var _ = FDescribe("SolrCloud controller - Field ownership", func() {
+ var (
+ solrCloud *solrv1beta1.SolrCloud
+ applied *unstructured.Unstructured
+ )
+
+ BeforeEach(func() {
+ solrCloud = &solrv1beta1.SolrCloud{}
+ solrCloud.Name = "foo"
+ solrCloud.Namespace = "default"
+
+ applied = &unstructured.Unstructured{Object:
map[string]interface{}{
+ "apiVersion": solrv1beta1.GroupVersion.String(),
+ "kind": "SolrCloud",
+ "metadata": map[string]interface{}{
+ "name": solrCloud.Name,
+ "namespace": solrCloud.Namespace,
+ },
+ "spec": map[string]interface{}{
+ "replicas": int64(1),
+ "zookeeperRef": map[string]interface{}{
+ "connectionInfo":
map[string]interface{}{
+ "internalConnectionString":
"host:7271",
+ },
+ },
+ "customSolrKubeOptions": map[string]interface{}{
+ "podOptions": map[string]interface{}{
+ "initContainers": []interface{}{
+ map[string]interface{}{
+ "name":
"custom-init",
+ "image":
"busybox:1.37",
+ "command":
[]interface{}{"true"},
+ },
+ },
+ },
+ },
+ },
+ }}
+ })
+
+ AfterEach(func(ctx context.Context) {
+ cleanupTest(ctx, solrCloud)
+ })
+
+ FIt("does not keep ownership of the fields it defaults", func(ctx
context.Context) {
+ By("applying the SolrCloud as another field manager")
+ applyAsHelm(ctx, applied)
+
+ By("waiting for the operator to default the SolrCloud and
release the defaulted fields")
+ expectSolrCloudWithChecks(ctx, solrCloud, func(g Gomega, found
*solrv1beta1.SolrCloud) {
+ g.Expect(found.Spec.SolrImage).ToNot(BeNil(), "The
SolrCloud should be defaulted")
+
g.Expect(found.Spec.SolrImage.Tag).To(Equal(solrv1beta1.DefaultSolrVersion))
+ expectNoOperatorFieldOwnership(g, found)
+ })
+
+ By("applying a different value for a defaulted field, without
forcing conflicts")
+ Expect(unstructured.SetNestedField(applied.Object, "9.10.0",
"spec", "solrImage", "tag")).To(Succeed())
+ applyAsHelm(ctx, applied)
+ expectSolrCloudWithChecks(ctx, solrCloud, func(g Gomega, found
*solrv1beta1.SolrCloud) {
+ g.Expect(found.Spec.SolrImage.Tag).To(Equal("9.10.0"))
+ })
+ })
+
+ FIt("does not keep ownership of the storage finalizer", func(ctx
context.Context) {
+ By("applying a SolrCloud whose PVCs are deleted with it")
+ Expect(unstructured.SetNestedField(applied.Object, "5Gi",
"spec", "dataStorage", "persistent", "pvcTemplate", "spec", "resources",
"requests", "storage")).To(Succeed())
+ Expect(unstructured.SetNestedField(applied.Object, "Delete",
"spec", "dataStorage", "persistent", "reclaimPolicy")).To(Succeed())
+ applyAsHelm(ctx, applied)
+ expectSolrCloudWithChecks(ctx, solrCloud, func(g Gomega, found
*solrv1beta1.SolrCloud) {
+
g.Expect(found.Finalizers).To(ContainElement(util.SolrStorageFinalizer))
+ expectNoOperatorFieldOwnership(g, found)
+ })
+
+ By("keeping the PVCs instead, which removes the finalizer")
+ Expect(unstructured.SetNestedField(applied.Object, "Retain",
"spec", "dataStorage", "persistent", "reclaimPolicy")).To(Succeed())
+ applyAsHelm(ctx, applied)
+ expectSolrCloudWithChecks(ctx, solrCloud, func(g Gomega, found
*solrv1beta1.SolrCloud) {
+
g.Expect(found.Finalizers).ToNot(ContainElement(util.SolrStorageFinalizer))
+ expectNoOperatorFieldOwnership(g, found)
+ })
+ })
+})
+
+var _ = FDescribe("SolrPrometheusExporter controller - Field ownership",
func() {
+ var exporter *solrv1beta1.SolrPrometheusExporter
+
+ BeforeEach(func() {
+ exporter = &solrv1beta1.SolrPrometheusExporter{}
+ exporter.Name = "foo"
+ exporter.Namespace = "default"
+ })
+
+ AfterEach(func(ctx context.Context) {
+ cleanupTest(ctx, exporter)
+ })
+
+ FIt("does not keep ownership of the fields it defaults", func(ctx
context.Context) {
+ applied := &unstructured.Unstructured{Object:
map[string]interface{}{
+ "apiVersion": solrv1beta1.GroupVersion.String(),
+ "kind": "SolrPrometheusExporter",
+ "metadata": map[string]interface{}{
+ "name": exporter.Name,
+ "namespace": exporter.Namespace,
+ },
+ "spec": map[string]interface{}{
+ "solrReference": map[string]interface{}{
+ "standalone": map[string]interface{}{
+ "address":
"http://solr:8983/solr",
+ },
+ },
+ },
+ }}
+
+ By("applying the exporter as another field manager")
+ applyAsHelm(ctx, applied)
+
+ By("waiting for the operator to default the exporter and
release the defaulted fields")
+ expectSolrPrometheusExporterWithChecks(ctx, exporter, func(g
Gomega, found *solrv1beta1.SolrPrometheusExporter) {
+ g.Expect(found.Spec.Image).ToNot(BeNil(), "The exporter
should be defaulted")
+
g.Expect(found.Spec.Image.Tag).To(Equal(solrv1beta1.DefaultSolrVersion))
+ expectNoOperatorFieldOwnership(g, found)
+ })
+
+ By("applying a different value for a defaulted field, without
forcing conflicts")
+ Expect(unstructured.SetNestedField(applied.Object, "9.10.0",
"spec", "image", "tag")).To(Succeed())
+ applyAsHelm(ctx, applied)
+ expectSolrPrometheusExporterWithChecks(ctx, exporter, func(g
Gomega, found *solrv1beta1.SolrPrometheusExporter) {
+ g.Expect(found.Spec.Image.Tag).To(Equal("9.10.0"))
+ })
+ })
+})
+
+func applyAsHelm(ctx context.Context, obj *unstructured.Unstructured) {
+ ExpectWithOffset(1, k8sClient.Apply(ctx,
client.ApplyConfigurationFromUnstructured(obj.DeepCopy()),
client.FieldOwner("helm"))).To(Succeed())
+}
+
+func expectNoOperatorFieldOwnership(g Gomega, obj metav1.Object) {
+ for _, entry := range obj.GetManagedFields() {
+ if entry.Manager == FieldManager {
+ g.Expect(entry.Subresource).To(Equal("status"), "The
operator should not own any spec or metadata fields")
+ }
+ }
+}
diff --git a/controllers/solrprometheusexporter_controller.go
b/controllers/solrprometheusexporter_controller.go
index 56a1b972..a9fecf9f 100644
--- a/controllers/solrprometheusexporter_controller.go
+++ b/controllers/solrprometheusexporter_controller.go
@@ -81,14 +81,18 @@ func (r *SolrPrometheusExporterReconciler) Reconcile(ctx
context.Context, req ct
return ctrl.Result{}, err
}
+ unmodifiedExporter := prometheusExporter.DeepCopy()
changed := prometheusExporter.WithDefaults()
if changed {
logger.Info("Setting default settings for Solr
PrometheusExporter")
- if err := r.Update(ctx, prometheusExporter); err != nil {
+ if err := patchResource(ctx, r.Client, prometheusExporter,
unmodifiedExporter); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{Requeue: true}, nil
}
+ if err := releaseFieldOwnership(ctx, r.Client, prometheusExporter); err
!= nil {
+ return ctrl.Result{}, err
+ }
requeueOrNot := ctrl.Result{}
diff --git a/controllers/suite_test.go b/controllers/suite_test.go
index 81209a9f..fa45378f 100644
--- a/controllers/suite_test.go
+++ b/controllers/suite_test.go
@@ -100,6 +100,7 @@ var _ = BeforeSuite(func(ctx context.Context) {
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
+ Client: client.Options{FieldOwner: FieldManager},
})
Expect(err).ToNot(HaveOccurred())
diff --git a/helm/solr-operator/Chart.yaml b/helm/solr-operator/Chart.yaml
index bbaad8fc..03d2904b 100644
--- a/helm/solr-operator/Chart.yaml
+++ b/helm/solr-operator/Chart.yaml
@@ -262,6 +262,13 @@ annotations:
url: https://github.com/apache/solr-operator/issues/828
- name: Github PR
url: https://github.com/apache/solr-operator/pull/888
+ - kind: fixed
+ description: The operator no longer keeps ownership of SolrCloud and
SolrPrometheusExporter fields, including the ones it defaults such as the image
tag, which broke `helm upgrade` with Helm 4's server-side apply
+ links:
+ - name: GitHub Issue
+ url: https://github.com/apache/solr-operator/issues/872
+ - name: GitHub PR
+ url: https://github.com/apache/solr-operator/pull/905
- kind: fixed
description: With `solrSecurity.probesRequireAuth`, the Solr pod probes
now fail when the request to Solr fails, e.g. with a 401, instead of always
succeeding
links:
diff --git a/main.go b/main.go
index 27003dfd..319f704a 100644
--- a/main.go
+++ b/main.go
@@ -32,6 +32,7 @@ import (
"path/filepath"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
+ "sigs.k8s.io/controller-runtime/pkg/client"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"strings"
@@ -138,6 +139,7 @@ func main() {
operatorOptions := ctrl.Options{
Scheme: scheme,
+ Client: client.Options{FieldOwner:
controllers.FieldManager},
Metrics: metricsserver.Options{BindAddress:
metricsAddr},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,