123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- package deployment
- import (
- "fmt"
- "sort"
- apps "k8s.io/api/apps/v1"
- "k8s.io/klog"
- "k8s.io/kubernetes/pkg/controller"
- deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
- "k8s.io/utils/integer"
- )
- func (dc *DeploymentController) rolloutRolling(d *apps.Deployment, rsList []*apps.ReplicaSet) error {
- newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(d, rsList, true)
- if err != nil {
- return err
- }
- allRSs := append(oldRSs, newRS)
-
- scaledUp, err := dc.reconcileNewReplicaSet(allRSs, newRS, d)
- if err != nil {
- return err
- }
- if scaledUp {
-
- return dc.syncRolloutStatus(allRSs, newRS, d)
- }
-
- scaledDown, err := dc.reconcileOldReplicaSets(allRSs, controller.FilterActiveReplicaSets(oldRSs), newRS, d)
- if err != nil {
- return err
- }
- if scaledDown {
-
- return dc.syncRolloutStatus(allRSs, newRS, d)
- }
- if deploymentutil.DeploymentComplete(d, &d.Status) {
- if err := dc.cleanupDeployment(oldRSs, d); err != nil {
- return err
- }
- }
-
- return dc.syncRolloutStatus(allRSs, newRS, d)
- }
- func (dc *DeploymentController) reconcileNewReplicaSet(allRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet, deployment *apps.Deployment) (bool, error) {
- if *(newRS.Spec.Replicas) == *(deployment.Spec.Replicas) {
-
- return false, nil
- }
- if *(newRS.Spec.Replicas) > *(deployment.Spec.Replicas) {
-
- scaled, _, err := dc.scaleReplicaSetAndRecordEvent(newRS, *(deployment.Spec.Replicas), deployment)
- return scaled, err
- }
- newReplicasCount, err := deploymentutil.NewRSNewReplicas(deployment, allRSs, newRS)
- if err != nil {
- return false, err
- }
- scaled, _, err := dc.scaleReplicaSetAndRecordEvent(newRS, newReplicasCount, deployment)
- return scaled, err
- }
- func (dc *DeploymentController) reconcileOldReplicaSets(allRSs []*apps.ReplicaSet, oldRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet, deployment *apps.Deployment) (bool, error) {
- oldPodsCount := deploymentutil.GetReplicaCountForReplicaSets(oldRSs)
- if oldPodsCount == 0 {
-
- return false, nil
- }
- allPodsCount := deploymentutil.GetReplicaCountForReplicaSets(allRSs)
- klog.V(4).Infof("New replica set %s/%s has %d available pods.", newRS.Namespace, newRS.Name, newRS.Status.AvailableReplicas)
- maxUnavailable := deploymentutil.MaxUnavailable(*deployment)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- minAvailable := *(deployment.Spec.Replicas) - maxUnavailable
- newRSUnavailablePodCount := *(newRS.Spec.Replicas) - newRS.Status.AvailableReplicas
- maxScaledDown := allPodsCount - minAvailable - newRSUnavailablePodCount
- if maxScaledDown <= 0 {
- return false, nil
- }
-
-
- oldRSs, cleanupCount, err := dc.cleanupUnhealthyReplicas(oldRSs, deployment, maxScaledDown)
- if err != nil {
- return false, nil
- }
- klog.V(4).Infof("Cleaned up unhealthy replicas from old RSes by %d", cleanupCount)
-
- allRSs = append(oldRSs, newRS)
- scaledDownCount, err := dc.scaleDownOldReplicaSetsForRollingUpdate(allRSs, oldRSs, deployment)
- if err != nil {
- return false, nil
- }
- klog.V(4).Infof("Scaled down old RSes of deployment %s by %d", deployment.Name, scaledDownCount)
- totalScaledDown := cleanupCount + scaledDownCount
- return totalScaledDown > 0, nil
- }
- func (dc *DeploymentController) cleanupUnhealthyReplicas(oldRSs []*apps.ReplicaSet, deployment *apps.Deployment, maxCleanupCount int32) ([]*apps.ReplicaSet, int32, error) {
- sort.Sort(controller.ReplicaSetsByCreationTimestamp(oldRSs))
-
-
-
- totalScaledDown := int32(0)
- for i, targetRS := range oldRSs {
- if totalScaledDown >= maxCleanupCount {
- break
- }
- if *(targetRS.Spec.Replicas) == 0 {
-
- continue
- }
- klog.V(4).Infof("Found %d available pods in old RS %s/%s", targetRS.Status.AvailableReplicas, targetRS.Namespace, targetRS.Name)
- if *(targetRS.Spec.Replicas) == targetRS.Status.AvailableReplicas {
-
- continue
- }
- scaledDownCount := int32(integer.IntMin(int(maxCleanupCount-totalScaledDown), int(*(targetRS.Spec.Replicas)-targetRS.Status.AvailableReplicas)))
- newReplicasCount := *(targetRS.Spec.Replicas) - scaledDownCount
- if newReplicasCount > *(targetRS.Spec.Replicas) {
- return nil, 0, fmt.Errorf("when cleaning up unhealthy replicas, got invalid request to scale down %s/%s %d -> %d", targetRS.Namespace, targetRS.Name, *(targetRS.Spec.Replicas), newReplicasCount)
- }
- _, updatedOldRS, err := dc.scaleReplicaSetAndRecordEvent(targetRS, newReplicasCount, deployment)
- if err != nil {
- return nil, totalScaledDown, err
- }
- totalScaledDown += scaledDownCount
- oldRSs[i] = updatedOldRS
- }
- return oldRSs, totalScaledDown, nil
- }
- func (dc *DeploymentController) scaleDownOldReplicaSetsForRollingUpdate(allRSs []*apps.ReplicaSet, oldRSs []*apps.ReplicaSet, deployment *apps.Deployment) (int32, error) {
- maxUnavailable := deploymentutil.MaxUnavailable(*deployment)
-
- minAvailable := *(deployment.Spec.Replicas) - maxUnavailable
-
- availablePodCount := deploymentutil.GetAvailableReplicaCountForReplicaSets(allRSs)
- if availablePodCount <= minAvailable {
-
- return 0, nil
- }
- klog.V(4).Infof("Found %d available pods in deployment %s, scaling down old RSes", availablePodCount, deployment.Name)
- sort.Sort(controller.ReplicaSetsByCreationTimestamp(oldRSs))
- totalScaledDown := int32(0)
- totalScaleDownCount := availablePodCount - minAvailable
- for _, targetRS := range oldRSs {
- if totalScaledDown >= totalScaleDownCount {
-
- break
- }
- if *(targetRS.Spec.Replicas) == 0 {
-
- continue
- }
-
- scaleDownCount := int32(integer.IntMin(int(*(targetRS.Spec.Replicas)), int(totalScaleDownCount-totalScaledDown)))
- newReplicasCount := *(targetRS.Spec.Replicas) - scaleDownCount
- if newReplicasCount > *(targetRS.Spec.Replicas) {
- return 0, fmt.Errorf("when scaling down old RS, got invalid request to scale down %s/%s %d -> %d", targetRS.Namespace, targetRS.Name, *(targetRS.Spec.Replicas), newReplicasCount)
- }
- _, _, err := dc.scaleReplicaSetAndRecordEvent(targetRS, newReplicasCount, deployment)
- if err != nil {
- return totalScaledDown, err
- }
- totalScaledDown += scaleDownCount
- }
- return totalScaledDown, nil
- }
|