123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- package deployment
- import (
- "context"
- "fmt"
- "reflect"
- "time"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/klog"
- apps "k8s.io/api/apps/v1"
- "k8s.io/api/core/v1"
- "k8s.io/kubernetes/pkg/controller/deployment/util"
- )
- func (dc *DeploymentController) syncRolloutStatus(allRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet, d *apps.Deployment) error {
- newStatus := calculateStatus(allRSs, newRS, d)
-
- if !util.HasProgressDeadline(d) {
- util.RemoveDeploymentCondition(&newStatus, apps.DeploymentProgressing)
- }
-
-
-
- currentCond := util.GetDeploymentCondition(d.Status, apps.DeploymentProgressing)
- isCompleteDeployment := newStatus.Replicas == newStatus.UpdatedReplicas && currentCond != nil && currentCond.Reason == util.NewRSAvailableReason
-
-
- if util.HasProgressDeadline(d) && !isCompleteDeployment {
- switch {
- case util.DeploymentComplete(d, &newStatus):
-
-
- msg := fmt.Sprintf("Deployment %q has successfully progressed.", d.Name)
- if newRS != nil {
- msg = fmt.Sprintf("ReplicaSet %q has successfully progressed.", newRS.Name)
- }
- condition := util.NewDeploymentCondition(apps.DeploymentProgressing, v1.ConditionTrue, util.NewRSAvailableReason, msg)
- util.SetDeploymentCondition(&newStatus, *condition)
- case util.DeploymentProgressing(d, &newStatus):
-
-
- msg := fmt.Sprintf("Deployment %q is progressing.", d.Name)
- if newRS != nil {
- msg = fmt.Sprintf("ReplicaSet %q is progressing.", newRS.Name)
- }
- condition := util.NewDeploymentCondition(apps.DeploymentProgressing, v1.ConditionTrue, util.ReplicaSetUpdatedReason, msg)
-
-
-
-
-
-
-
- if currentCond != nil {
- if currentCond.Status == v1.ConditionTrue {
- condition.LastTransitionTime = currentCond.LastTransitionTime
- }
- util.RemoveDeploymentCondition(&newStatus, apps.DeploymentProgressing)
- }
- util.SetDeploymentCondition(&newStatus, *condition)
- case util.DeploymentTimedOut(d, &newStatus):
-
-
- msg := fmt.Sprintf("Deployment %q has timed out progressing.", d.Name)
- if newRS != nil {
- msg = fmt.Sprintf("ReplicaSet %q has timed out progressing.", newRS.Name)
- }
- condition := util.NewDeploymentCondition(apps.DeploymentProgressing, v1.ConditionFalse, util.TimedOutReason, msg)
- util.SetDeploymentCondition(&newStatus, *condition)
- }
- }
-
-
- if replicaFailureCond := dc.getReplicaFailures(allRSs, newRS); len(replicaFailureCond) > 0 {
-
- util.SetDeploymentCondition(&newStatus, replicaFailureCond[0])
- } else {
- util.RemoveDeploymentCondition(&newStatus, apps.DeploymentReplicaFailure)
- }
-
- if reflect.DeepEqual(d.Status, newStatus) {
-
- dc.requeueStuckDeployment(d, newStatus)
- return nil
- }
- newDeployment := d
- newDeployment.Status = newStatus
- _, err := dc.client.AppsV1().Deployments(newDeployment.Namespace).UpdateStatus(context.TODO(), newDeployment, metav1.UpdateOptions{})
- return err
- }
- func (dc *DeploymentController) getReplicaFailures(allRSs []*apps.ReplicaSet, newRS *apps.ReplicaSet) []apps.DeploymentCondition {
- var conditions []apps.DeploymentCondition
- if newRS != nil {
- for _, c := range newRS.Status.Conditions {
- if c.Type != apps.ReplicaSetReplicaFailure {
- continue
- }
- conditions = append(conditions, util.ReplicaSetToDeploymentCondition(c))
- }
- }
-
- if len(conditions) > 0 {
- return conditions
- }
- for i := range allRSs {
- rs := allRSs[i]
- if rs == nil {
- continue
- }
- for _, c := range rs.Status.Conditions {
- if c.Type != apps.ReplicaSetReplicaFailure {
- continue
- }
- conditions = append(conditions, util.ReplicaSetToDeploymentCondition(c))
- }
- }
- return conditions
- }
- var nowFn = func() time.Time { return time.Now() }
- func (dc *DeploymentController) requeueStuckDeployment(d *apps.Deployment, newStatus apps.DeploymentStatus) time.Duration {
- currentCond := util.GetDeploymentCondition(d.Status, apps.DeploymentProgressing)
-
- if !util.HasProgressDeadline(d) || currentCond == nil {
- return time.Duration(-1)
- }
-
- if util.DeploymentComplete(d, &newStatus) || currentCond.Reason == util.TimedOutReason {
- return time.Duration(-1)
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- after := currentCond.LastUpdateTime.Time.Add(time.Duration(*d.Spec.ProgressDeadlineSeconds) * time.Second).Sub(nowFn())
-
-
-
- if after < time.Second {
- klog.V(4).Infof("Queueing up deployment %q for a progress check now", d.Name)
- dc.enqueueRateLimited(d)
- return time.Duration(0)
- }
- klog.V(4).Infof("Queueing up deployment %q for a progress check after %ds", d.Name, int(after.Seconds()))
-
-
- dc.enqueueAfter(d, after+time.Second)
- return after
- }
|