Repository: openshift-pipelines/pipelines-as-code
Pull request #344 (2021-11-02 11:19:57)

configmap
- Read configmap directly instead of using env variable
- feat(consoleui): Add Tekton Dashboard support



Patch:
diff --git a/cmd/pipelines-as-code/main.go b/cmd/pipelines-as-code/main.go
index 1e85ccec..f3ab3b16 100644
--- a/cmd/pipelines-as-code/main.go
+++ b/cmd/pipelines-as-code/main.go
@@ -6,14 +6,10 @@ import (
 
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/pipelineascode"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
 )
 
 func main() {
-	clients := &params.Run{Info: info.Info{
-		Event: &info.Event{},
-		Pac:   &info.PacOpts{},
-	}}
+	clients := params.New()
 	cmd := pipelineascode.Command(clients)
 	if err := cmd.Execute(); err != nil {
 		log.Fatal(err)
diff --git a/cmd/tkn-pac/main.go b/cmd/tkn-pac/main.go
index 93f1bc25..855d9f87 100644
--- a/cmd/tkn-pac/main.go
+++ b/cmd/tkn-pac/main.go
@@ -5,14 +5,10 @@ import (
 
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
 )
 
 func main() {
-	clients := &params.Run{Info: info.Info{
-		Event: &info.Event{},
-		Pac:   &info.PacOpts{},
-	}}
+	clients := params.New()
 	pac := tknpac.Root(clients)
 
 	if err := pac.Execute(); err != nil {
diff --git a/config/401-github-issue-recheck.yaml b/config/401-github-issue-recheck.yaml
index 19e66abc..b0fda505 100644
--- a/config/401-github-issue-recheck.yaml
+++ b/config/401-github-issue-recheck.yaml
@@ -167,16 +167,6 @@ spec:
                     imagePullPolicy: Always
                     image: "ko://github.com/openshift-pipelines/pipelines-as-code/cmd/pipelines-as-code"
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_APIURL
                         value: $(params.ghe_host)
                       - name: PAC_WEBVCS_TYPE
diff --git a/config/402-github-pull-request.yaml b/config/402-github-pull-request.yaml
index e5a0ba7a..f4665cbf 100644
--- a/config/402-github-pull-request.yaml
+++ b/config/402-github-pull-request.yaml
@@ -171,16 +171,6 @@ spec:
                 steps:
                   - name: apply-and-launch
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_APIURL
                         value: $(params.ghe_host)
                       - name: PAC_WEBVCS_TYPE
diff --git a/config/403-github-push.yaml b/config/403-github-push.yaml
index ea901e24..8eae94f9 100644
--- a/config/403-github-push.yaml
+++ b/config/403-github-push.yaml
@@ -158,16 +158,6 @@ spec:
                     imagePullPolicy: Always
                     image: "ko://github.com/openshift-pipelines/pipelines-as-code/cmd/pipelines-as-code"
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_APIURL
                         value: $(params.ghe_host)
                       - name: PAC_WEBVCS_TYPE
diff --git a/config/422-bitbucket-cloud-pull-request.yaml b/config/422-bitbucket-cloud-pull-request.yaml
index 90b2c332..71105347 100644
--- a/config/422-bitbucket-cloud-pull-request.yaml
+++ b/config/422-bitbucket-cloud-pull-request.yaml
@@ -163,16 +163,6 @@ spec:
                 steps:
                   - name: apply-and-launch
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_BITBUCKET_CLOUD_CHECK_SOURCE_IP
                         valueFrom:
                           configMapKeyRef:
diff --git a/config/423-bitbucket-cloud-push.yaml b/config/423-bitbucket-cloud-push.yaml
index f948eed5..456c9200 100644
--- a/config/423-bitbucket-cloud-push.yaml
+++ b/config/423-bitbucket-cloud-push.yaml
@@ -141,16 +141,6 @@ spec:
                 steps:
                   - name: apply-and-launch
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_BITBUCKET_CLOUD_CHECK_SOURCE_IP
                         valueFrom:
                           configMapKeyRef:
diff --git a/config/442-bitbucket-server-pull-request.yaml b/config/442-bitbucket-server-pull-request.yaml
index bc54ed74..5788c445 100644
--- a/config/442-bitbucket-server-pull-request.yaml
+++ b/config/442-bitbucket-server-pull-request.yaml
@@ -209,16 +209,6 @@ spec:
                 steps:
                   - name: apply-and-launch
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_TYPE
                         value: "bitbucket-server"
                       - name: PAC_TRIGGER_TARGET
diff --git a/config/443-bitbucket-server-push.yaml b/config/443-bitbucket-server-push.yaml
index 8a1dd670..f88ebd23 100644
--- a/config/443-bitbucket-server-push.yaml
+++ b/config/443-bitbucket-server-push.yaml
@@ -144,16 +144,6 @@ spec:
                 steps:
                   - name: apply-and-launch
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_TYPE
                         value: "bitbucket-server"
                       - name: PAC_TRIGGER_TARGET
diff --git a/docs/INSTALL.md b/docs/INSTALL.md
index adb04de3..76130af8 100644
--- a/docs/INSTALL.md
+++ b/docs/INSTALL.md
@@ -418,10 +418,13 @@ and for the nightly :
 kubectl apply -f https://raw.githubusercontent.com/openshift-pipelines/pipelines-as-code/release-$VERSION/release.k8s.yaml
 ```
 
-Kubernetes Dashboard is not yet supported for logs links but help is always welcome ;)
+If you have [Tekton Dashboard](https://github.com/tektoncd/dashboard). You can
+just add the key `tekton-dashboard-url` in the `pipelines-as-code` configmap
+set to the full url of the `Ingress` host to get tekton dashboard logs url.
 
 ## CLI
 
-`Pipelines as Code` provide a CLI which is designed to work as tkn plugin. To install the plugin see the instruction below.
+`Pipelines as Code` provide a CLI which is designed to work as tkn plugin. To
+install the plugin see the instruction below.
 
-See the [CLI](./cli.md) for the documentation on the CLI and how to install it.
\ No newline at end of file
+See the [CLI](./cli.md) for the documentation on the CLI and how to install it.
diff --git a/pkg/cmd/pipelineascode/pipelineascode.go b/pkg/cmd/pipelineascode/pipelineascode.go
index 7ca0a713..dbe74710 100644
--- a/pkg/cmd/pipelineascode/pipelineascode.go
+++ b/pkg/cmd/pipelineascode/pipelineascode.go
@@ -35,6 +35,11 @@ func Command(cs *params.Run) *cobra.Command {
 			if err != nil {
 				return err
 			}
+
+			if err := cs.GetConfigFromConfigMap(ctx); err != nil {
+				return err
+			}
+
 			vcsintf, err := getVCS(cs.Info.Pac)
 			if err != nil {
 				return err
diff --git a/pkg/consoleui/interface.go b/pkg/consoleui/interface.go
index c74fd283..883378bb 100644
--- a/pkg/consoleui/interface.go
+++ b/pkg/consoleui/interface.go
@@ -3,6 +3,7 @@ package consoleui
 import (
 	"context"
 
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
 	"k8s.io/client-go/dynamic"
 )
 
@@ -31,10 +32,11 @@ func (f FallBackConsole) URL() string {
 	return "https://giphy.com/explore/random"
 }
 
-func New(ctx context.Context, kdyn dynamic.Interface) Interface {
+func New(ctx context.Context, kdyn dynamic.Interface, runinfo *info.Info) Interface {
 	oc := &OpenshiftConsole{}
 	if err := oc.UI(ctx, kdyn); err == nil {
 		return oc
 	}
+
 	return FallBackConsole{}
 }
diff --git a/pkg/consoleui/tektondashboard.go b/pkg/consoleui/tektondashboard.go
new file mode 100644
index 00000000..d002c4c8
--- /dev/null
+++ b/pkg/consoleui/tektondashboard.go
@@ -0,0 +1,28 @@
+package consoleui
+
+import (
+	"context"
+	"fmt"
+
+	"k8s.io/client-go/dynamic"
+)
+
+type TektonDashboard struct {
+	BaseURL string
+}
+
+func (t *TektonDashboard) DetailURL(ns string, pr string) string {
+	return fmt.Sprintf("%s/#/namespaces/%s/pipelineruns/%s", t.BaseURL, ns, pr)
+}
+
+func (t *TektonDashboard) TaskLogURL(ns string, pr string, task string) string {
+	return fmt.Sprintf("%s?pipelineTask=%s", t.DetailURL(ns, pr), task)
+}
+
+func (t *TektonDashboard) URL() string {
+	return t.BaseURL
+}
+
+func (t *TektonDashboard) UI(ctx context.Context, kdyn dynamic.Interface) error {
+	return nil
+}
diff --git a/pkg/params/clients/clients.go b/pkg/params/clients/clients.go
index 44dc4ab6..d7a2aeef 100644
--- a/pkg/params/clients/clients.go
+++ b/pkg/params/clients/clients.go
@@ -110,8 +110,8 @@ func (c *Clients) pacClient(config *rest.Config) (versioned.Interface, error) {
 	return cs, nil
 }
 
-func (c *Clients) consoleUIClient(ctx context.Context, dynamic dynamic.Interface) consoleui.Interface {
-	return consoleui.New(ctx, dynamic)
+func (c *Clients) consoleUIClient(ctx context.Context, dynamic dynamic.Interface, info *info.Info) consoleui.Interface {
+	return consoleui.New(ctx, dynamic, info)
 }
 
 func (c *Clients) NewClients(ctx context.Context, info *info.Info) error {
@@ -149,7 +149,7 @@ func (c *Clients) NewClients(ctx context.Context, info *info.Info) error {
 		return err
 	}
 
-	c.ConsoleUI = c.consoleUIClient(ctx, c.Dynamic)
+	c.ConsoleUI = c.consoleUIClient(ctx, c.Dynamic, info)
 	if err != nil {
 		return err
 	}
diff --git a/pkg/params/info/defaults.go b/pkg/params/info/defaults.go
index a1b9aa47..21c106d7 100644
--- a/pkg/params/info/defaults.go
+++ b/pkg/params/info/defaults.go
@@ -1,5 +1,7 @@
 package info
 
 const (
-	defaultApplicationName = "Pipelines as Code CI"
+	PACInstallNS       = "pipelines-as-code"
+	PACConfigmapNS     = "pipelines-as-code"
+	PACApplicationName = "Pipelines as Code CI"
 )
diff --git a/pkg/params/info/pac.go b/pkg/params/info/pac.go
index ae717c8e..00c964e8 100644
--- a/pkg/params/info/pac.go
+++ b/pkg/params/info/pac.go
@@ -18,6 +18,7 @@ type PacOpts struct {
 	VCSInfoFromRepo    bool // wether the webvcs info come from the repository
 	WebhookType        string
 	PayloadFile        string
+	TektonDashboardURL string
 }
 
 func (p *PacOpts) AddFlags(cmd *cobra.Command) error {
@@ -45,9 +46,6 @@ func (p *PacOpts) AddFlags(cmd *cobra.Command) error {
 		"payload-file", "", os.Getenv("PAC_PAYLOAD_FILE"), "A file containing the webhook payload")
 
 	applicationName := os.Getenv("PAC_APPLICATION_NAME")
-	if applicationName == "" {
-		applicationName = defaultApplicationName
-	}
 	cmd.Flags().StringVar(&p.ApplicationName,
 		"application-name", applicationName,
 		"The name of the application.")
diff --git a/pkg/params/run.go b/pkg/params/run.go
index 62cab189..559b0b37 100644
--- a/pkg/params/run.go
+++ b/pkg/params/run.go
@@ -1,11 +1,63 @@
 package params
 
 import (
+	"context"
+	"os"
+	"strings"
+
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
+	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
 type Run struct {
 	Clients clients.Clients
 	Info    info.Info
 }
+
+// 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 {
+	cfg, err := r.Clients.Kube.CoreV1().ConfigMaps(info.PACInstallNS).Get(ctx, info.PACConfigmapNS, v1.GetOptions{})
+	if err != nil {
+		return err
+	}
+
+	if r.Info.Pac.ApplicationName == "" {
+		if applicationName, ok := cfg.Data["application-name"]; ok {
+			r.Info.Pac.ApplicationName = applicationName
+		} else {
+			r.Info.Pac.ApplicationName = info.PACApplicationName
+		}
+	}
+
+	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
+	}
+
+	if tektonDashboardURL, ok := cfg.Data["tekton-dashboard-url"]; ok {
+		r.Clients.Log.Infof("using tekton dashboard url on: %s", tektonDashboardURL)
+		r.Clients.ConsoleUI = &consoleui.TektonDashboard{BaseURL: tektonDashboardURL}
+	}
+	if os.Getenv("PAC_TEKTON_DASHBOARD_URL") != "" {
+		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")}
+	}
+	return nil
+}
+
+func New() *Run {
+	return &Run{
+		Info: info.Info{
+			Event: &info.Event{},
+			Pac:   &info.PacOpts{},
+		},
+	}
+}


Test patch:
diff --git a/config/404-github-retest-comment.yaml b/config/404-github-retest-comment.yaml
index 515c350e..c686963d 100644
--- a/config/404-github-retest-comment.yaml
+++ b/config/404-github-retest-comment.yaml
@@ -133,16 +133,6 @@ spec:
                     imagePullPolicy: Always
                     image: "ko://github.com/openshift-pipelines/pipelines-as-code/cmd/pipelines-as-code"
                     env:
-                      - name: PAC_APPLICATION_NAME
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: application-name
-                      - name: PAC_SECRET_AUTO_CREATE
-                        valueFrom:
-                          configMapKeyRef:
-                            name: pipelines-as-code
-                            key: secret-auto-create
                       - name: PAC_WEBVCS_APIURL
                         value: $(params.ghe_host)
                       - name: PAC_WEBVCS_TYPE
diff --git a/pkg/consoleui/tektondashboard_test.go b/pkg/consoleui/tektondashboard_test.go
new file mode 100644
index 00000000..bd1a63c6
--- /dev/null
+++ b/pkg/consoleui/tektondashboard_test.go
@@ -0,0 +1,17 @@
+package consoleui
+
+import (
+	"strings"
+	"testing"
+
+	"gotest.tools/v3/assert"
+)
+
+func TestTektonDashboard(t *testing.T) {
+	tr := &TektonDashboard{
+		BaseURL: "https://test",
+	}
+	assert.Assert(t, strings.Contains(tr.DetailURL("ns", "pr"), "namespaces/ns"))
+	assert.Assert(t, strings.Contains(tr.TaskLogURL("ns", "pr", "task"), "pipelineTask=task"))
+	assert.Assert(t, strings.Contains(tr.URL(), "test"))
+}