diff --git a/api/v1/devcontainerapp_types.go b/api/v1/devcontainerapp_types.go index 0aac191..50e84ce 100644 --- a/api/v1/devcontainerapp_types.go +++ b/api/v1/devcontainerapp_types.go @@ -98,6 +98,10 @@ type DevcontainerAppStatus struct { // NodePortAssigned 存储 DevcontainerApp CRD调度后集群分配的 NodePort // +optional NodePortAssigned uint16 `json:"nodePortAssigned"` + + // Ready 标识 DevcontainerApp 管理的 Pod 的 Readiness Probe 是否达到就绪状态 + // +optional + Ready bool `json:"ready"` } // +kubebuilder:object:root=true diff --git a/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml b/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml index 41453fa..eb00683 100644 --- a/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml +++ b/config/crd/bases/devcontainer.devstar.cn_devcontainerapps.yaml @@ -153,6 +153,9 @@ spec: nodePortAssigned: description: NodePortAssigned 存储 DevcontainerApp CRD调度后集群分配的 NodePort type: integer + ready: + description: Ready 标识 DevcontainerApp 管理的 Pod 的 Readiness Probe 是否达到就绪状态 + type: boolean type: object type: object served: true diff --git a/internal/controller/devcontainerapp_controller.go b/internal/controller/devcontainerapp_controller.go index 021c142..b4aa976 100644 --- a/internal/controller/devcontainerapp_controller.go +++ b/internal/controller/devcontainerapp_controller.go @@ -86,6 +86,23 @@ func (r *DevcontainerAppReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } } else { + // 若 StatefulSet.Status.readyReplicas 变化,则更新 DevcontainerApp.Status.Ready 域(mark/un-mark) + if statefulSetInNamespace.Status.ReadyReplicas > 0 { + app.Status.Ready = true + if err := r.Status().Update(ctx, app); err != nil { + logger.Error(err, "Failed to update DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready) + return ctrl.Result{}, err + } + logger.Info("DevContainer is now ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas) + } else { + app.Status.Ready = false + if err := r.Status().Update(ctx, app); err != nil { + logger.Error(err, "Failed to un-mark DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready) + return ctrl.Result{}, err + } + logger.Info("DevContainer is NOT ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas) + } + // 这里会反复触发更新 // 原因:在 SetupWithManager方法中,监听了 StatefulSet ,所以只要更新 StatefulSet 就会触发 // 此处更新和 controllerManager 更新 StatefulSet 都会触发更新事件,导致循环触发