Skip to content

Commit 6fc3892

Browse files
committed
doc: provide example of client.Patch
Signed-off-by: knight42 <anonymousknight96@gmail.com>
1 parent a2d55b5 commit 6fc3892

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

pkg/client/example_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"time"
2324

2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
"k8s.io/apimachinery/pkg/runtime/schema"
30+
"k8s.io/apimachinery/pkg/types"
31+
2932
"sigs.k8s.io/controller-runtime/pkg/client"
3033
"sigs.k8s.io/controller-runtime/pkg/client/config"
3134
)
@@ -97,8 +100,10 @@ func ExampleClient_create() {
97100
// Using a unstructured object.
98101
u := &unstructured.Unstructured{}
99102
u.Object = map[string]interface{}{
100-
"name": "name",
101-
"namespace": "namespace",
103+
"metadata": map[string]interface{}{
104+
"name": "name",
105+
"namespace": "namespace",
106+
},
102107
"spec": map[string]interface{}{
103108
"replicas": 2,
104109
"selector": map[string]interface{}{
@@ -173,6 +178,35 @@ func ExampleClient_update() {
173178
_ = c.Update(context.Background(), u)
174179
}
175180

181+
// This example shows how to use the client with typed and unstructured objects to patch objects.
182+
func ExampleClient_patch() {
183+
patch := []byte(`{"metadata":{"annotations":{"version": "v2"}}}`)
184+
_ = c.Patch(context.Background(), &corev1.Pod{
185+
ObjectMeta: metav1.ObjectMeta{
186+
Namespace: "namespace",
187+
Name: "name",
188+
},
189+
}, client.RawPatch(types.StrategicMergePatchType, patch))
190+
}
191+
192+
// This example shows how to use the client with typed and unstructured objects to patch objects' status.
193+
func ExampleClient_patchStatus() {
194+
u := &unstructured.Unstructured{}
195+
u.Object = map[string]interface{}{
196+
"metadata": map[string]interface{}{
197+
"name": "foo",
198+
"namespace": "namespace",
199+
},
200+
}
201+
u.SetGroupVersionKind(schema.GroupVersionKind{
202+
Group: "batch",
203+
Version: "v1beta1",
204+
Kind: "CronJob",
205+
})
206+
patch := []byte(fmt.Sprintf(`{"status":{"lastScheduleTime":"%s"}}`, time.Now().Format(time.RFC3339)))
207+
_ = c.Status().Patch(context.Background(), u, client.RawPatch(types.MergePatchType, patch))
208+
}
209+
176210
// This example shows how to use the client with typed and unstructured objects to delete objects.
177211
func ExampleClient_delete() {
178212
// Using a typed object.

0 commit comments

Comments
 (0)