Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Status for K8s Custom Resource

I'm trying to update the status of a custom resource and I'm unable to figure out why its not working..

Here is the _types.go:

// ScoringServerStatus defines the observed state of ScoringServer
type ScoringServerStatus struct {
    // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
    // Important: Run "make" to regenerate code after modifying this file
    Reason     string             `json:"reason"`
    Message    string             `json:"message"`
    Conditions []metav1.Condition `json:"conditions"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

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

    Spec   ScoringServerSpec   `json:"spec,omitempty"`
    Status ScoringServerStatus `json:"status,omitempty"`
}

I'm trying to set the value of Reason and Message in this status:

if !isProjectAvailable {
        infoMessage = "Unable to find requested project, can't deploy scoring server"
        log.Log.Info(infoMessage)
        statusUpdate := scoringv1.ScoringServerStatus{Reason: "Unable to verify project in Machinify", Message: infoMessage}
        log.Log.Info(statusUpdate.Reason)
        scoringServer.Status = statusUpdate
        if err := r.Status().Update(ctx, scoringServer); err != nil {
            log.Log.Info(err.Error())
        }
        return ctrl.Result{Requeue: false, RequeueAfter: 0}, nil
    }

But nothing changes when I run this. I'm not receiving any errors and if I describe the resource I don't see an updated status...

like image 534
TravelingLex Avatar asked Oct 14 '25 21:10

TravelingLex


2 Answers

i just faced the same issue and browsed for answers and somehow your code gave me a hint.

For me this worked fine:

err := r.Client.Status().Update(ctx, k8sproj)
if err != nil {
  fmt.Printf("Could not update project id: \n%s\n ", err)
}

Notice the Client()...

Hope it helps!

like image 155
Anton Engelhardt Avatar answered Oct 17 '25 10:10

Anton Engelhardt


If you are using kubebuilder, remember to re-apply your CRD yaml file; I just spent 1h debugging this.

❯ k apply -f config/crd/bases/<my.domain_type>.yaml
❯ make run

That did it for me!

like image 26
kilianc Avatar answered Oct 17 '25 11:10

kilianc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!