forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploymentconfigs.go
259 lines (232 loc) · 7.75 KB
/
deploymentconfigs.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
package deploymentconfigs
import (
"fmt"
"reflect"
"strings"
"github.com/golang/glog"
"k8s.io/client-go/tools/cache"
kapi "k8s.io/kubernetes/pkg/api"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
appsclient "github.com/openshift/origin/pkg/apps/generated/internalclientset/typed/apps/internalversion"
triggerapi "github.com/openshift/origin/pkg/image/apis/image/v1/trigger"
"github.com/openshift/origin/pkg/image/trigger"
)
func indicesForContainerNames(containers []kapi.Container, names []string) []int {
var index []int
for _, name := range names {
for i, container := range containers {
if name == container.Name {
index = append(index, i)
}
}
}
return index
}
func namesInclude(names []string, name string) bool {
for _, n := range names {
if name == n {
return true
}
}
return false
}
// calculateDeploymentConfigTrigger resolves a particular trigger against the deployment config and extracts a list of triggers.
// It will silently ignore triggers that do not point to valid container. It returns empty if no triggers can be found.
func calculateDeploymentConfigTrigger(t appsapi.DeploymentTriggerPolicy, dc *appsapi.DeploymentConfig) []triggerapi.ObjectFieldTrigger {
if t.ImageChangeParams == nil {
return nil
}
from := t.ImageChangeParams.From
if from.Kind != "ImageStreamTag" || len(from.Name) == 0 {
return nil
}
var triggers []triggerapi.ObjectFieldTrigger
// add one trigger per init container and container
for _, index := range indicesForContainerNames(dc.Spec.Template.Spec.Containers, t.ImageChangeParams.ContainerNames) {
fieldPath := fmt.Sprintf("spec.template.spec.containers[@name==\"%s\"].image", dc.Spec.Template.Spec.Containers[index].Name)
triggers = append(triggers, triggerapi.ObjectFieldTrigger{
From: triggerapi.ObjectReference{
Name: from.Name,
Namespace: from.Namespace,
Kind: from.Kind,
APIVersion: from.APIVersion,
},
FieldPath: fieldPath,
Paused: !t.ImageChangeParams.Automatic,
})
}
for _, index := range indicesForContainerNames(dc.Spec.Template.Spec.InitContainers, t.ImageChangeParams.ContainerNames) {
fieldPath := fmt.Sprintf("spec.template.spec.initContainers[@name==\"%s\"].image", dc.Spec.Template.Spec.Containers[index].Name)
triggers = append(triggers, triggerapi.ObjectFieldTrigger{
From: triggerapi.ObjectReference{
Name: from.Name,
Namespace: from.Namespace,
Kind: from.Kind,
APIVersion: from.APIVersion,
},
FieldPath: fieldPath,
Paused: !t.ImageChangeParams.Automatic,
})
}
return triggers
}
// calculateDeploymentConfigTriggers transforms a deployment config into a set of image change triggers. If retrieveChanges
// is true an array of the latest state of the triggers will be returned.
func calculateDeploymentConfigTriggers(dc *appsapi.DeploymentConfig) []triggerapi.ObjectFieldTrigger {
var triggers []triggerapi.ObjectFieldTrigger
for _, t := range dc.Spec.Triggers {
addedTriggers := calculateDeploymentConfigTrigger(t, dc)
triggers = append(triggers, addedTriggers...)
}
return triggers
}
// deploymentConfigTriggerIndexer converts deployment config events into entries for the trigger cache, and
// also calculates the latest state of the changes on the object.
type deploymentConfigTriggerIndexer struct {
prefix string
}
func NewDeploymentConfigTriggerIndexer(prefix string) trigger.Indexer {
return deploymentConfigTriggerIndexer{prefix: prefix}
}
func (i deploymentConfigTriggerIndexer) Index(obj, old interface{}) (string, *trigger.CacheEntry, cache.DeltaType, error) {
var (
triggers []triggerapi.ObjectFieldTrigger
dc *appsapi.DeploymentConfig
change cache.DeltaType
)
switch {
case obj != nil && old == nil:
// added
dc = obj.(*appsapi.DeploymentConfig)
triggers = calculateDeploymentConfigTriggers(dc)
change = cache.Added
case old != nil && obj == nil:
// deleted
dc = old.(*appsapi.DeploymentConfig)
triggers = calculateDeploymentConfigTriggers(dc)
change = cache.Deleted
default:
// updated
dc = obj.(*appsapi.DeploymentConfig)
triggers = calculateDeploymentConfigTriggers(dc)
oldTriggers := calculateDeploymentConfigTriggers(old.(*appsapi.DeploymentConfig))
switch {
case len(oldTriggers) == 0:
change = cache.Added
case !reflect.DeepEqual(oldTriggers, triggers):
change = cache.Updated
}
}
if len(triggers) > 0 {
key := i.prefix + dc.Namespace + "/" + dc.Name
return key, &trigger.CacheEntry{
Key: key,
Namespace: dc.Namespace,
Triggers: triggers,
}, change, nil
}
return "", nil, change, nil
}
// DeploymentConfigReactor converts image trigger changes into updates on deployments.
type DeploymentConfigReactor struct {
Client appsclient.DeploymentConfigsGetter
}
// UpdateDeploymentConfigImages sets the latest image value from all triggers onto each container, returning false if
// one or more triggers could not be resolved yet or an error. The returned dc will be copied if mutated.
func UpdateDeploymentConfigImages(dc *appsapi.DeploymentConfig, tagRetriever trigger.TagRetriever) (*appsapi.DeploymentConfig, bool, error) {
var updated *appsapi.DeploymentConfig
// copy the object and reset dc to the copy
copyObject := func() {
if updated != nil {
return
}
copied, err := kapi.Scheme.Copy(dc)
if err != nil {
return
}
dc = copied.(*appsapi.DeploymentConfig)
updated = dc
}
for i, t := range dc.Spec.Triggers {
p := t.ImageChangeParams
if p == nil || p.From.Kind != "ImageStreamTag" {
continue
}
if !p.Automatic {
continue
}
namespace := p.From.Namespace
if len(namespace) == 0 {
namespace = dc.Namespace
}
ref, _, ok := tagRetriever.ImageStreamTag(namespace, p.From.Name)
if !ok && len(p.LastTriggeredImage) == 0 {
glog.V(4).Infof("trigger %#v in deployment %s is not resolveable", p, dc.Name)
return nil, false, nil
}
if ref == p.LastTriggeredImage {
continue
}
if len(ref) == 0 {
ref = p.LastTriggeredImage
}
if p.LastTriggeredImage != ref {
copyObject()
p = dc.Spec.Triggers[i].ImageChangeParams
p.LastTriggeredImage = ref
}
for i, c := range dc.Spec.Template.Spec.InitContainers {
if !namesInclude(p.ContainerNames, c.Name) || c.Image == ref {
continue
}
copyObject()
container := &dc.Spec.Template.Spec.InitContainers[i]
container.Image = ref
}
for i, c := range dc.Spec.Template.Spec.Containers {
if !namesInclude(p.ContainerNames, c.Name) || c.Image == ref {
continue
}
copyObject()
container := &dc.Spec.Template.Spec.Containers[i]
container.Image = ref
}
}
return updated, true, nil
}
// ImageChanged is passed a deployment config and a set of changes.
func (r *DeploymentConfigReactor) ImageChanged(obj interface{}, tagRetriever trigger.TagRetriever) error {
dc := obj.(*appsapi.DeploymentConfig)
copied, err := kapi.Scheme.DeepCopy(dc)
if err != nil {
return err
}
newDC := copied.(*appsapi.DeploymentConfig)
updated, resolvable, err := UpdateDeploymentConfigImages(newDC, tagRetriever)
if err != nil {
return err
}
if !resolvable {
if glog.V(4) {
glog.Infof("Ignoring changes to deployment config %s, has unresolved images: %s", dc.Name, printDeploymentTriggers(newDC.Spec.Triggers))
}
return nil
}
if updated == nil {
glog.V(4).Infof("Deployment config %s has not changed", dc.Name)
return nil
}
glog.V(4).Infof("Deployment config %s has changed", dc.Name)
_, err = r.Client.DeploymentConfigs(dc.Namespace).Update(updated)
return err
}
func printDeploymentTriggers(triggers []appsapi.DeploymentTriggerPolicy) string {
var values []string
for _, t := range triggers {
if t.ImageChangeParams == nil {
continue
}
values = append(values, fmt.Sprintf("[from=%s last=%s]", t.ImageChangeParams.From.Name, t.ImageChangeParams.LastTriggeredImage))
}
return strings.Join(values, ", ")
}