Skip to content

Commit

Permalink
fix the pod-recreate issue in partition style
Browse files Browse the repository at this point in the history
Signed-off-by: yunbo <[email protected]>
  • Loading branch information
Funinu committed Dec 25, 2024
1 parent efbd8ba commit dbcf7c3
Show file tree
Hide file tree
Showing 29 changed files with 229 additions and 98 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func ConversionToV1beta1TrafficRoutingRef(src TrafficRoutingRef) (dst v1beta1.Tr

func ConversionToV1beta1TrafficRoutingStrategy(src TrafficRoutingStrategy) (dst v1beta1.TrafficRoutingStrategy) {
if src.Weight != nil {
dst.Traffic = utilpointer.StringPtr(fmt.Sprintf("%d", *src.Weight) + "%")
dst.Traffic = utilpointer.String(fmt.Sprintf("%d", *src.Weight) + "%")
}
dst.RequestHeaderModifier = src.RequestHeaderModifier
for _, match := range src.Matches {
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/rollout_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ func (r *RolloutStatus) GetSubStatus() *CommonStatus {
return nil
}
if r.CanaryStatus != nil {
return &(r.CanaryStatus.CommonStatus)
return &r.CanaryStatus.CommonStatus
}
return &(r.BlueGreenStatus.CommonStatus)
return &r.BlueGreenStatus.CommonStatus
}

func (r *RolloutStatus) IsSubStatusEmpty() bool {
Expand Down
16 changes: 8 additions & 8 deletions pkg/controller/batchrelease/batchrelease_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var (
},
},
Spec: apps.DeploymentSpec{
Replicas: pointer.Int32Ptr(100),
Replicas: pointer.Int32(100),
Strategy: apps.DeploymentStrategy{
Type: apps.RollingUpdateDeploymentStrategyType,
RollingUpdate: &apps.RollingUpdateDeployment{
Expand Down Expand Up @@ -146,7 +146,7 @@ var (
Name: "sample",
},
ReleasePlan: v1beta1.ReleasePlan{
BatchPartition: pointer.Int32Ptr(0),
BatchPartition: pointer.Int32(0),
RollingStyle: v1beta1.PartitionRollingStyle,
Batches: []v1beta1.ReleaseBatch{
{
Expand Down Expand Up @@ -178,7 +178,7 @@ var (
},
},
Spec: kruiseappsv1alpha1.CloneSetSpec{
Replicas: pointer.Int32Ptr(100),
Replicas: pointer.Int32(100),
UpdateStrategy: kruiseappsv1alpha1.CloneSetUpdateStrategy{
Partition: &intstr.IntOrString{Type: intstr.Int, IntVal: int32(1)},
MaxSurge: &intstr.IntOrString{Type: intstr.Int, IntVal: int32(2)},
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestReconcile_CloneSet(t *testing.T) {
},
GetCloneSet: func() []client.Object {
stable := getStableWithReady(stableClone, "v2").(*kruiseappsv1alpha1.CloneSet)
stable.Spec.Replicas = pointer.Int32Ptr(200)
stable.Spec.Replicas = pointer.Int32(200)
canary := getCanaryWithStage(stable, "v2", 0, true)
return []client.Object{
canary,
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestReconcile_CloneSet(t *testing.T) {
release.Status.UpdateRevision = util.ComputeHash(canaryTemplate, nil)
release.Status.CanaryStatus.UpdatedReplicas = 10
release.Status.CanaryStatus.UpdatedReadyReplicas = 10
release.Spec.ReleasePlan.BatchPartition = pointer.Int32Ptr(1)
release.Spec.ReleasePlan.BatchPartition = pointer.Int32(1)
release.Status.ObservedReleasePlanHash = util.HashReleasePlanBatches(&release.Spec.ReleasePlan)
return release
},
Expand Down Expand Up @@ -651,7 +651,7 @@ func TestReconcile_Deployment(t *testing.T) {
release := releaseDeploy.DeepCopy()
release.Status.CanaryStatus.UpdatedReplicas = 10
release.Status.CanaryStatus.UpdatedReadyReplicas = 10
release.Spec.ReleasePlan.BatchPartition = pointer.Int32Ptr(1)
release.Spec.ReleasePlan.BatchPartition = pointer.Int32(1)
return setState(release, v1beta1.ReadyBatchState)
},
GetDeployments: func() []client.Object {
Expand Down Expand Up @@ -694,7 +694,7 @@ func TestReconcile_Deployment(t *testing.T) {
},
GetDeployments: func() []client.Object {
stable := getStableWithReady(stableDeploy, "v2").(*apps.Deployment)
stable.Spec.Replicas = pointer.Int32Ptr(200)
stable.Spec.Replicas = pointer.Int32(200)
canary := getCanaryWithStage(stable, "v2", 0, true)
return []client.Object{
stable, canary,
Expand Down Expand Up @@ -891,7 +891,7 @@ func getCanaryWithStage(workload client.Object, version string, stage int, ready
d.ResourceVersion = strconv.Itoa(rand.Intn(100000000000))
d.Labels[util.CanaryDeploymentLabel] = "87076677"
d.Finalizers = []string{util.CanaryDeploymentFinalizer}
d.Spec.Replicas = pointer.Int32Ptr(int32(stageReplicas))
d.Spec.Replicas = pointer.Int32(int32(stageReplicas))
d.Spec.Template.Spec.Containers = containers(version)
d.Status.Replicas = int32(stageReplicas)
d.Status.ReadyReplicas = int32(stageReplicas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func TestWorkloadEventHandler_Update(t *testing.T) {
oldObject := getStableWithReady(stableDeploy, "v2").(*apps.Deployment)
oldObject.SetGeneration(2)
oldObject.Status.ObservedGeneration = 2
oldObject.Spec.Replicas = pointer.Int32Ptr(1000)
oldObject.Spec.Replicas = pointer.Int32(1000)
return oldObject
},
GetNewWorkload: func() client.Object {
newObject := getStableWithReady(stableDeploy, "v2").(*apps.Deployment)
newObject.SetGeneration(2)
newObject.Status.ObservedGeneration = 2
newObject.Spec.Replicas = pointer.Int32Ptr(1000)
newObject.Spec.Replicas = pointer.Int32(1000)
newObject.Status.Replicas = 1000
return newObject
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/batchrelease/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type BatchContext struct {
// the next two fields are only used for bluegreen style
CurrentSurge intstr.IntOrString `json:"currentSurge,omitempty"`
DesiredSurge intstr.IntOrString `json:"desiredSurge,omitempty"`
// batches field is added to support complicated scenes such as batch-id label
Batches []v1beta1.ReleaseBatch
}

type FilterFuncType func(pods []*corev1.Pod, ctx *BatchContext) []*corev1.Pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) (
UpdatedReadyReplicas: rc.Status.UpdatedReadyReplicas,
DesiredUpdatedReplicas: int32(desired),
PlannedUpdatedReplicas: int32(desired),
Batches: release.Spec.ReleasePlan.Batches, //shallow copy
}
// the number of no need update pods that marked before rollout
// if noNeedUpdate := release.Status.CanaryStatus.NoNeedUpdateReplicas; noNeedUpdate != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestCalculateBatchContext(t *testing.T) {
workload: func() *kruiseappsv1alpha1.CloneSet {
return &kruiseappsv1alpha1.CloneSet{
Spec: kruiseappsv1alpha1.CloneSetSpec{
Replicas: pointer.Int32Ptr(10),
Replicas: pointer.Int32(10),
UpdateStrategy: kruiseappsv1alpha1.CloneSetUpdateStrategy{
MaxSurge: func() *intstr.IntOrString { p := intstr.FromInt(1); return &p }(),
},
Expand Down Expand Up @@ -348,6 +348,11 @@ func TestCalculateBatchContext(t *testing.T) {
UpdateRevision: "update-version",
PlannedUpdatedReplicas: 5,
DesiredUpdatedReplicas: 5,
Batches: []v1beta1.ReleaseBatch{
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "50%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
},
},
},

Expand Down Expand Up @@ -398,13 +403,18 @@ func TestCalculateBatchContext(t *testing.T) {
UpdateRevision: "update-version",
PlannedUpdatedReplicas: 10,
DesiredUpdatedReplicas: 10,
Batches: []v1beta1.ReleaseBatch{
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "50%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
},
},
},
"normal case batch2": {
workload: func() *kruiseappsv1alpha1.CloneSet {
return &kruiseappsv1alpha1.CloneSet{
Spec: kruiseappsv1alpha1.CloneSetSpec{
Replicas: pointer.Int32Ptr(10),
Replicas: pointer.Int32(10),
UpdateStrategy: kruiseappsv1alpha1.CloneSetUpdateStrategy{
MaxSurge: func() *intstr.IntOrString { p := intstr.FromString("100%"); return &p }(),
},
Expand Down Expand Up @@ -448,6 +458,11 @@ func TestCalculateBatchContext(t *testing.T) {
UpdatedReadyReplicas: 10,
PlannedUpdatedReplicas: 10,
DesiredUpdatedReplicas: 10,
Batches: []v1beta1.ReleaseBatch{
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "50%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
{CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"}},
},
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) (
UpdatedReadyReplicas: rc.Status.UpdatedReadyReplicas,
PlannedUpdatedReplicas: PlannedUpdatedReplicas,
DesiredUpdatedReplicas: PlannedUpdatedReplicas,
Batches: release.Spec.ReleasePlan.Batches, //shallow copy
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ func TestCalculateBatchContext(t *testing.T) {
UpdatedReadyReplicas: 2,
PlannedUpdatedReplicas: 10,
DesiredUpdatedReplicas: 10,
Batches: []v1beta1.ReleaseBatch{
{
CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "50%"},
},
{
CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "100%"},
},
},
},
},
"maxSurge=99%, replicas=5": {
Expand All @@ -439,7 +447,7 @@ func TestCalculateBatchContext(t *testing.T) {
AvailableReplicas: 9,
ReadyReplicas: 9,
}
deployment.Spec.Replicas = pointer.Int32Ptr(5)
deployment.Spec.Replicas = pointer.Int32(5)
// current partition, ie. maxSurge
deployment.Spec.Strategy.RollingUpdate.MaxSurge = &intstr.IntOrString{Type: intstr.String, StrVal: "90%"}
newRss := makeCanaryReplicaSets(deployment).(*apps.ReplicaSet)
Expand Down Expand Up @@ -480,6 +488,14 @@ func TestCalculateBatchContext(t *testing.T) {
UpdatedReadyReplicas: 4,
PlannedUpdatedReplicas: 4,
DesiredUpdatedReplicas: 4,
Batches: []v1beta1.ReleaseBatch{
{
CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "90%"},
},
{
CanaryReplicas: intstr.IntOrString{Type: intstr.String, StrVal: "99%"},
},
},
},
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (r *realCanaryController) create(release *v1beta1.BatchRelease, template *a
canary.Spec.Template.Annotations[k] = v
}
}
canary.Spec.Replicas = pointer.Int32Ptr(0)
canary.Spec.Replicas = pointer.Int32(0)
canary.Spec.Paused = false

if err := r.canaryClient.Create(context.TODO(), canary); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) *
FailureThreshold: release.Spec.ReleasePlan.FailureThreshold,
UpdatedReplicas: rc.canaryObject.Status.Replicas,
UpdatedReadyReplicas: rc.canaryObject.Status.AvailableReplicas,
Batches: release.Spec.ReleasePlan.Batches, //shallow copy
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var (
UpdatedReplicas: 10,
ReadyReplicas: 10,
AvailableReplicas: 10,
CollisionCount: pointer.Int32Ptr(1),
CollisionCount: pointer.Int32(1),
ObservedGeneration: 1,
},
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestCalculateBatchContext(t *testing.T) {
workload: func() (*apps.Deployment, *apps.Deployment) {
stable := &apps.Deployment{
Spec: apps.DeploymentSpec{
Replicas: pointer.Int32Ptr(10),
Replicas: pointer.Int32(10),
},
Status: apps.DeploymentStatus{
Replicas: 10,
Expand All @@ -173,7 +173,7 @@ func TestCalculateBatchContext(t *testing.T) {
}
canary := &apps.Deployment{
Spec: apps.DeploymentSpec{
Replicas: pointer.Int32Ptr(5),
Replicas: pointer.Int32(5),
},
Status: apps.DeploymentStatus{
Replicas: 5,
Expand Down Expand Up @@ -211,6 +211,11 @@ func TestCalculateBatchContext(t *testing.T) {
UpdatedReplicas: 5,
UpdatedReadyReplicas: 5,
DesiredUpdatedReplicas: 2,
Batches: []v1beta1.ReleaseBatch{
{
CanaryReplicas: percent,
},
},
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) (
NoNeedUpdatedReplicas: noNeedUpdate,
PlannedUpdatedReplicas: plannedUpdate,
DesiredUpdatedReplicas: desiredUpdate,
Batches: release.Spec.ReleasePlan.Batches, //shallow copy
}

if noNeedUpdate != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var (
UpdateRevision: "version-2",
CurrentRevision: "version-1",
ObservedGeneration: 1,
CollisionCount: pointer.Int32Ptr(1),
CollisionCount: pointer.Int32(1),
},
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestCalculateBatchContext(t *testing.T) {
workload: func() *kruiseappsv1alpha1.CloneSet {
return &kruiseappsv1alpha1.CloneSet{
Spec: kruiseappsv1alpha1.CloneSetSpec{
Replicas: pointer.Int32Ptr(10),
Replicas: pointer.Int32(10),
UpdateStrategy: kruiseappsv1alpha1.CloneSetUpdateStrategy{
Partition: func() *intstr.IntOrString { p := intstr.FromString("100%"); return &p }(),
},
Expand Down Expand Up @@ -205,13 +205,14 @@ func TestCalculateBatchContext(t *testing.T) {
DesiredUpdatedReplicas: 2,
CurrentPartition: intstr.FromString("100%"),
DesiredPartition: intstr.FromString("80%"),
Batches: []v1beta1.ReleaseBatch{{CanaryReplicas: percent}},
},
},
"with NoNeedUpdate": {
workload: func() *kruiseappsv1alpha1.CloneSet {
return &kruiseappsv1alpha1.CloneSet{
Spec: kruiseappsv1alpha1.CloneSetSpec{
Replicas: pointer.Int32Ptr(20),
Replicas: pointer.Int32(20),
UpdateStrategy: kruiseappsv1alpha1.CloneSetUpdateStrategy{
Partition: func() *intstr.IntOrString { p := intstr.FromString("100%"); return &p }(),
},
Expand Down Expand Up @@ -253,13 +254,14 @@ func TestCalculateBatchContext(t *testing.T) {
Replicas: 20,
UpdatedReplicas: 10,
UpdatedReadyReplicas: 10,
NoNeedUpdatedReplicas: pointer.Int32Ptr(10),
NoNeedUpdatedReplicas: pointer.Int32(10),
PlannedUpdatedReplicas: 4,
DesiredUpdatedReplicas: 12,
CurrentPartition: intstr.FromString("100%"),
DesiredPartition: intstr.FromString("40%"),
FailureThreshold: &percent,
FilterFunc: labelpatch.FilterPodsForUnorderedUpdate,
Batches: []v1beta1.ReleaseBatch{{CanaryReplicas: percent}},
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (rc *realController) CalculateBatchContext(release *v1beta1.BatchRelease) (
NoNeedUpdatedReplicas: noNeedUpdate,
PlannedUpdatedReplicas: plannedUpdate,
DesiredUpdatedReplicas: desiredUpdate,
Batches: release.Spec.ReleasePlan.Batches, //shallow copy
}

if noNeedUpdate != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestCalculateBatchContext(t *testing.T) {
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "foo"}},
UpdateStrategy: kruiseappsv1alpha1.DaemonSetUpdateStrategy{
RollingUpdate: &kruiseappsv1alpha1.RollingUpdateDaemonSet{
Partition: pointer.Int32Ptr(10),
Partition: pointer.Int32(10),
},
},
},
Expand Down Expand Up @@ -219,6 +219,11 @@ func TestCalculateBatchContext(t *testing.T) {
CurrentPartition: intstr.FromInt(10),
DesiredPartition: intstr.FromInt(8),
Pods: generatePods(10, "", ""),
Batches: []v1beta1.ReleaseBatch{
{
CanaryReplicas: percent,
},
},
},
},
"with NoNeedUpdate": {
Expand All @@ -233,7 +238,7 @@ func TestCalculateBatchContext(t *testing.T) {
Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "foo"}},
UpdateStrategy: kruiseappsv1alpha1.DaemonSetUpdateStrategy{
RollingUpdate: &kruiseappsv1alpha1.RollingUpdateDaemonSet{
Partition: pointer.Int32Ptr(10),
Partition: pointer.Int32(10),
},
},
},
Expand Down Expand Up @@ -288,14 +293,19 @@ func TestCalculateBatchContext(t *testing.T) {
Replicas: 10,
UpdatedReplicas: 5,
UpdatedReadyReplicas: 5,
NoNeedUpdatedReplicas: pointer.Int32Ptr(5),
NoNeedUpdatedReplicas: pointer.Int32(5),
PlannedUpdatedReplicas: 2,
DesiredUpdatedReplicas: 6,
CurrentPartition: intstr.FromInt(10),
DesiredPartition: intstr.FromInt(4),
FailureThreshold: &percent,
FilterFunc: labelpatch.FilterPodsForUnorderedUpdate,
Pods: generatePods(10, "", ""),
Batches: []v1beta1.ReleaseBatch{
{
CanaryReplicas: percent,
},
},
},
},
}
Expand Down
Loading

0 comments on commit dbcf7c3

Please sign in to comment.