Skip to content

Stop updating resources on every reconcile because of Kubernetes defaults - #933

Open
HoustonPutman wants to merge 2 commits into
apache:mainfrom
HoustonPutman:no-op-updates
Open

HoustonPutman wants to merge 2 commits into
apache:mainfrom
HoustonPutman:no-op-updates

Conversation

@HoustonPutman

@HoustonPutman HoustonPutman commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

The operator compares the resources it wants against the ones in the cluster, which Kubernetes has filled in defaults for. Any field that the operator leaves unset, but that Kubernetes defaults, looked like a change. So the operator sent an Update on every reconcile and logged Update required because field changed, even though the Update changed nothing. For example:

"field":"Spec.Template.Spec.Volumes[0].VolumeSource",
"from":{"configMap":{"name":"solr-exporter-config", ..., "defaultMode":420}},
"to":{"configMap":{"name":"solr-exporter-config", ...}}

These are the fields that caused it:

Where Field Kubernetes defaults
ConfigMap, Secret, DownwardAPI and projected volumes defaultMode
DownwardAPI volumes and env vars the apiVersion of a fieldRef
Projected service account tokens expirationSeconds
HostPath volumes, including the operator's own ephemeral hostPath data volume type
AzureDisk volumes cachingMode, kind
Volumes without a source become an emptyDir
Probes timeoutSeconds, periodSeconds, successThreshold, failureThreshold, the HTTP GET scheme and the gRPC service
Lifecycle hooks the HTTP GET scheme
Services with ClientIP session affinity sessionAffinityConfig

Where the desired container, volume or Service leaves one of these fields unset, the operator now keeps the value it finds in the cluster. So these fields are only reconciled when a value is explicitly set. This follows what CopyServiceFields already did for sessionAffinity. sessionAffinityConfig is still removed when the session affinity is no longer ClientIP, since Kubernetes only allows it with ClientIP.

The trade-off is that if one of these fields is set and later removed, the old value stays on the cluster instead of going back to the Kubernetes default.

The operator now also logs when one of its Updates doesn't change anything. That message, with the Update required because field changed lines before it, points at a field that is still compared wrongly, e.g. one that a mutating webhook fills in, which envtest can't see.

Tests

Every Update the operator sends should change the resource it is sent for. One that doesn't means the operator's comparison misses a default, so the operator would send that same Update on every reconcile. The API server skips writing such an Update and doesn't bump the resourceVersion. So comparing objects before and after can't catch this; the requests themselves have to be checked.

  • write_recording_client_test.go, suite_test.go: the reconcilers' client in the envtest suite records every Update that leaves the resourceVersion unchanged. A suite-wide AfterEach fails any spec that sent one, pointing at the Update required because field changed log that names the field. This makes every existing spec part of the check. Without the fixes, it fails existing specs on defaultMode and sessionAffinityConfig.
  • every_option_fixtures_test.go: the existing fixtures fill in Kubernetes' defaults themselves, e.g. probe timeouts and apiVersion: v1 on field refs, which is why they never caught this. The new fixtures are SolrClouds and SolrPrometheusExporters that between them set every option of the CRDs. They're spread over five SolrCloud and three exporter variants, because some options can't be combined, e.g. persistent and ephemeral storage, a provided ZooKeeper or a connection string, Ingress or ExternalDNS, and PKCS12 or mounted TLS. Wherever they pass Kubernetes types through, they deliberately leave out the fields Kubernetes defaults, with a volume of every kind that gets defaults, probes of every handler, lifecycle hooks, sidecars and native sidecar init containers.
  • no_repeated_updates_test.go: creates each variant, waits for the operator to stop writing, makes it reconcile once more, and then checks each type of resource the operator creates in its own spec: the CR itself, StatefulSet, Services, ConfigMap, Ingress, PodDisruptionBudget and ZookeeperCluster, or Deployment, Service and ConfigMap for exporters. A last spec catches Updates to any other resource. These specs found most of the fields above.
  • every_option_coverage_test.go: a plain Go test, which doesn't need envtest. It walks the SolrCloud and SolrPrometheusExporter spec types by reflection and fails for any field that no variant sets. So adding an option to the CRDs means adding it to a fixture. It walks into the solr-operator API types and only requires Kubernetes types, e.g. a corev1.Probe, to be set.

…ults

The operator compares the resources it wants against the ones in the
cluster, which Kubernetes has filled in defaults for. Any field that the
operator leaves unset, but Kubernetes defaults, looked like a change, so
the operator sent an Update on every reconcile and logged "Update
required because field changed", even though the Update changed nothing.

This happened for custom probes, lifecycle hooks, env vars, volumes and
Service session affinity, e.g. a ConfigMap volume without a defaultMode,
a probe without a scheme or timeouts, or a hostPath volume without a
type, which includes the operator's own ephemeral hostPath data volume.

Where the desired container, volume or Service leaves one of these fields
unset, the value found in the cluster is now kept, so the fields are only
reconciled when a value is explicitly desired. SessionAffinityConfig is
still removed when the session affinity is no longer ClientIP, since it
is not allowed otherwise.

The operator now also logs when one of its Updates does not change
anything, which points at a field that is compared wrongly, e.g. one
that a mutating webhook fills in.
Every Update the operator sends should change the resource it is sent
for. One that does not means the operator's comparison misses a default
that Kubernetes fills in, so it would send the same Update on every
reconcile. The API server skips writing such an Update and keeps the
resourceVersion, so the reconcilers' client now records every Update
that did not change the resourceVersion, and every envtest spec fails if
there was one.

To make sure every option is covered, new specs create SolrClouds and
SolrPrometheusExporters that between them set every option of the CRDs,
spread over a few variants for the options that cannot be used together.
Where they pass Kubernetes types through, they leave out the fields that
Kubernetes defaults. Each variant checks, per type of resource that the
operator creates, that the operator does not keep updating it. A plain
Go test checks that the variants set every field of the SolrCloud and
SolrPrometheusExporter specs, so a new option has to be added to one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant