@@ -20,12 +20,15 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"os"
23
+ "time"
23
24
24
25
corev1 "k8s.io/api/core/v1"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
27
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
27
28
"k8s.io/apimachinery/pkg/runtime"
28
29
"k8s.io/apimachinery/pkg/runtime/schema"
30
+ "k8s.io/apimachinery/pkg/types"
31
+
29
32
"sigs.k8s.io/controller-runtime/pkg/client"
30
33
"sigs.k8s.io/controller-runtime/pkg/client/config"
31
34
)
@@ -97,8 +100,10 @@ func ExampleClient_create() {
97
100
// Using a unstructured object.
98
101
u := & unstructured.Unstructured {}
99
102
u .Object = map [string ]interface {}{
100
- "name" : "name" ,
101
- "namespace" : "namespace" ,
103
+ "metadata" : map [string ]interface {}{
104
+ "name" : "name" ,
105
+ "namespace" : "namespace" ,
106
+ },
102
107
"spec" : map [string ]interface {}{
103
108
"replicas" : 2 ,
104
109
"selector" : map [string ]interface {}{
@@ -173,6 +178,35 @@ func ExampleClient_update() {
173
178
_ = c .Update (context .Background (), u )
174
179
}
175
180
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
+
176
210
// This example shows how to use the client with typed and unstructured objects to delete objects.
177
211
func ExampleClient_delete () {
178
212
// Using a typed object.
0 commit comments