Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for env in podtemplate #3566

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/podtemplates.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Pod templates support fields listed in the table below.
<td><code>volumes</code></td>
<td>Specifies a list of volumes that containers within the Pod can mount. This allows you to specify a volume type for each <code>volumeMount</code> in a <code>Task</code>.</td>
</tr>
<tr>
<td><code>env</code></td>
<td>Specifies a list of environments that all containers within the Pod will be populated</td>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: environments -> environment variables?

</tr>
<tr>
<td><code>runtimeClassName</code></td>
<td>Specifies the <a href=https://kubernetes.io/docs/concepts/containers/runtime-class/>runtime class</a> for the Pod.</td>
Expand Down
15 changes: 15 additions & 0 deletions examples/v1beta1/taskruns/podtemplate-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
generateName: podtemplate-env-
spec:
podTemplate:
env:
- name: MY_VAR1
value: foo
taskSpec:
steps:
- image: ubuntu
script: |
#!/usr/bin/env bash
[[ $MY_VAR1 == foo ]]
6 changes: 6 additions & 0 deletions pkg/apis/pipeline/pod/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ type Template struct {
// +patchStrategy=merge,retainKeys
Volumes []corev1.Volume `json:"volumes,omitempty" patchStrategy:"merge,retainKeys" patchMergeKey:"name" protobuf:"bytes,1,rep,name=volumes"`

// List of environment variables that can be provided to the containers belonging to the pod.
// +optional
// +patchMergeKey=name
// +patchStrategy=merge,retainKeys
Env []corev1.EnvVar `json:"env,omitempty" patchStrategy:"merge,retainKeys" patchMergeKey:"name" protobuf:"bytes,1,rep,name=env"`

// RuntimeClassName refers to a RuntimeClass object in the node.k8s.io
// group, which should be used to run this pod. If no RuntimeClass resource
// matches the named class, the pod will not be run. If unset or empty, the
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/pod/zz_generated.deepcopy.go

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

21 changes: 20 additions & 1 deletion pkg/apis/pipeline/v1beta1/openapi_generated.go

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

9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
"description": "EnableServiceLinks indicates whether information about services should be injected into pod's environment variables, matching the syntax of Docker links. Optional: Defaults to true.",
"type": "boolean"
},
"env": {
"description": "List of environment variables that can be provided to the containers belonging to the pod.",
"type": "array",
"items": {
"$ref": "#/definitions/v1.EnvVar"
},
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge,retainKeys"
},
"hostNetwork": {
"description": "HostNetwork specifies whether the pod may use the node network namespace",
"type": "boolean"
Expand Down
43 changes: 43 additions & 0 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ func (b *Builder) Build(ctx context.Context, taskRun *v1beta1.TaskRun, taskSpec
podTemplate = *taskRun.Spec.PodTemplate
}

// Add envs specified in podtemplate to all step containers
// Envs specified in podtemplate should take precedence over
// the one specified in step and step template
for i := range stepContainers {
envs := updateAndMergeEnv(stepContainers[i].Env, podTemplate.Env)
stepContainers[i].Env = envs
}

// Add envs specified in podtemplate to all sidecar containers
// Envs specified in podtemplate should take precedence over
// the one specified in sidecar and step template
for i := range sidecarContainers {
envs := updateAndMergeEnv(sidecarContainers[i].Env, podTemplate.Env)
sidecarContainers[i].Env = envs
}

// Add envs specified in podtemplate to all initContainers
// Envs specified in podtemplate should take precedence over
// the one specified in init and step template
for i := range initContainers {
envs := updateAndMergeEnv(initContainers[i].Env, podTemplate.Env)
initContainers[i].Env = envs
}

// Add podTemplate Volumes to the explicitly declared use volumes
volumes = append(volumes, taskSpec.Volumes...)
volumes = append(volumes, podTemplate.Volumes...)
Expand Down Expand Up @@ -403,3 +427,22 @@ func shouldAddReadyAnnotationOnPodCreate(ctx context.Context, sidecars []v1beta1
cfg := config.FromContextOrDefaults(ctx)
return !cfg.FeatureFlags.RunningInEnvWithInjectedSidecars
}

// updateAndMergeEnv will merge two slices of env
// precedence will be given to second input if exist with same name key
func updateAndMergeEnv(containerenvs []corev1.EnvVar, podtemplateEnvs []corev1.EnvVar) []corev1.EnvVar {
for _, env := range podtemplateEnvs {
var updated bool
for i := range containerenvs {
if env.Name == containerenvs[i].Name {
containerenvs[i].Value = env.Value
containerenvs[i].ValueFrom = env.ValueFrom
updated = true
}
}
if !updated {
containerenvs = append(containerenvs, env)
}
}
return containerenvs
}
Loading