-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨ (go/v4): Add support to Server-Side Apply #5458
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:server-side-apply-plugin
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.
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
|
camilamacedo86 marked this conversation as resolved.
|
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,99 @@ | ||
| # Server-Side Apply | ||
|
|
||
| The `--ssa` flag scaffolds APIs with [Server-Side Apply][server-side-apply] support, enabling safer field management when multiple actors modify the same resources. | ||
|
|
||
| This adds: | ||
| - API markers (`+genclient`, `+kubebuilder:ac:generate=true`) for ApplyConfiguration generation | ||
| - Makefile integration to generate type-safe ApplyConfiguration types alongside DeepCopy methods | ||
|
|
||
| ## When to use it | ||
|
|
||
| Use Server-Side Apply when: | ||
| - Multiple controllers or users manage the same resource | ||
| - Users customize CRs with labels, annotations, or spec fields your controller shouldn't overwrite | ||
| - You want declarative field ownership tracking | ||
| - Other operators will manage instances of your CRs (they can import your generated ApplyConfiguration types) | ||
|
|
||
| <aside class="note" role="note"> | ||
| <p class="note-title">Note</p> | ||
|
|
||
| For controllers that are the sole owner of a resource and manage the entire object, traditional `Update()` is simpler and sufficient. Use Server-Side Apply when field ownership matters. | ||
|
|
||
| </aside> | ||
|
|
||
| ## How it works | ||
|
|
||
| Traditional `Update()` overwrites the entire object. Server-Side Apply with `client.Apply()` only manages the fields you specify. | ||
|
|
||
| After running `make generate`, ApplyConfiguration types are created at: | ||
| ``` | ||
| api/<group>/<version>/applyconfiguration/<group>/<version>/ | ||
| ``` | ||
|
|
||
| Import them as: | ||
| ```go | ||
| appsv1apply "example.com/myproject/api/apps/v1/applyconfiguration/apps/v1" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Create an API with the `--ssa` flag: | ||
|
|
||
| ```shell | ||
| kubebuilder create api --group apps --version v1 --kind Application --ssa | ||
| ``` | ||
|
|
||
| Implement Server-Side Apply in your controller: | ||
|
|
||
| ```go | ||
| import ( | ||
| "context" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| appsv1apply "example.com/myproject/api/apps/v1/applyconfiguration/apps/v1" | ||
| ) | ||
|
|
||
| func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| // Build desired state - specify only fields you manage | ||
| app := appsv1apply.Application(req.Name, req.Namespace). | ||
| WithStatus(appsv1apply.ApplicationStatus(). | ||
| WithConditions(metav1.Condition{ | ||
| Type: "Available", | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "Reconciled", | ||
| })) | ||
|
|
||
| // Apply status | ||
| if err := r.SubResource("status").Apply(ctx, app, client.FieldOwner("application-controller")); err != nil { | ||
| if apierrors.IsConflict(err) { | ||
| return ctrl.Result{Requeue: true}, nil | ||
| } | ||
| return ctrl.Result{}, err | ||
| } | ||
| return ctrl.Result{}, nil | ||
| } | ||
| ``` | ||
|
|
||
| Then run: | ||
| ```shell | ||
| make generate manifests | ||
| ``` | ||
|
|
||
|
|
||
| ## Best practices | ||
|
|
||
| - Always specify `client.FieldOwner("controller-name")` to identify your controller | ||
| - Handle conflicts by checking `apierrors.IsConflict(err)` and requeueing | ||
| - Only specify fields your controller manages using the builder pattern | ||
| - Use `client.ForceOwnership` only when your controller must have final authority | ||
|
|
||
| ## Additional resources | ||
|
|
||
| - [Kubernetes Server-Side Apply Documentation][server-side-apply] | ||
| - [controller-gen CLI Reference](./controller-gen.md) | ||
|
|
||
| [server-side-apply]: https://kubernetes.io/docs/reference/using-api/server-side-apply/ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JoelSpeed @alvaroaleman @sbueringer Could you please give a hand on the review of this doc? |
||
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
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
Oops, something went wrong.
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.
Should we specify if there's an easy path to "enable" the plugin on existing APIs? And if there's not maybe simply add a note that suggests that a person willing to do that should regenerate their API and migrate manually?
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.
Offen in kubebuilder for we migrate something, we would need to re-scaffold. In this case, I think the best would be to re-scaffold using the plugin, or add the markers in the API and update the makefile.
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.
We can study a option to migrate manually and using AI, for example, but then it is out os scope of this one where we need to define how to introduce the feature.
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.
Yeah that's totally fine and probably not too hard to rescaffold and manually "merge" the scaffolding