Skip to content

Commit 9ff4f88

Browse files
QuanZhang-Williamtekton-robot
authored andcommitted
[TEP-0133]: Configure Default resolver
This commit introduces a new `default-resolver-type` field to the `config-defaults` ConfigMap, which configures the default resolver type to be used when the `resolver` is not explicitly provided in the input. Supporting the default resolver type improves simplicity at the authoring time. More details can be found in [TEP-0113: Conifgure Default Resolver]. /kind feature [TEP-0113: Conifgure Default Resolver]: https://github.com/tektoncd/community/blob/main/teps/0133-configure-default-resolver.md
1 parent 089efd8 commit 9ff4f88

19 files changed

+468
-62
lines changed

config/config-defaults.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ data:
8282
# default-forbidden-env contains comma seperated environment variables that cannot be
8383
# overridden by podTemplate.
8484
default-forbidden-env:
85+
86+
# default-resolver-type contains the default resolver type to be used in the cluster,
87+
# no default-resolver-type is specified by default
88+
default-resolver-type:

docs/additional-configs.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ _In the above example the environment variable `TEST_TEKTON` will not be overrid
147147

148148
## Customizing basic execution parameters
149149

150-
You can specify your own values that replace the default service account (`ServiceAccount`), timeout (`Timeout`), and Pod template (`PodTemplate`) values used by Tekton Pipelines in `TaskRun` and `PipelineRun` definitions. To do so, modify the ConfigMap `config-defaults` with your desired values.
150+
You can specify your own values that replace the default service account (`ServiceAccount`), timeout (`Timeout`), resolver (`Resolver`), and Pod template (`PodTemplate`) values used by Tekton Pipelines in `TaskRun` and `PipelineRun` definitions. To do so, modify the ConfigMap `config-defaults` with your desired values.
151151

152152
The example below customizes the following:
153153

@@ -156,9 +156,10 @@ The example below customizes the following:
156156
- the default `app.kubernetes.io/managed-by` label is applied to all Pods created to execute `TaskRuns`.
157157
- the default Pod template to include a node selector to select the node where the Pod will be scheduled by default. A list of supported fields is available [here](https://github.com/tektoncd/pipeline/blob/main/docs/podtemplates.md#supported-fields).
158158
For more information, see [`PodTemplate` in `TaskRuns`](./taskruns.md#specifying-a-pod-template) or [`PodTemplate` in `PipelineRuns`](./pipelineruns.md#specifying-a-pod-template).
159-
- the default `Workspace` configuration can be set for any `Workspaces` that a Task declares but that a TaskRun does not explicitly provide
159+
- the default `Workspace` configuration can be set for any `Workspaces` that a Task declares but that a TaskRun does not explicitly provide.
160160
- the default maximum combinations of `Parameters` in a `Matrix` that can be used to fan out a `PipelineTask`. For
161161
more information, see [`Matrix`](matrix.md).
162+
- the default resolver type to `git`.
162163

163164
```yaml
164165
apiVersion: v1
@@ -175,6 +176,7 @@ data:
175176
default-task-run-workspace-binding: |
176177
emptyDir: {}
177178
default-max-matrix-combinations-count: "1024"
179+
default-resolver-type: "git"
178180
```
179181

180182
**Note:** The `_example` key in the provided [config-defaults.yaml](./../config/config-defaults.yaml)
@@ -288,6 +290,7 @@ Features currently in "alpha" are:
288290
| [Trusted Resources](./trusted-resources.md) | [TEP-0091](https://github.com/tektoncd/community/blob/main/teps/0091-trusted-resources.md) | N/A | `resource-verification-mode` |
289291
| [`Provenance` field in Status](pipeline-api.md#provenance) | [issue#5550](https://github.com/tektoncd/pipeline/issues/5550) | N/A | `enable-provenance-in-status` |
290292
| [Larger Results via Sidecar Logs](#enabling-larger-results-using-sidecar-logs) | [TEP-0127](https://github.com/tektoncd/community/blob/main/teps/0127-larger-results-via-sidecar-logs.md) | [v0.43.0](https://github.com/tektoncd/pipeline/releases/tag/v0.43.0) | `results-from` |
293+
| [Configure Default Resolver](./resolution.md#configuring-built-in-resolvers) | [TEP-0133](https://github.com/tektoncd/community/blob/main/teps/0133-configure-default-resolver.md) | N/A | |
291294

292295
### Beta Features
293296

docs/resolution.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ For new users getting started with Tekton Pipeline remote resolution, check out
2121
These resolvers are enabled by setting the appropriate feature flag in the `resolvers-feature-flags`
2222
ConfigMap in the `tekton-pipelines-resolvers` namespace. See the [section in install.md](install.md#configuring-built-in-remote-task-and-pipeline-resolution) for details.
2323

24+
The default resolver type can be configured by the `default-resolver-type` field in the `config-defaults` ConfigMap (`alpha` feature). See [additional-configs.md](./additional-configs.md) for details.
25+
2426
## Developer Howto: Writing a Resolver From Scratch
2527

2628
For a developer getting started with writing a new Resolver, see

pkg/apis/config/default.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const (
4444
DefaultCloudEventSinkValue = ""
4545
// DefaultMaxMatrixCombinationsCount is used when no max matrix combinations count is specified.
4646
DefaultMaxMatrixCombinationsCount = 256
47+
// DefaultResolverTypeValue is used when no default resolver type is specified
48+
DefaultResolverTypeValue = ""
4749

4850
defaultTimeoutMinutesKey = "default-timeout-minutes"
4951
defaultServiceAccountKey = "default-service-account"
@@ -54,6 +56,7 @@ const (
5456
defaultTaskRunWorkspaceBinding = "default-task-run-workspace-binding"
5557
defaultMaxMatrixCombinationsCountKey = "default-max-matrix-combinations-count"
5658
defaultForbiddenEnv = "default-forbidden-env"
59+
defaultResolverTypeKey = "default-resolver-type"
5760
)
5861

5962
// Defaults holds the default configurations
@@ -68,6 +71,7 @@ type Defaults struct {
6871
DefaultTaskRunWorkspaceBinding string
6972
DefaultMaxMatrixCombinationsCount int
7073
DefaultForbiddenEnv []string
74+
DefaultResolverType string
7175
}
7276

7377
// GetDefaultsConfigName returns the name of the configmap containing all
@@ -97,6 +101,7 @@ func (cfg *Defaults) Equals(other *Defaults) bool {
97101
other.DefaultCloudEventsSink == cfg.DefaultCloudEventsSink &&
98102
other.DefaultTaskRunWorkspaceBinding == cfg.DefaultTaskRunWorkspaceBinding &&
99103
other.DefaultMaxMatrixCombinationsCount == cfg.DefaultMaxMatrixCombinationsCount &&
104+
other.DefaultResolverType == cfg.DefaultResolverType &&
100105
reflect.DeepEqual(other.DefaultForbiddenEnv, cfg.DefaultForbiddenEnv)
101106
}
102107

@@ -108,6 +113,7 @@ func NewDefaultsFromMap(cfgMap map[string]string) (*Defaults, error) {
108113
DefaultManagedByLabelValue: DefaultManagedByLabelValue,
109114
DefaultCloudEventsSink: DefaultCloudEventSinkValue,
110115
DefaultMaxMatrixCombinationsCount: DefaultMaxMatrixCombinationsCount,
116+
DefaultResolverType: DefaultResolverTypeValue,
111117
}
112118

113119
if defaultTimeoutMin, ok := cfgMap[defaultTimeoutMinutesKey]; ok {
@@ -166,6 +172,10 @@ func NewDefaultsFromMap(cfgMap map[string]string) (*Defaults, error) {
166172
tc.DefaultForbiddenEnv = tmpString.List()
167173
}
168174

175+
if defaultResolverType, ok := cfgMap[defaultResolverTypeKey]; ok {
176+
tc.DefaultResolverType = defaultResolverType
177+
}
178+
169179
return &tc, nil
170180
}
171181

pkg/apis/config/default_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestNewDefaultsFromConfigMap(t *testing.T) {
4040
DefaultServiceAccount: "tekton",
4141
DefaultManagedByLabelValue: "something-else",
4242
DefaultMaxMatrixCombinationsCount: 256,
43+
DefaultResolverType: "git",
4344
},
4445
fileName: config.GetDefaultsConfigName(),
4546
},

pkg/apis/config/testdata/config-defaults.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ data:
2121
default-timeout-minutes: "50"
2222
default-service-account: "tekton"
2323
default-managed-by-label-value: "something-else"
24+
default-resolver-type: "git"

pkg/apis/pipeline/v1/pipeline_defaults.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v1
1919
import (
2020
"context"
2121

22+
"github.com/tektoncd/pipeline/pkg/apis/config"
2223
"knative.dev/pkg/apis"
2324
)
2425

@@ -31,6 +32,7 @@ func (p *Pipeline) SetDefaults(ctx context.Context) {
3132

3233
// SetDefaults sets default values for the PipelineSpec's Params, Tasks, and Finally
3334
func (ps *PipelineSpec) SetDefaults(ctx context.Context) {
35+
cfg := config.FromContextOrDefaults(ctx)
3436
for i := range ps.Params {
3537
ps.Params[i].SetDefaults(ctx)
3638
}
@@ -40,6 +42,9 @@ func (ps *PipelineSpec) SetDefaults(ctx context.Context) {
4042
if pt.TaskRef.Kind == "" {
4143
pt.TaskRef.Kind = NamespacedTaskKind
4244
}
45+
if pt.TaskRef.Name == "" && pt.TaskRef.Resolver == "" {
46+
pt.TaskRef.Resolver = ResolverName(cfg.Defaults.DefaultResolverType)
47+
}
4348
}
4449
if pt.TaskSpec != nil {
4550
pt.TaskSpec.SetDefaults(ctx)

pkg/apis/pipeline/v1/pipeline_defaults_test.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/google/go-cmp/cmp"
24+
dfttesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
2425
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
2526
"github.com/tektoncd/pipeline/test/diff"
2627
)
@@ -37,9 +38,10 @@ func TestPipeline_SetDefaults(t *testing.T) {
3738

3839
func TestPipelineSpec_SetDefaults(t *testing.T) {
3940
cases := []struct {
40-
desc string
41-
ps *v1.PipelineSpec
42-
want *v1.PipelineSpec
41+
desc string
42+
ps *v1.PipelineSpec
43+
want *v1.PipelineSpec
44+
defaults map[string]string
4345
}{{
4446
desc: "empty pipelineSpec must not change after setting defaults",
4547
ps: &v1.PipelineSpec{},
@@ -120,6 +122,54 @@ func TestPipelineSpec_SetDefaults(t *testing.T) {
120122
},
121123
}},
122124
},
125+
}, {
126+
desc: "pipeline task with taskRef - with default resolver",
127+
ps: &v1.PipelineSpec{
128+
Tasks: []v1.PipelineTask{{
129+
Name: "foo",
130+
TaskRef: &v1.TaskRef{},
131+
}},
132+
},
133+
want: &v1.PipelineSpec{
134+
Tasks: []v1.PipelineTask{{
135+
Name: "foo",
136+
TaskRef: &v1.TaskRef{
137+
Kind: v1.NamespacedTaskKind,
138+
ResolverRef: v1.ResolverRef{
139+
Resolver: "git",
140+
},
141+
},
142+
}},
143+
},
144+
defaults: map[string]string{
145+
"default-resolver-type": "git",
146+
},
147+
}, {
148+
desc: "pipeline task with taskRef - user-provided resolver overwrites default resolver",
149+
ps: &v1.PipelineSpec{
150+
Tasks: []v1.PipelineTask{{
151+
Name: "foo",
152+
TaskRef: &v1.TaskRef{
153+
ResolverRef: v1.ResolverRef{
154+
Resolver: "custom resolver",
155+
},
156+
},
157+
}},
158+
},
159+
want: &v1.PipelineSpec{
160+
Tasks: []v1.PipelineTask{{
161+
Name: "foo",
162+
TaskRef: &v1.TaskRef{
163+
Kind: v1.NamespacedTaskKind,
164+
ResolverRef: v1.ResolverRef{
165+
Resolver: "custom resolver",
166+
},
167+
},
168+
}},
169+
},
170+
defaults: map[string]string{
171+
"default-resolver-type": "git",
172+
},
123173
}, {
124174
desc: "final pipeline task with taskSpec - default param type must be " + string(v1.ParamTypeString),
125175
ps: &v1.PipelineSpec{
@@ -149,6 +199,9 @@ func TestPipelineSpec_SetDefaults(t *testing.T) {
149199
for _, tc := range cases {
150200
t.Run(tc.desc, func(t *testing.T) {
151201
ctx := context.Background()
202+
if len(tc.defaults) > 0 {
203+
ctx = dfttesting.SetDefaults(context.Background(), t, tc.defaults)
204+
}
152205
tc.ps.SetDefaults(ctx)
153206
if d := cmp.Diff(tc.want, tc.ps); d != "" {
154207
t.Errorf("Mismatch of pipelineSpec after setting defaults: %s", diff.PrintWantGot(d))

pkg/apis/pipeline/v1/pipelinerun_defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func (pr *PipelineRun) SetDefaults(ctx context.Context) {
3636
// SetDefaults implements apis.Defaultable
3737
func (prs *PipelineRunSpec) SetDefaults(ctx context.Context) {
3838
cfg := config.FromContextOrDefaults(ctx)
39+
if prs.PipelineRef != nil && prs.PipelineRef.Name == "" && prs.PipelineRef.Resolver == "" {
40+
prs.PipelineRef.Resolver = ResolverName(cfg.Defaults.DefaultResolverType)
41+
}
3942

4043
if prs.Timeouts == nil || prs.Timeouts.Pipeline == nil {
4144
prs.Timeouts = &TimeoutFields{

pkg/apis/pipeline/v1/pipelinerun_defaults_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,56 @@ func TestPipelineRunDefaulting(t *testing.T) {
332332
"default-service-account": "tekton",
333333
"default-pod-template": "nodeSelector: { 'label': 'value' }\nhostNetwork: true",
334334
},
335+
}, {
336+
name: "PipelineRef uses default resolver",
337+
in: &v1.PipelineRun{Spec: v1.PipelineRunSpec{PipelineRef: &v1.PipelineRef{}}},
338+
want: &v1.PipelineRun{
339+
Spec: v1.PipelineRunSpec{
340+
TaskRunTemplate: v1.PipelineTaskRunTemplate{
341+
ServiceAccountName: config.DefaultServiceAccountValue,
342+
},
343+
Timeouts: &v1.TimeoutFields{
344+
Pipeline: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
345+
},
346+
PipelineRef: &v1.PipelineRef{
347+
ResolverRef: v1.ResolverRef{
348+
Resolver: "git",
349+
},
350+
},
351+
},
352+
},
353+
defaults: map[string]string{
354+
"default-resolver-type": "git",
355+
},
356+
}, {
357+
name: "PipelineRef user-provided resolver overwrites default resolver",
358+
in: &v1.PipelineRun{
359+
Spec: v1.PipelineRunSpec{
360+
PipelineRef: &v1.PipelineRef{
361+
ResolverRef: v1.ResolverRef{
362+
Resolver: "hub",
363+
},
364+
},
365+
},
366+
},
367+
want: &v1.PipelineRun{
368+
Spec: v1.PipelineRunSpec{
369+
TaskRunTemplate: v1.PipelineTaskRunTemplate{
370+
ServiceAccountName: config.DefaultServiceAccountValue,
371+
},
372+
Timeouts: &v1.TimeoutFields{
373+
Pipeline: &metav1.Duration{Duration: config.DefaultTimeoutMinutes * time.Minute},
374+
},
375+
PipelineRef: &v1.PipelineRef{
376+
ResolverRef: v1.ResolverRef{
377+
Resolver: "hub",
378+
},
379+
},
380+
},
381+
},
382+
defaults: map[string]string{
383+
"default-resolver-type": "git",
384+
},
335385
}}
336386
for _, tc := range tests {
337387
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)