From fa6e0db6a6b54650d759d248c2887ffbe0936205 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 3 Jan 2025 15:58:36 +0100 Subject: [PATCH] operational --- .../api/v1alpha1/workload_types.go | 5 +++++ .../config/crd/bases/cozystack.io_workloads.yaml | 8 ++++++++ .../internal/controller/workload_controller.go | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/packages/system/cozystack-workload-controller/api/v1alpha1/workload_types.go b/packages/system/cozystack-workload-controller/api/v1alpha1/workload_types.go index ae55302f..503fc48e 100644 --- a/packages/system/cozystack-workload-controller/api/v1alpha1/workload_types.go +++ b/packages/system/cozystack-workload-controller/api/v1alpha1/workload_types.go @@ -40,6 +40,10 @@ type WorkloadStatus struct { // Resources specifies the compute resources allocated to this workload // +required Resources map[string]resource.Quantity `json:"resources"` + + // Operational indicates if all pods of the workload are ready + // +optional + Operational bool `json:"operational"` } // +kubebuilder:object:root=true @@ -47,6 +51,7 @@ type WorkloadStatus struct { // +kubebuilder:printcolumn:name="Type",type="string",JSONPath=".status.type" // +kubebuilder:printcolumn:name="CPU",type="string",JSONPath=".status.resources.cpu" // +kubebuilder:printcolumn:name="Memory",type="string",JSONPath=".status.resources.memory" +// +kubebuilder:printcolumn:name="Operational",type="boolean",JSONPath=`.status.operational` // Workload is the Schema for the workloads API type Workload struct { diff --git a/packages/system/cozystack-workload-controller/config/crd/bases/cozystack.io_workloads.yaml b/packages/system/cozystack-workload-controller/config/crd/bases/cozystack.io_workloads.yaml index c80fca03..247f6fe3 100644 --- a/packages/system/cozystack-workload-controller/config/crd/bases/cozystack.io_workloads.yaml +++ b/packages/system/cozystack-workload-controller/config/crd/bases/cozystack.io_workloads.yaml @@ -27,6 +27,10 @@ spec: - jsonPath: .status.resources.memory name: Memory type: string + - format: upper + jsonPath: .status.operational + name: Operational + type: boolean name: v1alpha1 schema: openAPIV3Schema: @@ -59,6 +63,10 @@ spec: description: Kind represents the type of workload (redis, postgres, etc.) type: string + operational: + description: Operational indicates if all pods of the workload are + ready + type: boolean resources: additionalProperties: anyOf: diff --git a/packages/system/cozystack-workload-controller/internal/controller/workload_controller.go b/packages/system/cozystack-workload-controller/internal/controller/workload_controller.go index fa7dbf17..9b80c476 100644 --- a/packages/system/cozystack-workload-controller/internal/controller/workload_controller.go +++ b/packages/system/cozystack-workload-controller/internal/controller/workload_controller.go @@ -41,6 +41,15 @@ type WorkloadReconciler struct { // +kubebuilder:rbac:groups=cozystack.io,resources=workloads,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=cozystack.io,resources=workloads/status,verbs=get;update;patch +func (r *WorkloadReconciler) isPodReady(pod *corev1.Pod) bool { + for _, condition := range pod.Status.Conditions { + if condition.Type == corev1.PodReady { + return condition.Status == corev1.ConditionTrue + } + } + return false +} + func (r *WorkloadReconciler) reconcilePod(ctx context.Context, pod *corev1.Pod) (ctrl.Result, error) { logger := log.FromContext(ctx) @@ -99,6 +108,7 @@ func (r *WorkloadReconciler) reconcilePod(ctx context.Context, pod *corev1.Pod) workload.Status.Kind = kind workload.Status.Type = workloadType workload.Status.Resources = resources + workload.Status.Operational = r.isPodReady(pod) return nil })