Search code examples
gokuberneteskubernetes-go-clientkubernetes-operatorkubernetes-custom-resources

How to create Kubernetes objects with Status fields?


I am using the kubernetes operator to create a custom resource in the cluster, the CR has the Status field populated, but when the object gets created the Status field is empty.

This is how I am creating the CR:

reconcile.Create(ctx, &object)

This is what I am trying to accomplish with k8s operator:

enter image description here


Solution

  • The architecture of Kubernetes API and resources follows a pattern.

    1. Clients may create resources, by specifying a desired state (This is the spec: part of a resource). This is a "create" request sent to the API Server.

    2. Controllers, subscribe/watch to changes of resources, while doing actions in a reconciliation loop, they might update the Status of the resource (this is the status: part of the resource).

    For an example of how a controller is implemented and updates the status, see the Kubebuilder book: Implementing a Controller - Update the Status.

    The client in the example is a "controller runtime client":

    "sigs.k8s.io/controller-runtime/pkg/client"
    

    Example code, where the reconciler updates the status sub-resource:

    if err := r.Status().Update(ctx, &cronJob); err != nil {
        log.Error(err, "unable to update CronJob status")
        return ctrl.Result{}, err
    }