Skip to content

Commit 791ca57

Browse files
committed
Doc updates for status writer
1 parent cfda7f2 commit 791ca57

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

doc/user-guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ For this example replace the generated Controller file `pkg/controller/memcached
116116
The example Controller executes the following reconciliation logic for each `Memcached` CR:
117117
- Create a memcached Deployment if it doesn't exist
118118
- Ensure that the Deployment size is the same as specified by the `Memcached` CR spec
119-
- Update the `Memcached` CR status with the names of the memcached pods
119+
- Update the `Memcached` CR status using the status writer with the names of the memcached pods
120120

121121
The next two subsections explain how the Controller watches resources and how the reconcile loop is triggered. Skip to the [Build](#build-and-run-the-operator) section to see how to build and run the operator.
122122

doc/user/client.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,41 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
244244
}
245245
```
246246

247+
##### Updating Status Subresource
248+
When updating the status subresource from the client, the StatusWriter must be
249+
used which can be gotten with `Status()`
250+
##### Status
251+
```Go
252+
// Status() returns a StatusWriter object that can be used to update the
253+
// object's status subresource
254+
func (c Client) Status() (client.StatusWriter, error)
255+
```
256+
257+
Example:
258+
```Go
259+
import (
260+
"context"
261+
cachev1alpha1 "github.com/example-inc/memcached-operator/pkg/apis/cache/v1alpha1"
262+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
263+
)
264+
265+
func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, error) {
266+
...
267+
268+
mem := &cachev1alpha.Memcached{}
269+
err := r.client.Get(context.TODO(), request.NamespacedName, mem)
270+
271+
...
272+
273+
ctx := context.TODO()
274+
mem.Status.Nodes = []string{"pod1", "pod2"}
275+
err := r.client.Status().Update(ctx, mem)
276+
277+
...
278+
}
279+
```
280+
281+
247282
#### Delete
248283

249284
```Go
@@ -385,7 +420,7 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
385420
podNames := getPodNames(podList.Items)
386421
if !reflect.DeepEqual(podNames, app.Status.Nodes) {
387422
app.Status.Nodes = podNames
388-
if err := r.client.Update(context.TODO(), app); err != nil {
423+
if err := r.client.Status().Update(context.TODO(), app); err != nil {
389424
return reconcile.Result{}, err
390425
}
391426
}

0 commit comments

Comments
 (0)