Skip to content

Commit d9bc749

Browse files
committed
update runtime Client options and method signatures
1 parent 5a64d9b commit d9bc749

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- The package `sigs.k8s.io/controller-runtime/pkg/runtime/scheme` is deprecated, and contains no code. Replace this import with `sigs.k8s.io/controller-runtime/pkg/scheme` where relevant.
2020
- The package `sigs.k8s.io/controller-runtime/pkg/runtime/log` is deprecated. Replace this import with `sigs.k8s.io/controller-runtime/pkg/log` where relevant.
2121
- The package `sigs.k8s.io/controller-runtime/pkg/runtime/signals` is deprecated. Replace this import with `sigs.k8s.io/controller-runtime/pkg/manager/signals` where relevant.
22+
- All methods on [`sigs.k8s.io/controller-runtime/pkg/client.Client`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/interfaces.go#L104) (except for `Get()`) have been updated. Instead of each using a `struct`-typed or variadic functional option parameter, or having no option parameter, each now uses a variadic interface option parameter typed for each method. See `List()` below for an example.
2223
- [`sigs.k8s.io/controller-runtime/pkg/client.Client`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/interfaces.go#L104)'s `List()` method signature has been updated: `List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error` is now [`List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/interfaces.go#L61). To migrate:
2324
```go
2425
import (

doc/user/client.md

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
137137
func (c Client) List(ctx context.Context, list runtime.Object, opts ...client.ListOption) error
138138
```
139139

140-
A `client.ListOption` is an interface that sets [`client.ListOptions`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L257) fields. A `client.ListOption` is created by using one of the provided implementations: [`LableSelector`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L304), [`FieldSelector`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L326), [`Namespace`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L339).
140+
A `client.ListOption` is an interface that sets [`client.ListOptions`][list-options] fields. A `client.ListOption` is created by using one of the provided implementations: [`MatchingLabels`][matchinglabels], [`MatchingFields`][matchingfields], [`InNamespace`][innamespace].
141141

142142
Example:
143143

@@ -168,14 +168,23 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
168168
}
169169
```
170170

171+
[list-options]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#ListOptions
172+
[matchinglabels]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#MatchingLabels
173+
[matchingfields]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#MatchingFields
174+
[innamespace]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#InNamespace
175+
171176
#### Create
172177

173178
```Go
174179
// Create saves the object obj in the Kubernetes cluster.
175180
// Returns an error
176-
func (c Client) Create(ctx context.Context, obj runtime.Object) error
181+
func (c Client) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error
177182
```
183+
184+
A `client.CreateOption` is an interface that sets [`client.CreateOptions`][create-options] fields. A `client.CreateOption` is created by using one of the provided implementations: [`DryRunAll`][dryrunall], [`ForceOwnership`][forceownership]. Generally these options are not needed.
185+
178186
Example:
187+
179188
```Go
180189
import (
181190
"context"
@@ -196,16 +205,22 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
196205
}
197206
```
198207

208+
[create-options]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#CreateOptions
209+
199210
#### Update
200211

201212
```Go
202213
// Update updates the given obj in the Kubernetes cluster. obj must be a
203214
// struct pointer so that obj can be updated with the content returned
204215
// by the API server. Update does *not* update the resource's status
205216
// subresource
206-
func (c Client) Update(ctx context.Context, obj runtime.Object) error
217+
func (c Client) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error
207218
```
219+
220+
A `client.UpdateOption` is an interface that sets [`client.UpdateOptions`][update-options] fields. A `client.UpdateOption` is created by using one of the provided implementations: [`DryRunAll`][dryrunall], [`ForceOwnership`][forceownership]. Generally these options are not needed.
221+
208222
Example:
223+
209224
```Go
210225
import (
211226
"context"
@@ -229,10 +244,54 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
229244
}
230245
```
231246

247+
[update-options]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#UpdateOptions
248+
249+
#### Patch
250+
251+
```Go
252+
// Patch patches the given obj in the Kubernetes cluster. obj must be a
253+
// struct pointer so that obj can be updated with the content returned by the Server.
254+
func (c Client) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.UpdateOption) error
255+
```
256+
257+
A `client.PatchOption` is an interface that sets [`client.PatchOptions`][patch-options] fields. A `client.PatchOption` is created by using one of the provided implementations: [`DryRunAll`][dryrunall], [`ForceOwnership`][forceownership]. Generally these options are not needed.
258+
259+
Example:
260+
261+
```Go
262+
import (
263+
"context"
264+
"k8s.io/api/apps/v1"
265+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
266+
)
267+
268+
func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, error) {
269+
...
270+
271+
dep := &v1.Deployment{}
272+
err := r.client.Get(context.TODO(), request.NamespacedName, dep)
273+
274+
...
275+
276+
ctx := context.TODO()
277+
dep.Spec.Selector.MatchLabels["is_running"] = "true"
278+
// A marge patch will preserve other fields modified at runtime.
279+
patch := client.MergeFrom(dep)
280+
err := r.client.Patch(ctx, dep, patch)
281+
282+
...
283+
}
284+
```
285+
286+
[patch-options]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#PatchOption
287+
[dryrunall]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#DryRunAll
288+
[forceownership]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#ForceOwnership
289+
232290
##### Updating Status Subresource
233291

234-
When updating the [status subresource][cr-status-subresource] from the client,
235-
the StatusWriter must be used which can be gotten with `Status()`
292+
When updating the [status subresource][cr-status-subresource] from the client, the [`StatusWriter`][statuswriter] must be used. The status subresource is retrieved with `Status()` and updated with `Update()` or patched with `Patch()`.
293+
294+
`Update()` takes variadic `client.UpdateOption`'s, and `Patch()` takes variadic `client.PatchOption`'s. See [`Client.Update()`](#update) and [`Client.Patch()`](#patch) for more details. Generally these options are not needed.
236295

237296
##### Status
238297

@@ -253,19 +312,27 @@ import (
253312
func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, error) {
254313
...
255314

315+
ctx := context.TODO()
256316
mem := &cachev1alpha1.Memcached{}
257-
err := r.client.Get(context.TODO(), request.NamespacedName, mem)
317+
err := r.client.Get(ctx, request.NamespacedName, mem)
258318

259319
...
260320

261-
ctx := context.TODO()
321+
// Update
262322
mem.Status.Nodes = []string{"pod1", "pod2"}
263323
err := r.client.Status().Update(ctx, mem)
264324

265325
...
326+
327+
// Patch
328+
patch := client.MergeFrom(mem)
329+
err := r.client.Status().Patch(ctx, mem, patch)
330+
331+
...
266332
}
267333
```
268334

335+
[statuswriter]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#StatusWriter
269336

270337
#### Delete
271338

@@ -274,7 +341,7 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
274341
func (c Client) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error
275342
```
276343

277-
A `client.DeleteOption` is an interface that sets [`client.DeleteOptions`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L156) fields. A `client.DeleteOption` is created by using one of the provided implementations: [`GracePeriodSeconds`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L216), [`Preconditions`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L227), [`PropagationPolicy`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.2.0/pkg/client/options.go#L238).
344+
A `client.DeleteOption` is an interface that sets [`client.DeleteOptions`][delete-opts] fields. A `client.DeleteOption` is created by using one of the provided implementations: [`GracePeriodSeconds`][graceperiodseconds], [`Preconditions`][preconditions], [`PropagationPolicy`][propagationpolicy].
278345

279346
Example:
280347

@@ -305,6 +372,22 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
305372
}
306373
```
307374

375+
[delete-opts]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#DeleteOptions
376+
[graceperiodseconds]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#GracePeriodSeconds
377+
[preconditions]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#Preconditions
378+
[propagationpolicy]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#PropagationPolicy
379+
380+
#### Delete
381+
382+
```Go
383+
// DeleteAllOf deletes all objects of the given type matching the given options.
384+
func (c Client) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error
385+
```
386+
387+
A `client.DeleteAllOfOption` is an interface that sets [`client.DeleteAllOfOptions`][deleteallof-opts] fields. A `client.DeleteAllOfOption` wraps a [`client.ListOption`](#list) and [`client.DeleteOption`](#delete).
388+
389+
[deleteallof-opts]:https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client#DeleteAllOfOptions
390+
308391
### Example usage
309392

310393
```Go

0 commit comments

Comments
 (0)