Skip to content

Commit

Permalink
Support overriding the SCM type and server URL
Browse files Browse the repository at this point in the history
When using the git resolver it was only possible to use one single
provider across cluster.

If the user wanted to mix different providers to fetch the task from
they would not have been able to do so.

With this change we now support overriding the SCM type and server URL
as a parameters in the PipelineRun for the user to pass.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and tekton-robot committed Dec 4, 2023
1 parent d7f8c99 commit 158e13e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 20 deletions.
26 changes: 16 additions & 10 deletions docs/git-resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ This Resolver responds to type `git`.

## Parameters

| Param Name | Description | Example Value |
|--------------|------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
| `url` | URL of the repo to fetch and clone anonymously. Either `url`, or `repo` (with `org`) must be specified, but not both. | `https://github.com/tektoncd/catalog.git` |
| `repo` | The repository to find the resource in. Either `url`, or `repo` (with `org`) must be specified, but not both. | `pipeline`, `test-infra` |
| `org` | The organization to find the repository in. Default can be set in [configuration](#configuration). | `tektoncd`, `kubernetes` |
| `token` | An optional secret name in the `PipelineRun` namespace to fetch the token from. Defaults to empty, meaning it will try to use the configuration from the global configmap. | `secret-name`, (empty) |
| `tokenKey` | An optional key in the token secret name in the `PipelineRun` namespace to fetch the token from. Defaults to `token`. | `token` |
| `revision` | Git revision to checkout a file from. This can be commit SHA, branch or tag. | `aeb957601cf41c012be462827053a21a420befca` `main` `v0.38.2` |
| `pathInRepo` | Where to find the file in the repo. | `task/golang-build/0.3/golang-build.yaml` |
| Param Name | Description | Example Value |
|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------|
| `url` | URL of the repo to fetch and clone anonymously. Either `url`, or `repo` (with `org`) must be specified, but not both. | `https://github.com/tektoncd/catalog.git` |
| `repo` | The repository to find the resource in. Either `url`, or `repo` (with `org`) must be specified, but not both. | `pipeline`, `test-infra` |
| `org` | The organization to find the repository in. Default can be set in [configuration](#configuration). | `tektoncd`, `kubernetes` |
| `token` | An optional secret name in the `PipelineRun` namespace to fetch the token from. Defaults to empty, meaning it will try to use the configuration from the global configmap. | `secret-name`, (empty) |
| `tokenKey` | An optional key in the token secret name in the `PipelineRun` namespace to fetch the token from. Defaults to `token`. | `token` |
| `revision` | Git revision to checkout a file from. This can be commit SHA, branch or tag. | `aeb957601cf41c012be462827053a21a420befca` `main` `v0.38.2` |
| `pathInRepo` | Where to find the file in the repo. | `task/golang-build/0.3/golang-build.yaml` |
| `serverURL` | An optional server URL (that includes the https:// prefix) to connect for API operations | `https:/github.mycompany.com` |
| `scmType` | An optional SCM type to use for API operations | `github`, `gitlab`, `gitea` |

## Requirements

Expand Down Expand Up @@ -137,7 +139,7 @@ spec:
value: task/git-clone/0.6/git-clone.yaml
```

#### Task Resolution with a custom token to the SCM provider
#### Task Resolution with a custom token to a custom SCM provider

```yaml
apiVersion: tekton.dev/v1beta1
Expand All @@ -163,6 +165,10 @@ spec:
value: my-secret-token
- name: tokenKey
value: token
- name: scmType
value: github
- name: serverURL
value: https://ghe.mycompany.com
```

#### Pipeline resolution
Expand Down
47 changes: 47 additions & 0 deletions examples/v1/pipelineruns/no-ci/git-resolver-custom-apiurl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
generateName: git-resolver-
spec:
workspaces:
- name: output # this workspace name must be declared in the Pipeline
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce # access mode may affect how you can use this volume in parallel tasks
resources:
requests:
storage: 1Gi
pipelineSpec:
workspaces:
- name: output
tasks:
- name: task1
workspaces:
- name: output
taskRef:
resolver: git
params:
- name: url
value: https://github.com/tektoncd/catalog.git
- name: pathInRepo
value: /task/git-clone/0.7/git-clone.yaml
- name: revision
value: main
# my-secret-token should be created in the namespace where the
# pipelinerun is created and contain a GitHub personal access
# token in the token key of the secret for the GitHub endpoint
# where the serverURL is configured to
- name: token
value: my-secret-token
- name: tokenKey
value: token
- name: serverURL
value: https://github.my-company.com
- name: scmType
value: github
params:
- name: url
value: https://github.com/tektoncd/catalog
- name: deleteExisting
value: "true"
4 changes: 4 additions & 0 deletions pkg/resolution/resolver/git/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ const (
tokenKeyParam string = "tokenKey"
// defaultTokenKeyParam is the default key in the tokenParam secret for SCM API authentication
defaultTokenKeyParam string = "token"
// scmTypeParams is an optional string overriding the scm-type configuration (ie: github, gitea, gitlab etc..)
scmTypeParam string = "scmType"
// serverURLParams is an optional string to the server URL for the SCM API to connect to
serverURLParam string = "serverURL"
)
30 changes: 22 additions & 8 deletions pkg/resolution/resolver/git/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *Resolver) Resolve(ctx context.Context, origParams []pipelinev1.Param) (

func (r *Resolver) resolveAPIGit(ctx context.Context, params map[string]string) (framework.ResolvedResource, error) {
// If we got here, the "repo" param was specified, so use the API approach
scmType, serverURL, err := r.getSCMTypeAndServerURL(ctx)
scmType, serverURL, err := r.getSCMTypeAndServerURL(ctx, params)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -366,16 +366,30 @@ type secretCacheKey struct {
key string
}

func (r *Resolver) getSCMTypeAndServerURL(ctx context.Context) (string, string, error) {
func (r *Resolver) getSCMTypeAndServerURL(ctx context.Context, params map[string]string) (string, string, error) {
conf := framework.GetResolverConfigFromContext(ctx)

scmType, ok := conf[SCMTypeKey]
if !ok || scmType == "" {
return "", "", fmt.Errorf("missing or empty %s value in configmap", SCMTypeKey)
var scmType, serverURL string
if key, ok := params[scmTypeParam]; ok {
scmType = key
}
if scmType == "" {
if key, ok := conf[SCMTypeKey]; ok && scmType == "" {
scmType = key
} else {
return "", "", fmt.Errorf("missing or empty %s value in configmap", SCMTypeKey)
}
}
if key, ok := params[serverURLParam]; ok {
serverURL = key
}
if serverURL == "" {
if key, ok := conf[ServerURLKey]; ok && serverURL == "" {
serverURL = key
} else {
return "", "", fmt.Errorf("missing or empty %s value in configmap", ServerURLKey)
}
}

serverURL := conf[ServerURLKey]

return scmType, serverURL, nil
}

Expand Down
39 changes: 37 additions & 2 deletions pkg/resolution/resolver/git/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ type params struct {
token string
tokenKey string
namespace string
serverURL string
scmType string
}

func TestResolve(t *testing.T) {
Expand Down Expand Up @@ -402,6 +404,27 @@ func TestResolve(t *testing.T) {
apiToken: "some-token",
expectedCommitSHA: commitSHAsInSCMRepo[1],
expectedStatus: internal.CreateResolutionRequestStatusWithData(otherPipelineYAML),
}, {
name: "api: successful override scm type and server URL from user params",

args: &params{
revision: "main",
pathInRepo: "tasks/example-task.yaml",
org: testOrg,
repo: testRepo,
token: "token-secret",
tokenKey: "token",
namespace: "foo",
scmType: "fake",
serverURL: "fake",
},
config: map[string]string{
ServerURLKey: "notsofake",
SCMTypeKey: "definitivelynotafake",
},
apiToken: "some-token",
expectedCommitSHA: commitSHAsInSCMRepo[0],
expectedStatus: internal.CreateResolutionRequestStatusWithData(mainTaskYAML),
}, {
name: "api: file does not exist",
args: &params{
Expand Down Expand Up @@ -487,8 +510,7 @@ func TestResolve(t *testing.T) {
apiToken: "some-token",
expectedStatus: internal.CreateResolutionRequestFailureStatus(),
expectedErr: createError("missing or empty scm-type value in configmap"),
},
}
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -747,6 +769,19 @@ func createRequest(args *params) *v1beta1.ResolutionRequest {
})
}

if args.serverURL != "" {
rr.Spec.Params = append(rr.Spec.Params, pipelinev1.Param{
Name: serverURLParam,
Value: *pipelinev1.NewStructuredValues(args.serverURL),
})
}
if args.scmType != "" {
rr.Spec.Params = append(rr.Spec.Params, pipelinev1.Param{
Name: scmTypeParam,
Value: *pipelinev1.NewStructuredValues(args.scmType),
})
}

if args.url != "" {
rr.Spec.Params = append(rr.Spec.Params, pipelinev1.Param{
Name: urlParam,
Expand Down

0 comments on commit 158e13e

Please sign in to comment.