Skip to content

Commit

Permalink
[TEP-0142] Support default resolver for Ref to remote StepAction
Browse files Browse the repository at this point in the history
This commit fixes tektoncd#7329. It adds the default resolver for the Ref to
remote StepAction.

Signed-off-by: Yongxuan Zhang yongxuanzhang@google.com
  • Loading branch information
Yongxuanzhang committed Nov 9, 2023
1 parent c2098de commit c8d96e4
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/stepactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,5 @@ spec:
- name: pathInRepo
value: remote_step.yaml
```

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.
7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1/task_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1
import (
"context"

"github.com/tektoncd/pipeline/pkg/apis/config"
"knative.dev/pkg/apis"
)

Expand All @@ -31,6 +32,12 @@ func (t *Task) SetDefaults(ctx context.Context) {

// SetDefaults set any defaults for the task spec
func (ts *TaskSpec) SetDefaults(ctx context.Context) {
cfg := config.FromContextOrDefaults(ctx)
for _, s := range ts.Steps {
if s.Ref != nil && s.Ref.Name == "" && s.Ref.Resolver == "" {
s.Ref.Resolver = ResolverName(cfg.Defaults.DefaultResolverType)
}
}
for i := range ts.Params {
ts.Params[i].SetDefaults(ctx)
}
Expand Down
100 changes: 100 additions & 0 deletions pkg/apis/pipeline/v1/task_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright 2023 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 v1_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
cfgtesting "github.com/tektoncd/pipeline/pkg/apis/config/testing"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
)

func TestTask_SetDefaults(t *testing.T) {
tests := []struct {
name string
in *v1.Task
want *v1.Task
}{{
name: "remote Ref uses default resolver",
in: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{},
}},
},
},
want: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{ResolverRef: v1.ResolverRef{Resolver: "git"}},
}},
},
},
}, {
name: "remote Ref has resolver not overwritten by default resolver",
in: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{ResolverRef: v1.ResolverRef{Resolver: "bundle"}},
}},
},
},
want: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{ResolverRef: v1.ResolverRef{Resolver: "bundle"}},
}},
},
},
}, {
name: "local Ref not adding default resolver",
in: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{Name: "local"},
}},
},
},
want: &v1.Task{
Spec: v1.TaskSpec{
Steps: []v1.Step{{
Name: "foo",
Ref: &v1.Ref{Name: "local"},
}},
},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := cfgtesting.SetDefaults(context.Background(), t, map[string]string{
"default-resolver-type": "git",
})
got := tc.in
got.SetDefaults(ctx)
if d := cmp.Diff(tc.want, got); d != "" {
t.Errorf("SetDefaults %s", diff.PrintWantGot(d))
}
})
}
}

0 comments on commit c8d96e4

Please sign in to comment.