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
📖 Add more examples in documentation #2498
Merged
k8s-ci-robot
merged 2 commits into
kubernetes-sigs:main
from
aerfio:aerfio/more-examples
Sep 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -25,11 +25,14 @@ import ( | |
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
corev1ac "k8s.io/client-go/applyconfigurations/core/v1" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
var ( | ||
|
@@ -159,7 +162,7 @@ func ExampleClient_update() { | |
Namespace: "namespace", | ||
Name: "name", | ||
}, pod) | ||
pod.SetFinalizers(append(pod.GetFinalizers(), "new-finalizer")) | ||
controllerutil.AddFinalizer(pod, "new-finalizer") | ||
_ = c.Update(context.Background(), pod) | ||
|
||
// Using a unstructured object. | ||
|
@@ -173,7 +176,7 @@ func ExampleClient_update() { | |
Namespace: "namespace", | ||
Name: "name", | ||
}, u) | ||
u.SetFinalizers(append(u.GetFinalizers(), "new-finalizer")) | ||
controllerutil.AddFinalizer(u, "new-finalizer") | ||
_ = c.Update(context.Background(), u) | ||
} | ||
|
||
|
@@ -188,6 +191,16 @@ func ExampleClient_patch() { | |
}, client.RawPatch(types.StrategicMergePatchType, patch)) | ||
} | ||
|
||
// This example shows how to use the client with unstructured objects to create/patch objects using Server Side Apply, | ||
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. Please mention why the dance of converting to unstructured is needed |
||
func ExampleClient_apply() { | ||
// Using a typed object. | ||
configMap := corev1ac.ConfigMap("name", "namespace").WithData(map[string]string{"key": "value"}) | ||
// c is a created client. | ||
u := &unstructured.Unstructured{} | ||
u.Object, _ = runtime.DefaultUnstructuredConverter.ToUnstructured(configMap) | ||
_ = c.Patch(context.Background(), u, client.Apply, client.ForceOwnership, client.FieldOwner("field-owner")) | ||
} | ||
|
||
// This example shows how to use the client with typed and unstructured objects to patch objects' status. | ||
func ExampleClient_patchStatus() { | ||
u := &unstructured.Unstructured{} | ||
|
This file contains 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.
I don't think the fact that this has a watch is the special bit about this example. And I'd rather not have an example that doesn't implement
DeepCopy
but just panics. Maybe do something like a special configmap key that references something else or such?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 was my intention, to have an easy to grasp example for "Watches" 😢
I didn't want to complicate this example too much, I thought about switching from LocalObjectReference to e.g ConfigMapKeySelector, but the code inside Watches would a too complicated for an example imho.
Sure, I've changed it to json.Marshal + json.Unmarshal with panic on error, is that enough? I could use
controller-gen
for that, but that would result in new file in the root of the repo + some changes in makefile, which I wanted to omitUpdate: I tried it and controller-gen seems to ignore *_test.go files 🤷🏻
And lastly, for example name I changed it now like that:
to follow other examples in this repo (in this file also, see https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.2/example_test.go#L77) and also https://go.dev/blog/examples#example-function-names