DEV Community

Cover image for K8s Operator - Manage status
Maxime Guilbert
Maxime Guilbert

Posted on

K8s Operator - Manage status

In this serie dedicated to Kubernetes operators, we will see today some that can be really useful, especially for debugging or to know the status of custom resources.

What is the objective of the status?

Like any other Kubernetes resources, a resource status gives us some informations about its current state. It can be represented by a lot of things from a boolean to express if everything goes right, or the name of each generated sub resource.

Status structure definition

Status must have its structure defined before being filled. This structure is defined in files api/../xxx_types.go.


type MyProxyStatus struct {  
Enter fullscreen mode Exit fullscreen mode

Completly empty at the begining, you can add there what ever you want/need!

In our example, we will complete what was done previously (a resource MyProxy which deploys a nginx deployment with 2 pods), and we will list all the pods name generated by the nginx deployment.

type MyProxyStatus struct {  
   PodNames []string `json:"pod_names"`  
Enter fullscreen mode Exit fullscreen mode

As we update a file in the api folder, don't forget to execute make manifests & make generate !

Fill the status

Now that we have the structure of our resource, we can go to our controller, more precisely in the Reconcile function.

For our example, the first thing to do is trying to get the list of pods related to our deployment.

podList := &corev1.PodList{}  
listOpts := []client.ListOption{  
      "test_label": myProxy.Spec.Name,  
if err = r.List(ctx, podList, listOpts...); err != nil {  
   log.Error(err, "Failed to list pods", "Example.Namespace", "test_ns")  
   return ctrl.Result{}, err  
podNames := getPodNames(podList.Items)
Enter fullscreen mode Exit fullscreen mode

As you can see, to retrieve a list of items we use filters. In our case, we use two of them:

  • the namespace's name where pods should be deployed
  • a label that pods must have Depending if you are looking for something precise or not, you can update this list of filters to find what you are looking for.

Then, we can just update the myProxy instance like we updated its spec.

if !reflect.DeepEqual(podNames, myProxy.Status.PodNames) {
    myProxy.Status.PodNames = podNames
    err := r.Status().Update(ctx, myProxy)
    if err != nil {
        log.Error(err, "Failed to update MyProxy status")
        return ctrl.Result{}, err
Enter fullscreen mode Exit fullscreen mode

And that's it! You are now able to manage the status of a custom resource!

In the next post of this serie, we will see how to complete this status part with conditions!

I hope it will help you and if you have any questions (there are not dumb questions) or some points are not clear for you, don't hesitate to add your question in the comments or to contact me directly on LinkedIn.

You want to support me?

Buy Me A Coffee

Top comments (0)