Skip to content

Commit

Permalink
Readd mounting volumes in workingdir
Browse files Browse the repository at this point in the history
Mounting volume was dropped when we did the large refactoring in
d7f492c which basically making
it ineffective and fails when not running as root.

Readding it in there

Closes #1608

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Nov 24, 2019
1 parent c9a7a35 commit d9030d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
13 changes: 7 additions & 6 deletions pkg/pod/workingdir_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
21 changes: 15 additions & 6 deletions pkg/pod/workingdir_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/taskrun/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/reconciler/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d9030d2

Please sign in to comment.