-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move working dir initialization out to pkg/pod/
This simplifies pod.go and makes this logic more easily testable/understandable in isolation. This also removes stepTemplate merging from init containers, where users shouldn't know/care about the details of those containers anyway. This makes some simplifying changes to creds-init as well, and adds TODOs to further simplify.
- Loading branch information
1 parent
5ef2ec0
commit 9fc4e0d
Showing
5 changed files
with
188 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package pod | ||
|
||
import ( | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
"github.com/tektoncd/pipeline/pkg/names" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
workspaceDir = "/workspace" | ||
workingDirInit = "working-dir-initializer" | ||
) | ||
|
||
// WorkingDirInit returns a Container that should be run as an init | ||
// container to ensure that all steps' workingDirs relative to the workspace | ||
// exist. | ||
// | ||
// If no such directories need to be created (i.e., no relative workingDirs | ||
// are specified), this method returns nil, as no init container is necessary. | ||
// | ||
// 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 { | ||
// Gather all unique workingDirs. | ||
workingDirs := map[string]struct{}{} | ||
for _, step := range steps { | ||
if step.WorkingDir != "" { | ||
workingDirs[step.WorkingDir] = struct{}{} | ||
} | ||
} | ||
if len(workingDirs) == 0 { | ||
return nil | ||
} | ||
|
||
// Sort unique workingDirs. | ||
var orderedDirs []string | ||
for wd := range workingDirs { | ||
orderedDirs = append(orderedDirs, wd) | ||
} | ||
sort.Strings(orderedDirs) | ||
|
||
// Clean and append each relative workingDir. | ||
var relativeDirs []string | ||
for _, wd := range orderedDirs { | ||
p := filepath.Clean(wd) | ||
if !filepath.IsAbs(p) || strings.HasPrefix(p, "/workspace/") { | ||
relativeDirs = append(relativeDirs, p) | ||
} | ||
} | ||
|
||
if len(relativeDirs) == 0 { | ||
return nil | ||
} | ||
|
||
return &corev1.Container{ | ||
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(workingDirInit), | ||
Image: shellImage, | ||
Command: []string{"sh"}, | ||
Args: []string{"-c", "mkdir -p " + strings.Join(relativeDirs, " ")}, | ||
WorkingDir: workspaceDir, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package pod | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
"github.com/tektoncd/pipeline/test/names" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const shellImage = "shell-image" | ||
|
||
func TestWorkingDirInit(t *testing.T) { | ||
names.TestingSeed() | ||
for _, c := range []struct { | ||
desc string | ||
stepContainers []corev1.Container | ||
want *corev1.Container | ||
}{{ | ||
desc: "no workingDirs", | ||
stepContainers: []corev1.Container{{ | ||
Name: "no-working-dir", | ||
}}, | ||
want: nil, | ||
}, { | ||
desc: "workingDirs are unique and sorted, absolute dirs are ignored", | ||
stepContainers: []corev1.Container{{ | ||
WorkingDir: "zzz", | ||
}, { | ||
WorkingDir: "aaa", | ||
}, { | ||
WorkingDir: "/ignored", | ||
}, { | ||
WorkingDir: "/workspace", // ignored | ||
}, { | ||
WorkingDir: "zzz", | ||
}, { | ||
// Even though it's specified absolute, it's relative | ||
// to /workspace, so we need to create it. | ||
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, | ||
}, | ||
}} { | ||
t.Run(c.desc, func(t *testing.T) { | ||
// TODO(jasonhall): WorkingDirInit should take | ||
// Containers instead of Steps, but while we're | ||
// cleaning up pod.go it's easier to have it take | ||
// Steps. This test doesn't care, so let's hide this | ||
// conversion in the test where it's easier to remove | ||
// later. | ||
var steps []v1alpha1.Step | ||
for _, c := range c.stepContainers { | ||
steps = append(steps, v1alpha1.Step{Container: c}) | ||
} | ||
|
||
got := WorkingDirInit(shellImage, steps) | ||
if d := cmp.Diff(c.want, got); d != "" { | ||
t.Fatalf("Diff (-want, +got): %s", d) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.