From 1fdc44b1d914463e14f9e965987242674cc6d79f Mon Sep 17 00:00:00 2001 From: machine3 Date: Mon, 3 Jul 2023 19:13:51 +0800 Subject: [PATCH] Feat: add workload status to rollout status (#145) Signed-off-by: machine3 --- Makefile | 2 +- api/v1alpha1/rollout_types.go | 18 +++++++++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 20 +++++++++++++++++++ .../bases/rollouts.kruise.io_rollouts.yaml | 11 ++++++++++ pkg/controller/rollout/rollout_status.go | 10 ++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 69312295..be630f1e 100644 --- a/Makefile +++ b/Makefile @@ -119,7 +119,7 @@ TMP_DIR=$$(mktemp -d) ;\ cd $$TMP_DIR ;\ go mod init tmp ;\ echo "Downloading $(2)" ;\ -GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ +GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\ rm -rf $$TMP_DIR ;\ } endef diff --git a/api/v1alpha1/rollout_types.go b/api/v1alpha1/rollout_types.go index 4661058c..7574af0a 100644 --- a/api/v1alpha1/rollout_types.go +++ b/api/v1alpha1/rollout_types.go @@ -201,6 +201,9 @@ type RolloutStatus struct { // observedGeneration is the most recent generation observed for this Rollout. ObservedGeneration int64 `json:"observedGeneration,omitempty"` + // WorkloadStatus describes the state of the workload. + // +optional + WorkloadStatus *WorkloadStatus `json:"workloadStatus,omitempty"` // Canary describes the state of the canary rollout // +optional CanaryStatus *CanaryStatus `json:"canaryStatus,omitempty"` @@ -259,6 +262,21 @@ const ( TerminatingReasonCompleted = "Completed" ) +type WorkloadStatus struct { + // WorkloadState is the state of the workload. + WorkloadState WorkloadState `json:"workloadState,omitempty"` + // Replicas is the number of desired replicas. + Replicas int32 `json:"replicas,omitempty"` +} + +type WorkloadState string + +const ( + WorkloadStateNormal WorkloadState = "Normal" + WorkloadStateIgnore WorkloadState = "Ignore" + WorkloadStateNotFound WorkloadState = "NotFound" +) + // CanaryStatus status fields that only pertain to the canary rollout type CanaryStatus struct { // observedWorkloadGeneration is the most recent generation observed for this Rollout ref workload generation. diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index fcf7dc17..7e209a9f 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -720,6 +720,11 @@ func (in *RolloutSpec) DeepCopy() *RolloutSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RolloutStatus) DeepCopyInto(out *RolloutStatus) { *out = *in + if in.WorkloadStatus != nil { + in, out := &in.WorkloadStatus, &out.WorkloadStatus + *out = new(WorkloadStatus) + **out = **in + } if in.CanaryStatus != nil { in, out := &in.CanaryStatus, &out.CanaryStatus *out = new(CanaryStatus) @@ -861,3 +866,18 @@ func (in *WorkloadRef) DeepCopy() *WorkloadRef { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *WorkloadStatus) DeepCopyInto(out *WorkloadStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadStatus. +func (in *WorkloadStatus) DeepCopy() *WorkloadStatus { + if in == nil { + return nil + } + out := new(WorkloadStatus) + in.DeepCopyInto(out) + return out +} diff --git a/config/crd/bases/rollouts.kruise.io_rollouts.yaml b/config/crd/bases/rollouts.kruise.io_rollouts.yaml index 10850a4e..3bf294a6 100644 --- a/config/crd/bases/rollouts.kruise.io_rollouts.yaml +++ b/config/crd/bases/rollouts.kruise.io_rollouts.yaml @@ -374,6 +374,17 @@ spec: description: BlueGreenStatus *BlueGreenStatus `json:"blueGreenStatus,omitempty"` Phase is the rollout phase. type: string + workloadStatus: + description: WorkloadStatus describes the state of the workload. + properties: + replicas: + description: Replicas is the number of desired replicas. + format: int32 + type: integer + workloadState: + description: WorkloadState is the state of the workload. + type: string + type: object type: object type: object served: true diff --git a/pkg/controller/rollout/rollout_status.go b/pkg/controller/rollout/rollout_status.go index bf62ccf1..cf0a53a5 100644 --- a/pkg/controller/rollout/rollout_status.go +++ b/pkg/controller/rollout/rollout_status.go @@ -62,6 +62,9 @@ func (r *RolloutReconciler) calculateRolloutStatus(rollout *v1alpha1.Rollout) (r ObservedGeneration: rollout.Generation, Phase: v1alpha1.RolloutPhaseInitial, Message: "Workload Not Found", + WorkloadStatus: &v1alpha1.WorkloadStatus{ + WorkloadState: v1alpha1.WorkloadStateNotFound, + }, } klog.Infof("rollout(%s/%s) workload not found, and reset status be Initial", rollout.Namespace, rollout.Name) return false, newStatus, nil @@ -72,6 +75,13 @@ func (r *RolloutReconciler) calculateRolloutStatus(rollout *v1alpha1.Rollout) (r return true, nil, nil } + // update workload status + if newStatus.WorkloadStatus == nil { + newStatus.WorkloadStatus = &v1alpha1.WorkloadStatus{} + } + newStatus.WorkloadStatus.WorkloadState = v1alpha1.WorkloadStateNormal + newStatus.WorkloadStatus.Replicas = workload.Replicas + // update workload generation to canaryStatus.ObservedWorkloadGeneration // rollout is a target ref bypass, so there needs to be a field to identify the rollout execution process or results, // which version of deployment is targeted, ObservedWorkloadGeneration that is to compare with the workload generation