Replies: 3 comments 2 replies
-
To achieve the desired output where the READY column displays as a fraction (e.g., 1/1), you'd need to use the JSONPath feature in kubebuilder:printcolumn. Here's how you can set it up for a single field: // +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".spec.ready" For your desired output, you may have to look into extending kubectl or writing a custom script to fetch and format the resources in your desired way. If the core Kubernetes project or the controller-tools project (which handles the generation of CRD manifests from the kubebuilder annotations) so you might want check the documentation and raise issues for their projects as well as raise questions in the controller-runtime slack channel. I hope that it answered your question. |
Beta Was this translation helpful? Give feedback.
-
Following an example if the above explanation is not clear enough: type FooSpec struct {
Ready int `json:"ready"`
Total int `json:"total"`
}
// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="READY",type=string,JSONPath=`{.spec.ready}/{.spec.total}`
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec FooSpec `json:"spec,omitempty"`
Status FooStatus `json:"status,omitempty"`
} Please, let us know if that is not what you are looking for. If not please, could you please clarify what is missing? |
Beta Was this translation helpful? Give feedback.
-
As noted in #2285 (reply in thread), this seems to not be available yet (the ability to specify a complex expression that uses one or more JSON paths to reference more than one part of the CR). I think I was able to come up with a workaround though. I add a string to my CRD's status that can be used to convey this information. Then, I point the printcolumn annotation to that single field in the status. type: type FooSpec struct {
Total int `json:"total,omitempty"`
}
type FooStatus struct {
Ready string `json:"ready"`
}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="READY",type=string,JSONPath=`.status.ready`
type Foo struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec FooSpec `json:"spec,omitempty"`
Status FooStatus `json:"status,omitempty"`
} controller: func (r *FooReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// Log the request for debugging purposes
log := log.FromContext(ctx)
log.Info("Reconciling Foo", "namespace", req.Namespace, "name", req.Name)
// Fetch the Foo resource
var foo examplesv1.Foo
if err := r.Get(ctx, req.NamespacedName, &foo); err != nil {
if apierrors.IsNotFound(err) {
// Resource not found, nothing to reconcile
log.Info("Foo resource not found, ignoring since object must be deleted")
return ctrl.Result{}, nil
}
// Error reading the object
log.Error(err, "Failed to get Foo resource")
return ctrl.Result{}, err
}
// Update the status - use "1" for the number ready for this test
foo.Status.Ready = fmt.Sprintf("%s/%d", "1", foo.Spec.Total)
// Save the updated status back to the cluster
if err := r.Status().Update(ctx, &foo); err != nil {
log.Error(err, "Failed to update Foo status")
return ctrl.Result{}, err
}
log.Info("Successfully updated Foo status", "namespace", req.Namespace, "name", req.Name)
return ctrl.Result{}, nil
} sample CR: apiVersion: examples.example.com/v1
kind: Foo
metadata:
labels:
app.kubernetes.io/name: kubebuilder-test-dec9
app.kubernetes.io/managed-by: kustomize
name: foo-sample
spec:
total: 2 This causes the string field to be included as is in the status: apiVersion: examples.example.com/v1
kind: Foo
...
status:
ready: 1/2 It's a bit ugly if you don't need to read it that way. But it presumably does no harm being there. If clients don't need it, they can ignore it. But, it looks nice when reported from
One thing I liked about this approach is that it seemed so flexible. While playing around with it, I was able to add a boolean field "Total > 1" and link it to the
I could see that being handy for reporting situations like "all ready" etc. It seems like any data can be conveyed this way, even multiple pieces of data as long as it can be formatted into a string. |
Beta Was this translation helpful? Give feedback.
-
I wander how to set the printcolum to make ready column like this.
Beta Was this translation helpful? Give feedback.
All reactions