From 4b9813f69066acf737476523a539ee2a1e8ed71f Mon Sep 17 00:00:00 2001 From: hxcGit Date: Sat, 8 Apr 2023 14:46:51 +0200 Subject: [PATCH] add readyNumber field in CRD status Signed-off-by: hxcGit --- .../yurt-manager-auto-generated.yaml | 4 +- pkg/apis/apps/v1alpha1/staticpod_types.go | 4 +- .../staticpod/staticpod_controller.go | 38 +++++++++++-------- .../staticpod/upgradeinfo/upgrade_info.go | 18 ++++++++- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/charts/openyurt/templates/yurt-manager-auto-generated.yaml b/charts/openyurt/templates/yurt-manager-auto-generated.yaml index 2626361dbb3..095acc0b686 100644 --- a/charts/openyurt/templates/yurt-manager-auto-generated.yaml +++ b/charts/openyurt/templates/yurt-manager-auto-generated.yaml @@ -527,7 +527,7 @@ spec: jsonPath: .status.totalNumber name: TotalNumber type: integer - - description: The number of static pods that desired to be upgraded + - description: The number of ready static pods jsonPath: .status.readyNumber name: ReadyNumber type: integer @@ -592,7 +592,7 @@ spec: format: int64 type: integer readyNumber: - description: The number of nodes that are running ready static pod. + description: The number of ready static pods. format: int32 type: integer totalNumber: diff --git a/pkg/apis/apps/v1alpha1/staticpod_types.go b/pkg/apis/apps/v1alpha1/staticpod_types.go index bf6bfe04d2b..425df7b852b 100644 --- a/pkg/apis/apps/v1alpha1/staticpod_types.go +++ b/pkg/apis/apps/v1alpha1/staticpod_types.go @@ -66,7 +66,7 @@ type StaticPodStatus struct { // The total number of nodes that are running the static pod. TotalNumber int32 `json:"totalNumber"` - // The number of nodes that are running ready static pod. + // The number of ready static pods. ReadyNumber int32 `json:"readyNumber"` // The number of nodes that are running updated static pod. @@ -84,7 +84,7 @@ type StaticPodStatus struct { // +kubebuilder:resource:shortName=sp // +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp",description="CreationTimestamp is a timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC." //+kubebuilder:printcolumn:name="TotalNumber",type="integer",JSONPath=".status.totalNumber",description="The total number of static pods" -//+kubebuilder:printcolumn:name="ReadyNumber",type="integer",JSONPath=".status.readyNumber",description="The number of static pods that desired to be upgraded" +//+kubebuilder:printcolumn:name="ReadyNumber",type="integer",JSONPath=".status.readyNumber",description="The number of ready static pods" //+kubebuilder:printcolumn:name="UpgradedNumber",type="integer",JSONPath=".status.upgradedNumber",description="The number of static pods that have been upgraded" // StaticPod is the Schema for the staticpods API diff --git a/pkg/controller/staticpod/staticpod_controller.go b/pkg/controller/staticpod/staticpod_controller.go index c458d7eed78..d73fda8916f 100644 --- a/pkg/controller/staticpod/staticpod_controller.go +++ b/pkg/controller/staticpod/staticpod_controller.go @@ -139,18 +139,18 @@ var _ reconcile.Reconciler = &ReconcileStaticPod{} // ReconcileStaticPod reconciles a StaticPod object type ReconcileStaticPod struct { client.Client - scheme *runtime.Scheme - recorder record.EventRecorder - Configration config.StaticPodControllerConfiguration + scheme *runtime.Scheme + recorder record.EventRecorder + Configuration config.StaticPodControllerConfiguration } // newReconciler returns a new reconcile.Reconciler func newReconciler(c *appconfig.CompletedConfig, mgr manager.Manager) reconcile.Reconciler { return &ReconcileStaticPod{ - Client: utilclient.NewClientFromManager(mgr, controllerName), - scheme: mgr.GetScheme(), - recorder: mgr.GetEventRecorderFor(controllerName), - Configration: c.ComponentConfig.StaticPodController, + Client: utilclient.NewClientFromManager(mgr, controllerName), + scheme: mgr.GetScheme(), + recorder: mgr.GetEventRecorderFor(controllerName), + Configuration: c.ComponentConfig.StaticPodController, } } @@ -264,6 +264,10 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ var ( // totalNumber represents the total number of nodes running the target static pod totalNumber int32 + + // readyNumber represents the number of ready static pods + readyNumber int32 + // upgradedNumber represents the number of nodes that have been upgraded upgradedNumber int32 ) @@ -279,7 +283,7 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ // There are no nodes running target static pods in the cluster if totalNumber == 0 { klog.Infof(Format("No static pods need to be upgraded of StaticPod %v", request.NamespacedName)) - return r.updateStaticPodStatus(instance, totalNumber, totalNumber) + return r.updateStaticPodStatus(instance, totalNumber, totalNumber, totalNumber) } // The latest hash value for static pod spec @@ -308,6 +312,8 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ // Count the number of upgraded nodes upgradedNumber = upgradeinfo.SetUpgradeNeededInfos(upgradeInfos, latestHash) + readyNumber = upgradeinfo.ReadyStaticPodsNumber(upgradeInfos) + // Set node ready info if err := checkReadyNodes(r.Client, upgradeInfos); err != nil { klog.Errorf(Format("Fail to check node ready status of StaticPod %v,%v", request.NamespacedName, err)) @@ -361,7 +367,7 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ // Put this here because we need to clean up the worker pods first if totalNumber == upgradedNumber { klog.Infof(Format("All static pods have been upgraded of StaticPod %v", request.NamespacedName)) - return r.updateStaticPodStatus(instance, totalNumber, upgradedNumber) + return r.updateStaticPodStatus(instance, totalNumber, readyNumber, upgradedNumber) } switch instance.Spec.UpgradeStrategy.Type { @@ -370,14 +376,14 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ case appsv1alpha1.AutoStaticPodUpgradeStrategyType: if !allSucceeded { klog.V(5).Infof(Format("Wait last round auto upgrade to finish of StaticPod %v", request.NamespacedName)) - return r.updateStaticPodStatus(instance, totalNumber, upgradedNumber) + return r.updateStaticPodStatus(instance, totalNumber, readyNumber, upgradedNumber) } if err := r.autoUpgrade(instance, upgradeInfos, latestHash); err != nil { klog.Errorf(Format("Fail to auto upgrade of StaticPod %v, %v", request.NamespacedName, err)) return ctrl.Result{}, err } - return r.updateStaticPodStatus(instance, totalNumber, upgradedNumber) + return r.updateStaticPodStatus(instance, totalNumber, readyNumber, upgradedNumber) // OTA Upgrade can help users control the timing of static pods upgrade // It will set PodNeedUpgrade condition and work with YurtHub component @@ -386,7 +392,7 @@ func (r *ReconcileStaticPod) Reconcile(_ context.Context, request reconcile.Requ klog.Errorf(Format("Fail to ota upgrade of StaticPod %v, %v", request.NamespacedName, err)) return ctrl.Result{}, err } - return r.updateStaticPodStatus(instance, totalNumber, upgradedNumber) + return r.updateStaticPodStatus(instance, totalNumber, readyNumber, upgradedNumber) } return ctrl.Result{}, nil @@ -455,7 +461,7 @@ func (r *ReconcileStaticPod) autoUpgrade(instance *appsv1alpha1.StaticPod, infos readyUpgradeWaitingNodes = readyUpgradeWaitingNodes[:max] if err := createUpgradeWorker(r.Client, instance, readyUpgradeWaitingNodes, hash, - string(appsv1alpha1.AutoStaticPodUpgradeStrategyType), r.Configration.UpgradeWorkerImage); err != nil { + string(appsv1alpha1.AutoStaticPodUpgradeStrategyType), r.Configuration.UpgradeWorkerImage); err != nil { return err } return nil @@ -483,7 +489,7 @@ func (r *ReconcileStaticPod) otaUpgrade(instance *appsv1alpha1.StaticPod, infos // Create worker pod to issue the latest manifest to ready node readyUpgradeWaitingNodes := upgradeinfo.OTAReadyUpgradeWaitingNodes(infos, hash) if err := createUpgradeWorker(r.Client, instance, readyUpgradeWaitingNodes, hash, - string(appsv1alpha1.OTAStaticPodUpgradeStrategyType), r.Configration.UpgradeWorkerImage); err != nil { + string(appsv1alpha1.OTAStaticPodUpgradeStrategyType), r.Configuration.UpgradeWorkerImage); err != nil { return err } @@ -571,9 +577,9 @@ func checkReadyNodes(client client.Client, infos map[string]*upgradeinfo.Upgrade } // updateStatus set the status of instance to the given values -func (r *ReconcileStaticPod) updateStaticPodStatus(instance *appsv1alpha1.StaticPod, totalNum, upgradedNum int32) (reconcile.Result, error) { +func (r *ReconcileStaticPod) updateStaticPodStatus(instance *appsv1alpha1.StaticPod, totalNum, readyNum, upgradedNum int32) (reconcile.Result, error) { instance.Status.TotalNumber = totalNum - instance.Status.ReadyNumber = upgradedNum + instance.Status.ReadyNumber = readyNum instance.Status.UpgradedNumber = upgradedNum if err := r.Client.Status().Update(context.TODO(), instance); err != nil { diff --git a/pkg/controller/staticpod/upgradeinfo/upgrade_info.go b/pkg/controller/staticpod/upgradeinfo/upgrade_info.go index 618e702ce24..b8f7e043c99 100644 --- a/pkg/controller/staticpod/upgradeinfo/upgrade_info.go +++ b/pkg/controller/staticpod/upgradeinfo/upgrade_info.go @@ -21,6 +21,7 @@ import ( "strings" corev1 "k8s.io/api/core/v1" + "k8s.io/kubectl/pkg/util/podutils" "sigs.k8s.io/controller-runtime/pkg/client" appsv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1" @@ -173,7 +174,7 @@ func UpgradedNodes(infos map[string]*UpgradeInfo) []string { return nodes } -// SetUpgradeNeededInfo sets `UpgradeNeeded` flag and counts the number of upgraded nodes +// SetUpgradeNeededInfos sets `UpgradeNeeded` flag and counts the number of upgraded nodes func SetUpgradeNeededInfos(infos map[string]*UpgradeInfo, latestHash string) int32 { var upgradedNumber int32 @@ -190,3 +191,18 @@ func SetUpgradeNeededInfos(infos map[string]*UpgradeInfo, latestHash string) int return upgradedNumber } + +// ReadyStaticPodsNumber counts the number of ready static pods +func ReadyStaticPodsNumber(infos map[string]*UpgradeInfo) int32 { + var readyNumber int32 + + for _, info := range infos { + if info.StaticPod != nil { + if podutils.IsPodReady(info.StaticPod) { + readyNumber++ + } + } + } + + return readyNumber +}