forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
355 lines (307 loc) · 12.6 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package util
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
deployapi "github.com/openshift/origin/pkg/deploy/api"
"github.com/openshift/origin/pkg/util/namer"
)
// LatestDeploymentNameForConfig returns a stable identifier for config based on its version.
func LatestDeploymentNameForConfig(config *deployapi.DeploymentConfig) string {
return fmt.Sprintf("%s-%d", config.Name, config.Status.LatestVersion)
}
// LatestDeploymentInfo returns info about the latest deployment for a config,
// or nil if there is no latest deployment. The latest deployment is not
// always the same as the active deployment.
func LatestDeploymentInfo(config *deployapi.DeploymentConfig, deployments *api.ReplicationControllerList) (bool, *api.ReplicationController) {
if config.Status.LatestVersion == 0 || len(deployments.Items) == 0 {
return false, nil
}
sort.Sort(ByLatestVersionDesc(deployments.Items))
candidate := &deployments.Items[0]
return DeploymentVersionFor(candidate) == config.Status.LatestVersion, candidate
}
// ActiveDeployment returns the latest complete deployment, or nil if there is
// no such deployment. The active deployment is not always the same as the
// latest deployment.
func ActiveDeployment(config *deployapi.DeploymentConfig, deployments *api.ReplicationControllerList) *api.ReplicationController {
sort.Sort(ByLatestVersionDesc(deployments.Items))
var activeDeployment *api.ReplicationController
for _, deployment := range deployments.Items {
if DeploymentStatusFor(&deployment) == deployapi.DeploymentStatusComplete {
activeDeployment = &deployment
break
}
}
return activeDeployment
}
// DeployerPodSuffix is the suffix added to pods created from a deployment
const DeployerPodSuffix = "deploy"
// DeployerPodNameForDeployment returns the name of a pod for a given deployment
func DeployerPodNameForDeployment(deployment string) string {
return namer.GetPodName(deployment, DeployerPodSuffix)
}
// LabelForDeployment builds a string identifier for a Deployment.
func LabelForDeployment(deployment *api.ReplicationController) string {
return fmt.Sprintf("%s/%s", deployment.Namespace, deployment.Name)
}
// LabelForDeploymentConfig builds a string identifier for a DeploymentConfig.
func LabelForDeploymentConfig(config *deployapi.DeploymentConfig) string {
return fmt.Sprintf("%s/%s", config.Namespace, config.Name)
}
// DeploymentNameForConfigVersion returns the name of the version-th deployment
// for the config that has the provided name
func DeploymentNameForConfigVersion(name string, version int) string {
return fmt.Sprintf("%s-%d", name, version)
}
// ConfigSelector returns a label Selector which can be used to find all
// deployments for a DeploymentConfig.
//
// TODO: Using the annotation constant for now since the value is correct
// but we could consider adding a new constant to the public types.
func ConfigSelector(name string) labels.Selector {
return labels.Set{deployapi.DeploymentConfigAnnotation: name}.AsSelector()
}
// DeployerPodSelector returns a label Selector which can be used to find all
// deployer pods associated with a deployment with name.
func DeployerPodSelector(name string) labels.Selector {
return labels.Set{deployapi.DeployerPodForDeploymentLabel: name}.AsSelector()
}
// AnyDeployerPodSelector returns a label Selector which can be used to find
// all deployer pods across all deployments, including hook and custom
// deployer pods.
func AnyDeployerPodSelector() labels.Selector {
sel, _ := labels.Parse(deployapi.DeployerPodForDeploymentLabel)
return sel
}
// HasChangeTrigger returns whether the provided deployment configuration has
// a config change trigger or not
func HasChangeTrigger(config *deployapi.DeploymentConfig) bool {
for _, trigger := range config.Spec.Triggers {
if trigger.Type == deployapi.DeploymentTriggerOnConfigChange {
return true
}
}
return false
}
// CauseFromAutomaticImageChange inspects any existing deployment config cause and
// validates if it comes from the image change controller.
func CauseFromAutomaticImageChange(config *deployapi.DeploymentConfig) bool {
if config.Status.Details != nil && len(config.Status.Details.Causes) > 0 {
for _, trigger := range config.Spec.Triggers {
if trigger.Type == deployapi.DeploymentTriggerOnImageChange &&
trigger.ImageChangeParams.Automatic &&
config.Status.Details.Causes[0].Type == deployapi.DeploymentTriggerOnImageChange &&
reflect.DeepEqual(trigger.ImageChangeParams.From, config.Status.Details.Causes[0].ImageTrigger.From) {
return true
}
}
}
return false
}
// DecodeDeploymentConfig decodes a DeploymentConfig from controller using codec. An error is returned
// if the controller doesn't contain an encoded config.
func DecodeDeploymentConfig(controller *api.ReplicationController, decoder runtime.Decoder) (*deployapi.DeploymentConfig, error) {
encodedConfig := []byte(EncodedDeploymentConfigFor(controller))
if decoded, err := runtime.Decode(decoder, encodedConfig); err == nil {
if config, ok := decoded.(*deployapi.DeploymentConfig); ok {
return config, nil
} else {
return nil, fmt.Errorf("decoded DeploymentConfig from controller is not a DeploymentConfig: %v", err)
}
} else {
return nil, fmt.Errorf("failed to decode DeploymentConfig from controller: %v", err)
}
}
// EncodeDeploymentConfig encodes config as a string using codec.
func EncodeDeploymentConfig(config *deployapi.DeploymentConfig, codec runtime.Codec) (string, error) {
if bytes, err := runtime.Encode(codec, config); err == nil {
return string(bytes[:]), nil
} else {
return "", err
}
}
// MakeDeployment creates a deployment represented as a ReplicationController and based on the given
// DeploymentConfig. The controller replica count will be zero.
func MakeDeployment(config *deployapi.DeploymentConfig, codec runtime.Codec) (*api.ReplicationController, error) {
var err error
var encodedConfig string
if encodedConfig, err = EncodeDeploymentConfig(config, codec); err != nil {
return nil, err
}
deploymentName := LatestDeploymentNameForConfig(config)
podSpec := api.PodSpec{}
if err := api.Scheme.Convert(&config.Spec.Template.Spec, &podSpec); err != nil {
return nil, fmt.Errorf("couldn't clone podSpec: %v", err)
}
controllerLabels := make(labels.Set)
for k, v := range config.Labels {
controllerLabels[k] = v
}
// Correlate the deployment with the config.
// TODO: Using the annotation constant for now since the value is correct
// but we could consider adding a new constant to the public types.
controllerLabels[deployapi.DeploymentConfigAnnotation] = config.Name
// Ensure that pods created by this deployment controller can be safely associated back
// to the controller, and that multiple deployment controllers for the same config don't
// manipulate each others' pods.
selector := map[string]string{}
for k, v := range config.Spec.Selector {
selector[k] = v
}
selector[deployapi.DeploymentConfigLabel] = config.Name
selector[deployapi.DeploymentLabel] = deploymentName
podLabels := make(labels.Set)
for k, v := range config.Spec.Template.Labels {
podLabels[k] = v
}
podLabels[deployapi.DeploymentConfigLabel] = config.Name
podLabels[deployapi.DeploymentLabel] = deploymentName
podAnnotations := make(labels.Set)
for k, v := range config.Spec.Template.Annotations {
podAnnotations[k] = v
}
podAnnotations[deployapi.DeploymentAnnotation] = deploymentName
podAnnotations[deployapi.DeploymentConfigAnnotation] = config.Name
podAnnotations[deployapi.DeploymentVersionAnnotation] = strconv.Itoa(config.Status.LatestVersion)
deployment := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: deploymentName,
Annotations: map[string]string{
deployapi.DeploymentConfigAnnotation: config.Name,
deployapi.DeploymentStatusAnnotation: string(deployapi.DeploymentStatusNew),
deployapi.DeploymentEncodedConfigAnnotation: encodedConfig,
deployapi.DeploymentVersionAnnotation: strconv.Itoa(config.Status.LatestVersion),
// This is the target replica count for the new deployment.
deployapi.DesiredReplicasAnnotation: strconv.Itoa(config.Spec.Replicas),
deployapi.DeploymentReplicasAnnotation: strconv.Itoa(0),
},
Labels: controllerLabels,
},
Spec: api.ReplicationControllerSpec{
// The deployment should be inactive initially
Replicas: 0,
Selector: selector,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: podLabels,
Annotations: podAnnotations,
},
Spec: podSpec,
},
},
}
return deployment, nil
}
func DeploymentConfigNameFor(obj runtime.Object) string {
return annotationFor(obj, deployapi.DeploymentConfigAnnotation)
}
func DeploymentNameFor(obj runtime.Object) string {
return annotationFor(obj, deployapi.DeploymentAnnotation)
}
func DeployerPodNameFor(obj runtime.Object) string {
return annotationFor(obj, deployapi.DeploymentPodAnnotation)
}
func DeploymentStatusFor(obj runtime.Object) deployapi.DeploymentStatus {
return deployapi.DeploymentStatus(annotationFor(obj, deployapi.DeploymentStatusAnnotation))
}
func DeploymentStatusReasonFor(obj runtime.Object) string {
return annotationFor(obj, deployapi.DeploymentStatusReasonAnnotation)
}
func DeploymentDesiredReplicas(obj runtime.Object) (int, bool) {
return intAnnotationFor(obj, deployapi.DesiredReplicasAnnotation)
}
func DeploymentReplicas(obj runtime.Object) (int, bool) {
return intAnnotationFor(obj, deployapi.DeploymentReplicasAnnotation)
}
func EncodedDeploymentConfigFor(obj runtime.Object) string {
return annotationFor(obj, deployapi.DeploymentEncodedConfigAnnotation)
}
func DeploymentVersionFor(obj runtime.Object) int {
v, err := strconv.Atoi(annotationFor(obj, deployapi.DeploymentVersionAnnotation))
if err != nil {
return -1
}
return v
}
func IsDeploymentCancelled(deployment *api.ReplicationController) bool {
value := annotationFor(deployment, deployapi.DeploymentCancelledAnnotation)
return strings.EqualFold(value, deployapi.DeploymentCancelledAnnotationValue)
}
// IsTerminatedDeployment returns true if the passed deployment has terminated (either
// complete or failed).
func IsTerminatedDeployment(deployment *api.ReplicationController) bool {
current := DeploymentStatusFor(deployment)
return current == deployapi.DeploymentStatusComplete || current == deployapi.DeploymentStatusFailed
}
// CanTransitionPhase returns whether it is allowed to go from the current to the next phase.
func CanTransitionPhase(current, next deployapi.DeploymentStatus) bool {
switch current {
case deployapi.DeploymentStatusNew:
switch next {
case deployapi.DeploymentStatusPending,
deployapi.DeploymentStatusRunning,
deployapi.DeploymentStatusFailed,
deployapi.DeploymentStatusComplete:
return true
}
case deployapi.DeploymentStatusPending:
switch next {
case deployapi.DeploymentStatusRunning,
deployapi.DeploymentStatusFailed,
deployapi.DeploymentStatusComplete:
return true
}
case deployapi.DeploymentStatusRunning:
switch next {
case deployapi.DeploymentStatusFailed, deployapi.DeploymentStatusComplete:
return true
}
}
return false
}
// annotationFor returns the annotation with key for obj.
func annotationFor(obj runtime.Object, key string) string {
meta, err := api.ObjectMetaFor(obj)
if err != nil {
return ""
}
return meta.Annotations[key]
}
func intAnnotationFor(obj runtime.Object, key string) (int, bool) {
s := annotationFor(obj, key)
if len(s) == 0 {
return 0, false
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, false
}
return i, true
}
// ByLatestVersionAsc sorts deployments by LatestVersion ascending.
type ByLatestVersionAsc []api.ReplicationController
func (d ByLatestVersionAsc) Len() int { return len(d) }
func (d ByLatestVersionAsc) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d ByLatestVersionAsc) Less(i, j int) bool {
return DeploymentVersionFor(&d[i]) < DeploymentVersionFor(&d[j])
}
// ByLatestVersionDesc sorts deployments by LatestVersion descending.
type ByLatestVersionDesc []api.ReplicationController
func (d ByLatestVersionDesc) Len() int { return len(d) }
func (d ByLatestVersionDesc) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d ByLatestVersionDesc) Less(i, j int) bool {
return DeploymentVersionFor(&d[j]) < DeploymentVersionFor(&d[i])
}
// ByMostRecent sorts deployments by most recently created.
type ByMostRecent []*api.ReplicationController
func (s ByMostRecent) Len() int { return len(s) }
func (s ByMostRecent) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByMostRecent) Less(i, j int) bool {
return !s[i].CreationTimestamp.Before(s[j].CreationTimestamp)
}