Skip to content

Commit

Permalink
Doc updates for status writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dymurray committed Dec 5, 2018
1 parent cfda7f2 commit 791ca57
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ For this example replace the generated Controller file `pkg/controller/memcached
The example Controller executes the following reconciliation logic for each `Memcached` CR:
- Create a memcached Deployment if it doesn't exist
- Ensure that the Deployment size is the same as specified by the `Memcached` CR spec
- Update the `Memcached` CR status with the names of the memcached pods
- Update the `Memcached` CR status using the status writer with the names of the memcached pods

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.

Expand Down
37 changes: 36 additions & 1 deletion doc/user/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,41 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
}
```

##### Updating Status Subresource
When updating the status subresource from the client, the StatusWriter must be
used which can be gotten with `Status()`
##### Status
```Go
// Status() returns a StatusWriter object that can be used to update the
// object's status subresource
func (c Client) Status() (client.StatusWriter, error)
```

Example:
```Go
import (
"context"
cachev1alpha1 "github.com/example-inc/memcached-operator/pkg/apis/cache/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, error) {
...

mem := &cachev1alpha.Memcached{}
err := r.client.Get(context.TODO(), request.NamespacedName, mem)

...

ctx := context.TODO()
mem.Status.Nodes = []string{"pod1", "pod2"}
err := r.client.Status().Update(ctx, mem)

...
}
```


#### Delete

```Go
Expand Down Expand Up @@ -385,7 +420,7 @@ func (r *ReconcileApp) Reconcile(request reconcile.Request) (reconcile.Result, e
podNames := getPodNames(podList.Items)
if !reflect.DeepEqual(podNames, app.Status.Nodes) {
app.Status.Nodes = podNames
if err := r.client.Update(context.TODO(), app); err != nil {
if err := r.client.Status().Update(context.TODO(), app); err != nil {
return reconcile.Result{}, err
}
}
Expand Down

0 comments on commit 791ca57

Please sign in to comment.