From 7f5d9a4d3f03e8828c52ec300566f2df25a6945e Mon Sep 17 00:00:00 2001 From: Dan Lorenc Date: Mon, 16 Nov 2020 14:34:52 -0600 Subject: [PATCH] Plumb the "TEKTON_RESOURCE_NAME" environment variable into more resource containers. As part of TEP-0025 (hermetic builds), we need to know which containers/steps are part of the "Tekton system" and which ones are part of the Task definition. This PR adds the TEKTON_RESOURCE_NAME environment variable to the injected copy containers. This variable is already used by several of the existing resources (git, image). --- pkg/apis/resource/v1alpha1/storage/artifact_pvc.go | 8 ++++++++ pkg/apis/resource/v1alpha1/storage/artifact_pvc_test.go | 2 ++ pkg/reconciler/taskrun/resources/input_resource_test.go | 2 ++ pkg/reconciler/taskrun/resources/output_resource_test.go | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/apis/resource/v1alpha1/storage/artifact_pvc.go b/pkg/apis/resource/v1alpha1/storage/artifact_pvc.go index edf32b1030e..27560dfec55 100644 --- a/pkg/apis/resource/v1alpha1/storage/artifact_pvc.go +++ b/pkg/apis/resource/v1alpha1/storage/artifact_pvc.go @@ -54,6 +54,10 @@ func (p *ArtifactPVC) GetCopyFromStorageToSteps(name, sourcePath, destinationPat Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("source-copy-%s", name)), Image: p.ShellImage, Command: []string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), destinationPath}, + Env: []corev1.EnvVar{{ + Name: "TEKTON_RESOURCE_NAME", + Value: name, + }}, }}} } @@ -69,6 +73,10 @@ func (p *ArtifactPVC) GetCopyToStorageFromSteps(name, sourcePath, destinationPat Image: p.ShellImage, Command: []string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), destinationPath}, VolumeMounts: []corev1.VolumeMount{GetPvcMount(p.Name)}, + Env: []corev1.EnvVar{{ + Name: "TEKTON_RESOURCE_NAME", + Value: name, + }}, }}} } diff --git a/pkg/apis/resource/v1alpha1/storage/artifact_pvc_test.go b/pkg/apis/resource/v1alpha1/storage/artifact_pvc_test.go index 627b2028bc2..53f104af884 100644 --- a/pkg/apis/resource/v1alpha1/storage/artifact_pvc_test.go +++ b/pkg/apis/resource/v1alpha1/storage/artifact_pvc_test.go @@ -40,6 +40,7 @@ func TestPVCGetCopyFromContainerSpec(t *testing.T) { Image: "busybox", Command: []string{"cp", "-r", "src-path/.", "/workspace/destination"}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "workspace"}}, }}} got := pvc.GetCopyFromStorageToSteps("workspace", "src-path", "/workspace/destination") @@ -65,6 +66,7 @@ func TestPVCGetCopyToContainerSpec(t *testing.T) { Image: "busybox", Command: []string{"cp", "-r", "src-path/.", "/workspace/destination"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "workspace"}}, }}} got := pvc.GetCopyToStorageFromSteps("workspace", "src-path", "/workspace/destination") diff --git a/pkg/reconciler/taskrun/resources/input_resource_test.go b/pkg/reconciler/taskrun/resources/input_resource_test.go index 668fa6311a8..2623fc13332 100644 --- a/pkg/reconciler/taskrun/resources/input_resource_test.go +++ b/pkg/reconciler/taskrun/resources/input_resource_test.go @@ -594,6 +594,7 @@ func TestAddInputResourceToTask(t *testing.T) { Image: "busybox", Command: []string{"cp", "-r", "prev-task-path/.", "/workspace/gitspace"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "gitspace"}}, }}}, Volumes: []corev1.Volume{{ Name: "pipelinerun-pvc", @@ -727,6 +728,7 @@ gsutil cp gs://fake-bucket/rules.zip /workspace/gcs-dir Image: "busybox", Command: []string{"cp", "-r", "prev-task-path/.", "/workspace/gcs-dir"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "workspace"}}, }}}, Volumes: []corev1.Volume{{ Name: "pipelinerun-pvc", diff --git a/pkg/reconciler/taskrun/resources/output_resource_test.go b/pkg/reconciler/taskrun/resources/output_resource_test.go index 40bb83f6fa2..f7ed04ca1cb 100644 --- a/pkg/reconciler/taskrun/resources/output_resource_test.go +++ b/pkg/reconciler/taskrun/resources/output_resource_test.go @@ -238,6 +238,7 @@ func TestValidOutputResources(t *testing.T) { Name: "pipelinerun-pvc", MountPath: "/pvc", }}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "source-git"}}, }}}, wantVolumes: []corev1.Volume{ { @@ -311,6 +312,7 @@ func TestValidOutputResources(t *testing.T) { Name: "pipelinerun-pvc", MountPath: "/pvc", }}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "source-git"}}, }}}, wantVolumes: []corev1.Volume{ { @@ -483,6 +485,7 @@ func TestValidOutputResources(t *testing.T) { Image: "busybox", Command: []string{"cp", "-r", "/workspace/output/source-workspace/.", "pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-parent-pvc", MountPath: "/pvc"}}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "source-gcs"}}, }}, {Container: corev1.Container{ Name: "upload-source-gcs-78c5n", @@ -573,6 +576,7 @@ func TestValidOutputResources(t *testing.T) { Image: "busybox", Command: []string{"cp", "-r", "/workspace/output/source-workspace/.", "pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-pvc", MountPath: "/pvc"}}, + Env: []corev1.EnvVar{{Name: "TEKTON_RESOURCE_NAME", Value: "source-gcs"}}, }}, {Container: corev1.Container{ Name: "upload-source-gcs-78c5n", @@ -1471,7 +1475,6 @@ func TestInputOutputBucketResources(t *testing.T) { VolumeMounts: nil, Command: []string{"gsutil"}, Args: []string{"rsync", "-d", "-r", "/workspace/output/source-workspace", "gs://some-bucket"}, - Env: nil, }}, }, wantVolumes: []corev1.Volume{{