From d9030d2bd1cf1c4f29f7cfb3a69acb6a2c219cf7 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Sat, 23 Nov 2019 09:32:54 +0100 Subject: [PATCH] Readd mounting volumes in workingdir Mounting volume was dropped when we did the large refactoring in d7f492cb8e018a8d2c65a8702b3746e710501616 which basically making it ineffective and fails when not running as root. Readding it in there Closes #1608 Signed-off-by: Chmouel Boudjnah --- pkg/pod/workingdir_init.go | 13 ++++++------ pkg/pod/workingdir_init_test.go | 21 ++++++++++++++------ pkg/reconciler/taskrun/resources/pod.go | 2 +- pkg/reconciler/taskrun/resources/pod_test.go | 11 +++++----- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/pkg/pod/workingdir_init.go b/pkg/pod/workingdir_init.go index 4565810b6f8..2c0d5a73f5a 100644 --- a/pkg/pod/workingdir_init.go +++ b/pkg/pod/workingdir_init.go @@ -41,7 +41,7 @@ const ( // TODO(jasonhall): This should take []corev1.Container instead of // []corev1.Step, but this makes it easier to use in pod.go. When pod.go is // cleaned up, this can take []corev1.Container. -func WorkingDirInit(shellImage string, steps []v1alpha1.Step) *corev1.Container { +func WorkingDirInit(shellImage string, steps []v1alpha1.Step, volumeMounts []corev1.VolumeMount) *corev1.Container { // Gather all unique workingDirs. workingDirs := map[string]struct{}{} for _, step := range steps { @@ -75,10 +75,11 @@ func WorkingDirInit(shellImage string, steps []v1alpha1.Step) *corev1.Container } return &corev1.Container{ - Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(workingDirInit), - Image: shellImage, - Command: []string{"sh"}, - Args: []string{"-c", "mkdir -p " + strings.Join(relativeDirs, " ")}, - WorkingDir: workspaceDir, + Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(workingDirInit), + Image: shellImage, + Command: []string{"sh"}, + Args: []string{"-c", "mkdir -p " + strings.Join(relativeDirs, " ")}, + WorkingDir: workspaceDir, + VolumeMounts: volumeMounts, } } diff --git a/pkg/pod/workingdir_init_test.go b/pkg/pod/workingdir_init_test.go index 8dc0f398e6b..ff3176bf805 100644 --- a/pkg/pod/workingdir_init_test.go +++ b/pkg/pod/workingdir_init_test.go @@ -28,6 +28,14 @@ import ( const shellImage = "shell-image" func TestWorkingDirInit(t *testing.T) { + volumeMounts := []corev1.VolumeMount{{ + Name: "workspace", + MountPath: "/workspace", + }, { + Name: "home", + MountPath: "/builder/home", + }} + names.TestingSeed() for _, c := range []struct { desc string @@ -57,11 +65,12 @@ func TestWorkingDirInit(t *testing.T) { WorkingDir: "/workspace/bbb", }}, want: &corev1.Container{ - Name: "working-dir-initializer-9l9zj", - Image: shellImage, - Command: []string{"sh"}, - Args: []string{"-c", "mkdir -p /workspace/bbb aaa zzz"}, - WorkingDir: workspaceDir, + Name: "working-dir-initializer-9l9zj", + Image: shellImage, + Command: []string{"sh"}, + Args: []string{"-c", "mkdir -p /workspace/bbb aaa zzz"}, + WorkingDir: workspaceDir, + VolumeMounts: volumeMounts, }, }} { t.Run(c.desc, func(t *testing.T) { @@ -76,7 +85,7 @@ func TestWorkingDirInit(t *testing.T) { steps = append(steps, v1alpha1.Step{Container: c}) } - got := WorkingDirInit(shellImage, steps) + got := WorkingDirInit(shellImage, steps, volumeMounts) if d := cmp.Diff(c.want, got); d != "" { t.Fatalf("Diff (-want, +got): %s", d) } diff --git a/pkg/reconciler/taskrun/resources/pod.go b/pkg/reconciler/taskrun/resources/pod.go index d146c2f259e..ea91c07b40a 100644 --- a/pkg/reconciler/taskrun/resources/pod.go +++ b/pkg/reconciler/taskrun/resources/pod.go @@ -120,7 +120,7 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha volumes = append(volumes, secretsVolumes...) } - if workingDirInit := pod.WorkingDirInit(images.ShellImage, taskSpec.Steps); workingDirInit != nil { + if workingDirInit := pod.WorkingDirInit(images.ShellImage, taskSpec.Steps, implicitVolumeMounts); workingDirInit != nil { initContainers = append(initContainers, *workingDirInit) } diff --git a/pkg/reconciler/taskrun/resources/pod_test.go b/pkg/reconciler/taskrun/resources/pod_test.go index 2d380e4ab96..73ae0d478f0 100644 --- a/pkg/reconciler/taskrun/resources/pod_test.go +++ b/pkg/reconciler/taskrun/resources/pod_test.go @@ -292,11 +292,12 @@ func TestMakePod(t *testing.T) { want: &corev1.PodSpec{ RestartPolicy: corev1.RestartPolicyNever, InitContainers: []corev1.Container{{ - Name: "working-dir-initializer-9l9zj", - Image: shellImage, - Command: []string{"sh"}, - Args: []string{"-c", fmt.Sprintf("mkdir -p %s", filepath.Join(workspaceDir, "test"))}, - WorkingDir: workspaceDir, + Name: "working-dir-initializer-9l9zj", + Image: shellImage, + Command: []string{"sh"}, + Args: []string{"-c", fmt.Sprintf("mkdir -p %s", filepath.Join(workspaceDir, "test"))}, + WorkingDir: workspaceDir, + VolumeMounts: implicitVolumeMounts, }}, Containers: []corev1.Container{{ Name: "step-name",