Repository navigation
Stop updating resources on every reconcile because of Kubernetes defaults - #933
Open
HoustonPutman wants to merge 2 commits into
Open
HoustonPutman wants to merge 2 commits into
HoustonPutman wants to merge 2 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:These are the fields that caused it:
defaultModeapiVersionof afieldRefexpirationSecondshostPathdata volumetypecachingMode,kindemptyDirtimeoutSeconds,periodSeconds,successThreshold,failureThreshold, the HTTP GETschemeand the gRPCserviceschemeClientIPsession affinitysessionAffinityConfigWhere 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
CopyServiceFieldsalready did forsessionAffinity.sessionAffinityConfigis still removed when the session affinity is no longerClientIP, since Kubernetes only allows it withClientIP.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 changedlines 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 theresourceVersionunchanged. A suite-wideAfterEachfails any spec that sent one, pointing at theUpdate required because field changedlog that names the field. This makes every existing spec part of the check. Without the fixes, it fails existing specs ondefaultModeandsessionAffinityConfig.every_option_fixtures_test.go: the existing fixtures fill in Kubernetes' defaults themselves, e.g. probe timeouts andapiVersion: v1on 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. acorev1.Probe, to be set.