5
5
"reflect"
6
6
"strconv"
7
7
"strings"
8
+ "time"
8
9
9
10
"k8s.io/api/core/v1"
10
11
"k8s.io/apimachinery/pkg/api/meta"
@@ -60,6 +61,42 @@ func DecodeDeploymentConfig(controller metav1.ObjectMetaAccessor) (*appsv1.Deplo
60
61
return externalConfig , nil
61
62
}
62
63
64
+ // RolloutExceededTimeoutSeconds returns true if the current deployment exceeded
65
+ // the timeoutSeconds defined for its strategy.
66
+ // Note that this is different than activeDeadlineSeconds which is the timeout
67
+ // set for the deployer pod. In some cases, the deployer pod cannot be created
68
+ // (like quota, etc...). In that case deployer controller use this function to
69
+ // measure if the created deployment (RC) exceeded the timeout.
70
+ func RolloutExceededTimeoutSeconds (config * appsv1.DeploymentConfig , latestRC * v1.ReplicationController ) bool {
71
+ timeoutSeconds := GetTimeoutSecondsForStrategy (config )
72
+ // If user set the timeoutSeconds to 0, we assume there should be no timeout.
73
+ if timeoutSeconds <= 0 {
74
+ return false
75
+ }
76
+ return int64 (time .Since (latestRC .CreationTimestamp .Time ).Seconds ()) > timeoutSeconds
77
+ }
78
+
79
+ // GetTimeoutSecondsForStrategy returns the timeout in seconds defined in the
80
+ // deployment config strategy.
81
+ func GetTimeoutSecondsForStrategy (config * appsv1.DeploymentConfig ) int64 {
82
+ var timeoutSeconds int64
83
+ switch config .Spec .Strategy .Type {
84
+ case appsv1 .DeploymentStrategyTypeRolling :
85
+ timeoutSeconds = DefaultRollingTimeoutSeconds
86
+ if t := config .Spec .Strategy .RollingParams .TimeoutSeconds ; t != nil {
87
+ timeoutSeconds = * t
88
+ }
89
+ case appsv1 .DeploymentStrategyTypeRecreate :
90
+ timeoutSeconds = DefaultRecreateTimeoutSeconds
91
+ if t := config .Spec .Strategy .RecreateParams .TimeoutSeconds ; t != nil {
92
+ timeoutSeconds = * t
93
+ }
94
+ case appsv1 .DeploymentStrategyTypeCustom :
95
+ timeoutSeconds = DefaultRecreateTimeoutSeconds
96
+ }
97
+ return timeoutSeconds
98
+ }
99
+
63
100
func NewReplicationControllerScaler (client kubernetes.Interface ) kubectl.Scaler {
64
101
return kubectl .NewScaler (NewReplicationControllerScaleClient (client ))
65
102
}
@@ -80,7 +117,7 @@ func LabelForDeployment(deployment *v1.ReplicationController) string {
80
117
81
118
// DeploymentDesiredReplicas returns number of desired replica for the given replication controller
82
119
func DeploymentDesiredReplicas (obj runtime.Object ) (int32 , bool ) {
83
- return int32AnnotationFor (obj , desiredReplicasAnnotation )
120
+ return int32AnnotationFor (obj , DesiredReplicasAnnotation )
84
121
}
85
122
86
123
// LatestDeploymentNameForConfig returns a stable identifier for deployment config
@@ -94,25 +131,25 @@ func LatestDeploymentNameForConfigAndVersion(name string, version int64) string
94
131
}
95
132
96
133
func DeployerPodNameFor (obj runtime.Object ) string {
97
- return AnnotationFor (obj , deploymentPodAnnotation )
134
+ return AnnotationFor (obj , DeploymentPodAnnotation )
98
135
}
99
136
100
137
func DeploymentConfigNameFor (obj runtime.Object ) string {
101
- return AnnotationFor (obj , deploymentConfigAnnotation )
138
+ return AnnotationFor (obj , DeploymentConfigAnnotation )
102
139
}
103
140
104
141
func DeploymentStatusReasonFor (obj runtime.Object ) string {
105
- return AnnotationFor (obj , deploymentStatusReasonAnnotation )
142
+ return AnnotationFor (obj , DeploymentStatusReasonAnnotation )
106
143
}
107
144
108
145
func DeleteStatusReasons (rc * v1.ReplicationController ) {
109
- delete (rc .Annotations , deploymentStatusReasonAnnotation )
146
+ delete (rc .Annotations , DeploymentStatusReasonAnnotation )
110
147
delete (rc .Annotations , deploymentCancelledAnnotation )
111
148
}
112
149
113
150
func SetCancellationReasons (rc * v1.ReplicationController ) {
114
151
rc .Annotations [deploymentCancelledAnnotation ] = "true"
115
- rc .Annotations [deploymentStatusReasonAnnotation ] = deploymentCancelledByUser
152
+ rc .Annotations [DeploymentStatusReasonAnnotation ] = deploymentCancelledByUser
116
153
}
117
154
118
155
// HasSynced checks if the provided deployment config has been noticed by the deployment
@@ -203,7 +240,7 @@ func ActiveDeployment(input []*v1.ReplicationController) *v1.ReplicationControll
203
240
// TODO: Using the annotation constant for now since the value is correct
204
241
// but we could consider adding a new constant to the public types.
205
242
func ConfigSelector (name string ) labels.Selector {
206
- return labels .SelectorFromValidatedSet (labels.Set {deploymentConfigAnnotation : name })
243
+ return labels .SelectorFromValidatedSet (labels.Set {DeploymentConfigAnnotation : name })
207
244
}
208
245
209
246
// IsCompleteDeployment returns true if the passed deployment is in state complete.
@@ -246,7 +283,7 @@ func DeploymentVersionFor(obj runtime.Object) int64 {
246
283
}
247
284
248
285
func DeploymentNameFor (obj runtime.Object ) string {
249
- return AnnotationFor (obj , deploymentAnnotation )
286
+ return AnnotationFor (obj , DeploymentAnnotation )
250
287
}
251
288
252
289
// GetDeploymentCondition returns the condition with the provided type.
@@ -319,6 +356,33 @@ func HasImageChangeTrigger(config *appsv1.DeploymentConfig) bool {
319
356
return false
320
357
}
321
358
359
+ // CanTransitionPhase returns whether it is allowed to go from the current to the next phase.
360
+ func CanTransitionPhase (current , next DeploymentStatus ) bool {
361
+ switch current {
362
+ case DeploymentStatusNew :
363
+ switch next {
364
+ case DeploymentStatusPending ,
365
+ DeploymentStatusRunning ,
366
+ DeploymentStatusFailed ,
367
+ DeploymentStatusComplete :
368
+ return true
369
+ }
370
+ case DeploymentStatusPending :
371
+ switch next {
372
+ case DeploymentStatusRunning ,
373
+ DeploymentStatusFailed ,
374
+ DeploymentStatusComplete :
375
+ return true
376
+ }
377
+ case DeploymentStatusRunning :
378
+ switch next {
379
+ case DeploymentStatusFailed , DeploymentStatusComplete :
380
+ return true
381
+ }
382
+ }
383
+ return false
384
+ }
385
+
322
386
func int32AnnotationFor (obj runtime.Object , key string ) (int32 , bool ) {
323
387
s := AnnotationFor (obj , key )
324
388
if len (s ) == 0 {
0 commit comments