Skip to content

Commit fc7d8d2

Browse files
committed
apps: move deployer controller to external types
1 parent 1dfb60b commit fc7d8d2

File tree

7 files changed

+205
-136
lines changed

7 files changed

+205
-136
lines changed

pkg/apps/controller/deployer/deployer_controller.go

Lines changed: 100 additions & 104 deletions
Large diffs are not rendered by default.

pkg/apps/util/const.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
// DefaultRecreateTimeoutSeconds is the default TimeoutSeconds for RecreateDeploymentStrategyParams.
6767
// Used by strategies:
6868
DefaultRecreateTimeoutSeconds int64 = 10 * 60
69+
DefaultRollingTimeoutSeconds int64 = 10 * 60
6970

7071
// PreHookPodSuffix is the suffix added to all pre hook pods
7172
PreHookPodSuffix = "hook-pre"
@@ -76,18 +77,27 @@ const (
7677

7778
// Used only internally by utils:
7879

79-
// deploymentStatusReasonAnnotation represents the reason for deployment being in a given state
80+
// DeploymentStatusReasonAnnotation represents the reason for deployment being in a given state
8081
// Used for specifying the reason for cancellation or failure of a deployment
81-
deploymentStatusReasonAnnotation = "openshift.io/deployment.status-reason"
82-
deploymentCancelledAnnotation = "openshift.io/deployment.cancelled"
83-
deploymentCancelledByUser = "cancelled by the user"
82+
DeploymentStatusReasonAnnotation = "openshift.io/deployment.status-reason"
83+
DeploymentIgnorePodAnnotation = "deploy.openshift.io/deployer-pod.ignore"
84+
DeploymentPodAnnotation = "openshift.io/deployer-pod.name"
85+
DeployerPodCreatedAtAnnotation = "openshift.io/deployer-pod.created-at"
86+
DeployerPodStartedAtAnnotation = "openshift.io/deployer-pod.started-at"
87+
DeployerPodCompletedAtAnnotation = "openshift.io/deployer-pod.completed-at"
88+
DeploymentReplicasAnnotation = "openshift.io/deployment.replicas"
89+
DesiredReplicasAnnotation = "kubectl.kubernetes.io/desired-replicas"
90+
DeploymentAnnotation = "openshift.io/deployment.name"
91+
DeploymentConfigAnnotation = "openshift.io/deployment-config.name"
92+
93+
DeploymentFailedUnrelatedDeploymentExists = "unrelated pod with the same name as this deployment is already running"
94+
DeploymentFailedUnableToCreateDeployerPod = "unable to create deployer pod"
95+
DeploymentFailedDeployerPodNoLongerExists = "deployer pod no longer exists"
96+
97+
deploymentCancelledAnnotation = "openshift.io/deployment.cancelled"
98+
deploymentCancelledByUser = "cancelled by the user"
8499

85-
deploymentIgnorePodAnnotation = "deploy.openshift.io/deployer-pod.ignore"
86100
deploymentEncodedConfigAnnotation = "openshift.io/encoded-deployment-config"
87-
deploymentPodAnnotation = "openshift.io/deployer-pod.name"
88-
deploymentAnnotation = "openshift.io/deployment.name"
89-
desiredReplicasAnnotation = "kubectl.kubernetes.io/desired-replicas"
90-
deploymentReplicasAnnotation = "openshift.io/deployment.replicas"
91-
deploymentConfigAnnotation = "openshift.io/deployment-config.name"
92-
deploymentVersionAnnotation = "openshift.io/deployment-config.latest-version"
101+
102+
deploymentVersionAnnotation = "openshift.io/deployment-config.latest-version"
93103
)

pkg/apps/util/deployment.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func MakeDeployment(config *appsv1.DeploymentConfig) (*v1.ReplicationController,
5757
// Correlate the deployment with the config.
5858
// TODO: Using the annotation constant for now since the value is correct
5959
// but we could consider adding a new constant to the public types.
60-
controllerLabels[deploymentConfigAnnotation] = config.Name
60+
controllerLabels[DeploymentConfigAnnotation] = config.Name
6161

6262
// Ensure that pods created by this deployment controller can be safely associated back
6363
// to the controller, and that multiple deployment controllers for the same config don't
@@ -80,8 +80,8 @@ func MakeDeployment(config *appsv1.DeploymentConfig) (*v1.ReplicationController,
8080
for k, v := range config.Spec.Template.Annotations {
8181
podAnnotations[k] = v
8282
}
83-
podAnnotations[deploymentAnnotation] = deploymentName
84-
podAnnotations[deploymentConfigAnnotation] = config.Name
83+
podAnnotations[DeploymentAnnotation] = deploymentName
84+
podAnnotations[DeploymentConfigAnnotation] = config.Name
8585
podAnnotations[deploymentVersionAnnotation] = strconv.FormatInt(config.Status.LatestVersion, 10)
8686

8787
controllerRef := newControllerRef(config)
@@ -91,13 +91,13 @@ func MakeDeployment(config *appsv1.DeploymentConfig) (*v1.ReplicationController,
9191
Name: deploymentName,
9292
Namespace: config.Namespace,
9393
Annotations: map[string]string{
94-
deploymentConfigAnnotation: config.Name,
94+
DeploymentConfigAnnotation: config.Name,
9595
deploymentEncodedConfigAnnotation: string(encodedConfig),
9696
DeploymentStatusAnnotation: string(DeploymentStatusNew),
9797
deploymentVersionAnnotation: strconv.FormatInt(config.Status.LatestVersion, 10),
9898
// This is the target replica count for the new deployment.
99-
desiredReplicasAnnotation: strconv.Itoa(int(config.Spec.Replicas)),
100-
deploymentReplicasAnnotation: strconv.Itoa(0),
99+
DesiredReplicasAnnotation: strconv.Itoa(int(config.Spec.Replicas)),
100+
DeploymentReplicasAnnotation: strconv.Itoa(0),
101101
},
102102
Labels: controllerLabels,
103103
OwnerReferences: []metav1.OwnerReference{*controllerRef},
@@ -117,10 +117,10 @@ func MakeDeployment(config *appsv1.DeploymentConfig) (*v1.ReplicationController,
117117
},
118118
}
119119
if config.Status.Details != nil && len(config.Status.Details.Message) > 0 {
120-
deployment.Annotations[deploymentStatusReasonAnnotation] = config.Status.Details.Message
120+
deployment.Annotations[DeploymentStatusReasonAnnotation] = config.Status.Details.Message
121121
}
122-
if value, ok := config.Annotations[deploymentIgnorePodAnnotation]; ok {
123-
deployment.Annotations[deploymentIgnorePodAnnotation] = value
122+
if value, ok := config.Annotations[DeploymentIgnorePodAnnotation]; ok {
123+
deployment.Annotations[DeploymentIgnorePodAnnotation] = value
124124
}
125125

126126
return deployment, nil

pkg/apps/util/util.go

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"strconv"
77
"strings"
8+
"time"
89

910
"k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/api/meta"
@@ -60,6 +61,42 @@ func DecodeDeploymentConfig(controller metav1.ObjectMetaAccessor) (*appsv1.Deplo
6061
return externalConfig, nil
6162
}
6263

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+
63100
func NewReplicationControllerScaler(client kubernetes.Interface) kubectl.Scaler {
64101
return kubectl.NewScaler(NewReplicationControllerScaleClient(client))
65102
}
@@ -80,7 +117,7 @@ func LabelForDeployment(deployment *v1.ReplicationController) string {
80117

81118
// DeploymentDesiredReplicas returns number of desired replica for the given replication controller
82119
func DeploymentDesiredReplicas(obj runtime.Object) (int32, bool) {
83-
return int32AnnotationFor(obj, desiredReplicasAnnotation)
120+
return int32AnnotationFor(obj, DesiredReplicasAnnotation)
84121
}
85122

86123
// LatestDeploymentNameForConfig returns a stable identifier for deployment config
@@ -94,25 +131,25 @@ func LatestDeploymentNameForConfigAndVersion(name string, version int64) string
94131
}
95132

96133
func DeployerPodNameFor(obj runtime.Object) string {
97-
return AnnotationFor(obj, deploymentPodAnnotation)
134+
return AnnotationFor(obj, DeploymentPodAnnotation)
98135
}
99136

100137
func DeploymentConfigNameFor(obj runtime.Object) string {
101-
return AnnotationFor(obj, deploymentConfigAnnotation)
138+
return AnnotationFor(obj, DeploymentConfigAnnotation)
102139
}
103140

104141
func DeploymentStatusReasonFor(obj runtime.Object) string {
105-
return AnnotationFor(obj, deploymentStatusReasonAnnotation)
142+
return AnnotationFor(obj, DeploymentStatusReasonAnnotation)
106143
}
107144

108145
func DeleteStatusReasons(rc *v1.ReplicationController) {
109-
delete(rc.Annotations, deploymentStatusReasonAnnotation)
146+
delete(rc.Annotations, DeploymentStatusReasonAnnotation)
110147
delete(rc.Annotations, deploymentCancelledAnnotation)
111148
}
112149

113150
func SetCancellationReasons(rc *v1.ReplicationController) {
114151
rc.Annotations[deploymentCancelledAnnotation] = "true"
115-
rc.Annotations[deploymentStatusReasonAnnotation] = deploymentCancelledByUser
152+
rc.Annotations[DeploymentStatusReasonAnnotation] = deploymentCancelledByUser
116153
}
117154

118155
// HasSynced checks if the provided deployment config has been noticed by the deployment
@@ -203,7 +240,7 @@ func ActiveDeployment(input []*v1.ReplicationController) *v1.ReplicationControll
203240
// TODO: Using the annotation constant for now since the value is correct
204241
// but we could consider adding a new constant to the public types.
205242
func ConfigSelector(name string) labels.Selector {
206-
return labels.SelectorFromValidatedSet(labels.Set{deploymentConfigAnnotation: name})
243+
return labels.SelectorFromValidatedSet(labels.Set{DeploymentConfigAnnotation: name})
207244
}
208245

209246
// IsCompleteDeployment returns true if the passed deployment is in state complete.
@@ -246,7 +283,7 @@ func DeploymentVersionFor(obj runtime.Object) int64 {
246283
}
247284

248285
func DeploymentNameFor(obj runtime.Object) string {
249-
return AnnotationFor(obj, deploymentAnnotation)
286+
return AnnotationFor(obj, DeploymentAnnotation)
250287
}
251288

252289
// GetDeploymentCondition returns the condition with the provided type.
@@ -319,6 +356,33 @@ func HasImageChangeTrigger(config *appsv1.DeploymentConfig) bool {
319356
return false
320357
}
321358

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+
322386
func int32AnnotationFor(obj runtime.Object, key string) (int32, bool) {
323387
s := AnnotationFor(obj, key)
324388
if len(s) == 0 {

pkg/oc/cli/deploy/deploy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ func (o DeployOptions) RunDeploy() error {
234234
if o.follow {
235235
return o.getLogs(config)
236236
}
237-
// TODO: HERE
238237
describer := describe.NewLatestDeploymentsDescriber(o.appsClient, o.kubeClient, -1)
239238
desc, err := describer.Describe(config.Namespace, config.Name)
240239
if err != nil {
@@ -343,7 +342,7 @@ func (o DeployOptions) retry(config *appsv1.DeploymentConfig) error {
343342
}
344343
}
345344

346-
deployment.Annotations[appsutil.DeploymentStatusAnnotation] = "New"
345+
deployment.Annotations[appsutil.DeploymentStatusAnnotation] = string(appsutil.DeploymentStatusNew)
347346
// clear out the cancellation flag as well as any previous status-reason annotation
348347
appsutil.DeleteStatusReasons(deployment)
349348
_, err = o.kubeClient.CoreV1().ReplicationControllers(deployment.Namespace).Update(deployment)

pkg/oc/cli/rollout/cancel.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ func (o CancelOptions) Run() error {
232232
}
233233

234234
func (o CancelOptions) forEachControllerInConfig(namespace, name string, mutateFunc func(*corev1.ReplicationController) bool) ([]*corev1.ReplicationController, bool, error) {
235-
deploymentList, err := o.KubeClient.CoreV1().ReplicationControllers(namespace).List(metav1.ListOptions{LabelSelector: appsutil.ConfigSelector(
236-
name).String()})
235+
deploymentList, err := o.KubeClient.CoreV1().ReplicationControllers(namespace).List(metav1.ListOptions{LabelSelector: appsutil.ConfigSelector(name).String()})
237236
if err != nil {
238237
return nil, false, err
239238
}

pkg/oc/lib/describe/deployments_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestDeploymentConfigDescriber(t *testing.T) {
6363
}
6464

6565
podList.Items = []corev1.Pod{*mkV1Pod(corev1.PodRunning, 0)}
66+
// TODO: re-enable when we switch describer to external client
6667
/*
6768
substr := "Autoscaling:\tbetween 1 and 3 replicas"
6869
if !strings.Contains(out, substr) {

0 commit comments

Comments
 (0)