Skip to content

Commit

Permalink
Adds Env to TaskRunSpec
Browse files Browse the repository at this point in the history
Adds a `Env` field to `TaskRunSpec` to allow a user to set environment
variables on each step container. This mimics Knative-Build's
`TemplateInstantiationSpec`'s `Env` field.

Setting environment variables is useful for when a `TaskRun` author may
not know what variables are required ahead of time (e.g., Buildpacks).

fixes tektoncd#1606
  • Loading branch information
poy committed Dec 1, 2019
1 parent ec9db68 commit c5319bd
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 0 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ type TaskRunSpec struct {
// Refer Go's ParseDuration documentation for expected format: https://golang.org/pkg/time/#ParseDuration
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`

// PodTemplate holds pod specific configuration
PodTemplate PodTemplate `json:"podTemplate,omitempty"`
// Env, if specified will provide variables to all task steps.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
}

// TaskRunSpecStatus defines the taskrun spec status the user can provide
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha
}

// Add implicit env vars.
// They're prepended to the list, so that if the user specified any
// themselves their value takes precedence.
// Append to an empty list to ensure we don't alter implicitEnvVars.
// Precedence: step.Env > taskRun.Spec.Env > implicitEnvVars
for i, s := range stepContainers {
env := append(implicitEnvVars, s.Env...)
env := append([]corev1.EnvVar{}, implicitEnvVars...)
env = append(env, taskRun.Spec.Env...)
env = append(env, s.Env...)
stepContainers[i].Env = env
}

Expand Down
50 changes: 49 additions & 1 deletion pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,55 @@ script-heredoc-randomly-generated-6nl7g
}},
Volumes: append(implicitVolumes, scriptsVolume, toolsVolume, downwardVolume),
},
}} {
}, {
desc: "env is set",
ts: v1alpha1.TaskSpec{
Steps: []v1alpha1.Step{{Container: corev1.Container{
Name: "step-with-env",
Image: "image",
Command: []string{"cmd"}, // avoid entrypoint lookup.
}}},
},
trs: v1alpha1.TaskRunSpec{
Env: []corev1.EnvVar{
{Name: "FOO", Value: "bar"},
},
},
want: &corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
InitContainers: []corev1.Container{placeToolsInit},
Containers: []corev1.Container{{
Name: "step-step-with-env",
Image: "image",
Command: []string{"/tekton/tools/entrypoint"},
Args: []string{
"-wait_file",
"/tekton/downward/ready",
"-wait_file_content",
"-post_file",
"/tekton/tools/0",
"-entrypoint",
"cmd",
"--",
},
// Append to an empty slice to avoid manipulating
// implicitEnvVars, but keep user variables last to ensure
// precedence.
Env: append(append([]corev1.EnvVar{}, implicitEnvVars...), corev1.EnvVar{Name: "FOO", Value: "bar"}),
VolumeMounts: append([]corev1.VolumeMount{toolsMount, downwardMount}, implicitVolumeMounts...),
WorkingDir: workspaceDir,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("0"),
corev1.ResourceMemory: resource.MustParse("0"),
corev1.ResourceEphemeralStorage: resource.MustParse("0"),
},
},
}},
Volumes: append(implicitVolumes, toolsVolume, downwardVolume),
},
},
} {
t.Run(c.desc, func(t *testing.T) {
names.TestingSeed()
kubeclient := fakek8s.NewSimpleClientset(
Expand Down

0 comments on commit c5319bd

Please sign in to comment.