Skip to content

Commit d63ba11

Browse files
Default container name on execPod lifecycle hooks
Makes it easier for a user to use lifecycle hooks for single container pods.
1 parent 415e2b1 commit d63ba11

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

pkg/deploy/api/v1/defaults.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
deployapi "github.com/openshift/origin/pkg/deploy/api"
88
)
99

10-
func defaultTagImagesHookContainerName(hook *LifecycleHook, containerName string) {
10+
func defaultHookContainerName(hook *LifecycleHook, containerName string) {
1111
if hook == nil {
1212
return
1313
}
@@ -16,6 +16,11 @@ func defaultTagImagesHookContainerName(hook *LifecycleHook, containerName string
1616
hook.TagImages[i].ContainerName = containerName
1717
}
1818
}
19+
if hook.ExecNewPod != nil {
20+
if len(hook.ExecNewPod.ContainerName) == 0 {
21+
hook.ExecNewPod.ContainerName = containerName
22+
}
23+
}
1924
}
2025

2126
func addDefaultingFuncs(scheme *runtime.Scheme) {
@@ -38,13 +43,13 @@ func addDefaultingFuncs(scheme *runtime.Scheme) {
3843
if obj.Template != nil && len(obj.Template.Spec.Containers) == 1 {
3944
containerName := obj.Template.Spec.Containers[0].Name
4045
if p := obj.Strategy.RecreateParams; p != nil {
41-
defaultTagImagesHookContainerName(p.Pre, containerName)
42-
defaultTagImagesHookContainerName(p.Mid, containerName)
43-
defaultTagImagesHookContainerName(p.Post, containerName)
46+
defaultHookContainerName(p.Pre, containerName)
47+
defaultHookContainerName(p.Mid, containerName)
48+
defaultHookContainerName(p.Post, containerName)
4449
}
4550
if p := obj.Strategy.RollingParams; p != nil {
46-
defaultTagImagesHookContainerName(p.Pre, containerName)
47-
defaultTagImagesHookContainerName(p.Post, containerName)
51+
defaultHookContainerName(p.Pre, containerName)
52+
defaultHookContainerName(p.Post, containerName)
4853
}
4954
}
5055
},

pkg/deploy/api/v1/defaults_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,80 @@ func TestDeepDefaults(t *testing.T) {
326326
return *obj.Spec.Strategy.RecreateParams.TimeoutSeconds == deployapi.DefaultRollingTimeoutSeconds
327327
},
328328
},
329+
{
330+
External: &deployv1.DeploymentConfig{
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+
},
329403
{
330404
External: &deployv1.DeploymentConfig{
331405
Spec: deployv1.DeploymentConfigSpec{

0 commit comments

Comments
 (0)