Kaderma
Kaderma

Reputation: 155

Cannot update status field of the custom resource

I'm building a GO-based operator using Operator-sdk to manage my custom resource called Module which owns a deployment. My _types.go looks like this:

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:subresource:scale:specpath=.spec.workerSettings.replicas,statuspath=.status.replicas,selectorpath=.status.selector
//+kubebuilder:printcolumn:name="Replicas",type="integer",JSONPath=".spec.workerSettings.replicas",description="The number of pods for this Module"
//+kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.readyReplicas",description="The number of ready pods for this Module"
//+kubebuilder:resource:shortName=mod;mods

// Module is the Schema for the Module API
type Module struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`

    Spec   ModuleSpec   `json:"spec,omitempty"`
    Status ModuleStatus `json:"status,omitempty"`
}

// ModuleStatus defines the observed state of Module
type ModuleStatus struct {
    Replicas      int32              `json:"replicas"`
    ReadyReplicas int32              `json:"readyReplicas"`
    Selector      string             `json:"selector"`
    Conditions    []metav1.Condition `json:"conditions"`
}

In reconcile I'm trying to set value of the ReadyReplicas field of my CR, which corresponds to the similar field of the deployment my custom resource (Module) owns.

func (r *ModuleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var mod modulev1.Module
    err := r.Get(ctx, req.NamespacedName, &mod)
    if errors.IsNotFound(err) {
        return ctrl.Result{}, nil
    } else if err != nil {
        return ctrl.Result{Requeue: true}, err
    }

    var deploy appsv1.Deployment
    if created, err := r.createDeployIfNotExists(ctx, &deploy, &mod); err != nil {
        return ctrl.Result{}, err
    } else if created {
        return ctrl.Result{Requeue: true}, nil
    }

    ...
   
    // trying to set status the CR
    mod.Status.ReadyReplicas = deploy.Status.ReadyReplicas
    if r.Client.Status().Update(ctx, &mod); err != nil {
        log.Error(err, "failed to update Module status")
        return ctrl.Result{}, err
    }

    return ctrl.Result{}, nil

But nothing changes.. r.Client.Status().Update(ctx, &mod) performs silently, without any error, but the same costume resource instance comes back to the reconcile with default values of the all status fields.

v1alpha1.ModuleStatus {Replicas: 0, ReadyReplicas: 0, Selector: "", Conditions: []k8s.io/apimachinery/pkg/apis/meta/v1.Condition len: 0, cap: 0, nil}

I checked how the manifest of custom resource looks "on the cluster" and it has no status section at all. Wherein it seems to be generated properly in my CRD:

CRD generated by running "make manifest" command

Please, help me figure out what I'm missing.

Upvotes: 5

Views: 1479

Answers (0)

Related Questions