Skip to content

Commit

Permalink
Generate volume and volume mount names
Browse files Browse the repository at this point in the history
Before this, if a user wanted to attach a Volume named "workspace"
they'd get a confusing error message (tektoncd#1402). tektoncd#1404 improves the error
message, but it would be nice to not have an error at all and just allow
user-defined volumes named "workspace"
  • Loading branch information
imjasonh committed Nov 15, 2019
1 parent f5fee88 commit f7551bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
45 changes: 27 additions & 18 deletions pkg/reconciler/taskrun/resources/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,6 @@ var (
Name: "HOME",
Value: homeDir,
}}
implicitVolumeMounts = []corev1.VolumeMount{{
Name: "workspace",
MountPath: workspaceDir,
}, {
Name: "home",
MountPath: homeDir,
}}
implicitVolumes = []corev1.Volume{{
Name: "workspace",
VolumeSource: emptyVolumeSource,
}, {
Name: "home",
VolumeSource: emptyVolumeSource,
}}

zeroQty = resource.MustParse("0")

Expand Down Expand Up @@ -115,7 +101,28 @@ const (
sidecarPrefix = "sidecar-"
)

func makeCredentialInitializer(credsImage, serviceAccountName, namespace string, kubeclient kubernetes.Interface) (*v1alpha1.Step, []corev1.Volume, error) {
// implicitVolumesAndVolumeMounts returns implicit Volumes and VolumeMounts for
// workspace and home volumes, with a generated name.
func implicitVolumesAndVolumeMounts() ([]corev1.Volume, []corev1.VolumeMount) {
workspaceVolumeName := names.SimpleNameGenerator.RestrictLength("workspace-")
homeVolumeName := names.SimpleNameGenerator.RestrictLength("home-")
return []corev1.Volume{{
Name: workspaceVolumeName,
VolumeSource: emptyVolumeSource,
}, {
Name: homeVolumeName,
VolumeSource: emptyVolumeSource,
}},
[]corev1.VolumeMount{{
Name: workspaceVolumeName,
MountPath: workspaceDir,
}, {
Name: homeVolumeName,
MountPath: homeDir,
}}
}

func makeCredentialInitializer(credsImage, serviceAccountName, namespace string, implicitVolumeMounts []corev1.VolumeMount, kubeclient kubernetes.Interface) (*v1alpha1.Step, []corev1.Volume, error) {
if serviceAccountName == "" {
serviceAccountName = "default"
}
Expand Down Expand Up @@ -198,7 +205,7 @@ func makeWorkingDirScript(workingDirs map[string]bool) string {
return script
}

func makeWorkingDirInitializer(shellImage string, steps []v1alpha1.Step) *v1alpha1.Step {
func makeWorkingDirInitializer(shellImage string, steps []v1alpha1.Step, implicitVolumeMounts []corev1.VolumeMount) *v1alpha1.Step {
workingDirs := make(map[string]bool)
for _, step := range steps {
workingDirs[step.WorkingDir] = true
Expand Down Expand Up @@ -238,14 +245,16 @@ func TryGetPod(taskRunStatus v1alpha1.TaskRunStatus, gp GetPod) (*corev1.Pod, er
// MakePod converts TaskRun and TaskSpec objects to a Pod which implements the taskrun specified
// by the supplied CRD.
func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha1.TaskSpec, kubeclient kubernetes.Interface) (*corev1.Pod, error) {
cred, secrets, err := makeCredentialInitializer(images.CredsImage, taskRun.GetServiceAccountName(), taskRun.Namespace, kubeclient)
implicitVolumes, implicitVolumeMounts := implicitVolumesAndVolumeMounts()

cred, secrets, err := makeCredentialInitializer(images.CredsImage, taskRun.GetServiceAccountName(), taskRun.Namespace, implicitVolumeMounts, kubeclient)
if err != nil {
return nil, err
}
initSteps := []v1alpha1.Step{*cred}
var podSteps []v1alpha1.Step

if workingDir := makeWorkingDirInitializer(images.ShellImage, taskSpec.Steps); workingDir != nil {
if workingDir := makeWorkingDirInitializer(images.ShellImage, taskSpec.Steps, implicitVolumeMounts); workingDir != nil {
initSteps = append(initSteps, *workingDir)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/reconciler/taskrun/resources/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var (
})
credsImage = "override-with-creds:latest"
shellImage = "busybox"

implicitVolumes, implicitVolumeMounts = implicitVolumesAndVolumeMounts()
)

func TestTryGetPod(t *testing.T) {
Expand Down

0 comments on commit f7551bc

Please sign in to comment.