-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement implicit parameter resolution.
This is the implementation of [TEP-0023](https://github.com/tektoncd/community/blob/main/teps/0023-implicit-mapping.md). This adds information to the defaulting context to allow parameters to be passed down the stack during evaluation. To start, this is only implemented in the v1alpha1 API. Additionally, Tasks are now allowed to accept more params than are actually used. This should generally be safe, since extra params that are provided should not affect the behavior of the Task itself since they are not used. Because v1alpha1 relies on v1beta1 param types, this change affects both v1alpha1 and v1beta1.
- Loading branch information
Showing
13 changed files
with
402 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/v1alpha1/pipelineruns/pipelinerun-implicit-params.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: PipelineRun | ||
metadata: | ||
generateName: echo- | ||
spec: | ||
pipelineSpec: | ||
tasks: | ||
- name: echo-message | ||
taskSpec: | ||
steps: | ||
- name: echo | ||
image: ubuntu | ||
script: | | ||
#!/usr/bin/env bash | ||
echo "$(params.MESSAGE)" | ||
params: | ||
- name: MESSAGE | ||
value: "Good Morning!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: TaskRun | ||
metadata: | ||
generateName: hello- | ||
spec: | ||
params: | ||
- name: message | ||
value: "hello world!" | ||
taskSpec: | ||
# There are no explicit params defined here. They are derived from the TaskRun. | ||
steps: | ||
- name: default | ||
image: ubuntu | ||
script: | | ||
echo $(params.message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// paramCtxKey is the unique identifier for referencing param information from | ||
// a context.Context. See [context.Context.Value](https://pkg.go.dev/context#Context) | ||
// for more details. | ||
var paramCtxKey struct{} | ||
|
||
// paramCtxVal is the data type stored in the param context. | ||
// This maps param names -> ParamSpec. | ||
type paramCtxVal map[string]ParamSpec | ||
|
||
// AddContextParams adds the given Params to the param context. This only | ||
// preserves the fields included in ParamSpec - Name and Type. | ||
func AddContextParams(ctx context.Context, in []Param) context.Context { | ||
if in == nil { | ||
return ctx | ||
} | ||
|
||
out := paramCtxVal{} | ||
// Copy map to ensure that contexts are unique. | ||
v := ctx.Value(paramCtxKey) | ||
if v != nil { | ||
for n, cps := range v.(paramCtxVal) { | ||
out[n] = cps | ||
} | ||
} | ||
for _, p := range in { | ||
// The user may have omitted type data. Fill this in in to normalize data. | ||
if v := p.Value; v.Type == "" { | ||
if len(v.ArrayVal) > 0 { | ||
p.Value.Type = ParamTypeArray | ||
} | ||
if v.StringVal != "" { | ||
p.Value.Type = ParamTypeString | ||
} | ||
} | ||
out[p.Name] = ParamSpec{ | ||
Name: p.Name, | ||
Type: p.Value.Type, | ||
} | ||
} | ||
return context.WithValue(ctx, paramCtxKey, out) | ||
} | ||
|
||
// AddContextParamSpec adds the given ParamSpecs to the param context. | ||
func AddContextParamSpec(ctx context.Context, in []ParamSpec) context.Context { | ||
if in == nil { | ||
return ctx | ||
} | ||
|
||
out := paramCtxVal{} | ||
// Copy map to ensure that contexts are unique. | ||
v := ctx.Value(paramCtxKey) | ||
if v != nil { | ||
for n, ps := range v.(paramCtxVal) { | ||
out[n] = ps | ||
} | ||
} | ||
for _, p := range in { | ||
cps := ParamSpec{ | ||
Name: p.Name, | ||
Type: p.Type, | ||
Description: p.Description, | ||
Default: p.Default, | ||
} | ||
out[p.Name] = cps | ||
} | ||
return context.WithValue(ctx, paramCtxKey, out) | ||
} | ||
|
||
// GetContextParams returns the current context parameters overlayed with a | ||
// given set of params. Overrides should generally be the current layer you | ||
// are trying to evaluate. Any context params not in the overrides will default | ||
// to a generic pass-through param of the given type (i.e. $(params.name) or | ||
// $(params.name[*])). | ||
func GetContextParams(ctx context.Context, overlays ...Param) []Param { | ||
pv := paramCtxVal{} | ||
v := ctx.Value(paramCtxKey) | ||
if v == nil && len(overlays) == 0 { | ||
return nil | ||
} | ||
if v != nil { | ||
pv = v.(paramCtxVal) | ||
} | ||
out := make([]Param, 0, len(pv)) | ||
|
||
// Overlays take precedence over any context params. Keep track of | ||
// these and automatically add them to the output. | ||
overrideSet := make(map[string]Param, len(overlays)) | ||
for _, p := range overlays { | ||
overrideSet[p.Name] = p | ||
out = append(out, p) | ||
} | ||
|
||
// Include the rest of the context params. | ||
for _, ps := range pv { | ||
// Don't do anything for any overlay params - these are already | ||
// included. | ||
if _, ok := overrideSet[ps.Name]; ok { | ||
continue | ||
} | ||
|
||
// If there is no overlay, pass through the param to the next level. | ||
// e.g. for strings $(params.name), for arrays $(params.name[*]). | ||
p := Param{ | ||
Name: ps.Name, | ||
} | ||
if ps.Type == ParamTypeString { | ||
p.Value = ArrayOrString{ | ||
Type: ParamTypeString, | ||
StringVal: fmt.Sprintf("$(params.%s)", ps.Name), | ||
} | ||
} else { | ||
p.Value = ArrayOrString{ | ||
Type: ParamTypeArray, | ||
ArrayVal: []string{fmt.Sprintf("$(params.%s[*])", ps.Name)}, | ||
} | ||
} | ||
out = append(out, p) | ||
} | ||
|
||
return out | ||
} | ||
|
||
// GetContextParamSpecs returns the current context ParamSpecs. | ||
func GetContextParamSpecs(ctx context.Context) []ParamSpec { | ||
v := ctx.Value(paramCtxKey) | ||
if v == nil { | ||
return nil | ||
} | ||
|
||
pv := v.(paramCtxVal) | ||
out := make([]ParamSpec, 0, len(pv)) | ||
for _, ps := range pv { | ||
out = append(out, ParamSpec{ | ||
Name: ps.Name, | ||
Type: ps.Type, | ||
Description: ps.Description, | ||
Default: ps.Default, | ||
}) | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.