Skip to content

Commit a460e2c

Browse files
committed
simplify ScalingReplicaSet event building in the deployment controller
1 parent 2c4a863 commit a460e2c

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

pkg/controller/deployment/sync.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,12 @@ func (dc *DeploymentController) scale(ctx context.Context, deployment *apps.Depl
340340
// drives what happens in case we are trying to scale replica sets of the same size.
341341
// In such a case when scaling up, we should scale up newer replica sets first, and
342342
// when scaling down, we should scale down older replica sets first.
343-
var scalingOperation string
344343
switch {
345344
case deploymentReplicasToAdd > 0:
346345
sort.Sort(controller.ReplicaSetsBySizeNewer(allRSs))
347-
scalingOperation = "up"
348346

349347
case deploymentReplicasToAdd < 0:
350348
sort.Sort(controller.ReplicaSetsBySizeOlder(allRSs))
351-
scalingOperation = "down"
352349
}
353350

354351
// Iterate over all active replica sets and estimate proportions for each of them.
@@ -386,7 +383,7 @@ func (dc *DeploymentController) scale(ctx context.Context, deployment *apps.Depl
386383
}
387384

388385
// TODO: Use transactions when we have them.
389-
if _, _, err := dc.scaleReplicaSet(ctx, rs, nameToSize[rs.Name], deployment, scalingOperation); err != nil {
386+
if _, _, err := dc.scaleReplicaSet(ctx, rs, nameToSize[rs.Name], deployment); err != nil {
390387
// Return as soon as we fail, the deployment is requeued
391388
return err
392389
}
@@ -400,17 +397,11 @@ func (dc *DeploymentController) scaleReplicaSetAndRecordEvent(ctx context.Contex
400397
if *(rs.Spec.Replicas) == newScale {
401398
return false, rs, nil
402399
}
403-
var scalingOperation string
404-
if *(rs.Spec.Replicas) < newScale {
405-
scalingOperation = "up"
406-
} else {
407-
scalingOperation = "down"
408-
}
409-
scaled, newRS, err := dc.scaleReplicaSet(ctx, rs, newScale, deployment, scalingOperation)
400+
scaled, newRS, err := dc.scaleReplicaSet(ctx, rs, newScale, deployment)
410401
return scaled, newRS, err
411402
}
412403

413-
func (dc *DeploymentController) scaleReplicaSet(ctx context.Context, rs *apps.ReplicaSet, newScale int32, deployment *apps.Deployment, scalingOperation string) (bool, *apps.ReplicaSet, error) {
404+
func (dc *DeploymentController) scaleReplicaSet(ctx context.Context, rs *apps.ReplicaSet, newScale int32, deployment *apps.Deployment) (bool, *apps.ReplicaSet, error) {
414405

415406
sizeNeedsUpdate := *(rs.Spec.Replicas) != newScale
416407

@@ -425,6 +416,12 @@ func (dc *DeploymentController) scaleReplicaSet(ctx context.Context, rs *apps.Re
425416
deploymentutil.SetReplicasAnnotations(rsCopy, *(deployment.Spec.Replicas), *(deployment.Spec.Replicas)+deploymentutil.MaxSurge(*deployment))
426417
rs, err = dc.client.AppsV1().ReplicaSets(rsCopy.Namespace).Update(ctx, rsCopy, metav1.UpdateOptions{})
427418
if err == nil && sizeNeedsUpdate {
419+
var scalingOperation string
420+
if oldScale < newScale {
421+
scalingOperation = "up"
422+
} else {
423+
scalingOperation = "down"
424+
}
428425
scaled = true
429426
dc.eventRecorder.Eventf(deployment, v1.EventTypeNormal, "ScalingReplicaSet", "Scaled %s replica set %s from %d to %d", scalingOperation, rs.Name, oldScale, newScale)
430427
}

0 commit comments

Comments
 (0)