forked from projectatomic/atomic-enterprise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
200 lines (165 loc) · 7.67 KB
/
controller.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
package imagechange
import (
"fmt"
"github.com/golang/glog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
deployapi "github.com/openshift/origin/pkg/deploy/api"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// ImageChangeController increments the version of a DeploymentConfig which has an image
// change trigger when a tag update to a triggered ImageRepository is detected.
//
// Use the ImageChangeControllerFactory to create this controller.
type ImageChangeController struct {
deploymentConfigClient deploymentConfigClient
}
// fatalError is an error which can't be retried.
type fatalError string
func (e fatalError) Error() string { return "fatal error handling imageRepository: " + string(e) }
// Handle processes image change triggers associated with imageRepo.
func (c *ImageChangeController) Handle(imageRepo *imageapi.ImageRepository) error {
configsToGenerate := []*deployapi.DeploymentConfig{}
firedTriggersForConfig := make(map[string][]deployapi.DeploymentTriggerImageChangeParams)
configs, err := c.deploymentConfigClient.listDeploymentConfigs()
if err != nil {
return fmt.Errorf("couldn't get list of deploymentConfigs while handling imageRepo %s: %v", labelForRepo(imageRepo), err)
}
for _, config := range configs {
glog.V(4).Infof("Detecting changed images for deploymentConfig %s", labelFor(config))
// Extract relevant triggers for this imageRepo for this config
triggersForConfig := []deployapi.DeploymentTriggerImageChangeParams{}
for _, trigger := range config.Triggers {
if trigger.Type != deployapi.DeploymentTriggerOnImageChange ||
!trigger.ImageChangeParams.Automatic {
continue
}
if triggerMatchesImage(config, trigger.ImageChangeParams, imageRepo) {
glog.V(4).Infof("Found matching %s trigger for deploymentConfig %s: %#v", trigger.Type, labelFor(config), trigger.ImageChangeParams)
triggersForConfig = append(triggersForConfig, *trigger.ImageChangeParams)
}
}
for _, params := range triggersForConfig {
glog.V(4).Infof("Processing image triggers for deploymentConfig %s", labelFor(config))
containerNames := util.NewStringSet(params.ContainerNames...)
for _, container := range config.Template.ControllerTemplate.Template.Spec.Containers {
if !containerNames.Has(container.Name) {
continue
}
// The container image's tag name is by convention the same as the image ID it references
_, _, _, containerImageID, err := imageapi.SplitDockerPullSpec(container.Image)
if err != nil {
glog.V(4).Infof("Skipping container %s for config %s; container's image is invalid: %v", container.Name, labelFor(config), err)
continue
}
if repoImageID, repoHasTag := imageRepo.Tags[params.Tag]; repoHasTag && repoImageID != containerImageID {
configsToGenerate = append(configsToGenerate, config)
firedTriggersForConfig[config.Name] = append(firedTriggersForConfig[config.Name], params)
}
}
}
}
anyFailed := false
for _, config := range configsToGenerate {
err := c.regenerate(imageRepo, config, firedTriggersForConfig[config.Name])
if err != nil {
anyFailed = true
continue
}
glog.V(4).Infof("Updated deploymentConfig %s in response to image change trigger", labelFor(config))
}
if anyFailed {
return fatalError(fmt.Sprintf("couldn't update some deploymentConfigs for trigger on imageRepo %s", labelForRepo(imageRepo)))
}
glog.V(4).Infof("Updated all configs for trigger on imageRepo %s", labelForRepo(imageRepo))
return nil
}
// triggerMatchesImages decides whether a given trigger for config matches the provided image repo.
// When matching:
// - The trigger From field is preferred over the deprecated RepositoryName field.
// - The namespace of the trigger is preferred over the config's namespace.
func triggerMatchesImage(config *deployapi.DeploymentConfig, trigger *deployapi.DeploymentTriggerImageChangeParams, repo *imageapi.ImageRepository) bool {
if len(trigger.From.Name) > 0 {
namespace := trigger.From.Namespace
if len(namespace) == 0 {
namespace = config.Namespace
}
return repo.Namespace == namespace && repo.Name == trigger.From.Name
}
// This is an invalid state (as one of From.Name or RepositoryName is required), but
// account for it anyway.
if len(trigger.RepositoryName) == 0 {
return false
}
// If the repo's repository information isn't yet available, we can't assume it'll match.
return len(repo.Status.DockerImageRepository) > 0 &&
trigger.RepositoryName == repo.Status.DockerImageRepository
}
func (c *ImageChangeController) regenerate(imageRepo *imageapi.ImageRepository, config *deployapi.DeploymentConfig, triggers []deployapi.DeploymentTriggerImageChangeParams) error {
// Get a regenerated config which includes the new image repo references
newConfig, err := c.deploymentConfigClient.generateDeploymentConfig(config.Namespace, config.Name)
if err != nil {
return fmt.Errorf("error generating new version of deploymentConfig %s: %v", labelFor(config), err)
}
// Update the deployment config with the trigger that resulted in the new config
causes := []*deployapi.DeploymentCause{}
for _, trigger := range triggers {
repoName := trigger.RepositoryName
if len(repoName) == 0 {
if len(imageRepo.Status.DockerImageRepository) == 0 {
// If the trigger relies on a image repo reference, and we don't know what docker repo
// it points at, we can't build a cause for the reference yet.
continue
}
id, ok := imageRepo.Tags[trigger.Tag]
if !ok {
// TODO: not really sure what to do here
}
repoName = fmt.Sprintf("%s:%s", imageRepo.Status.DockerImageRepository, id)
}
causes = append(causes,
&deployapi.DeploymentCause{
Type: deployapi.DeploymentTriggerOnImageChange,
ImageTrigger: &deployapi.DeploymentCauseImageTrigger{
RepositoryName: repoName,
Tag: trigger.Tag,
},
})
}
newConfig.Details = &deployapi.DeploymentDetails{
Causes: causes,
}
// Persist the new config
_, err = c.deploymentConfigClient.updateDeploymentConfig(newConfig.Namespace, newConfig)
if err != nil {
return err
}
return nil
}
func labelForRepo(imageRepo *imageapi.ImageRepository) string {
return fmt.Sprintf("%s/%s", imageRepo.Namespace, imageRepo.Name)
}
// labelFor builds a string identifier for a DeploymentConfig.
func labelFor(config *deployapi.DeploymentConfig) string {
return fmt.Sprintf("%s/%s:%d", config.Namespace, config.Name, config.LatestVersion)
}
// ImageChangeControllerDeploymentConfigClient abstracts access to DeploymentConfigs.
type deploymentConfigClient interface {
listDeploymentConfigs() ([]*deployapi.DeploymentConfig, error)
updateDeploymentConfig(namespace string, config *deployapi.DeploymentConfig) (*deployapi.DeploymentConfig, error)
generateDeploymentConfig(namespace, name string) (*deployapi.DeploymentConfig, error)
}
// ImageChangeControllerDeploymentConfigClientImpl is a pluggable ChangeStrategy.
type deploymentConfigClientImpl struct {
listDeploymentConfigsFunc func() ([]*deployapi.DeploymentConfig, error)
generateDeploymentConfigFunc func(namespace, name string) (*deployapi.DeploymentConfig, error)
updateDeploymentConfigFunc func(namespace string, config *deployapi.DeploymentConfig) (*deployapi.DeploymentConfig, error)
}
func (i *deploymentConfigClientImpl) listDeploymentConfigs() ([]*deployapi.DeploymentConfig, error) {
return i.listDeploymentConfigsFunc()
}
func (i *deploymentConfigClientImpl) generateDeploymentConfig(namespace, name string) (*deployapi.DeploymentConfig, error) {
return i.generateDeploymentConfigFunc(namespace, name)
}
func (i *deploymentConfigClientImpl) updateDeploymentConfig(namespace string, config *deployapi.DeploymentConfig) (*deployapi.DeploymentConfig, error) {
return i.updateDeploymentConfigFunc(namespace, config)
}