diff --git a/api/v1alpha1/batchrelease_plan_types.go b/api/v1alpha1/batchrelease_plan_types.go index 0cf37e8c..7514b3cb 100644 --- a/api/v1alpha1/batchrelease_plan_types.go +++ b/api/v1alpha1/batchrelease_plan_types.go @@ -54,6 +54,8 @@ type ReleasePlan struct { // only support for canary deployment // +optional PatchPodTemplateMetadata *PatchPodTemplateMetadata `json:"patchPodTemplateMetadata,omitempty"` + // RollingStyle can be "Canary", "Partiton" or "BlueGreen" + RollingStyle RollingStyleType `json:"rollingStyle,omitempty"` } type FinalizingPolicyType string diff --git a/api/v1alpha1/conversion.go b/api/v1alpha1/conversion.go index 5164c1ca..5ac77233 100644 --- a/api/v1alpha1/conversion.go +++ b/api/v1alpha1/conversion.go @@ -104,18 +104,20 @@ func (src *Rollout) ConvertTo(dst conversion.Hub) error { return nil } obj.Status.CanaryStatus = &v1beta1.CanaryStatus{ - ObservedWorkloadGeneration: src.Status.CanaryStatus.ObservedWorkloadGeneration, - ObservedRolloutID: src.Status.CanaryStatus.ObservedRolloutID, - RolloutHash: src.Status.CanaryStatus.RolloutHash, - StableRevision: src.Status.CanaryStatus.StableRevision, - CanaryRevision: src.Status.CanaryStatus.CanaryRevision, - PodTemplateHash: src.Status.CanaryStatus.PodTemplateHash, - CanaryReplicas: src.Status.CanaryStatus.CanaryReplicas, - CanaryReadyReplicas: src.Status.CanaryStatus.CanaryReadyReplicas, - CurrentStepIndex: src.Status.CanaryStatus.CurrentStepIndex, - CurrentStepState: v1beta1.CanaryStepState(src.Status.CanaryStatus.CurrentStepState), - Message: src.Status.CanaryStatus.Message, - LastUpdateTime: src.Status.CanaryStatus.LastUpdateTime, + CommonStatus: v1beta1.CommonStatus{ + ObservedWorkloadGeneration: src.Status.CanaryStatus.ObservedWorkloadGeneration, + ObservedRolloutID: src.Status.CanaryStatus.ObservedRolloutID, + RolloutHash: src.Status.CanaryStatus.RolloutHash, + StableRevision: src.Status.CanaryStatus.StableRevision, + PodTemplateHash: src.Status.CanaryStatus.PodTemplateHash, + CurrentStepIndex: src.Status.CanaryStatus.CurrentStepIndex, + CurrentStepState: v1beta1.CanaryStepState(src.Status.CanaryStatus.CurrentStepState), + Message: src.Status.CanaryStatus.Message, + LastUpdateTime: src.Status.CanaryStatus.LastUpdateTime, + }, + CanaryRevision: src.Status.CanaryStatus.CanaryRevision, + CanaryReplicas: src.Status.CanaryStatus.CanaryReplicas, + CanaryReadyReplicas: src.Status.CanaryStatus.CanaryReadyReplicas, } return nil default: @@ -167,7 +169,9 @@ func (dst *Rollout) ConvertFrom(src conversion.Hub) error { case *v1beta1.Rollout: srcV1beta1 := src.(*v1beta1.Rollout) dst.ObjectMeta = srcV1beta1.ObjectMeta - + if !srcV1beta1.Spec.Strategy.IsCanaryStragegy() { + return fmt.Errorf("v1beta1 Rollout with %s strategy cannot be converted to v1alpha1", srcV1beta1.Spec.Strategy.GetRollingStyle()) + } // spec dst.Spec = RolloutSpec{ ObjectRef: ObjectRef{ @@ -338,8 +342,15 @@ func (src *BatchRelease) ConvertTo(dst conversion.Hub) error { obj.Spec.ReleasePlan.PatchPodTemplateMetadata.Labels[k] = v } } - if !strings.EqualFold(src.Annotations[RolloutStyleAnnotation], string(PartitionRollingStyle)) { - obj.Spec.ReleasePlan.EnableExtraWorkloadForCanary = true + + if strings.EqualFold(src.Annotations[RolloutStyleAnnotation], string(PartitionRollingStyle)) { + obj.Spec.ReleasePlan.RollingStyle = v1beta1.PartitionRollingStyle + } + if strings.EqualFold(src.Annotations[RolloutStyleAnnotation], string(CanaryRollingStyle)) { + obj.Spec.ReleasePlan.RollingStyle = v1beta1.CanaryRollingStyle + } + if strings.EqualFold(src.Annotations[RolloutStyleAnnotation], string(BlueGreenRollingStyle)) { + obj.Spec.ReleasePlan.RollingStyle = v1beta1.BlueGreenRollingStyle } // status @@ -417,11 +428,13 @@ func (dst *BatchRelease) ConvertFrom(src conversion.Hub) error { if dst.Annotations == nil { dst.Annotations = map[string]string{} } - if srcV1beta1.Spec.ReleasePlan.EnableExtraWorkloadForCanary { - dst.Annotations[RolloutStyleAnnotation] = strings.ToLower(string(CanaryRollingStyle)) - } else { - dst.Annotations[RolloutStyleAnnotation] = strings.ToLower(string(PartitionRollingStyle)) - } + dst.Annotations[RolloutStyleAnnotation] = strings.ToLower(string(srcV1beta1.Spec.ReleasePlan.RollingStyle)) + dst.Spec.ReleasePlan.RollingStyle = RollingStyleType(srcV1beta1.Spec.ReleasePlan.RollingStyle) + // if srcV1beta1.Spec.ReleasePlan.EnableExtraWorkloadForCanary { + // dst.Annotations[RolloutStyleAnnotation] = strings.ToLower(string(CanaryRollingStyle)) + // } else { + // dst.Annotations[RolloutStyleAnnotation] = strings.ToLower(string(PartitionRollingStyle)) + // } // status dst.Status = BatchReleaseStatus{ diff --git a/api/v1alpha1/deployment_types.go b/api/v1alpha1/deployment_types.go index 35202e5b..99f95cf7 100644 --- a/api/v1alpha1/deployment_types.go +++ b/api/v1alpha1/deployment_types.go @@ -59,6 +59,10 @@ const ( PartitionRollingStyle RollingStyleType = "Partition" // CanaryRollingStyle means rolling in canary way, and will create a canary Deployment. CanaryRollingStyle RollingStyleType = "Canary" + // BlueGreenRollingStyle means rolling in blue-green way, and will NOT create a canary Deployment. + BlueGreenRollingStyle RollingStyleType = "BlueGreen" + // Empty means both Canary and BlueGreen are empty + EmptyRollingStyle RollingStyleType = "Empty" ) // DeploymentExtraStatus is extra status field for Advanced Deployment @@ -74,7 +78,7 @@ type DeploymentExtraStatus struct { } func SetDefaultDeploymentStrategy(strategy *DeploymentStrategy) { - if strategy.RollingStyle == CanaryRollingStyle { + if strategy.RollingStyle != PartitionRollingStyle { return } if strategy.RollingUpdate == nil { diff --git a/api/v1alpha1/rollout_types.go b/api/v1alpha1/rollout_types.go index 697287d8..07d7f201 100644 --- a/api/v1alpha1/rollout_types.go +++ b/api/v1alpha1/rollout_types.go @@ -242,16 +242,23 @@ type CanaryStatus struct { CanaryReplicas int32 `json:"canaryReplicas"` // CanaryReadyReplicas the numbers of ready canary revision pods CanaryReadyReplicas int32 `json:"canaryReadyReplicas"` - // CurrentStepIndex defines the current step of the rollout is on. If the current step index is null, the - // controller will execute the rollout. + // NextStepIndex defines the next step of the rollout is on. + // In normal case, NextStepIndex is equal to CurrentStepIndex + 1 + // If the current step is the last step, NextStepIndex is equal to 0 + // Before the release, NextStepIndex is also equal to 0 + // It is allowed to modify NextStepIndex by design, + // e.g. if CurrentStepIndex is 2, user can patch NextStepIndex to 3 (if exists) to + // achieve batch jump, or patch NextStepIndex to 1 to implement a re-execution of step 1 // +optional - CurrentStepIndex int32 `json:"currentStepIndex"` - CurrentStepState CanaryStepState `json:"currentStepState"` - Message string `json:"message,omitempty"` - LastUpdateTime *metav1.Time `json:"lastUpdateTime,omitempty"` + CurrentStepIndex int32 `json:"currentStepIndex"` + CurrentStepState CanaryStepState `json:"currentStepState"` + Message string `json:"message,omitempty"` + LastUpdateTime *metav1.Time `json:"lastUpdateTime,omitempty"` + FinalisingStep FinalizeStateType `json:"finalisingStep"` } type CanaryStepState string +type FinalizeStateType string const ( CanaryStepStateUpgrade CanaryStepState = "StepUpgrade" diff --git a/api/v1beta1/batchrelease_plan_types.go b/api/v1beta1/batchrelease_plan_types.go index 8b3c21a2..bf1940be 100644 --- a/api/v1beta1/batchrelease_plan_types.go +++ b/api/v1beta1/batchrelease_plan_types.go @@ -54,10 +54,8 @@ type ReleasePlan struct { // only support for canary deployment // +optional PatchPodTemplateMetadata *PatchPodTemplateMetadata `json:"patchPodTemplateMetadata,omitempty"` - // If true, then it will create new deployment for canary, such as: workload-demo-canary. - // When user verifies that the canary version is ready, we will remove the canary deployment and release the deployment workload-demo in full. - // Current only support k8s native deployment - EnableExtraWorkloadForCanary bool `json:"enableExtraWorkloadForCanary"` + // RollingStyle can be "Canary", "Partiton" or "BlueGreen" + RollingStyle RollingStyleType `json:"rollingStyle,omitempty"` } type FinalizingPolicyType string diff --git a/api/v1beta1/deployment_types.go b/api/v1beta1/deployment_types.go index 3db56143..d36f6e9a 100644 --- a/api/v1beta1/deployment_types.go +++ b/api/v1beta1/deployment_types.go @@ -37,6 +37,16 @@ const ( // AdvancedDeploymentControlLabel is label for deployment, // which labels whether the deployment is controlled by advanced-deployment-controller. AdvancedDeploymentControlLabel = "rollouts.kruise.io/controlled-by-advanced-deployment-controller" + + // OriginalSettingAnnotation is annotation for workload in BlueGreen Release, + // it will storage the original setting of the workload, which will be used to restore the workload + OriginalSettingAnnotation = "rollouts.kruise.io/original-setting" + + // MaxProgressSeconds is the value we set for ProgressDeadlineSeconds + // MaxReadySeconds is the value we set for MinReadySeconds, which is one less than ProgressDeadlineSeconds + // MaxInt32: 2147483647, ≈ 68 years + MaxProgressSeconds = 1<<31 - 1 + MaxReadySeconds = MaxProgressSeconds - 1 ) // DeploymentStrategy is strategy field for Advanced Deployment @@ -52,6 +62,31 @@ type DeploymentStrategy struct { Partition intstr.IntOrString `json:"partition,omitempty"` } +// OriginalSetting storages part of the fileds of a workload, +// so that it can be restored when finalizing. +// It is only used for BlueGreen Release +// Similar to DeploymentStrategy, it is stored in workload annotation +// However, unlike DeploymentStrategy, it is only used to storage and restore +type OriginalSetting struct { + // The deployment strategy to use to replace existing pods with new ones. + // +optional + // +patchStrategy=retainKeys + Strategy *apps.DeploymentStrategy `json:"strategy,omitempty" patchStrategy:"retainKeys" protobuf:"bytes,4,opt,name=strategy"` + + // Minimum number of seconds for which a newly created pod should be ready + // without any of its container crashing, for it to be considered available. + // Defaults to 0 (pod will be considered available as soon as it is ready) + // +optional + MinReadySeconds int32 `json:"minReadySeconds,omitempty" protobuf:"varint,5,opt,name=minReadySeconds"` + + // The maximum time in seconds for a deployment to make progress before it + // is considered to be failed. The deployment controller will continue to + // process failed deployments and a condition with a ProgressDeadlineExceeded + // reason will be surfaced in the deployment status. Note that progress will + // not be estimated during the time a deployment is paused. Defaults to 600s. + ProgressDeadlineSeconds *int32 `json:"progressDeadlineSeconds,omitempty" protobuf:"varint,9,opt,name=progressDeadlineSeconds"` +} + type RollingStyleType string const ( @@ -59,6 +94,10 @@ const ( PartitionRollingStyle RollingStyleType = "Partition" // CanaryRollingStyle means rolling in canary way, and will create a canary Deployment. CanaryRollingStyle RollingStyleType = "Canary" + // BlueGreenRollingStyle means rolling in blue-green way, and will NOT create a extra Deployment. + BlueGreenRollingStyle RollingStyleType = "BlueGreen" + // Empty means both Canary and BlueGreen are empty + EmptyRollingStyle RollingStyleType = "Empty" ) // DeploymentExtraStatus is extra status field for Advanced Deployment @@ -74,7 +113,7 @@ type DeploymentExtraStatus struct { } func SetDefaultDeploymentStrategy(strategy *DeploymentStrategy) { - if strategy.RollingStyle == CanaryRollingStyle { + if strategy.RollingStyle != PartitionRollingStyle { return } if strategy.RollingUpdate == nil { @@ -101,3 +140,44 @@ func SetDefaultDeploymentStrategy(strategy *DeploymentStrategy) { } } } + +func SetDefaultSetting(setting *OriginalSetting) { + if setting.ProgressDeadlineSeconds == nil { + setting.ProgressDeadlineSeconds = new(int32) + *setting.ProgressDeadlineSeconds = 600 + } + if setting.Strategy == nil { + setting.Strategy = &apps.DeploymentStrategy{} + } + if setting.Strategy.Type == "" { + setting.Strategy.Type = apps.RollingUpdateDeploymentStrategyType + } + if setting.Strategy.Type == apps.RecreateDeploymentStrategyType { + return + } + strategy := setting.Strategy + if strategy.RollingUpdate == nil { + strategy.RollingUpdate = &apps.RollingUpdateDeployment{} + } + if strategy.RollingUpdate.MaxUnavailable == nil { + // Set MaxUnavailable as 25% by default + maxUnavailable := intstr.FromString("25%") + strategy.RollingUpdate.MaxUnavailable = &maxUnavailable + } + if strategy.RollingUpdate.MaxSurge == nil { + // Set MaxSurge as 25% by default + maxSurge := intstr.FromString("25%") + strategy.RollingUpdate.MaxUnavailable = &maxSurge + } + + // Cannot allow maxSurge==0 && MaxUnavailable==0, otherwise, no pod can be updated when rolling update. + maxSurge, _ := intstr.GetScaledValueFromIntOrPercent(strategy.RollingUpdate.MaxSurge, 100, true) + maxUnavailable, _ := intstr.GetScaledValueFromIntOrPercent(strategy.RollingUpdate.MaxUnavailable, 100, true) + if maxSurge == 0 && maxUnavailable == 0 { + strategy.RollingUpdate = &apps.RollingUpdateDeployment{ + MaxSurge: &intstr.IntOrString{Type: intstr.Int, IntVal: 0}, + MaxUnavailable: &intstr.IntOrString{Type: intstr.Int, IntVal: 1}, + } + } + +} diff --git a/api/v1beta1/rollout_types.go b/api/v1beta1/rollout_types.go index 25c91106..cf058244 100644 --- a/api/v1beta1/rollout_types.go +++ b/api/v1beta1/rollout_types.go @@ -75,6 +75,100 @@ type RolloutStrategy struct { Paused bool `json:"paused,omitempty"` // +optional Canary *CanaryStrategy `json:"canary,omitempty"` + // +optional + BlueGreen *BlueGreenStrategy `json:"blueGreen,omitempty" protobuf:"bytes,1,opt,name=blueGreen"` +} + +// Get the rolling style based on the strategy +func (r *RolloutStrategy) GetRollingStyle() RollingStyleType { + if r.BlueGreen == nil && r.Canary == nil { + return EmptyRollingStyle + } + if r.BlueGreen != nil { + return BlueGreenRollingStyle + } + //NOTE - even EnableExtraWorkloadForCanary is true, as long as it is not Deployment, + //we won't do canary release. BatchRelease will treat it as Partiton release + if r.Canary.EnableExtraWorkloadForCanary { + return CanaryRollingStyle + } + return PartitionRollingStyle +} + +// r.GetRollingStyle() == BlueGreenRollingStyle +func (r *RolloutStrategy) IsBlueGreenRelease() bool { + return r.GetRollingStyle() == BlueGreenRollingStyle +} + +// r.GetRollingStyle() == CanaryRollingStyle || r.GetRollingStyle() == PartitionRollingStyle +func (r *RolloutStrategy) IsCanaryStragegy() bool { + return r.GetRollingStyle() == CanaryRollingStyle || r.GetRollingStyle() == PartitionRollingStyle +} + +// r.GetRollingStyle() == EmptyRollingStyle +func (r *RolloutStrategy) IsEmptyRelease() bool { + return r.GetRollingStyle() == EmptyRollingStyle +} + +// Get the steps based on the rolling style +func (r *RolloutStrategy) GetSteps() []CanaryStep { + switch r.GetRollingStyle() { + case BlueGreenRollingStyle: + return r.BlueGreen.Steps + case CanaryRollingStyle, PartitionRollingStyle: + return r.Canary.Steps + default: + return nil + } +} + +// Get the traffic routing based on the rolling style +func (r *RolloutStrategy) GetTrafficRouting() []TrafficRoutingRef { + switch r.GetRollingStyle() { + case BlueGreenRollingStyle: + return r.BlueGreen.TrafficRoutings + case CanaryRollingStyle, PartitionRollingStyle: + return r.Canary.TrafficRoutings + default: + return nil + } +} + +// Check if there are traffic routings +func (r *RolloutStrategy) HasTrafficRoutings() bool { + return len(r.GetTrafficRouting()) > 0 +} + +// Check the value of DisableGenerateCanaryService +func (r *RolloutStrategy) DisableGenerateCanaryService() bool { + switch r.GetRollingStyle() { + case BlueGreenRollingStyle: + return r.BlueGreen.DisableGenerateCanaryService + case CanaryRollingStyle, PartitionRollingStyle: + return r.Canary.DisableGenerateCanaryService + default: + return false + } +} + +// BlueGreenStrategy defines parameters for Blue Green Release +type BlueGreenStrategy struct { + // Steps define the order of phases to execute release in batches(20%, 40%, 60%, 80%, 100%) + // +optional + Steps []CanaryStep `json:"steps,omitempty"` + // TrafficRoutings support ingress, gateway api and custom network resource(e.g. istio, apisix) to enable more fine-grained traffic routing + // and current only support one TrafficRouting + TrafficRoutings []TrafficRoutingRef `json:"trafficRoutings,omitempty"` + // FailureThreshold indicates how many failed pods can be tolerated in all upgraded pods. + // Only when FailureThreshold are satisfied, Rollout can enter ready state. + // If FailureThreshold is nil, Rollout will use the MaxUnavailable of workload as its + // FailureThreshold. + // Defaults to nil. + FailureThreshold *intstr.IntOrString `json:"failureThreshold,omitempty"` + // TrafficRoutingRef is TrafficRouting's Name + TrafficRoutingRef string `json:"trafficRoutingRef,omitempty"` + // canary service will not be generated if DisableGenerateCanaryService is true + DisableGenerateCanaryService bool `json:"disableGenerateCanaryService,omitempty"` } // CanaryStrategy defines parameters for a Replica Based Canary @@ -178,6 +272,9 @@ type RolloutStatus struct { // Canary describes the state of the canary rollout // +optional CanaryStatus *CanaryStatus `json:"canaryStatus,omitempty"` + // BlueGreen describes the state of the blueGreen rollout + // +optional + BlueGreenStatus *BlueGreenStatus `json:"blueGreenStatus,omitempty"` // Conditions a list of conditions a rollout can have. // +optional Conditions []RolloutCondition `json:"conditions,omitempty"` @@ -231,10 +328,22 @@ const ( // Terminating Reason TerminatingReasonInTerminating = "InTerminating" TerminatingReasonCompleted = "Completed" + + // Finalise Reason + FinaliseReasonSuccess = "Success" + FinaliseReasonRollback = "Rollback" + FinaliseReasonContinuous = "Continuous" + FinaliseReasonStepClear = "StepClear" + FinaliseReasonDisalbed = "RolloutDisabled" + FinaliseReasonDelete = "RolloutDeleting" ) -// CanaryStatus status fields that only pertain to the canary rollout -type CanaryStatus struct { +// fields in CommonStatus are shared between canary status and bluegreen status +// if a field is accessed in strategy-agnostic way, e.g. accessed from rollout_progressing.go, or rollout_status.go +// then it can be put into CommonStatus +// if a field is only accessed in strategy-specific way, e.g. accessed from rollout_canary.go or rollout_bluegreen.go +// then it should stay behind with CanaryStatus or BlueGreenStatus +type CommonStatus struct { // observedWorkloadGeneration is the most recent generation observed for this Rollout ref workload generation. ObservedWorkloadGeneration int64 `json:"observedWorkloadGeneration,omitempty"` // ObservedRolloutID will record the newest spec.RolloutID if status.canaryRevision equals to workload.updateRevision @@ -243,27 +352,125 @@ type CanaryStatus struct { RolloutHash string `json:"rolloutHash,omitempty"` // StableRevision indicates the revision of stable pods StableRevision string `json:"stableRevision,omitempty"` - // CanaryRevision is calculated by rollout based on podTemplateHash, and the internal logic flow uses - // It may be different from rs podTemplateHash in different k8s versions, so it cannot be used as service selector label - CanaryRevision string `json:"canaryRevision"` // pod template hash is used as service selector label PodTemplateHash string `json:"podTemplateHash"` - // CanaryReplicas the numbers of canary revision pods - CanaryReplicas int32 `json:"canaryReplicas"` - // CanaryReadyReplicas the numbers of ready canary revision pods - CanaryReadyReplicas int32 `json:"canaryReadyReplicas"` // CurrentStepIndex defines the current step of the rollout is on. If the current step index is null, the // controller will execute the rollout. // +optional - CurrentStepIndex int32 `json:"currentStepIndex"` + CurrentStepIndex int32 `json:"currentStepIndex"` + // NextStepIndex defines the next step of the rollout is on. + // In normal case, NextStepIndex is equal to CurrentStepIndex + 1 + // If the current step is the last step, NextStepIndex is equal to 0 + // Before the release, NextStepIndex is also equal to 0 + // It is allowed to modify NextStepIndex by design, + // e.g. if CurrentStepIndex is 2, user can patch NextStepIndex to 3 (if exists) to + // achieve batch jump, or patch NextStepIndex to 1 to implement a re-execution of step 1 + NextStepIndex int32 `json:"nextStepIndex"` CurrentStepState CanaryStepState `json:"currentStepState"` Message string `json:"message,omitempty"` LastUpdateTime *metav1.Time `json:"lastUpdateTime,omitempty"` } +// CanaryStatus status fields that only pertain to the canary rollout +type CanaryStatus struct { + // must be inline + CommonStatus `json:",inline"` + // CanaryRevision is calculated by rollout based on podTemplateHash, and the internal logic flow uses + // It may be different from rs podTemplateHash in different k8s versions, so it cannot be used as service selector label + CanaryRevision string `json:"canaryRevision"` + // CanaryReplicas the numbers of canary revision pods + CanaryReplicas int32 `json:"canaryReplicas"` + // CanaryReadyReplicas the numbers of ready canary revision pods + CanaryReadyReplicas int32 `json:"canaryReadyReplicas"` + // FinalisingStep the step of finalising + FinalisingStep FinalisingStepType `json:"finalisingStep"` +} + +// BlueGreenStatus status fields that only pertain to the blueGreen rollout +type BlueGreenStatus struct { + CommonStatus `json:",inline"` + // CanaryRevision is calculated by rollout based on podTemplateHash, and the internal logic flow uses + // It may be different from rs podTemplateHash in different k8s versions, so it cannot be used as service selector label + CanaryRevision string `json:"updatedRevision"` + // CanaryReplicas the numbers of canary revision pods + CanaryReplicas int32 `json:"updatedReplicas"` + // CanaryReadyReplicas the numbers of ready canary revision pods + CanaryReadyReplicas int32 `json:"updatedReadyReplicas"` + // FinalisingStep the step of finalising + FinalisingStep FinalisingStepType `json:"finalisingStep"` +} + +// GetSubStatus returns the ethier canary or bluegreen status +func (r *RolloutStatus) GetSubStatus() *CommonStatus { + if r.CanaryStatus != nil { + return &(r.CanaryStatus.CommonStatus) + } + return &(r.BlueGreenStatus.CommonStatus) +} + +func (r *RolloutStatus) IsSubStatusEmpty() bool { + return r.CanaryStatus == nil && r.BlueGreenStatus == nil +} + +func (r *RolloutStatus) Clear() { + r.CanaryStatus = nil + r.BlueGreenStatus = nil +} + +//TODO - the following functions seem awkward, is there better way for our case? + +func (r *RolloutStatus) GetCanaryRevision() string { + if r.CanaryStatus != nil { + return r.CanaryStatus.CanaryRevision + } + return r.BlueGreenStatus.CanaryRevision +} + +func (r *RolloutStatus) SetCanaryRevision(revision string) { + if r.CanaryStatus != nil { + r.CanaryStatus.CanaryRevision = revision + } + if r.BlueGreenStatus != nil { + r.BlueGreenStatus.CanaryRevision = revision + } +} + +func (r *RolloutStatus) GetCanaryReplicas() int32 { + if r.CanaryStatus != nil { + return r.CanaryStatus.CanaryReplicas + } + return r.BlueGreenStatus.CanaryReplicas +} + +func (r *RolloutStatus) SetCanaryReplicas(replicas int32) { + if r.CanaryStatus != nil { + r.CanaryStatus.CanaryReplicas = replicas + } + if r.BlueGreenStatus != nil { + r.BlueGreenStatus.CanaryReplicas = replicas + } +} + +func (r *RolloutStatus) GetCanaryReadyReplicas() int32 { + if r.CanaryStatus != nil { + return r.CanaryStatus.CanaryReadyReplicas + } + return r.BlueGreenStatus.CanaryReadyReplicas +} + +func (r *RolloutStatus) SetCanaryReadyReplicas(replicas int32) { + if r.CanaryStatus != nil { + r.CanaryStatus.CanaryReadyReplicas = replicas + } + if r.BlueGreenStatus != nil { + r.BlueGreenStatus.CanaryReadyReplicas = replicas + } +} + type CanaryStepState string const ( + CanaryStepStateInit CanaryStepState = "BeforeStepUpgrade" CanaryStepStateUpgrade CanaryStepState = "StepUpgrade" CanaryStepStateTrafficRouting CanaryStepState = "StepTrafficRouting" CanaryStepStateMetricsAnalysis CanaryStepState = "StepMetricsAnalysis" @@ -290,6 +497,17 @@ const ( RolloutPhaseDisabling RolloutPhase = "Disabling" ) +type FinalisingStepType string + +const ( + FinalisingStepTypePreparing FinalisingStepType = "Preparing" + FinalisingStepTypeBatchRelease FinalisingStepType = "PatchBatchRelease" + FinalisingStepTypeStableService FinalisingStepType = "RestoreStableService" + FinalisingStepTypeGateway FinalisingStepType = "RestoreGateway" + FinalisingStepTypeCanaryService FinalisingStepType = "DeleteCanayService" + FinalisingStepTypeDeleteBR FinalisingStepType = "DeleteBatchRelease" +) + // +genclient //+kubebuilder:object:root=true //+kubebuilder:subresource:status diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index 408b544f..528ed99d 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -157,12 +157,59 @@ func (in *BatchReleaseStatus) DeepCopy() *BatchReleaseStatus { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CanaryStatus) DeepCopyInto(out *CanaryStatus) { +func (in *BlueGreenStatus) DeepCopyInto(out *BlueGreenStatus) { *out = *in - if in.LastUpdateTime != nil { - in, out := &in.LastUpdateTime, &out.LastUpdateTime - *out = (*in).DeepCopy() + in.CommonStatus.DeepCopyInto(&out.CommonStatus) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BlueGreenStatus. +func (in *BlueGreenStatus) DeepCopy() *BlueGreenStatus { + if in == nil { + return nil + } + out := new(BlueGreenStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *BlueGreenStrategy) DeepCopyInto(out *BlueGreenStrategy) { + *out = *in + if in.Steps != nil { + in, out := &in.Steps, &out.Steps + *out = make([]CanaryStep, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.TrafficRoutings != nil { + in, out := &in.TrafficRoutings, &out.TrafficRoutings + *out = make([]TrafficRoutingRef, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.FailureThreshold != nil { + in, out := &in.FailureThreshold, &out.FailureThreshold + *out = new(intstr.IntOrString) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BlueGreenStrategy. +func (in *BlueGreenStrategy) DeepCopy() *BlueGreenStrategy { + if in == nil { + return nil } + out := new(BlueGreenStrategy) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CanaryStatus) DeepCopyInto(out *CanaryStatus) { + *out = *in + in.CommonStatus.DeepCopyInto(&out.CommonStatus) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CanaryStatus. @@ -236,6 +283,25 @@ func (in *CanaryStrategy) DeepCopy() *CanaryStrategy { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CommonStatus) DeepCopyInto(out *CommonStatus) { + *out = *in + if in.LastUpdateTime != nil { + in, out := &in.LastUpdateTime, &out.LastUpdateTime + *out = (*in).DeepCopy() + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CommonStatus. +func (in *CommonStatus) DeepCopy() *CommonStatus { + if in == nil { + return nil + } + out := new(CommonStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DeploymentExtraStatus) DeepCopyInto(out *DeploymentExtraStatus) { *out = *in @@ -344,6 +410,31 @@ func (in *ObjectRef) DeepCopy() *ObjectRef { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *OriginalSetting) DeepCopyInto(out *OriginalSetting) { + *out = *in + if in.Strategy != nil { + in, out := &in.Strategy, &out.Strategy + *out = new(v1.DeploymentStrategy) + (*in).DeepCopyInto(*out) + } + if in.ProgressDeadlineSeconds != nil { + in, out := &in.ProgressDeadlineSeconds, &out.ProgressDeadlineSeconds + *out = new(int32) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OriginalSetting. +func (in *OriginalSetting) DeepCopy() *OriginalSetting { + if in == nil { + return nil + } + out := new(OriginalSetting) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PatchPodTemplateMetadata) DeepCopyInto(out *PatchPodTemplateMetadata) { *out = *in @@ -545,6 +636,11 @@ func (in *RolloutStatus) DeepCopyInto(out *RolloutStatus) { *out = new(CanaryStatus) (*in).DeepCopyInto(*out) } + if in.BlueGreenStatus != nil { + in, out := &in.BlueGreenStatus, &out.BlueGreenStatus + *out = new(BlueGreenStatus) + (*in).DeepCopyInto(*out) + } if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions *out = make([]RolloutCondition, len(*in)) @@ -572,6 +668,11 @@ func (in *RolloutStrategy) DeepCopyInto(out *RolloutStrategy) { *out = new(CanaryStrategy) (*in).DeepCopyInto(*out) } + if in.BlueGreen != nil { + in, out := &in.BlueGreen, &out.BlueGreen + *out = new(BlueGreenStrategy) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RolloutStrategy. diff --git a/config/crd/bases/rollouts.kruise.io_batchreleases.yaml b/config/crd/bases/rollouts.kruise.io_batchreleases.yaml index 7eb29e17..ec704f93 100644 --- a/config/crd/bases/rollouts.kruise.io_batchreleases.yaml +++ b/config/crd/bases/rollouts.kruise.io_batchreleases.yaml @@ -118,6 +118,9 @@ spec: description: labels type: object type: object + rollingStyle: + description: RollingStyle can be "Canary", "Partiton" or "BlueGreen" + type: string rolloutID: description: RolloutID indicates an id for each rollout progress type: string @@ -345,13 +348,6 @@ spec: - canaryReplicas type: object type: array - enableExtraWorkloadForCanary: - description: 'If true, then it will create new deployment for - canary, such as: workload-demo-canary. When user verifies that - the canary version is ready, we will remove the canary deployment - and release the deployment workload-demo in full. Current only - support k8s native deployment' - type: boolean failureThreshold: anyOf: - type: integer @@ -382,11 +378,12 @@ spec: description: labels type: object type: object + rollingStyle: + description: RollingStyle can be "Canary", "Partiton" or "BlueGreen" + type: string rolloutID: description: RolloutID indicates an id for each rollout progress type: string - required: - - enableExtraWorkloadForCanary type: object workloadRef: description: WorkloadRef contains enough information to let you identify diff --git a/config/crd/bases/rollouts.kruise.io_rollouts.yaml b/config/crd/bases/rollouts.kruise.io_rollouts.yaml index 62c072a6..a52ea246 100644 --- a/config/crd/bases/rollouts.kruise.io_rollouts.yaml +++ b/config/crd/bases/rollouts.kruise.io_rollouts.yaml @@ -435,13 +435,20 @@ spec: so it cannot be used as service selector label type: string currentStepIndex: - description: CurrentStepIndex defines the current step of the - rollout is on. If the current step index is null, the controller - will execute the rollout. + description: NextStepIndex defines the next step of the rollout + is on. In normal case, NextStepIndex is equal to CurrentStepIndex + + 1 If the current step is the last step, NextStepIndex is equal + to 0 Before the release, NextStepIndex is also equal to 0 It + is allowed to modify NextStepIndex by design, e.g. if CurrentStepIndex + is 2, user can patch NextStepIndex to 3 (if exists) to achieve + batch jump, or patch NextStepIndex to 1 to implement a re-execution + of step 1 format: int32 type: integer currentStepState: type: string + finalisingStep: + type: string lastUpdateTime: format: date-time type: string @@ -470,6 +477,7 @@ spec: - canaryReplicas - canaryRevision - currentStepState + - finalisingStep - podTemplateHash type: object conditions: @@ -574,6 +582,308 @@ spec: strategy: description: rollout strategy properties: + blueGreen: + description: BlueGreenStrategy defines parameters for Blue Green + Release + properties: + disableGenerateCanaryService: + description: canary service will not be generated if DisableGenerateCanaryService + is true + type: boolean + failureThreshold: + anyOf: + - type: integer + - type: string + description: FailureThreshold indicates how many failed pods + can be tolerated in all upgraded pods. Only when FailureThreshold + are satisfied, Rollout can enter ready state. If FailureThreshold + is nil, Rollout will use the MaxUnavailable of workload + as its FailureThreshold. Defaults to nil. + x-kubernetes-int-or-string: true + steps: + description: Steps define the order of phases to execute release + in batches(20%, 40%, 60%, 80%, 100%) + items: + description: CanaryStep defines a step of a canary workload. + properties: + matches: + description: Matches define conditions used for matching + the incoming HTTP requests to canary service. Each + match is independent, i.e. this rule will be matched + if **any** one of the matches is satisfied. If Gateway + API, current only support one match. And cannot support + both weight and matches, if both are configured, then + matches takes precedence. + items: + properties: + headers: + description: Headers specifies HTTP request header + matchers. Multiple match values are ANDed together, + meaning, a request must match all the specified + headers to select the route. + items: + description: HTTPHeaderMatch describes how to + select a HTTP route by matching HTTP request + headers. + properties: + name: + description: "Name is the name of the HTTP + Header to be matched. Name matching MUST + be case insensitive. (See https://tools.ietf.org/html/rfc7230#section-3.2). + \n If multiple entries specify equivalent + header names, only the first entry with + an equivalent name MUST be considered + for a match. Subsequent entries with an + equivalent header name MUST be ignored. + Due to the case-insensitivity of header + names, \"foo\" and \"Foo\" are considered + equivalent. \n When a header is repeated + in an HTTP request, it is implementation-specific + behavior as to how this is represented. + Generally, proxies should follow the guidance + from the RFC: https://www.rfc-editor.org/rfc/rfc7230.html#section-3.2.2 + regarding processing a repeated header, + with special handling for \"Set-Cookie\"." + maxLength: 256 + minLength: 1 + pattern: ^[A-Za-z0-9!#$%&'*+\-.^_\x60|~]+$ + type: string + type: + default: Exact + description: "Type specifies how to match + against the value of the header. \n Support: + Core (Exact) \n Support: Custom (RegularExpression) + \n Since RegularExpression HeaderMatchType + has custom conformance, implementations + can support POSIX, PCRE or any other dialects + of regular expressions. Please read the + implementation's documentation to determine + the supported dialect." + enum: + - Exact + - RegularExpression + type: string + value: + description: Value is the value of HTTP + Header to be matched. + maxLength: 4096 + minLength: 1 + type: string + required: + - name + - value + type: object + maxItems: 16 + type: array + type: object + type: array + pause: + description: Pause defines a pause stage for a rollout, + manual or auto + properties: + duration: + description: Duration the amount of time to wait + before moving to the next step. + format: int32 + type: integer + type: object + replicas: + anyOf: + - type: integer + - type: string + description: 'Replicas is the number of expected canary + pods in this batch it can be an absolute number (ex: + 5) or a percentage of total pods.' + x-kubernetes-int-or-string: true + requestHeaderModifier: + description: "Set overwrites the request with the given + header (name, value) before the action. \n Input: + \ GET /foo HTTP/1.1 my-header: foo \n requestHeaderModifier: + \ set: - name: \"my-header\" value: \"bar\" + \n Output: GET /foo HTTP/1.1 my-header: bar" + properties: + add: + description: "Add adds the given header(s) (name, + value) to the request before the action. It appends + to any existing values associated with the header + name. \n Input: GET /foo HTTP/1.1 my-header: + foo \n Config: add: - name: \"my-header\" + \ value: \"bar\" \n Output: GET /foo HTTP/1.1 + \ my-header: foo my-header: bar" + items: + description: HTTPHeader represents an HTTP Header + name and value as defined by RFC 7230. + properties: + name: + description: "Name is the name of the HTTP + Header to be matched. Name matching MUST + be case insensitive. (See https://tools.ietf.org/html/rfc7230#section-3.2). + \n If multiple entries specify equivalent + header names, the first entry with an equivalent + name MUST be considered for a match. Subsequent + entries with an equivalent header name MUST + be ignored. Due to the case-insensitivity + of header names, \"foo\" and \"Foo\" are + considered equivalent." + maxLength: 256 + minLength: 1 + pattern: ^[A-Za-z0-9!#$%&'*+\-.^_\x60|~]+$ + type: string + value: + description: Value is the value of HTTP Header + to be matched. + maxLength: 4096 + minLength: 1 + type: string + required: + - name + - value + type: object + maxItems: 16 + type: array + x-kubernetes-list-map-keys: + - name + x-kubernetes-list-type: map + remove: + description: "Remove the given header(s) from the + HTTP request before the action. The value of Remove + is a list of HTTP header names. Note that the + header names are case-insensitive (see https://datatracker.ietf.org/doc/html/rfc2616#section-4.2). + \n Input: GET /foo HTTP/1.1 my-header1: foo + \ my-header2: bar my-header3: baz \n Config: + \ remove: [\"my-header1\", \"my-header3\"] \n + Output: GET /foo HTTP/1.1 my-header2: bar" + items: + type: string + maxItems: 16 + type: array + set: + description: "Set overwrites the request with the + given header (name, value) before the action. + \n Input: GET /foo HTTP/1.1 my-header: foo + \n Config: set: - name: \"my-header\" value: + \"bar\" \n Output: GET /foo HTTP/1.1 my-header: + bar" + items: + description: HTTPHeader represents an HTTP Header + name and value as defined by RFC 7230. + properties: + name: + description: "Name is the name of the HTTP + Header to be matched. Name matching MUST + be case insensitive. (See https://tools.ietf.org/html/rfc7230#section-3.2). + \n If multiple entries specify equivalent + header names, the first entry with an equivalent + name MUST be considered for a match. Subsequent + entries with an equivalent header name MUST + be ignored. Due to the case-insensitivity + of header names, \"foo\" and \"Foo\" are + considered equivalent." + maxLength: 256 + minLength: 1 + pattern: ^[A-Za-z0-9!#$%&'*+\-.^_\x60|~]+$ + type: string + value: + description: Value is the value of HTTP Header + to be matched. + maxLength: 4096 + minLength: 1 + type: string + required: + - name + - value + type: object + maxItems: 16 + type: array + x-kubernetes-list-map-keys: + - name + x-kubernetes-list-type: map + type: object + traffic: + description: Traffic indicate how many percentage of + traffic the canary pods should receive Value is of + string type and is a percentage, e.g. 5%. + type: string + type: object + type: array + trafficRoutingRef: + description: TrafficRoutingRef is TrafficRouting's Name + type: string + trafficRoutings: + description: TrafficRoutings support ingress, gateway api + and custom network resource(e.g. istio, apisix) to enable + more fine-grained traffic routing and current only support + one TrafficRouting + items: + description: TrafficRoutingRef hosts all the different configuration + for supported service meshes to enable more fine-grained + traffic routing + properties: + customNetworkRefs: + description: CustomNetworkRefs hold a list of custom + providers to route traffic + items: + description: ObjectRef holds a references to the Kubernetes + object + properties: + apiVersion: + description: API Version of the referent + type: string + kind: + description: Kind of the referent + type: string + name: + description: Name of the referent + type: string + required: + - apiVersion + - kind + - name + type: object + type: array + gateway: + description: Gateway holds Gateway specific configuration + to route traffic Gateway configuration only supports + >= v0.4.0 (v1alpha2). + properties: + httpRouteName: + description: HTTPRouteName refers to the name of + an `HTTPRoute` resource in the same namespace + as the `Rollout` + type: string + type: object + gracePeriodSeconds: + description: Optional duration in seconds the traffic + provider(e.g. nginx ingress controller) consumes the + service, ingress configuration changes gracefully. + format: int32 + type: integer + ingress: + description: Ingress holds Ingress specific configuration + to route traffic, e.g. Nginx, Alb. + properties: + classType: + description: ClassType refers to the type of `Ingress`. + current support nginx, aliyun-alb. default is + nginx. + type: string + name: + description: Name refers to the name of an `Ingress` + resource in the same namespace as the `Rollout` + type: string + required: + - name + type: object + service: + description: Service holds the name of a service which + selects pods with stable version and don't select + any pods with canary version. + type: string + required: + - service + type: object + type: array + type: object canary: description: CanaryStrategy defines parameters for a Replica Based Canary @@ -931,6 +1241,78 @@ spec: status: description: RolloutStatus defines the observed state of Rollout properties: + blueGreenStatus: + description: BlueGreen describes the state of the blueGreen rollout + properties: + currentStepIndex: + description: CurrentStepIndex defines the current step of the + rollout is on. If the current step index is null, the controller + will execute the rollout. + format: int32 + type: integer + currentStepState: + type: string + finalisingStep: + description: FinalisingStep the step of finalising + type: string + lastUpdateTime: + format: date-time + type: string + message: + type: string + nextStepIndex: + description: NextStepIndex defines the next step of the rollout + is on. In normal case, NextStepIndex is equal to CurrentStepIndex + + 1 If the current step is the last step, NextStepIndex is equal + to 0 Before the release, NextStepIndex is also equal to 0 It + is allowed to modify NextStepIndex by design, e.g. if CurrentStepIndex + is 2, user can patch NextStepIndex to 3 (if exists) to achieve + batch jump, or patch NextStepIndex to 1 to implement a re-execution + of step 1 + format: int32 + type: integer + observedRolloutID: + description: ObservedRolloutID will record the newest spec.RolloutID + if status.canaryRevision equals to workload.updateRevision + type: string + observedWorkloadGeneration: + description: observedWorkloadGeneration is the most recent generation + observed for this Rollout ref workload generation. + format: int64 + type: integer + podTemplateHash: + description: pod template hash is used as service selector label + type: string + rolloutHash: + description: RolloutHash from rollout.spec object + type: string + stableRevision: + description: StableRevision indicates the revision of stable pods + type: string + updatedReadyReplicas: + description: CanaryReadyReplicas the numbers of ready canary revision + pods + format: int32 + type: integer + updatedReplicas: + description: CanaryReplicas the numbers of canary revision pods + format: int32 + type: integer + updatedRevision: + description: CanaryRevision is calculated by rollout based on + podTemplateHash, and the internal logic flow uses It may be + different from rs podTemplateHash in different k8s versions, + so it cannot be used as service selector label + type: string + required: + - currentStepState + - finalisingStep + - nextStepIndex + - podTemplateHash + - updatedReadyReplicas + - updatedReplicas + - updatedRevision + type: object canaryStatus: description: Canary describes the state of the canary rollout properties: @@ -957,11 +1339,25 @@ spec: type: integer currentStepState: type: string + finalisingStep: + description: FinalisingStep the step of finalising + type: string lastUpdateTime: format: date-time type: string message: type: string + nextStepIndex: + description: NextStepIndex defines the next step of the rollout + is on. In normal case, NextStepIndex is equal to CurrentStepIndex + + 1 If the current step is the last step, NextStepIndex is equal + to 0 Before the release, NextStepIndex is also equal to 0 It + is allowed to modify NextStepIndex by design, e.g. if CurrentStepIndex + is 2, user can patch NextStepIndex to 3 (if exists) to achieve + batch jump, or patch NextStepIndex to 1 to implement a re-execution + of step 1 + format: int32 + type: integer observedRolloutID: description: ObservedRolloutID will record the newest spec.RolloutID if status.canaryRevision equals to workload.updateRevision @@ -985,6 +1381,8 @@ spec: - canaryReplicas - canaryRevision - currentStepState + - finalisingStep + - nextStepIndex - podTemplateHash type: object conditions: diff --git a/pkg/controller/batchrelease/batchrelease_controller_test.go b/pkg/controller/batchrelease/batchrelease_controller_test.go index c339b74b..8f79493e 100644 --- a/pkg/controller/batchrelease/batchrelease_controller_test.go +++ b/pkg/controller/batchrelease/batchrelease_controller_test.go @@ -67,8 +67,8 @@ var ( Name: "sample", }, ReleasePlan: v1beta1.ReleasePlan{ - EnableExtraWorkloadForCanary: true, - BatchPartition: pointer.Int32(0), + RollingStyle: v1beta1.CanaryRollingStyle, + BatchPartition: pointer.Int32(0), Batches: []v1beta1.ReleaseBatch{ { CanaryReplicas: intstr.FromString("10%"), @@ -147,6 +147,7 @@ var ( }, ReleasePlan: v1beta1.ReleasePlan{ BatchPartition: pointer.Int32Ptr(0), + RollingStyle: v1beta1.PartitionRollingStyle, Batches: []v1beta1.ReleaseBatch{ { CanaryReplicas: intstr.FromString("10%"), diff --git a/pkg/controller/batchrelease/batchrelease_executor.go b/pkg/controller/batchrelease/batchrelease_executor.go index e1aa563e..267239ea 100644 --- a/pkg/controller/batchrelease/batchrelease_executor.go +++ b/pkg/controller/batchrelease/batchrelease_executor.go @@ -198,27 +198,40 @@ func (r *Executor) getReleaseController(release *v1beta1.BatchRelease, newStatus Name: targetRef.Name, } - switch targetRef.APIVersion { - case appsv1alpha1.GroupVersion.String(): - if targetRef.Kind == reflect.TypeOf(appsv1alpha1.CloneSet{}).Name() { + rollingStyle := release.Spec.ReleasePlan.RollingStyle + klog.Infof("BatchRelease(%v) using %s-style release controller for this batch release", klog.KObj(release), rollingStyle) + switch rollingStyle { + case v1beta1.BlueGreenRollingStyle: + // if targetRef.APIVersion == appsv1alpha1.GroupVersion.String() && targetRef.Kind == reflect.TypeOf(appsv1alpha1.CloneSet{}).Name() { + // klog.InfoS("Using CloneSet bluegreen-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) + // return partitionstyle.NewControlPlane(cloneset.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil + // } + // if targetRef.APIVersion == apps.SchemeGroupVersion.String() && targetRef.Kind == reflect.TypeOf(apps.Deployment{}).Name() { + // klog.InfoS("Using Deployment bluegreen-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) + // return bluegreenstyle.NewControlPlane(deployment.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil + // } + + case v1beta1.CanaryRollingStyle: + if targetRef.APIVersion == apps.SchemeGroupVersion.String() && targetRef.Kind == reflect.TypeOf(apps.Deployment{}).Name() { + klog.InfoS("Using Deployment canary-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) + return canarystyle.NewControlPlane(canarydeployment.NewController, r.client, r.recorder, release, newStatus, targetKey), nil + } + fallthrough + + case v1beta1.PartitionRollingStyle, "": + if targetRef.APIVersion == appsv1alpha1.GroupVersion.String() && targetRef.Kind == reflect.TypeOf(appsv1alpha1.CloneSet{}).Name() { klog.InfoS("Using CloneSet partition-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) return partitionstyle.NewControlPlane(cloneset.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil } - if targetRef.Kind == reflect.TypeOf(appsv1alpha1.DaemonSet{}).Name() { + if targetRef.APIVersion == appsv1alpha1.GroupVersion.String() && targetRef.Kind == reflect.TypeOf(appsv1alpha1.DaemonSet{}).Name() { klog.InfoS("Using DaemonSet partition-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) return partitionstyle.NewControlPlane(daemonset.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil } - - case apps.SchemeGroupVersion.String(): - if targetRef.Kind == reflect.TypeOf(apps.Deployment{}).Name() { - if !release.Spec.ReleasePlan.EnableExtraWorkloadForCanary { - klog.InfoS("Using Deployment partition-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) - return partitionstyle.NewControlPlane(partitiondeployment.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil - } else { - klog.InfoS("Using Deployment canary-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) - return canarystyle.NewControlPlane(canarydeployment.NewController, r.client, r.recorder, release, newStatus, targetKey), nil - } + if targetRef.APIVersion == apps.SchemeGroupVersion.String() && targetRef.Kind == reflect.TypeOf(apps.Deployment{}).Name() { + klog.InfoS("Using Deployment partition-style release controller for this batch release", "workload name", targetKey.Name, "namespace", targetKey.Namespace) + return partitionstyle.NewControlPlane(partitiondeployment.NewController, r.client, r.recorder, release, newStatus, targetKey, gvk), nil } + klog.Info("Partition, but use StatefulSet-Like partition-style release controller for this batch release") } // try to use StatefulSet-like rollout controller by default diff --git a/pkg/controller/rollout/rollout_canary.go b/pkg/controller/rollout/rollout_canary.go index 41ed5f90..206bcbff 100644 --- a/pkg/controller/rollout/rollout_canary.go +++ b/pkg/controller/rollout/rollout_canary.go @@ -359,12 +359,12 @@ func createBatchRelease(rollout *v1beta1.Rollout, rolloutID string, batch int32, Name: rollout.Spec.WorkloadRef.Name, }, ReleasePlan: v1beta1.ReleasePlan{ - Batches: batches, - RolloutID: rolloutID, - BatchPartition: utilpointer.Int32Ptr(batch), - FailureThreshold: rollout.Spec.Strategy.Canary.FailureThreshold, - PatchPodTemplateMetadata: rollout.Spec.Strategy.Canary.PatchPodTemplateMetadata, - EnableExtraWorkloadForCanary: rollout.Spec.Strategy.Canary.EnableExtraWorkloadForCanary, + Batches: batches, + RolloutID: rolloutID, + BatchPartition: utilpointer.Int32Ptr(batch), + FailureThreshold: rollout.Spec.Strategy.Canary.FailureThreshold, + PatchPodTemplateMetadata: rollout.Spec.Strategy.Canary.PatchPodTemplateMetadata, + RollingStyle: rollout.Spec.Strategy.GetRollingStyle(), }, }, } diff --git a/pkg/controller/rollout/rollout_canary_test.go b/pkg/controller/rollout/rollout_canary_test.go index 89148109..573849cf 100644 --- a/pkg/controller/rollout/rollout_canary_test.go +++ b/pkg/controller/rollout/rollout_canary_test.go @@ -99,7 +99,7 @@ func TestRunCanary(t *testing.T) { }, } br.Spec.ReleasePlan.BatchPartition = utilpointer.Int32(0) - br.Spec.ReleasePlan.EnableExtraWorkloadForCanary = true + br.Spec.ReleasePlan.RollingStyle = v1beta1.CanaryRollingStyle return br }, }, @@ -158,10 +158,12 @@ func TestRunCanary(t *testing.T) { }, } br.Spec.ReleasePlan.BatchPartition = utilpointer.Int32(0) - br.Spec.ReleasePlan.EnableExtraWorkloadForCanary = true + br.Spec.ReleasePlan.RollingStyle = v1beta1.CanaryRollingStyle br.Status = v1beta1.BatchReleaseStatus{ - ObservedGeneration: 1, - ObservedReleasePlanHash: "d444a1007776da957d7d8549e3375c96179621b85670ad1e2bb0fc5fea16446a", + ObservedGeneration: 1, + // since we use RollingStyle over EnableExtraWorkloadForCanary now, former hardcoded hash + // should be re-calculated + ObservedReleasePlanHash: util.HashReleasePlanBatches(&br.Spec.ReleasePlan), CanaryStatus: v1beta1.BatchReleaseCanaryStatus{ CurrentBatchState: v1beta1.ReadyBatchState, CurrentBatch: 0, @@ -204,7 +206,7 @@ func TestRunCanary(t *testing.T) { }, } br.Spec.ReleasePlan.BatchPartition = utilpointer.Int32(0) - br.Spec.ReleasePlan.EnableExtraWorkloadForCanary = true + br.Spec.ReleasePlan.RollingStyle = v1beta1.CanaryRollingStyle return br }, }, diff --git a/pkg/controller/rollout/rollout_progressing.go b/pkg/controller/rollout/rollout_progressing.go index 0aaf5975..9eae32a5 100644 --- a/pkg/controller/rollout/rollout_progressing.go +++ b/pkg/controller/rollout/rollout_progressing.go @@ -66,17 +66,38 @@ func (r *RolloutReconciler) reconcileRolloutProgressing(rollout *v1beta1.Rollout switch cond.Reason { case v1alpha1.ProgressingReasonInitializing: klog.Infof("rollout(%s/%s) is Progressing, and in reason(%s)", rollout.Namespace, rollout.Name, cond.Reason) - // new canaryStatus - newStatus.CanaryStatus = &v1beta1.CanaryStatus{ - ObservedWorkloadGeneration: rolloutContext.Workload.Generation, - RolloutHash: rolloutContext.Rollout.Annotations[util.RolloutHashAnnotation], - ObservedRolloutID: getRolloutID(rolloutContext.Workload), - StableRevision: rolloutContext.Workload.StableRevision, - CanaryRevision: rolloutContext.Workload.CanaryRevision, - CurrentStepIndex: 1, - CurrentStepState: v1beta1.CanaryStepStateUpgrade, - LastUpdateTime: &metav1.Time{Time: time.Now()}, + // clear and create + newStatus.Clear() + if rollout.Spec.Strategy.IsBlueGreenRelease() { + newStatus.BlueGreenStatus = &v1beta1.BlueGreenStatus{ + CommonStatus: v1beta1.CommonStatus{ + ObservedWorkloadGeneration: rolloutContext.Workload.Generation, + RolloutHash: rolloutContext.Rollout.Annotations[util.RolloutHashAnnotation], + ObservedRolloutID: getRolloutID(rolloutContext.Workload), + StableRevision: rolloutContext.Workload.StableRevision, + CurrentStepIndex: 1, + NextStepIndex: util.NextBatchIndex(rollout, 1), + CurrentStepState: v1beta1.CanaryStepStateInit, + LastUpdateTime: &metav1.Time{Time: time.Now()}, + }, + CanaryRevision: rolloutContext.Workload.CanaryRevision, + } + } else { + newStatus.CanaryStatus = &v1beta1.CanaryStatus{ + CommonStatus: v1beta1.CommonStatus{ + ObservedWorkloadGeneration: rolloutContext.Workload.Generation, + RolloutHash: rolloutContext.Rollout.Annotations[util.RolloutHashAnnotation], + ObservedRolloutID: getRolloutID(rolloutContext.Workload), + StableRevision: rolloutContext.Workload.StableRevision, + CurrentStepIndex: 1, + NextStepIndex: util.NextBatchIndex(rollout, 1), + CurrentStepState: v1beta1.CanaryStepStateUpgrade, + LastUpdateTime: &metav1.Time{Time: time.Now()}, + }, + CanaryRevision: rolloutContext.Workload.CanaryRevision, + } } + done, err := r.doProgressingInitializing(rolloutContext) if err != nil { klog.Errorf("rollout(%s/%s) doProgressingInitializing error(%s)", rollout.Namespace, rollout.Name, err.Error()) diff --git a/pkg/controller/rollout/rollout_progressing_test.go b/pkg/controller/rollout/rollout_progressing_test.go index e9649394..696f5dbd 100644 --- a/pkg/controller/rollout/rollout_progressing_test.go +++ b/pkg/controller/rollout/rollout_progressing_test.go @@ -67,6 +67,11 @@ func TestReconcileRolloutProgressing(t *testing.T) { s.CanaryStatus.StableRevision = "pod-template-hash-v1" s.CanaryStatus.CanaryRevision = "6f8cc56547" s.CanaryStatus.CurrentStepIndex = 1 + // s.CanaryStatus.NextStepIndex will be initialized as 0 in ReconcileRolloutProgressing + // util.NextBatchIndex(rollout, s.CanaryStatus.CurrentStepIndex), which is 2 here. + // However the ReconcileRolloutProgressing won't update it, and thus expected value of it + // in the next cases will be 0 (zero in zero out), without update. This is as expected + s.CanaryStatus.NextStepIndex = 2 s.CanaryStatus.CurrentStepState = v1beta1.CanaryStepStateUpgrade return s }, @@ -99,6 +104,7 @@ func TestReconcileRolloutProgressing(t *testing.T) { s.CanaryStatus.StableRevision = "pod-template-hash-v1" s.CanaryStatus.CanaryRevision = "6f8cc56547" s.CanaryStatus.CurrentStepIndex = 1 + s.CanaryStatus.NextStepIndex = 2 s.CanaryStatus.CurrentStepState = v1beta1.CanaryStepStateUpgrade cond := util.GetRolloutCondition(*s, v1beta1.RolloutConditionProgressing) cond.Reason = v1alpha1.ProgressingReasonInRolling diff --git a/pkg/controller/rollout/rollout_status.go b/pkg/controller/rollout/rollout_status.go index 8165bcc4..c1c8a375 100755 --- a/pkg/controller/rollout/rollout_status.go +++ b/pkg/controller/rollout/rollout_status.go @@ -123,17 +123,36 @@ func (r *RolloutReconciler) calculateRolloutStatus(rollout *v1beta1.Rollout) (re // But at the first deployment of Rollout/Workload, CanaryStatus isn't set due to no rollout progression, // and PaaS platform cannot judge whether the deployment is completed base on the code above. So we have // to update the status just like the rollout was completed. - - newStatus.CanaryStatus = &v1beta1.CanaryStatus{ - ObservedRolloutID: getRolloutID(workload), - ObservedWorkloadGeneration: workload.Generation, - PodTemplateHash: workload.PodTemplateHash, - CanaryRevision: workload.CanaryRevision, - StableRevision: workload.StableRevision, - CurrentStepIndex: int32(len(rollout.Spec.Strategy.Canary.Steps)), - CurrentStepState: v1beta1.CanaryStepStateCompleted, - RolloutHash: rollout.Annotations[util.RolloutHashAnnotation], + if rollout.Spec.Strategy.IsBlueGreenRelease() { + newStatus.BlueGreenStatus = &v1beta1.BlueGreenStatus{ + CommonStatus: v1beta1.CommonStatus{ + ObservedRolloutID: getRolloutID(workload), + ObservedWorkloadGeneration: workload.Generation, + PodTemplateHash: workload.PodTemplateHash, + StableRevision: workload.StableRevision, + CurrentStepIndex: int32(len(rollout.Spec.Strategy.GetSteps())), + NextStepIndex: util.NextBatchIndex(rollout, int32(len(rollout.Spec.Strategy.GetSteps()))), + CurrentStepState: v1beta1.CanaryStepStateCompleted, + RolloutHash: rollout.Annotations[util.RolloutHashAnnotation], + }, + CanaryRevision: workload.CanaryRevision, + } + } else { + newStatus.CanaryStatus = &v1beta1.CanaryStatus{ + CommonStatus: v1beta1.CommonStatus{ + ObservedRolloutID: getRolloutID(workload), + ObservedWorkloadGeneration: workload.Generation, + PodTemplateHash: workload.PodTemplateHash, + StableRevision: workload.StableRevision, + CurrentStepIndex: int32(len(rollout.Spec.Strategy.GetSteps())), + NextStepIndex: util.NextBatchIndex(rollout, int32(len(rollout.Spec.Strategy.GetSteps()))), + CurrentStepState: v1beta1.CanaryStepStateCompleted, + RolloutHash: rollout.Annotations[util.RolloutHashAnnotation], + }, + CanaryRevision: workload.CanaryRevision, + } } + newStatus.Message = "workload deployment is completed" } case v1beta1.RolloutPhaseDisabled: diff --git a/pkg/trafficrouting/manager_test.go b/pkg/trafficrouting/manager_test.go index 366585e1..8ff7ce88 100644 --- a/pkg/trafficrouting/manager_test.go +++ b/pkg/trafficrouting/manager_test.go @@ -163,15 +163,17 @@ var ( Status: v1beta1.RolloutStatus{ Phase: v1beta1.RolloutPhaseProgressing, CanaryStatus: &v1beta1.CanaryStatus{ - ObservedWorkloadGeneration: 1, - RolloutHash: "rollout-hash-v1", - ObservedRolloutID: "rollout-id-1", - StableRevision: "podtemplatehash-v1", - CanaryRevision: "revision-v2", - CurrentStepIndex: 1, - CurrentStepState: v1beta1.CanaryStepStateTrafficRouting, - PodTemplateHash: "podtemplatehash-v2", - LastUpdateTime: &metav1.Time{Time: time.Now()}, + CommonStatus: v1beta1.CommonStatus{ + ObservedWorkloadGeneration: 1, + RolloutHash: "rollout-hash-v1", + ObservedRolloutID: "rollout-id-1", + StableRevision: "podtemplatehash-v1", + CurrentStepIndex: 1, + CurrentStepState: v1beta1.CanaryStepStateTrafficRouting, + PodTemplateHash: "podtemplatehash-v2", + LastUpdateTime: &metav1.Time{Time: time.Now()}, + }, + CanaryRevision: "revision-v2", }, Conditions: []v1beta1.RolloutCondition{ { @@ -249,15 +251,17 @@ var ( Status: v1beta1.RolloutStatus{ Phase: v1beta1.RolloutPhaseProgressing, CanaryStatus: &v1beta1.CanaryStatus{ - ObservedWorkloadGeneration: 1, - RolloutHash: "rollout-hash-v1", - ObservedRolloutID: "rollout-id-1", - StableRevision: "podtemplatehash-v1", - CanaryRevision: "revision-v2", - CurrentStepIndex: 1, - CurrentStepState: v1beta1.CanaryStepStateTrafficRouting, - PodTemplateHash: "podtemplatehash-v2", - LastUpdateTime: &metav1.Time{Time: time.Now()}, + CommonStatus: v1beta1.CommonStatus{ + ObservedWorkloadGeneration: 1, + RolloutHash: "rollout-hash-v1", + ObservedRolloutID: "rollout-id-1", + StableRevision: "podtemplatehash-v1", + CurrentStepIndex: 1, + CurrentStepState: v1beta1.CanaryStepStateTrafficRouting, + PodTemplateHash: "podtemplatehash-v2", + LastUpdateTime: &metav1.Time{Time: time.Now()}, + }, + CanaryRevision: "revision-v2", }, Conditions: []v1beta1.RolloutCondition{ { diff --git a/pkg/util/rollout_utils.go b/pkg/util/rollout_utils.go index f41d7bf7..34624d46 100644 --- a/pkg/util/rollout_utils.go +++ b/pkg/util/rollout_utils.go @@ -164,3 +164,14 @@ func DumpJSON(o interface{}) string { func EncodeHash(data string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(data))) } + +func NextBatchIndex(rollout *rolloutv1beta1.Rollout, CurrentStepIndex int32) int32 { + if rollout == nil { + return 0 + } + allSteps := int32(len(rollout.Spec.Strategy.GetSteps())) + if CurrentStepIndex >= allSteps { + return 0 + } + return CurrentStepIndex + 1 +} diff --git a/pkg/webhook/rollout/validating/rollout_create_update_handler_test.go b/pkg/webhook/rollout/validating/rollout_create_update_handler_test.go index 07eacd57..27ea4531 100644 --- a/pkg/webhook/rollout/validating/rollout_create_update_handler_test.go +++ b/pkg/webhook/rollout/validating/rollout_create_update_handler_test.go @@ -96,7 +96,9 @@ var ( }, Status: appsv1beta1.RolloutStatus{ CanaryStatus: &appsv1beta1.CanaryStatus{ - CurrentStepState: appsv1beta1.CanaryStepStateCompleted, + CommonStatus: appsv1beta1.CommonStatus{ + CurrentStepState: appsv1beta1.CanaryStepStateCompleted, + }, }, }, }