forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pods.go
454 lines (424 loc) · 16.8 KB
/
pods.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package meta
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
kapi "k8s.io/kubernetes/pkg/api"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/apps"
appsv1beta1 "k8s.io/kubernetes/pkg/apis/apps/v1beta1"
"k8s.io/kubernetes/pkg/apis/batch"
batchv1 "k8s.io/kubernetes/pkg/apis/batch/v1"
batchv2alpha1 "k8s.io/kubernetes/pkg/apis/batch/v2alpha1"
"k8s.io/kubernetes/pkg/apis/extensions"
extensionsv1beta1 "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
deployapi "github.com/openshift/origin/pkg/apps/apis/apps"
deployapiv1 "github.com/openshift/origin/pkg/apps/apis/apps/v1"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
securityapiv1 "github.com/openshift/origin/pkg/security/apis/security/v1"
)
type ContainerMutator interface {
GetName() string
GetImage() string
SetImage(image string)
}
type PodSpecReferenceMutator interface {
GetContainerByIndex(init bool, i int) (ContainerMutator, bool)
GetContainerByName(name string) (ContainerMutator, bool)
Path() *field.Path
}
// GetPodSpecReferenceMutator returns a mutator for the provided object, or an error if no
// such mutator is defined.
func GetPodSpecReferenceMutator(obj runtime.Object) (PodSpecReferenceMutator, error) {
if spec, path, err := GetPodSpec(obj); err == nil {
return &podSpecMutator{spec: spec, path: path}, nil
}
if spec, path, err := GetPodSpecV1(obj); err == nil {
return &podSpecV1Mutator{spec: spec, path: path}, nil
}
return nil, errNoImageMutator
}
// resourcesToCheck is a map of resources and corresponding kinds of things that
// we want handled in this plugin
var resourcesToCheck = map[schema.GroupResource]schema.GroupKind{
kapi.Resource("pods"): kapi.Kind("Pod"),
kapi.Resource("podtemplates"): kapi.Kind("PodTemplate"),
kapi.Resource("replicationcontrollers"): kapi.Kind("ReplicationController"),
batch.Resource("jobs"): batch.Kind("Job"),
batch.Resource("jobtemplates"): batch.Kind("JobTemplate"),
// TODO do we still need this or is cronjob sufficient?
batch.Resource("scheduledjobs"): batch.Kind("ScheduledJob"),
batch.Resource("cronjobs"): batch.Kind("CronJob"),
extensions.Resource("deployments"): extensions.Kind("Deployment"),
extensions.Resource("replicasets"): extensions.Kind("ReplicaSet"),
apps.Resource("statefulsets"): apps.Kind("StatefulSet"),
deployapi.Resource("deploymentconfigs"): deployapi.Kind("DeploymentConfig"),
deployapi.LegacyResource("deploymentconfigs"): deployapi.LegacyKind("DeploymentConfig"),
securityapi.Resource("podsecuritypolicysubjectreviews"): securityapi.Kind("PodSecurityPolicySubjectReview"),
securityapi.LegacyResource("podsecuritypolicysubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySubjectReview"),
securityapi.Resource("podsecuritypolicyselfsubjectreviews"): securityapi.Kind("PodSecurityPolicySelfSubjectReview"),
securityapi.LegacyResource("podsecuritypolicyselfsubjectreviews"): securityapi.LegacyKind("PodSecurityPolicySelfSubjectReview"),
securityapi.Resource("podsecuritypolicyreviews"): securityapi.Kind("PodSecurityPolicyReview"),
securityapi.LegacyResource("podsecuritypolicyreviews"): securityapi.LegacyKind("PodSecurityPolicyReview"),
}
// HasPodSpec returns true if the resource is known to have a pod spec.
func HasPodSpec(gr schema.GroupResource) (schema.GroupKind, bool) {
gk, ok := resourcesToCheck[gr]
return gk, ok
}
var errNoPodSpec = fmt.Errorf("No PodSpec available for this object")
// GetPodSpec returns a mutable pod spec out of the provided object, including a field path
// to the field in the object, or an error if the object does not contain a pod spec.
// This only returns internal objects.
func GetPodSpec(obj runtime.Object) (*kapi.PodSpec, *field.Path, error) {
switch r := obj.(type) {
case *kapi.Pod:
return &r.Spec, field.NewPath("spec"), nil
case *kapi.PodTemplate:
return &r.Template.Spec, field.NewPath("template", "spec"), nil
case *kapi.ReplicationController:
if r.Spec.Template != nil {
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
}
case *extensions.DaemonSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *extensions.Deployment:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *extensions.ReplicaSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *batch.Job:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *batch.CronJob:
return &r.Spec.JobTemplate.Spec.Template.Spec, field.NewPath("spec", "jobTemplate", "spec", "template", "spec"), nil
case *batch.JobTemplate:
return &r.Template.Spec.Template.Spec, field.NewPath("template", "spec", "template", "spec"), nil
case *apps.StatefulSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapi.PodSecurityPolicySubjectReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapi.PodSecurityPolicySelfSubjectReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapi.PodSecurityPolicyReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *deployapi.DeploymentConfig:
if r.Spec.Template != nil {
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
}
}
return nil, nil, errNoPodSpec
}
// GetPodSpecV1 returns a mutable pod spec out of the provided object, including a field path
// to the field in the object, or an error if the object does not contain a pod spec.
// This only returns pod specs for v1 compatible objects.
func GetPodSpecV1(obj runtime.Object) (*kapiv1.PodSpec, *field.Path, error) {
switch r := obj.(type) {
case *kapiv1.Pod:
return &r.Spec, field.NewPath("spec"), nil
case *kapiv1.PodTemplate:
return &r.Template.Spec, field.NewPath("template", "spec"), nil
case *kapiv1.ReplicationController:
if r.Spec.Template != nil {
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
}
case *extensionsv1beta1.DaemonSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *extensionsv1beta1.Deployment:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *extensionsv1beta1.ReplicaSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *batchv1.Job:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *batchv2alpha1.CronJob:
return &r.Spec.JobTemplate.Spec.Template.Spec, field.NewPath("spec", "jobTemplate", "spec", "template", "spec"), nil
case *batchv2alpha1.JobTemplate:
return &r.Template.Spec.Template.Spec, field.NewPath("template", "spec", "template", "spec"), nil
case *appsv1beta1.StatefulSet:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *appsv1beta1.Deployment:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapiv1.PodSecurityPolicySubjectReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapiv1.PodSecurityPolicySelfSubjectReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *securityapiv1.PodSecurityPolicyReview:
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
case *deployapiv1.DeploymentConfig:
if r.Spec.Template != nil {
return &r.Spec.Template.Spec, field.NewPath("spec", "template", "spec"), nil
}
}
return nil, nil, errNoPodSpec
}
// GetTemplateMetaObject returns a mutable metav1.Object interface for the template
// the object contains, or false if no such object is available.
func GetTemplateMetaObject(obj runtime.Object) (metav1.Object, bool) {
switch r := obj.(type) {
case *kapiv1.PodTemplate:
return &r.Template.ObjectMeta, true
case *kapiv1.ReplicationController:
if r.Spec.Template != nil {
return &r.Spec.Template.ObjectMeta, true
}
case *extensionsv1beta1.DaemonSet:
return &r.Spec.Template.ObjectMeta, true
case *extensionsv1beta1.Deployment:
return &r.Spec.Template.ObjectMeta, true
case *extensionsv1beta1.ReplicaSet:
return &r.Spec.Template.ObjectMeta, true
case *batchv1.Job:
return &r.Spec.Template.ObjectMeta, true
case *batchv2alpha1.CronJob:
return &r.Spec.JobTemplate.Spec.Template.ObjectMeta, true
case *batchv2alpha1.JobTemplate:
return &r.Template.Spec.Template.ObjectMeta, true
case *appsv1beta1.StatefulSet:
return &r.Spec.Template.ObjectMeta, true
case *appsv1beta1.Deployment:
return &r.Spec.Template.ObjectMeta, true
case *securityapiv1.PodSecurityPolicySubjectReview:
return &r.Spec.Template.ObjectMeta, true
case *securityapiv1.PodSecurityPolicySelfSubjectReview:
return &r.Spec.Template.ObjectMeta, true
case *securityapiv1.PodSecurityPolicyReview:
return &r.Spec.Template.ObjectMeta, true
case *deployapiv1.DeploymentConfig:
if r.Spec.Template != nil {
return &r.Spec.Template.ObjectMeta, true
}
case *kapi.PodTemplate:
return &r.Template.ObjectMeta, true
case *kapi.ReplicationController:
if r.Spec.Template != nil {
return &r.Spec.Template.ObjectMeta, true
}
case *extensions.DaemonSet:
return &r.Spec.Template.ObjectMeta, true
case *extensions.Deployment:
return &r.Spec.Template.ObjectMeta, true
case *extensions.ReplicaSet:
return &r.Spec.Template.ObjectMeta, true
case *batch.Job:
return &r.Spec.Template.ObjectMeta, true
case *batch.CronJob:
return &r.Spec.JobTemplate.Spec.Template.ObjectMeta, true
case *batch.JobTemplate:
return &r.Template.Spec.Template.ObjectMeta, true
case *apps.StatefulSet:
return &r.Spec.Template.ObjectMeta, true
case *securityapi.PodSecurityPolicySubjectReview:
return &r.Spec.Template.ObjectMeta, true
case *securityapi.PodSecurityPolicySelfSubjectReview:
return &r.Spec.Template.ObjectMeta, true
case *securityapi.PodSecurityPolicyReview:
return &r.Spec.Template.ObjectMeta, true
case *deployapi.DeploymentConfig:
if r.Spec.Template != nil {
return &r.Spec.Template.ObjectMeta, true
}
}
return nil, false
}
type containerMutator struct {
*kapi.Container
}
func (m containerMutator) GetName() string { return m.Name }
func (m containerMutator) GetImage() string { return m.Image }
func (m containerMutator) SetImage(image string) { m.Image = image }
type containerV1Mutator struct {
*kapiv1.Container
}
func (m containerV1Mutator) GetName() string { return m.Name }
func (m containerV1Mutator) GetImage() string { return m.Image }
func (m containerV1Mutator) SetImage(image string) { m.Image = image }
// podSpecMutator implements the mutation interface over objects with a pod spec.
type podSpecMutator struct {
spec *kapi.PodSpec
oldSpec *kapi.PodSpec
path *field.Path
}
func (m *podSpecMutator) Path() *field.Path {
return m.path
}
func hasIdenticalPodSpecImage(spec *kapi.PodSpec, containerName, image string) bool {
if spec == nil {
return false
}
for i := range spec.InitContainers {
if spec.InitContainers[i].Name == containerName {
return spec.InitContainers[i].Image == image
}
}
for i := range spec.Containers {
if spec.Containers[i].Name == containerName {
return spec.Containers[i].Image == image
}
}
return false
}
// Mutate applies fn to all containers and init containers. If fn changes the Kind to
// any value other than "DockerImage", an error is set on that field.
func (m *podSpecMutator) Mutate(fn ImageReferenceMutateFunc) field.ErrorList {
var errs field.ErrorList
for i := range m.spec.InitContainers {
container := &m.spec.InitContainers[i]
if hasIdenticalPodSpecImage(m.oldSpec, container.Name, container.Image) {
continue
}
ref := kapi.ObjectReference{Kind: "DockerImage", Name: container.Image}
if err := fn(&ref); err != nil {
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("initContainers").Index(i).Child("image")))
continue
}
if ref.Kind != "DockerImage" {
errs = append(errs, fieldErrorOrInternal(fmt.Errorf("pod specs may only contain references to docker images, not %q", ref.Kind), m.path.Child("initContainers").Index(i).Child("image")))
continue
}
container.Image = ref.Name
}
for i := range m.spec.Containers {
container := &m.spec.Containers[i]
if hasIdenticalPodSpecImage(m.oldSpec, container.Name, container.Image) {
continue
}
ref := kapi.ObjectReference{Kind: "DockerImage", Name: container.Image}
if err := fn(&ref); err != nil {
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("containers").Index(i).Child("image")))
continue
}
if ref.Kind != "DockerImage" {
errs = append(errs, fieldErrorOrInternal(fmt.Errorf("pod specs may only contain references to docker images, not %q", ref.Kind), m.path.Child("containers").Index(i).Child("image")))
continue
}
container.Image = ref.Name
}
return errs
}
func (m *podSpecMutator) GetContainerByName(name string) (ContainerMutator, bool) {
spec := m.spec
for i := range spec.InitContainers {
if name != spec.InitContainers[i].Name {
continue
}
return containerMutator{&spec.InitContainers[i]}, true
}
for i := range spec.Containers {
if name != spec.Containers[i].Name {
continue
}
return containerMutator{&spec.Containers[i]}, true
}
return nil, false
}
func (m *podSpecMutator) GetContainerByIndex(init bool, i int) (ContainerMutator, bool) {
var container *kapi.Container
spec := m.spec
if init {
if i < 0 || i >= len(spec.InitContainers) {
return nil, false
}
container = &spec.InitContainers[i]
} else {
if i < 0 || i >= len(spec.Containers) {
return nil, false
}
container = &spec.Containers[i]
}
return containerMutator{container}, true
}
// podSpecV1Mutator implements the mutation interface over objects with a pod spec.
type podSpecV1Mutator struct {
spec *kapiv1.PodSpec
oldSpec *kapiv1.PodSpec
path *field.Path
}
func (m *podSpecV1Mutator) Path() *field.Path {
return m.path
}
func hasIdenticalPodSpecV1Image(spec *kapiv1.PodSpec, containerName, image string) bool {
if spec == nil {
return false
}
for i := range spec.InitContainers {
if spec.InitContainers[i].Name == containerName {
return spec.InitContainers[i].Image == image
}
}
for i := range spec.Containers {
if spec.Containers[i].Name == containerName {
return spec.Containers[i].Image == image
}
}
return false
}
// Mutate applies fn to all containers and init containers. If fn changes the Kind to
// any value other than "DockerImage", an error is set on that field.
func (m *podSpecV1Mutator) Mutate(fn ImageReferenceMutateFunc) field.ErrorList {
var errs field.ErrorList
for i := range m.spec.InitContainers {
container := &m.spec.InitContainers[i]
if hasIdenticalPodSpecV1Image(m.oldSpec, container.Name, container.Image) {
continue
}
ref := kapi.ObjectReference{Kind: "DockerImage", Name: container.Image}
if err := fn(&ref); err != nil {
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("initContainers").Index(i).Child("image")))
continue
}
if ref.Kind != "DockerImage" {
errs = append(errs, fieldErrorOrInternal(fmt.Errorf("pod specs may only contain references to docker images, not %q", ref.Kind), m.path.Child("initContainers").Index(i).Child("image")))
continue
}
container.Image = ref.Name
}
for i := range m.spec.Containers {
container := &m.spec.Containers[i]
if hasIdenticalPodSpecV1Image(m.oldSpec, container.Name, container.Image) {
continue
}
ref := kapi.ObjectReference{Kind: "DockerImage", Name: container.Image}
if err := fn(&ref); err != nil {
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("containers").Index(i).Child("image")))
continue
}
if ref.Kind != "DockerImage" {
errs = append(errs, fieldErrorOrInternal(fmt.Errorf("pod specs may only contain references to docker images, not %q", ref.Kind), m.path.Child("containers").Index(i).Child("image")))
continue
}
container.Image = ref.Name
}
return errs
}
func (m *podSpecV1Mutator) GetContainerByName(name string) (ContainerMutator, bool) {
spec := m.spec
for i := range spec.InitContainers {
if name != spec.InitContainers[i].Name {
continue
}
return containerV1Mutator{&spec.InitContainers[i]}, true
}
for i := range spec.Containers {
if name != spec.Containers[i].Name {
continue
}
return containerV1Mutator{&spec.Containers[i]}, true
}
return nil, false
}
func (m *podSpecV1Mutator) GetContainerByIndex(init bool, i int) (ContainerMutator, bool) {
var container *kapiv1.Container
spec := m.spec
if init {
if i < 0 || i >= len(spec.InitContainers) {
return nil, false
}
container = &spec.InitContainers[i]
} else {
if i < 0 || i >= len(spec.Containers) {
return nil, false
}
container = &spec.Containers[i]
}
return containerV1Mutator{container}, true
}