Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hub and Remote tasks settings #354

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/302-pac-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ data:
# Wether to automatically create a secret with the token to be use by git-clone
secret-auto-create: "true"

# Tekton HUB API urls
hub-url: "https://api.hub.tekton.dev/v1"

# Wether to allow fetching remote tasks
remote-tasks: "true"

# Since public bitbucket doesn't have the concept of Secret, we need to be
# able to secure the request by querying https://ip-ranges.atlassian.com/,
# this only happen for public bitbucket (ie: when provider.url is not set in
Expand Down
12 changes: 12 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ the `pipelines-as-code` namespace.
application to be used with private repositories. This feature is enabled by
default.

- `remote-tasks`

Let allows remote tasks from pipelinerun annotations. This feature is enabled by
default.

- `hub-url`

The base url for the [tekton hub](https://github.com/tektoncd/hub/)
API. default to the [public hub](https://hub.tekton.dev/):

<https://api.hub.tekton.dev/v1>

## Kubernetes

Pipelines as Code should work directly on kubernetes/minikube/kind. You just need to install the release.yaml
Expand Down
15 changes: 6 additions & 9 deletions pkg/hub/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
)

var (
tektonCatalogHubName = `tekton`
hubBaseURL = `https://api.hub.tekton.dev/v1`
)
var tektonCatalogHubName = `tekton`

func getSpecificVersion(ctx context.Context, cli *params.Run, task string) (string, error) {
func getSpecificVersion(ctx context.Context, cs *params.Run, task string) (string, error) {
split := strings.Split(task, ":")
version := split[len(split)-1]
taskName := split[0]
hr := hubResourceVersion{}
data, err := cli.Clients.GetURL(ctx,
fmt.Sprintf("%s/resource/%s/task/%s/%s", hubBaseURL, tektonCatalogHubName, taskName, version))
data, err := cs.Clients.GetURL(ctx,
fmt.Sprintf("%s/resource/%s/task/%s/%s", cs.Info.Pac.HubURL, tektonCatalogHubName, taskName, version))
if err != nil {
return "", fmt.Errorf("could not fetch specific task version from the hub %s:%s: %w", task, version, err)
}
Expand All @@ -31,9 +28,9 @@ func getSpecificVersion(ctx context.Context, cli *params.Run, task string) (stri
return *hr.Data.RawURL, nil
}

func getLatestVersion(ctx context.Context, cli *params.Run, task string) (string, error) {
func getLatestVersion(ctx context.Context, cs *params.Run, task string) (string, error) {
hr := new(hubResource)
data, err := cli.Clients.GetURL(ctx, fmt.Sprintf("%s/resource/%s/task/%s", hubBaseURL, tektonCatalogHubName, task))
data, err := cs.Clients.GetURL(ctx, fmt.Sprintf("%s/resource/%s/task/%s", cs.Info.Pac.HubURL, tektonCatalogHubName, task))
if err != nil {
return "", err
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/hub/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (

"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
httptesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/http"
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestGetTask(t *testing.T) {
const testHubURL = "https://myprecioushub"

tests := []struct {
name string
task string
Expand All @@ -24,7 +27,7 @@ func TestGetTask(t *testing.T) {
want: "This is Task1",
wantErr: false,
config: map[string]map[string]string{
fmt.Sprintf("%s/resource/%s/task/task1", hubBaseURL, tektonCatalogHubName): {
fmt.Sprintf("%s/resource/%s/task/task1", testHubURL, tektonCatalogHubName): {
"body": `{"data":{"latestVersion": {"rawURL": "https://get.me/task1"}}}`,
"code": "200",
},
Expand All @@ -39,7 +42,7 @@ func TestGetTask(t *testing.T) {
task: "task1",
wantErr: true,
config: map[string]map[string]string{
fmt.Sprintf("%s/resource/%s/task/task1", hubBaseURL, tektonCatalogHubName): {
fmt.Sprintf("%s/resource/%s/task/task1", testHubURL, tektonCatalogHubName): {
"code": "404",
},
},
Expand All @@ -49,7 +52,7 @@ func TestGetTask(t *testing.T) {
task: "task1:1.1",
wantErr: true,
config: map[string]map[string]string{
fmt.Sprintf("%s/resource/%s/task/task1/1.1", hubBaseURL, tektonCatalogHubName): {
fmt.Sprintf("%s/resource/%s/task/task1/1.1", testHubURL, tektonCatalogHubName): {
"code": "404",
},
},
Expand All @@ -60,7 +63,7 @@ func TestGetTask(t *testing.T) {
want: "This is Task2",
wantErr: false,
config: map[string]map[string]string{
fmt.Sprintf("%s/resource/%s/task/task2/1.1", hubBaseURL, tektonCatalogHubName): {
fmt.Sprintf("%s/resource/%s/task/task2/1.1", testHubURL, tektonCatalogHubName): {
"body": `{"data":{"rawURL": "https://get.me/task2"}}`,
"code": "200",
},
Expand All @@ -79,6 +82,7 @@ func TestGetTask(t *testing.T) {
Clients: clients.Clients{
HTTP: *httpTestClient,
},
Info: info.Info{Pac: &info.PacOpts{HubURL: testHubURL}},
}
got, err := GetTask(ctx, cs, tt.task)
if (err != nil) != tt.wantErr {
Expand Down
14 changes: 10 additions & 4 deletions pkg/matcher/annotation_tasks_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestMain(m *testing.M) {
}

func TestRemoteTasksGetTaskFromAnnotations(t *testing.T) {
simpletask := `---
const testHubURL = "https://mybelovedhub"
const simpletask = `---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
Expand Down Expand Up @@ -132,7 +133,7 @@ spec:
pipelinesascode.GroupName + "/task": "[chmouzie]",
},
remoteURLS: map[string]map[string]string{
"https://api.hub.tekton.dev/v1/resource/tekton/task/chmouzie": {
testHubURL + "/resource/tekton/task/chmouzie": {
"body": `{"data": {"LatestVersion": {"RawURL": "http://simple.task"}}}`,
"code": "200",
},
Expand All @@ -149,7 +150,7 @@ spec:
pipelinesascode.GroupName + "/task": "[chmouzie:0.2]",
},
remoteURLS: map[string]map[string]string{
"https://api.hub.tekton.dev/v1/resource/tekton/task/chmouzie/0.2": {
testHubURL + "/resource/tekton/task/chmouzie/0.2": {
"body": `{"data": {"RawURL": "http://simple.task"}}`,
"code": "200",
},
Expand All @@ -167,7 +168,12 @@ spec:
Clients: clients.Clients{
HTTP: *httpTestClient,
},
Info: info.Info{Event: &tt.runevent},
Info: info.Info{
Event: &tt.runevent,
Pac: &info.PacOpts{
HubURL: testHubURL,
},
},
}
ctx, _ := rtesting.SetupFakeContext(t)
rt := RemoteTasks{
Expand Down
1 change: 1 addition & 0 deletions pkg/params/info/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ const (
PACInstallNS = "pipelines-as-code"
PACConfigmapNS = "pipelines-as-code"
PACApplicationName = "Pipelines as Code CI"
HubURL = "https://api.hub.tekton.dev/v1"
)
2 changes: 2 additions & 0 deletions pkg/params/info/pac.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type PacOpts struct {
WebhookType string
PayloadFile string
TektonDashboardURL string
HubURL string
RemoteTasks bool
}

func (p *PacOpts) AddFlags(cmd *cobra.Command) error {
Expand Down
27 changes: 20 additions & 7 deletions pkg/params/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ type Run struct {
Info info.Info
}

func StringToBool(s string) bool {
if strings.ToLower(s) == "true" ||
strings.ToLower(s) == "yes" || s == "1" {
return true
}
return false
}

// GetConfigFromConfigMap get config from configmap, we should remove all the
// logics from cobra flags and just support configmap config and env config in the future.
func (r *Run) GetConfigFromConfigMap(ctx context.Context) error {
Expand All @@ -33,13 +41,7 @@ func (r *Run) GetConfigFromConfigMap(ctx context.Context) error {
}

if secretAutoCreation, ok := cfg.Data["secret-auto-create"]; ok {
sv := false

if strings.ToLower(secretAutoCreation) == "true" ||
strings.ToLower(secretAutoCreation) == "yes" || secretAutoCreation == "1" {
sv = true
}
r.Info.Pac.SecretAutoCreation = sv
r.Info.Pac.SecretAutoCreation = StringToBool(secretAutoCreation)
}

if tektonDashboardURL, ok := cfg.Data["tekton-dashboard-url"]; ok {
Expand All @@ -50,6 +52,17 @@ func (r *Run) GetConfigFromConfigMap(ctx context.Context) error {
r.Clients.Log.Infof("using tekton dashboard url on: %s", os.Getenv("PAC_TEKTON_DASHBOARD_URL"))
r.Clients.ConsoleUI = &consoleui.TektonDashboard{BaseURL: os.Getenv("PAC_TEKTON_DASHBOARD_URL")}
}

if hubURL, ok := cfg.Data["hub-url"]; ok {
r.Info.Pac.HubURL = hubURL
} else {
r.Info.Pac.HubURL = info.HubURL
}

if remoteTask, ok := cfg.Data["remote-tasks"]; ok {
r.Info.Pac.RemoteTasks = StringToBool(remoteTask)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,6 @@ func getAllPipelineRuns(ctx context.Context, cs *params.Run, providerintf provid
// Merge everything (i.e: tasks/pipeline etc..) as a single pipelinerun
return resolve.Resolve(ctx, cs, providerintf, allTemplates, &resolve.Opts{
GenerateName: true,
RemoteTasks: true, // TODO: add an option to disable remote tasking,
RemoteTasks: cs.Info.Pac.RemoteTasks,
})
}
2 changes: 1 addition & 1 deletion pkg/provider/bitbucketcloud/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func lastForwarderForIP(xff string) string {
// checkFromPublicCloudIPS Grab public IP from public cloud and make sure we match it
func (v *Provider) checkFromPublicCloudIPS(ctx context.Context, run *params.Run) (bool, error) {
enval, ok := os.LookupEnv("PAC_BITBUCKET_CLOUD_CHECK_SOURCE_IP")
if !ok || strings.ToLower(enval) != "true" {
if !ok || !params.StringToBool(enval) {
return true, nil
}

Expand Down