Browse code

Default container name on execPod lifecycle hooks

Makes it easier for a user to use lifecycle hooks for single container
pods.

Clayton Coleman authored on 2016/05/14 06:54:56
Showing 2 changed files
... ...
@@ -7,7 +7,7 @@ import (
7 7
 	deployapi "github.com/openshift/origin/pkg/deploy/api"
8 8
 )
9 9
 
10
-func defaultTagImagesHookContainerName(hook *LifecycleHook, containerName string) {
10
+func defaultHookContainerName(hook *LifecycleHook, containerName string) {
11 11
 	if hook == nil {
12 12
 		return
13 13
 	}
... ...
@@ -16,6 +16,11 @@ func defaultTagImagesHookContainerName(hook *LifecycleHook, containerName string
16 16
 			hook.TagImages[i].ContainerName = containerName
17 17
 		}
18 18
 	}
19
+	if hook.ExecNewPod != nil {
20
+		if len(hook.ExecNewPod.ContainerName) == 0 {
21
+			hook.ExecNewPod.ContainerName = containerName
22
+		}
23
+	}
19 24
 }
20 25
 
21 26
 func addDefaultingFuncs(scheme *runtime.Scheme) {
... ...
@@ -38,13 +43,13 @@ func addDefaultingFuncs(scheme *runtime.Scheme) {
38 38
 			if obj.Template != nil && len(obj.Template.Spec.Containers) == 1 {
39 39
 				containerName := obj.Template.Spec.Containers[0].Name
40 40
 				if p := obj.Strategy.RecreateParams; p != nil {
41
-					defaultTagImagesHookContainerName(p.Pre, containerName)
42
-					defaultTagImagesHookContainerName(p.Mid, containerName)
43
-					defaultTagImagesHookContainerName(p.Post, containerName)
41
+					defaultHookContainerName(p.Pre, containerName)
42
+					defaultHookContainerName(p.Mid, containerName)
43
+					defaultHookContainerName(p.Post, containerName)
44 44
 				}
45 45
 				if p := obj.Strategy.RollingParams; p != nil {
46
-					defaultTagImagesHookContainerName(p.Pre, containerName)
47
-					defaultTagImagesHookContainerName(p.Post, containerName)
46
+					defaultHookContainerName(p.Pre, containerName)
47
+					defaultHookContainerName(p.Post, containerName)
48 48
 				}
49 49
 			}
50 50
 		},
... ...
@@ -329,6 +329,80 @@ func TestDeepDefaults(t *testing.T) {
329 329
 		{
330 330
 			External: &deployv1.DeploymentConfig{
331 331
 				Spec: deployv1.DeploymentConfigSpec{
332
+					Template: &kapiv1.PodTemplateSpec{
333
+						Spec: kapiv1.PodSpec{
334
+							Containers: []kapiv1.Container{
335
+								{Name: "first"},
336
+							},
337
+						},
338
+					},
339
+					Strategy: deployv1.DeploymentStrategy{
340
+						Type: deployv1.DeploymentStrategyTypeRecreate,
341
+						RecreateParams: &deployv1.RecreateDeploymentStrategyParams{
342
+							Pre: &deployv1.LifecycleHook{
343
+								TagImages:  []deployv1.TagImageHook{{}},
344
+								ExecNewPod: &deployv1.ExecNewPodHook{},
345
+							},
346
+							Mid: &deployv1.LifecycleHook{
347
+								TagImages:  []deployv1.TagImageHook{{}},
348
+								ExecNewPod: &deployv1.ExecNewPodHook{},
349
+							},
350
+							Post: &deployv1.LifecycleHook{
351
+								TagImages:  []deployv1.TagImageHook{{}},
352
+								ExecNewPod: &deployv1.ExecNewPodHook{},
353
+							},
354
+						},
355
+					},
356
+				},
357
+			},
358
+			Internal: &deployapi.DeploymentConfig{},
359
+			Ok: func(out runtime.Object) bool {
360
+				obj := out.(*deployapi.DeploymentConfig)
361
+				return obj.Spec.Strategy.RecreateParams.Pre.ExecNewPod.ContainerName == "first" &&
362
+					obj.Spec.Strategy.RecreateParams.Mid.ExecNewPod.ContainerName == "first" &&
363
+					obj.Spec.Strategy.RecreateParams.Post.ExecNewPod.ContainerName == "first" &&
364
+					obj.Spec.Strategy.RecreateParams.Pre.TagImages[0].ContainerName == "first" &&
365
+					obj.Spec.Strategy.RecreateParams.Mid.TagImages[0].ContainerName == "first" &&
366
+					obj.Spec.Strategy.RecreateParams.Post.TagImages[0].ContainerName == "first"
367
+			},
368
+		},
369
+		{
370
+			External: &deployv1.DeploymentConfig{
371
+				Spec: deployv1.DeploymentConfigSpec{
372
+					Template: &kapiv1.PodTemplateSpec{
373
+						Spec: kapiv1.PodSpec{
374
+							Containers: []kapiv1.Container{
375
+								{Name: "first"},
376
+							},
377
+						},
378
+					},
379
+					Strategy: deployv1.DeploymentStrategy{
380
+						Type: deployv1.DeploymentStrategyTypeRecreate,
381
+						RollingParams: &deployv1.RollingDeploymentStrategyParams{
382
+							Pre: &deployv1.LifecycleHook{
383
+								TagImages:  []deployv1.TagImageHook{{}},
384
+								ExecNewPod: &deployv1.ExecNewPodHook{},
385
+							},
386
+							Post: &deployv1.LifecycleHook{
387
+								TagImages:  []deployv1.TagImageHook{{}},
388
+								ExecNewPod: &deployv1.ExecNewPodHook{},
389
+							},
390
+						},
391
+					},
392
+				},
393
+			},
394
+			Internal: &deployapi.DeploymentConfig{},
395
+			Ok: func(out runtime.Object) bool {
396
+				obj := out.(*deployapi.DeploymentConfig)
397
+				return obj.Spec.Strategy.RollingParams.Pre.ExecNewPod.ContainerName == "first" &&
398
+					obj.Spec.Strategy.RollingParams.Post.ExecNewPod.ContainerName == "first" &&
399
+					obj.Spec.Strategy.RollingParams.Pre.TagImages[0].ContainerName == "first" &&
400
+					obj.Spec.Strategy.RollingParams.Post.TagImages[0].ContainerName == "first"
401
+			},
402
+		},
403
+		{
404
+			External: &deployv1.DeploymentConfig{
405
+				Spec: deployv1.DeploymentConfigSpec{
332 406
 					Strategy: deployv1.DeploymentStrategy{
333 407
 						Type: deployv1.DeploymentStrategyTypeRecreate,
334 408
 					},