Skip to content

Commit 4c66b81

Browse files
committed
Detect if we have cluster task and use this for generation
By default if we have clustertasks use those when generating with tkn-pac. We will use the official git-clone task automatically when using RHOSP Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 3e0d392 commit 4c66b81

File tree

4 files changed

+150
-103
lines changed

4 files changed

+150
-103
lines changed

pkg/cmd/tknpac/generate/generate.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generate
22

33
import (
4+
"context"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -11,14 +12,18 @@ import (
1112
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
1213
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
1314
"github.com/openshift-pipelines/pipelines-as-code/pkg/git"
15+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1416
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
1517
"github.com/spf13/cobra"
18+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1619
)
1720

18-
var (
19-
eventTypes = map[string]string{"pull_request": "Pull Request", "push": "Push to a Branch or a Tag"}
20-
defaultEventType = "Pull Request"
21-
mainBranch = "main"
21+
var eventTypes = map[string]string{"pull_request": "Pull Request", "push": "Push to a Branch or a Tag"}
22+
23+
const (
24+
gitCloneClusterTaskName = "git-clone"
25+
defaultEventType = "Pull Request"
26+
mainBranch = "main"
2227
)
2328

2429
type Opts struct {
@@ -28,10 +33,11 @@ type Opts struct {
2833
IOStreams *cli.IOStreams
2934
CLIOpts *cli.PacCliOpts
3035

31-
pipelineRunName string
32-
fileName string
33-
overwrite bool
34-
language string
36+
pipelineRunName string
37+
fileName string
38+
overwrite bool
39+
language string
40+
generateWithClusterTask bool
3541
}
3642

3743
func MakeOpts() *Opts {
@@ -44,17 +50,30 @@ func MakeOpts() *Opts {
4450
}
4551
}
4652

47-
func Command(ioStreams *cli.IOStreams) *cobra.Command {
53+
func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
4854
gopt := MakeOpts()
4955
gopt.IOStreams = ioStreams
5056
cmd := &cobra.Command{
5157
Use: "generate",
5258
Aliases: []string{"gen"},
5359
Short: "Generate PipelineRun",
5460
RunE: func(cmd *cobra.Command, args []string) error {
61+
ctx := context.Background()
5562
gopt.CLIOpts = cli.NewCliOptions(cmd)
5663
gopt.IOStreams.SetColorEnabled(!gopt.CLIOpts.NoColoring)
5764

65+
if !gopt.generateWithClusterTask {
66+
if err := run.Clients.NewClients(ctx, &run.Info); err != nil {
67+
// if we don't have access to the cluster we can't do much about it
68+
gopt.generateWithClusterTask = false
69+
} else {
70+
_, err := run.Clients.Tekton.TektonV1beta1().ClusterTasks().Get(ctx, gitCloneClusterTaskName,
71+
metav1.GetOptions{})
72+
if err == nil {
73+
gopt.generateWithClusterTask = true
74+
}
75+
}
76+
}
5877
cwd, err := os.Getwd()
5978
if err != nil {
6079
return err
@@ -80,6 +99,8 @@ func Command(ioStreams *cli.IOStreams) *cobra.Command {
8099
"Wether to overwrite the file if it exist")
81100
cmd.PersistentFlags().StringVarP(&gopt.language, "language", "l", "",
82101
"Generate for this programming language")
102+
cmd.PersistentFlags().BoolVarP(&gopt.generateWithClusterTask, "use-clustertasks", "", false,
103+
"By default we will generate the pipeline using task from hub. If you want to use cluster tasks, set this flag")
83104
return cmd
84105
}
85106

pkg/cmd/tknpac/generate/template.go

Lines changed: 15 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package generate
22

33
import (
44
"bytes"
5+
"embed"
56
"fmt"
7+
"io/ioutil"
8+
"log"
69
"os"
710
"path/filepath"
811
"strings"
@@ -52,98 +55,8 @@ var languageDetection = map[string]langOpts{
5255
},
5356
}
5457

55-
var pipelineRunTmpl = `---
56-
apiVersion: tekton.dev/v1beta1
57-
kind: PipelineRun
58-
metadata:
59-
name: << .prName >>
60-
annotations:
61-
# The event we are targeting as seen from the webhook payload
62-
# this can be an array too, i.e: [pull_request, push]
63-
pipelinesascode.tekton.dev/on-event: "<< .event.EventType >>"
64-
65-
# The branch or tag we are targeting (ie: main, refs/tags/*)
66-
pipelinesascode.tekton.dev/on-target-branch: "<< .event.BaseBranch >>"
67-
68-
# Fetch the git-clone task from hub, we are able to reference later on it
69-
# with taskRef and it will automatically be embedded into our pipeline.
70-
pipelinesascode.tekton.dev/task: "git-clone"
71-
<<- if .extra_task.AnnotationTask >>
72-
73-
# Task for <<.extra_task.Language >>
74-
pipelinesascode.tekton.dev/task-1: "[<< .extra_task.AnnotationTask >>]"
75-
<< end >>
76-
# You can add more tasks in here to reuse, browse the one you like from here
77-
# https://hub.tekton.dev/
78-
# example:
79-
# pipelinesascode.tekton.dev/task-2: "[maven, buildah]"
80-
81-
# How many runs we want to keep attached to this event
82-
pipelinesascode.tekton.dev/max-keep-runs: "5"
83-
spec:
84-
params:
85-
# The variable with brackets are special to Pipelines as Code
86-
# They will automatically be expanded with the events from Github.
87-
- name: repo_url
88-
value: "{{ repo_url }}"
89-
- name: revision
90-
value: "{{ revision }}"
91-
pipelineSpec:
92-
params:
93-
- name: repo_url
94-
- name: revision
95-
workspaces:
96-
- name: source
97-
- name: basic-auth
98-
tasks:
99-
- name: fetch-repository
100-
taskRef:
101-
name: git-clone
102-
workspaces:
103-
- name: output
104-
workspace: source
105-
- name: basic-auth
106-
workspace: basic-auth
107-
params:
108-
- name: url
109-
value: $(params.repo_url)
110-
- name: revision
111-
value: $(params.revision)
112-
<< if .extra_task.Task>>
113-
<< .extra_task.Task >>
114-
<<- end >>
115-
# Customize this task if you like, or just do a taskRef
116-
# to one of the hub task.
117-
- name: noop-task
118-
runAfter:
119-
- fetch-repository
120-
workspaces:
121-
- name: source
122-
workspace: source
123-
taskSpec:
124-
workspaces:
125-
- name: source
126-
steps:
127-
- name: noop-task
128-
image: registry.access.redhat.com/ubi8/ubi-micro:8.4
129-
workingDir: $(workspaces.source.path)
130-
script: |
131-
exit 0
132-
workspaces:
133-
- name: source
134-
volumeClaimTemplate:
135-
spec:
136-
accessModes:
137-
- ReadWriteOnce
138-
resources:
139-
requests:
140-
storage: 1Gi
141-
# This workspace will inject secret to help the git-clone task to be able to
142-
# checkout the private repositories
143-
- name: basic-auth
144-
secret:
145-
secretName: "pac-git-basic-auth-{{repo_owner}}-{{repo_name}}"
146-
`
58+
//go:embed templates
59+
var resource embed.FS
14760

14861
func (o *Opts) detectLanguage() (langOpts, error) {
14962
if o.language != "" {
@@ -169,7 +82,15 @@ func (o *Opts) detectLanguage() (langOpts, error) {
16982

17083
func (o *Opts) genTmpl() (bytes.Buffer, error) {
17184
var outputBuffer bytes.Buffer
172-
t := template.Must(template.New("PipelineRun").Delims("<<", ">>").Parse(pipelineRunTmpl))
85+
embedfile, err := resource.Open("templates/pipelinerun.yaml.tmpl")
86+
if err != nil {
87+
log.Fatal(err)
88+
}
89+
defer embedfile.Close()
90+
tmplB, _ := ioutil.ReadAll(embedfile)
91+
// can't figure out how ParseFS works, so doing this manually..
92+
t := template.Must(template.New("PipelineRun").Delims("<<", ">>").Parse(string(tmplB)))
93+
17394
prName := fmt.Sprintf("%s-%s",
17495
filepath.Base(o.GitInfo.URL),
17596
strings.ReplaceAll(o.event.EventType, "_", "-"))
@@ -181,6 +102,7 @@ func (o *Opts) genTmpl() (bytes.Buffer, error) {
181102
"prName": prName,
182103
"event": o.event,
183104
"extra_task": lang,
105+
"use_cluster_task": o.generateWithClusterTask,
184106
"language_specific_tasks": "",
185107
}
186108
if err := t.Execute(&outputBuffer, data); err != nil {
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: PipelineRun
4+
metadata:
5+
name: << .prName >>
6+
annotations:
7+
# The event we are targeting as seen from the webhook payload
8+
# this can be an array too, i.e: [pull_request, push]
9+
pipelinesascode.tekton.dev/on-event: "<< .event.EventType >>"
10+
11+
# The branch or tag we are targeting (ie: main, refs/tags/*)
12+
pipelinesascode.tekton.dev/on-target-branch: "<< .event.BaseBranch >>"
13+
<< if .use_cluster_task >>
14+
# If we wanted to use the git-clone task from hub we could specify the
15+
# annotation `pipelinesascode.tekton.dev/task annotation` and reference it
16+
# in the taskRef. Pipelines as Code when it sees this will grab it from hub
17+
# and automatically use the latest version. right know by default we will
18+
# use the `git-clone` ClusterTasks as shipped on this cluster.
19+
#
20+
# pipelinesascode.tekton.dev/task: "git-clone"
21+
22+
<<- else >>
23+
# Fetch the git-clone task from hub, we are able to reference later on it
24+
# with taskRef and it will automatically be embedded into our pipeline.
25+
pipelinesascode.tekton.dev/task: "git-clone"
26+
<< end >>
27+
<<- if .extra_task.AnnotationTask >>
28+
29+
# Task for <<.extra_task.Language >>
30+
pipelinesascode.tekton.dev/task-1: "[<< .extra_task.AnnotationTask >>]"
31+
<< end >>
32+
# You can add more tasks in here to reuse, browse the one you like from here
33+
# https://hub.tekton.dev/
34+
# example:
35+
# pipelinesascode.tekton.dev/task-2: "[maven, buildah]"
36+
37+
# How many runs we want to keep attached to this event
38+
pipelinesascode.tekton.dev/max-keep-runs: "5"
39+
spec:
40+
params:
41+
# The variable with brackets are special to Pipelines as Code
42+
# They will automatically be expanded with the events from Github.
43+
- name: repo_url
44+
value: "{{ repo_url }}"
45+
- name: revision
46+
value: "{{ revision }}"
47+
pipelineSpec:
48+
params:
49+
- name: repo_url
50+
- name: revision
51+
workspaces:
52+
- name: source
53+
- name: basic-auth
54+
tasks:
55+
- name: fetch-repository
56+
taskRef:
57+
name: git-clone
58+
<<- if .use_cluster_task >>
59+
kind: ClusterTask
60+
<<- end >>
61+
workspaces:
62+
- name: output
63+
workspace: source
64+
- name: basic-auth
65+
workspace: basic-auth
66+
params:
67+
- name: url
68+
value: $(params.repo_url)
69+
- name: revision
70+
value: $(params.revision)
71+
<<- if .extra_task.Task>>
72+
<< .extra_task.Task >>
73+
<<- end >>
74+
# Customize this task if you like, or just do a taskRef
75+
# to one of the hub task.
76+
- name: noop-task
77+
runAfter:
78+
- fetch-repository
79+
workspaces:
80+
- name: source
81+
workspace: source
82+
taskSpec:
83+
workspaces:
84+
- name: source
85+
steps:
86+
- name: noop-task
87+
image: registry.access.redhat.com/ubi8/ubi-micro:8.5
88+
workingDir: $(workspaces.source.path)
89+
script: |
90+
exit 0
91+
workspaces:
92+
- name: source
93+
volumeClaimTemplate:
94+
spec:
95+
accessModes:
96+
- ReadWriteOnce
97+
resources:
98+
requests:
99+
storage: 1Gi
100+
# This workspace will inject secret to help the git-clone task to be able to
101+
# checkout the private repositories
102+
- name: basic-auth
103+
secret:
104+
secretName: "pac-git-basic-auth-{{repo_owner}}-{{repo_name}}"

pkg/cmd/tknpac/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ func Root(clients *params.Run) *cobra.Command {
3131
cmd.AddCommand(resolve.Command(clients))
3232
cmd.AddCommand(completion.Command())
3333
cmd.AddCommand(bootstrap.Command(clients, ioStreams))
34-
cmd.AddCommand(generate.Command(ioStreams))
34+
cmd.AddCommand(generate.Command(clients, ioStreams))
3535
return cmd
3636
}

0 commit comments

Comments
 (0)