4

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

Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • It's the controller that should write to the Status-part. The client that creates the resource should only write the _desired state_ (spec). – Jonas Sep 01 '22 at 20:28
  • The controller itself can create objects internally based on the sequence of actions it can take. – Vishrant Sep 01 '22 at 21:50
  • yes, but it is in the reconciliation-loop the controller writes the status for the resources that it reconciles. – Jonas Sep 01 '22 at 22:14

1 Answers1

3

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
}
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • Thanks, Jonas, for adding the details, I recognize the k8s operator pattern, I have updated my question with the architecture I am trying to achieve with the operator pattern, when I am syncing (creating) the k8s objects between the k8s clusters, as the status field is not populated in the other cluster that is creating issues. – Vishrant Sep 02 '22 at 05:27
  • in my architecture, I am `creating` the k8s object in another cluster, and that is triggering an event for the operator (which is expected), but as the status field is not there I am unable to judge if the k8s object is coming from another cluster. – Vishrant Sep 02 '22 at 05:30
  • The status of an object can be updated in the `"current"` reconcile loop and not outside (Jonas your explanation says the same thing, thanks). – Vishrant Sep 02 '22 at 18:48