Skip to content

Commit f057ac4

Browse files
author
Shivam Mukhade
authored
Merge pull request #560 from chmouel/issue-556-use-clustertasks-git-clone-on-openshift-for-tkn-pac-g
Detect if we have cluster task and use this for generation
2 parents e4c2ea6 + 02ca95d commit f057ac4

File tree

4 files changed

+147
-103
lines changed

4 files changed

+147
-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: 16 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.5
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,10 +82,19 @@ 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, "_", "-"))
97+
17698
lang, err := o.detectLanguage()
17799
if err != nil {
178100
return bytes.Buffer{}, err
@@ -181,6 +103,7 @@ func (o *Opts) genTmpl() (bytes.Buffer, error) {
181103
"prName": prName,
182104
"event": o.Event,
183105
"extra_task": lang,
106+
"use_cluster_task": o.generateWithClusterTask,
184107
"language_specific_tasks": "",
185108
}
186109
if err := t.Execute(&outputBuffer, data); err != nil {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 want to use the git-clone task from hub we can simply grab it with this annotations.
15+
# and referencing in the taskRef. Pipelines as Code will grab it from hub and automatically use the latest version.
16+
# pipelinesascode.tekton.dev/task: "git-clone"
17+
18+
<<- else >>
19+
# Fetch the git-clone task from hub, we are able to reference later on it
20+
# with taskRef and it will automatically be embedded into our pipeline.
21+
pipelinesascode.tekton.dev/task: "git-clone"
22+
<< end >>
23+
<<- if .extra_task.AnnotationTask >>
24+
25+
# Task for <<.extra_task.Language >>
26+
pipelinesascode.tekton.dev/task-1: "[<< .extra_task.AnnotationTask >>]"
27+
<< end >>
28+
# You can add more tasks in here to reuse, browse the one you like from here
29+
# https://hub.tekton.dev/
30+
# example:
31+
# pipelinesascode.tekton.dev/task-2: "[maven, buildah]"
32+
33+
# How many runs we want to keep attached to this event
34+
pipelinesascode.tekton.dev/max-keep-runs: "5"
35+
spec:
36+
params:
37+
# The variable with brackets are special to Pipelines as Code
38+
# They will automatically be expanded with the events from Github.
39+
- name: repo_url
40+
value: "{{ repo_url }}"
41+
- name: revision
42+
value: "{{ revision }}"
43+
pipelineSpec:
44+
params:
45+
- name: repo_url
46+
- name: revision
47+
workspaces:
48+
- name: source
49+
- name: basic-auth
50+
tasks:
51+
- name: fetch-repository
52+
taskRef:
53+
name: git-clone
54+
<<- if .use_cluster_task >>
55+
kind: ClusterTask
56+
<<- end >>
57+
workspaces:
58+
- name: output
59+
workspace: source
60+
- name: basic-auth
61+
workspace: basic-auth
62+
params:
63+
- name: url
64+
value: $(params.repo_url)
65+
- name: revision
66+
value: $(params.revision)
67+
<<- if .extra_task.Task>>
68+
<< .extra_task.Task >>
69+
<<- end >>
70+
# Customize this task if you like, or just do a taskRef
71+
# to one of the hub task.
72+
- name: noop-task
73+
runAfter:
74+
- fetch-repository
75+
workspaces:
76+
- name: source
77+
workspace: source
78+
taskSpec:
79+
workspaces:
80+
- name: source
81+
steps:
82+
- name: noop-task
83+
image: registry.access.redhat.com/ubi8/ubi-micro:8.5
84+
workingDir: $(workspaces.source.path)
85+
script: |
86+
exit 0
87+
workspaces:
88+
- name: source
89+
volumeClaimTemplate:
90+
spec:
91+
accessModes:
92+
- ReadWriteOnce
93+
resources:
94+
requests:
95+
storage: 1Gi
96+
# This workspace will inject secret to help the git-clone task to be able to
97+
# checkout the private repositories
98+
- name: basic-auth
99+
secret:
100+
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)