Skip to content

Commit

Permalink
Add support for secret and configMap valueFrom and envFrom fields in …
Browse files Browse the repository at this point in the history
…templating

Currently, we support template on the environment variables key, *but*
not if the environment variable comes from a `ConfigMap` or a
`Secret`.

The following won't work.

```
    env:
    - name: GOPATH
      value: /workspace
    - name: GITHUB_TOKEN
      valueFrom:
        secretKeyRef:
          name: ${inputs.params.github-token-secret}
          key: bot-token
```

This fixes that and add support for using variable interpolation on
`valueFrom.secretKeyRef` and `valueFrom.configMapKeyRef`.

This also adds variable interpolation on `envFrom`.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Jun 13, 2019
1 parent 5cecb00 commit faf8de2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ func ApplyReplacements(spec *v1alpha1.TaskSpec, replacements map[string]string)
}
for ie, e := range steps[i].Env {
steps[i].Env[ie].Value = templating.ApplyReplacements(e.Value, replacements)
if steps[i].Env[ie].ValueFrom != nil {
if e.ValueFrom.SecretKeyRef != nil {
steps[i].Env[ie].ValueFrom.SecretKeyRef.LocalObjectReference.Name = templating.ApplyReplacements(e.ValueFrom.SecretKeyRef.LocalObjectReference.Name, replacements)
steps[i].Env[ie].ValueFrom.SecretKeyRef.Key = templating.ApplyReplacements(e.ValueFrom.SecretKeyRef.Key, replacements)
}
if e.ValueFrom.ConfigMapKeyRef != nil {
steps[i].Env[ie].ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name = templating.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name, replacements)
steps[i].Env[ie].ValueFrom.ConfigMapKeyRef.Key = templating.ApplyReplacements(e.ValueFrom.ConfigMapKeyRef.Key, replacements)
}
}
}
for ie, e := range steps[i].EnvFrom {
steps[i].EnvFrom[ie].Prefix = templating.ApplyReplacements(e.Prefix, replacements)
if e.ConfigMapRef != nil {
steps[i].EnvFrom[ie].ConfigMapRef.LocalObjectReference.Name = templating.ApplyReplacements(e.ConfigMapRef.LocalObjectReference.Name, replacements)
}
if e.SecretRef != nil {
steps[i].EnvFrom[ie].SecretRef.LocalObjectReference.Name = templating.ApplyReplacements(e.SecretRef.LocalObjectReference.Name, replacements)
}
}
steps[i].WorkingDir = templating.ApplyReplacements(steps[i].WorkingDir, replacements)
for ic, c := range steps[i].Command {
Expand Down
65 changes: 65 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,44 @@ var simpleTaskSpec = &v1alpha1.TaskSpec{
}},
}

var envTaskSpec = &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Name: "foo",
Image: "busybox:${inputs.params.FOO}",
Env: []corev1.EnvVar{{
Name: "foo",
Value: "value-${inputs.params.FOO}",
}, {
Name: "bar",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "config-${inputs.params.FOO}"},
Key: "config-key-${inputs.params.FOO}",
},
},
}, {
Name: "baz",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "secret-${inputs.params.FOO}"},
Key: "secret-key-${inputs.params.FOO}",
},
},
}},
EnvFrom: []corev1.EnvFromSource{{
Prefix: "prefix-0-${inputs.params.FOO}",
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "config-${inputs.params.FOO}"},
},
}, {
Prefix: "prefix-1-${inputs.params.FOO}",
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: "secret-${inputs.params.FOO}"},
},
}},
}},
}

var volumeMountTaskSpec = &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Name: "foo",
Expand Down Expand Up @@ -183,6 +221,33 @@ func TestApplyParameters(t *testing.T) {
spec.Steps[0].VolumeMounts[0].MountPath = "path/to/world"
spec.Steps[0].Image = "busybox:world"
}),
}, {
name: "envs parameter",
args: args{
ts: envTaskSpec,
tr: &v1alpha1.TaskRun{
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Params: []v1alpha1.Param{{
Name: "FOO",
Value: "world",
}},
},
},
},
},
want: applyMutation(envTaskSpec, func(spec *v1alpha1.TaskSpec) {
spec.Steps[0].Env[0].Value = "value-world"
spec.Steps[0].Env[1].ValueFrom.ConfigMapKeyRef.LocalObjectReference.Name = "config-world"
spec.Steps[0].Env[1].ValueFrom.ConfigMapKeyRef.Key = "config-key-world"
spec.Steps[0].Env[2].ValueFrom.SecretKeyRef.LocalObjectReference.Name = "secret-world"
spec.Steps[0].Env[2].ValueFrom.SecretKeyRef.Key = "secret-key-world"
spec.Steps[0].EnvFrom[0].Prefix = "prefix-0-world"
spec.Steps[0].EnvFrom[0].ConfigMapRef.LocalObjectReference.Name = "config-world"
spec.Steps[0].EnvFrom[1].Prefix = "prefix-1-world"
spec.Steps[0].EnvFrom[1].SecretRef.LocalObjectReference.Name = "secret-world"
spec.Steps[0].Image = "busybox:world"
}),
}, {
name: "with default parameter",
args: args{
Expand Down

0 comments on commit faf8de2

Please sign in to comment.