-
Notifications
You must be signed in to change notification settings - Fork 1.6k
📖 Add doc about Strict Server-Side Field Validation #5215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
camilamacedo86
wants to merge
1
commit into
kubernetes-sigs:master
Choose a base branch
from
camilamacedo86:add-optional-strict-manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| # Strict Field Validation | ||
|
|
||
| ## Overview | ||
|
|
||
| By default, when your controller writes an object that contains fields not defined in the CRD schema, the API server: | ||
|
|
||
| - Accepts the request | ||
| - Drops the unknown fields | ||
| - May only log a warning | ||
|
|
||
| This can hide bugs and version skew between: | ||
| - The controller code (Go types) and | ||
| - The CRD schema installed in the cluster | ||
|
|
||
| `controller-runtime` exposes [client.WithFieldValidation][client-field-validation-docs] to turn on strict server-side field validation for all client writes. When enabled, the API server returns a hard error instead of silently dropping unknown fields. | ||
|
|
||
| CRDs should be installed before controllers. However, during upgrades this can be best effort rather than guaranteed. Deployment tools may apply resources without strict ordering, and even tools with ordering features don't guarantee the CRD update succeeds. For controllers using external CRDs from third-party operators, ordering may not be controllable at all. | ||
|
|
||
| When controllers and CRDs get out of sync, controller writes may fail with unknown field errors, status updates may not persist, and conversion webhooks can fail if the CRD schema is outdated. Controllers may crash-loop until CRDs are updated. | ||
|
|
||
| ## What does it solve | ||
|
|
||
| Strict validation prevents silent failures when your controller code and CRD schemas get out of sync. | ||
|
|
||
| For example, you add a new field `status.newField` to your controller, but the CRD in the cluster hasn't been updated yet. When the controller calls `client.Status().Patch(...)`: | ||
|
|
||
| **Without strict validation:** | ||
| - API server drops `status.newField` silently | ||
| - Controller sees no error | ||
| - Field never appears on the object - confusing debugging | ||
|
|
||
| **With strict validation:** | ||
| - API server returns clear error | ||
| - Controller knows CRDs need updating | ||
| - Fails fast instead of silent data loss | ||
|
|
||
| ## When to use it | ||
|
|
||
| Strict validation is a good fit when: | ||
|
|
||
| - You own both the CRDs and the controllers | ||
| - Your upgrade process applies CRDs first or ensures they update together | ||
| - You want to fail fast when a controller writes fields not in the schema | ||
| - You want to catch bugs in your types or conversions early | ||
| - You use typed schemas, or explicitly mark dynamic data with `x-kubernetes-preserve-unknown-fields: true` | ||
|
|
||
| ## When NOT to use it | ||
|
|
||
| Avoid strict validation in production when: | ||
|
|
||
| - Controllers and CRDs upgrade independently (i.e., common in Helm) | ||
| - You manage third-party CRDs whose schemas evolve independently | ||
| - Your CRDs use unstructured/dynamic data without `x-kubernetes-preserve-unknown-fields` | ||
| - You need upgrade tolerance when controller and CRD versions are temporarily mismatched | ||
|
|
||
| ## How Kubebuilder scaffold handles CRD ordering | ||
|
|
||
| ### Makefile Targets (`make install` + `make deploy`) | ||
|
|
||
| ```bash | ||
| make install # Installs CRDs into the cluster | ||
| make deploy # Deploys the controller | ||
| ``` | ||
|
|
||
| Two separate commands. CRDs are installed first and established before the controller starts. Order is guaranteed. | ||
|
|
||
| ### YAML bundle distribution (make build-installer) | ||
|
|
||
| ```bash | ||
| kubectl apply -f dist/install.yaml | ||
| ``` | ||
|
|
||
| The bundle positions CRDs early in the file, after Namespace and before Deployment. This works for new installations. | ||
|
|
||
| During upgrades, if the CRD already exists and the update fails or is slow, the Deployment may still update. The new controller may start before the CRD update completes. | ||
|
|
||
| ### Helm chart distribution | ||
|
|
||
| By using the [Helm plugin](../plugins/available/helm-v2-alpha.md), you can distribute your solution as a Helm chart package. Users can install or upgrade it with: | ||
|
|
||
| ```bash | ||
| helm install my-operator ./dist/chart | ||
| ``` | ||
|
|
||
| Kubebuilder places CRDs in `templates/crd/` to ensure they upgrade with the controller. Helm has a built-in resource order that helps during installation. | ||
|
|
||
| During upgrades, if the CRD update fails or is slow to propagate, Helm may still update the Deployment. | ||
|
|
||
| Moreover, users can skip CRD updates with `helm upgrade --set crd.enable=false`. | ||
|
|
||
| <aside class="note"> | ||
|
|
||
| Helm recommends a `crds/` directory, but CRDs there **never upgrade**. Kubebuilder uses `templates/crd/` to keep CRDs in sync with controllers but the order is **best effort**. See [Why CRDs are added under templates](../plugins/available/helm-v2-alpha.md#why-crds-are-added-under-templates) for details. | ||
|
|
||
| </aside> | ||
|
|
||
| <aside class="note"> | ||
| <h1>Upgrade and Lifecycle Considerations</h1> | ||
|
|
||
| During upgrades, CRD updates may not complete before the controller starts. This can happen when deployment tools apply resources without explicit ordering mechanisms. | ||
|
|
||
| CRD updates may hit errors or take time to propagate through the API server. If the controller Deployment updates at the same time, the new controller pod may start before the CRD update finishes. With strict validation enabled, controller writes will fail until the CRD is ready. | ||
|
|
||
| Ordering tools like ArgoCD sync waves or FluxCD dependsOn can help by controlling when resources are applied. However, these control timing, not whether the CRD update succeeds. If not properly configured or if the CRD apply encounters issues, the controller may still deploy. | ||
|
|
||
| For controllers that use CRDs from third-party operators (cert-manager, prometheus-operator, etc.), those CRDs have independent lifecycles. The third-party operator may upgrade its CRDs separately from your controller, which can lead to version mismatches. | ||
|
|
||
| </aside> | ||
|
|
||
| ## Wiring an opt-in flag in cmd/main.go | ||
|
|
||
| This feature is **not scaffolded by default**. Follow these steps to add it manually. | ||
|
|
||
| ### Step 1: Add the strictManager wrapper | ||
|
|
||
| In `cmd/main.go`, add this type definition after the `init()` function: | ||
|
|
||
| ```go | ||
| // strictManager wraps the manager to reject unknown fields instead of silently dropping them. | ||
| // When the controller writes a field that doesn't exist in the CRD, the write fails immediately. | ||
| // This helps catch typos and version mismatches between your code and cluster CRDs. | ||
| type strictManager struct { | ||
| ctrl.Manager | ||
| strictClient client.Client | ||
| } | ||
|
|
||
| func (m *strictManager) GetClient() client.Client { | ||
| return m.strictClient | ||
| } | ||
| ``` | ||
|
|
||
| ### Step 2: Add required imports | ||
|
|
||
| Add these imports to `cmd/main.go`: | ||
|
|
||
| ```go | ||
| import ( | ||
| // ... your existing imports ... | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
| ``` | ||
|
|
||
| ### Step 3: Add the command-line flag | ||
|
|
||
| In the `main()` function, where other flags are defined, add: | ||
|
|
||
| ```go | ||
| func main() { | ||
| var metricsAddr string | ||
| var enableLeaderElection bool | ||
| var probeAddr string | ||
| var strictFieldValidation bool // Add this | ||
|
|
||
| flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "...") | ||
| flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "...") | ||
| flag.BoolVar(&enableLeaderElection, "leader-elect", false, "...") | ||
|
|
||
| // Add this flag | ||
| flag.BoolVar(&strictFieldValidation, "strict-field-validation", false, | ||
| "Reject unknown fields instead of dropping them.") | ||
|
|
||
| // ... rest of your code ... | ||
| } | ||
| ``` | ||
|
|
||
| ### Step 4: Wrap the manager conditionally | ||
|
|
||
| After creating the manager with `ctrl.NewManager()`, add this wrapper logic: | ||
|
|
||
| ```go | ||
| mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
| Scheme: scheme, | ||
| // ... your other options ... | ||
| }) | ||
| if err != nil { | ||
| setupLog.Error(err, "unable to start manager") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // When enabled, the controller rejects writes with unknown fields instead of silently dropping them. | ||
| var finalMgr ctrl.Manager = mgr | ||
| if strictFieldValidation { | ||
| finalMgr = &strictManager{ | ||
| Manager: mgr, | ||
| strictClient: client.WithFieldValidation( | ||
| mgr.GetClient(), | ||
| metav1.FieldValidationStrict, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| // Use finalMgr for all subsequent setup | ||
| if err := (&controller.MyReconciler{ | ||
| Client: finalMgr.GetClient(), | ||
| Scheme: finalMgr.GetScheme(), | ||
| }).SetupWithManager(finalMgr); err != nil { | ||
| setupLog.Error(err, "unable to create controller", "controller", "My") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Continue using finalMgr for health checks, starting manager, etc. | ||
| if err := finalMgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { | ||
| setupLog.Error(err, "unable to set up health check") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if err := finalMgr.Start(ctrl.SetupSignalHandler()); err != nil { | ||
| setupLog.Error(err, "problem running manager") | ||
| os.Exit(1) | ||
| } | ||
| ``` | ||
|
|
||
| [client-field-validation-docs]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client#WithFieldValidation | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry was just quickly reading over it so I might have missed this. This seems to focus only on CRDs?
If strict validation is used with builtin-resources this can mean that the controller is only compatible with exactly the Kubernetes version it was compiled against, which is often not desired
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly the kind of amazing help that I am looking for !!! 🙌
It is something that we must clarify.
Thank you, I will try to add it.
You are amazing 🚀 ⭐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I heard that Server-Side Apply always uses strict field validation. How can that work? I guess that SSA does a Patch, not an Update.
I guess that strict field validation works (even with other Kubernetes versions), when you use Patch, and the schema matches for the fields of that patch.
Are there docs about that? I would like to learn more about that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSA does neither Patch nor Update it does Apply. If you send a field that doesn't exist it will fail
No idea about docs to be honest, maybe somewhere on the Kubernetes website
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But good point, maybe it's not too bad to use it with builtin types. You shouldn't try to set fields that might not exist on the Kubernetes versions you run against anyway. And Update is a bad idea in general anyway.