forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
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. This functionality is gated behind the alpha feature flag. 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.
- Loading branch information
1 parent
b47ed53
commit f597c92
Showing
18 changed files
with
910 additions
and
38 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
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/v1beta1/pipelineruns/alpha/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/v1beta1 | ||
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/v1beta1 | ||
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,172 @@ | ||
// Copyright 2021 The Tekton Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/tektoncd/pipeline/pkg/apis/config" | ||
) | ||
|
||
// 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 | ||
} | ||
|
||
if config.FromContextOrDefaults(ctx).FeatureFlags.EnableAPIFields != "alpha" { | ||
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 | ||
} | ||
|
||
if config.FromContextOrDefaults(ctx).FeatureFlags.EnableAPIFields != "alpha" { | ||
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 | ||
} |
Oops, something went wrong.