Skip to content

Commit f06e179

Browse files
committed
apps: deployment config stuck in the new state should respect timeoutSecods
1 parent 05d2e14 commit f06e179

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

pkg/apps/apis/apps/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ const (
113113
DeploymentCancelledNewerDeploymentExists = "newer deployment was found running"
114114
DeploymentFailedUnrelatedDeploymentExists = "unrelated pod with the same name as this deployment is already running"
115115
DeploymentFailedDeployerPodNoLongerExists = "deployer pod no longer exists"
116+
DeploymentFailedUnableToCreateDeployerPod = "unable to create deployer pod"
116117
)
117118

118119
// DeploymentStatus describes the possible states a deployment can be in.

pkg/apps/controller/deployer/deployer_controller.go

+15
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ func (c *DeploymentController) handle(deployment *v1.ReplicationController, will
127127
}
128128
break
129129
}
130+
// In case the deployment is stuck in "new" state because we fail to create
131+
// deployer pod (quota, etc..) we should respect the timeoutSeconds in the
132+
// config strategy and transition the rollout to failed instead of waiting for
133+
// the deployment pod forever.
134+
config, err := deployutil.DecodeDeploymentConfig(deployment, c.codec)
135+
if err != nil {
136+
return err
137+
}
138+
if deployutil.RolloutExceededTimeoutSeconds(config, deployment) {
139+
nextStatus = deployapi.DeploymentStatusFailed
140+
updatedAnnotations[deployapi.DeploymentStatusReasonAnnotation] = deployapi.DeploymentFailedUnableToCreateDeployerPod
141+
c.emitDeploymentEvent(deployment, v1.EventTypeWarning, "RolloutTimeout", fmt.Sprintf("Rollout for %q failed to create deployer pod (timeoutSeconds: %d", deployutil.LabelForDeploymentV1(deployment), deployutil.GetTimeoutSecondsForStrategy(config)))
142+
glog.V(4).Infof("Failing deployment %s/%s as we timeout out while waiting for the deployer pod to be created", deployment.Namespace, deployment.Name)
143+
break
144+
}
130145

131146
switch {
132147
case kerrors.IsNotFound(deployerErr):

pkg/apps/controller/deploymentconfig/deploymentconfig_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func (c *DeploymentConfigController) Handle(config *deployapi.DeploymentConfig)
128128
return err
129129
}
130130
}
131+
131132
// Process triggers and start an initial rollouts
132133
configCopy, err := deployutil.DeploymentConfigDeepCopy(config)
133134
if err != nil {

pkg/apps/util/util.go

+37
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ func IsTerminatedDeployment(deployment runtime.Object) bool {
652652
return IsCompleteDeployment(deployment) || IsFailedDeployment(deployment)
653653
}
654654

655+
// IsNewDeployment returns true if the passed deployment is in new state.
656+
func IsNewDeployment(deployment runtime.Object) bool {
657+
current := DeploymentStatusFor(deployment)
658+
return current == deployapi.DeploymentStatusNew
659+
}
660+
655661
// IsCompleteDeployment returns true if the passed deployment is in state complete.
656662
func IsCompleteDeployment(deployment runtime.Object) bool {
657663
current := DeploymentStatusFor(deployment)
@@ -782,6 +788,37 @@ func DeploymentsForCleanup(configuration *deployapi.DeploymentConfig, deployment
782788
return relevantDeployments
783789
}
784790

791+
// GetTimeoutSecondsForStrategy returns the timeout in seconds defined in the
792+
// deployment config strategy.
793+
func GetTimeoutSecondsForStrategy(config *deployapi.DeploymentConfig) int64 {
794+
var timeoutSeconds int64
795+
switch config.Spec.Strategy.Type {
796+
case deployapi.DeploymentStrategyTypeRolling:
797+
timeoutSeconds = deployapi.DefaultRollingTimeoutSeconds
798+
if t := config.Spec.Strategy.RollingParams.TimeoutSeconds; t != nil {
799+
timeoutSeconds = *t
800+
}
801+
case deployapi.DeploymentStrategyTypeRecreate:
802+
timeoutSeconds = deployapi.DefaultRecreateTimeoutSeconds
803+
if t := config.Spec.Strategy.RecreateParams.TimeoutSeconds; t != nil {
804+
timeoutSeconds = *t
805+
}
806+
case deployapi.DeploymentStrategyTypeCustom:
807+
timeoutSeconds = deployapi.DefaultRecreateTimeoutSeconds
808+
}
809+
return timeoutSeconds
810+
}
811+
812+
// RolloutExceededTimeoutSeconds returns true if the current deployment exceeded
813+
// the timeoutSeconds defined for its strategy.
814+
// Note that this is different than activeDeadlineSeconds which is the timeout
815+
// set for the deployer pod. In some cases, the deployer pod cannot be created
816+
// (like quota, etc...). In that case deployer controller use this function to
817+
// measure if the created deployment (RC) exceeded the timeout.
818+
func RolloutExceededTimeoutSeconds(config *deployapi.DeploymentConfig, latestRC *v1.ReplicationController) bool {
819+
return int64(time.Since(latestRC.CreationTimestamp.Time).Seconds()) > GetTimeoutSecondsForStrategy(config)
820+
}
821+
785822
// WaitForRunningDeployerPod waits a given period of time until the deployer pod
786823
// for given replication controller is not running.
787824
func WaitForRunningDeployerPod(podClient kcoreclient.PodsGetter, rc *api.ReplicationController, timeout time.Duration) error {

pkg/apps/util/util_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,22 @@ func TestRemoveCondition(t *testing.T) {
586586
}
587587
}
588588
}
589+
590+
func TestRolloutExceededTimeoutSeconds(t *testing.T) {
591+
config := deploytest.OkDeploymentConfig(1)
592+
timeoutSeconds := int64(10)
593+
config.Spec.Strategy.RecreateParams.TimeoutSeconds = &timeoutSeconds
594+
deployment, err := MakeDeploymentV1(config, kapi.Codecs.LegacyCodec(deployv1.SchemeGroupVersion))
595+
if err != nil {
596+
t.Fatalf("unexpected error: %v", err)
597+
}
598+
deployment.ObjectMeta.CreationTimestamp = metav1.Time{Time: time.Now().Add(-20 * time.Second)}
599+
if !RolloutExceededTimeoutSeconds(config, deployment) {
600+
t.Errorf("expected rollout timeout")
601+
}
602+
deployment.ObjectMeta.CreationTimestamp = metav1.Time{Time: time.Now().Add(-5 * time.Second)}
603+
if RolloutExceededTimeoutSeconds(config, deployment) {
604+
t.Errorf("unexpected rollout timeout")
605+
}
606+
607+
}

0 commit comments

Comments
 (0)