gokuberneteskubernetes-operatorkubebuilder

Update status in a custom Kubernetes controller in golang


I am building a Go Kubernetes operator. I have used kubebuilder to create it. I want to store some internal details in the CRD status. I have tried :

if err = r.Client.Update(ctx, upCRD); err != nil {
    return ctrl.Result{}, client.IgnoreNotFound(err)
}
if err = r.Status().Update(ctx, upCRD); err != nil {
    return reconcile.Result{}, client.IgnoreNotFound(err)
}

The status struct is defined as follows :

type HAAuditStatus struct {
    ChaosStrategyCron  cron.EntryID       `json:"chaosStrategyCron,omitempty"`
    TestStatus         TestStatus         `json:"testStatus,omitempty"`
    MetricStatus       MetricStatus       `json:"metricStatus,omitempty"`
    RoundRobinStrategy RoundRobinStrategy `json:"roundRobinStrategy,omitempty"`
    FixedStrategy      FixedStrategy      `json:"fixedStrategy,omitempty"`
    NextChaosDateTime  int64              `json:"nextChaosDateTime,omitempty"`
    Created            bool               `json:"created,default=false"`
}

No error is raised and the specs fields modified are actually persisted but not the status field whose values remain the default at the next reconciling step. I have looked at the other issues on GitHub or StackOverflow but any suggestion made solved my issue and I can't figure out what is the problem. For a a bigger picture, you can refer to the repo where the operator is located.

Any suggestion is very welcomed :)


Solution

  • I might have found the reason why the status were not updated.

    Before updating the status, I was also updating the spec fields (to give some feedback to the user on created resources).

    The issue is caused by the fact that the specs updates trigger a new reconcilation, and the instruction after this update (among them the status update) were not execute.

    I realized that using specs to give feedback to the user is not suitable and the events were more appropriate for this purpose.