Skip to content

Commit

Permalink
add readyNumber field in CRD status
Browse files Browse the repository at this point in the history
Signed-off-by: hxcGit <[email protected]>
  • Loading branch information
xavier-hou committed Apr 11, 2023
1 parent 35d1aae commit 4b9813f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
4 changes: 2 additions & 2 deletions charts/openyurt/templates/yurt-manager-auto-generated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/apps/v1alpha1/staticpod_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
38 changes: 22 additions & 16 deletions pkg/controller/staticpod/staticpod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 17 additions & 1 deletion pkg/controller/staticpod/upgradeinfo/upgrade_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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
}

0 comments on commit 4b9813f

Please sign in to comment.