forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollback_generator.go
68 lines (55 loc) · 2.21 KB
/
rollback_generator.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
package rollback
import (
"fmt"
kapi "k8s.io/kubernetes/pkg/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
// RollbackGenerator generates a new DeploymentConfig by merging a pair of DeploymentConfigs
// in a configurable way.
type RollbackGenerator struct{}
// GenerateRollback creates a new DeploymentConfig by merging to onto from
// based on the options provided by spec. The LatestVersion of the result is
// unconditionally incremented, as rollback candidates are should be possible
// to be deployed manually regardless of other system behavior such as
// triggering.
//
// Any image change triggers on the new config are disabled to prevent
// triggered deployments from immediately replacing the rollback.
func (g *RollbackGenerator) GenerateRollback(from, to *deployapi.DeploymentConfig, spec *deployapi.DeploymentConfigRollbackSpec) (*deployapi.DeploymentConfig, error) {
rollback := &deployapi.DeploymentConfig{}
if err := kapi.Scheme.Convert(&from, &rollback); err != nil {
return nil, fmt.Errorf("couldn't clone 'from' DeploymentConfig: %v", err)
}
// construct the candidate deploymentConfig based on the rollback spec
if spec.IncludeTemplate {
if err := kapi.Scheme.Convert(&to.Spec.Template, &rollback.Spec.Template); err != nil {
return nil, fmt.Errorf("couldn't copy template to rollback:: %v", err)
}
}
if spec.IncludeReplicationMeta {
rollback.Spec.Replicas = to.Spec.Replicas
rollback.Spec.Selector = map[string]string{}
for k, v := range to.Spec.Selector {
rollback.Spec.Selector[k] = v
}
}
if spec.IncludeTriggers {
if err := kapi.Scheme.Convert(&to.Spec.Triggers, &rollback.Spec.Triggers); err != nil {
return nil, fmt.Errorf("couldn't copy triggers to rollback:: %v", err)
}
}
if spec.IncludeStrategy {
if err := kapi.Scheme.Convert(&to.Spec.Strategy, &rollback.Spec.Strategy); err != nil {
return nil, fmt.Errorf("couldn't copy strategy to rollback:: %v", err)
}
}
// Disable any image change triggers.
for _, trigger := range rollback.Spec.Triggers {
if trigger.Type == deployapi.DeploymentTriggerOnImageChange {
trigger.ImageChangeParams.Automatic = false
}
}
// TODO: add a new cause?
rollback.Status.LatestVersion++
return rollback, nil
}