From 791ca573a6c4fc7580926dde7e9a997285346bd4 Mon Sep 17 00:00:00 2001 From: Dylan Murray Date: Wed, 5 Dec 2018 15:49:14 -0500 Subject: [PATCH] Doc updates for status writer --- doc/user-guide.md | 2 +- doc/user/client.md | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/doc/user-guide.md b/doc/user-guide.md index 585db5c469..e2d9108578 100644 --- a/doc/user-guide.md +++ b/doc/user-guide.md @@ -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. diff --git a/doc/user/client.md b/doc/user/client.md index 7a3d7b9b80..1cc23f1992 100644 --- a/doc/user/client.md +++ b/doc/user/client.md @@ -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 @@ -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 } }