Skip to content

Commit 991bbb7

Browse files
committed
pkg/tekton: add support for parsing secrets and configmaps…
… and fixing a small issue with commented yamls. Signed-off-by: Vincent Demeester <[email protected]>
1 parent 076d8a9 commit 991bbb7

File tree

5 files changed

+409
-21
lines changed

5 files changed

+409
-21
lines changed

pkg/tekton/load.go

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package tekton
22

33
import (
4+
"regexp"
45
"strings"
56

67
"github.com/pkg/errors"
78
"github.com/sirupsen/logrus"
89
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
10+
corev1 "k8s.io/api/core/v1"
911
k8scheme "k8s.io/client-go/kubernetes/scheme"
1012
)
1113

@@ -14,19 +16,29 @@ type objects struct {
1416
taskruns []*v1beta1.TaskRun
1517
pipelines []*v1beta1.Pipeline
1618
pipelineruns []*v1beta1.PipelineRun
19+
secrets []*corev1.Secret
20+
configs []*corev1.ConfigMap
1721
}
1822

1923
type TaskRun struct {
20-
main *v1beta1.TaskRun
21-
tasks map[string]*v1beta1.Task
24+
main *v1beta1.TaskRun
25+
tasks map[string]*v1beta1.Task
26+
secrets []*corev1.Secret
27+
configs []*corev1.ConfigMap
2228
}
2329

2430
type PipelineRun struct {
2531
main *v1beta1.PipelineRun
2632
tasks map[string]*v1beta1.Task
2733
pipelines map[string]*v1beta1.Pipeline
34+
secrets []*corev1.Secret
35+
configs []*corev1.ConfigMap
2836
}
2937

38+
var (
39+
reg = regexp.MustCompile(`(?m)^\s*#([^#].*?)$`)
40+
)
41+
3042
func readResources(main string, additionals []string) (interface{}, error) {
3143
s := k8scheme.Scheme
3244
if err := v1beta1.AddToScheme(s); err != nil {
@@ -38,9 +50,22 @@ func readResources(main string, additionals []string) (interface{}, error) {
3850
}
3951
switch {
4052
case len(objs.taskruns) == 1 && len(objs.pipelineruns) == 0:
41-
return populateTaskRun(objs.taskruns[0], additionals)
53+
r := TaskRun{
54+
main: objs.taskruns[0],
55+
secrets: objs.secrets,
56+
configs: objs.configs,
57+
tasks: map[string]*v1beta1.Task{},
58+
}
59+
return populateTaskRun(r, additionals)
4260
case len(objs.taskruns) == 0 && len(objs.pipelineruns) == 1:
43-
return populatePipelineRun(objs.pipelineruns[0], additionals)
61+
r := PipelineRun{
62+
main: objs.pipelineruns[0],
63+
secrets: objs.secrets,
64+
configs: objs.configs,
65+
tasks: map[string]*v1beta1.Task{},
66+
pipelines: map[string]*v1beta1.Pipeline{},
67+
}
68+
return populatePipelineRun(r, additionals)
4469
case len(objs.taskruns) == 0 && len(objs.pipelineruns) == 0:
4570
return nil, errors.New("No taskrun or pipelinern to run")
4671
case len(objs.taskruns) == 1 && len(objs.pipelineruns) == 1:
@@ -52,13 +77,9 @@ func readResources(main string, additionals []string) (interface{}, error) {
5277
}
5378
}
5479

55-
func populateTaskRun(tr *v1beta1.TaskRun, additionals []string) (TaskRun, error) {
56-
r := TaskRun{
57-
main: tr,
58-
tasks: map[string]*v1beta1.Task{},
59-
}
80+
func populateTaskRun(r TaskRun, additionals []string) (TaskRun, error) {
6081
for _, data := range additionals {
61-
for _, doc := range strings.Split(strings.Trim(data, "-"), "---") {
82+
for _, doc := range strings.Split(strings.Trim(reg.ReplaceAllString(data, ""), "-"), "---") {
6283
obj, err := parseTektonYAML(doc)
6384
if err != nil {
6485
return r, errors.Wrapf(err, "failed to unmarshal %v", doc)
@@ -74,14 +95,9 @@ func populateTaskRun(tr *v1beta1.TaskRun, additionals []string) (TaskRun, error)
7495
return r, nil
7596
}
7697

77-
func populatePipelineRun(pr *v1beta1.PipelineRun, additionals []string) (PipelineRun, error) {
78-
r := PipelineRun{
79-
main: pr,
80-
tasks: map[string]*v1beta1.Task{},
81-
pipelines: map[string]*v1beta1.Pipeline{},
82-
}
98+
func populatePipelineRun(r PipelineRun, additionals []string) (PipelineRun, error) {
8399
for _, data := range additionals {
84-
for _, doc := range strings.Split(strings.Trim(data, "-"), "---") {
100+
for _, doc := range strings.Split(strings.Trim(reg.ReplaceAllString(data, ""), "-"), "---") {
85101
obj, err := parseTektonYAML(doc)
86102
if err != nil {
87103
return r, errors.Wrapf(err, "failed to unmarshal %v", doc)
@@ -105,8 +121,11 @@ func parseTektonYAMLs(s string) (*objects, error) {
105121
taskruns: []*v1beta1.TaskRun{},
106122
pipelines: []*v1beta1.Pipeline{},
107123
pipelineruns: []*v1beta1.PipelineRun{},
124+
secrets: []*corev1.Secret{},
125+
configs: []*corev1.ConfigMap{},
108126
}
109-
for _, doc := range strings.Split(strings.Trim(s, "-"), "---") {
127+
128+
for _, doc := range strings.Split(strings.Trim(reg.ReplaceAllString(s, ""), "-"), "---") {
110129
obj, err := parseTektonYAML(doc)
111130
if err != nil {
112131
return r, errors.Wrapf(err, "failed to unmarshal %v", doc)
@@ -120,6 +139,10 @@ func parseTektonYAMLs(s string) (*objects, error) {
120139
r.pipelines = append(r.pipelines, o)
121140
case *v1beta1.PipelineRun:
122141
r.pipelineruns = append(r.pipelineruns, o)
142+
case *corev1.Secret:
143+
r.secrets = append(r.secrets, o)
144+
case *corev1.ConfigMap:
145+
r.configs = append(r.configs, o)
123146
}
124147
}
125148
return r, nil

pkg/tekton/load_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ func TestReadResources(t *testing.T) {
1919
tt := []struct {
2020
main string
2121
additionals []string
22-
}{}
22+
}{{
23+
main: "another-pipelinerun.yaml",
24+
additionals: []string{"git-clone.yaml"},
25+
}, {
26+
main: "pipelinerun-with-parallel-tasks-using-pvc.yaml",
27+
additionals: []string{"serviceaccount.yaml"},
28+
}, {
29+
main: "workspaces.yaml",
30+
}}
2331
for _, tc := range tt {
2432
tc := tc
2533
name := fmt.Sprintf("%s-%s", tc.main, strings.Join(tc.additionals, "_"))
@@ -29,11 +37,22 @@ func TestReadResources(t *testing.T) {
2937

3038
func testReadResources(main string, additionals []string) func(*testing.T) {
3139
return func(t *testing.T) {
32-
_, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s", main))
40+
m, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s", main))
3341
if err != nil {
3442
t.Fatalf("ReadFile() = %v", err)
3543
}
36-
44+
a := []string{}
45+
for _, ad := range additionals {
46+
d, err := ioutil.ReadFile(fmt.Sprintf("testdata/%s", ad))
47+
if err != nil {
48+
t.Fatalf("ReadFile() = %v", err)
49+
}
50+
a = append(a, string(d))
51+
}
52+
_, err = readResources(string(m), a)
53+
if err != nil {
54+
t.Fatalf("readResources() = %v", err)
55+
}
3756
}
3857
}
3958

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# This example shows how both sequential and parallel Tasks can share data
2+
# using a PersistentVolumeClaim as a workspace. The TaskRun pods that share
3+
# workspace will be scheduled to the same Node in your cluster with an
4+
# Affinity Assistant (unless it is disabled). The REPORTER task does not
5+
# use a workspace so it does not get affinity to the Affinity Assistant
6+
# and can be scheduled to any Node. If multiple concurrent PipelineRuns are
7+
# executed, their Affinity Assistant pods will repel eachother to different
8+
# Nodes in a Best Effort fashion.
9+
#
10+
# A PipelineRun will pass a message parameter to the Pipeline in this example.
11+
# The STARTER task will write the message to a file in the workspace. The UPPER
12+
# and LOWER tasks will execute in parallel and process the message written by
13+
# the STARTER, and transform it to upper case and lower case. The REPORTER task
14+
# is will use the Task Result from the UPPER task and print it - it is intended
15+
# to mimic a Task that sends data to an external service and shows a Task that
16+
# doesn't use a workspace. The VALIDATOR task will validate the result from
17+
# UPPER and LOWER.
18+
#
19+
# Use the runAfter property in a Pipeline to configure that a task depend on
20+
# another task. Output can be shared both via Task Result (e.g. like REPORTER task)
21+
# or via files in a workspace.
22+
#
23+
# -- (upper) -- (reporter)
24+
# / \
25+
# (starter) (validator)
26+
# \ /
27+
# -- (lower) ------------
28+
29+
apiVersion: tekton.dev/v1beta1
30+
kind: Pipeline
31+
metadata:
32+
name: parallel-pipeline
33+
spec:
34+
params:
35+
- name: message
36+
type: string
37+
38+
workspaces:
39+
- name: ws
40+
41+
tasks:
42+
- name: starter # Tasks that does not declare a runAfter property
43+
taskRef: # will start execution immediately
44+
name: persist-param
45+
params:
46+
- name: message
47+
value: $(params.message)
48+
workspaces:
49+
- name: task-ws
50+
workspace: ws
51+
subPath: init
52+
53+
- name: upper
54+
runAfter: # Note the use of runAfter here to declare that this task
55+
- starter # depends on a previous task
56+
taskRef:
57+
name: to-upper
58+
params:
59+
- name: input-path
60+
value: init/message
61+
workspaces:
62+
- name: w
63+
workspace: ws
64+
65+
- name: lower
66+
runAfter:
67+
- starter
68+
taskRef:
69+
name: to-lower
70+
params:
71+
- name: input-path
72+
value: init/message
73+
workspaces:
74+
- name: w
75+
workspace: ws
76+
77+
- name: reporter # This task does not use workspace and may be scheduled to
78+
runAfter: # any Node in the cluster.
79+
- upper
80+
taskRef:
81+
name: result-reporter
82+
params:
83+
- name: result-to-report
84+
value: $(tasks.upper.results.message) # A result from a previous task is used as param
85+
86+
- name: validator # This task validate the output from upper and lower Task
87+
runAfter: # It does not strictly depend on the reporter Task
88+
- reporter # But you may want to skip this task if the reporter Task fail
89+
- lower
90+
taskRef:
91+
name: validator
92+
workspaces:
93+
- name: files
94+
workspace: ws
95+
---
96+
apiVersion: tekton.dev/v1beta1
97+
kind: Task
98+
metadata:
99+
name: persist-param
100+
spec:
101+
params:
102+
- name: message
103+
type: string
104+
results:
105+
- name: message
106+
description: A result message
107+
steps:
108+
- name: write
109+
image: ubuntu
110+
script: echo $(params.message) | tee $(workspaces.task-ws.path)/message $(results.message.path)
111+
workspaces:
112+
- name: task-ws
113+
---
114+
apiVersion: tekton.dev/v1beta1
115+
kind: Task
116+
metadata:
117+
name: to-upper
118+
spec:
119+
description: |
120+
This task read and process a file from the workspace and write the result
121+
both to a file in the workspace and as a Task Result.
122+
params:
123+
- name: input-path
124+
type: string
125+
results:
126+
- name: message
127+
description: Input message in upper case
128+
steps:
129+
- name: to-upper
130+
image: ubuntu
131+
script: cat $(workspaces.w.path)/$(params.input-path) | tr '[:lower:]' '[:upper:]' | tee $(workspaces.w.path)/upper $(results.message.path)
132+
workspaces:
133+
- name: w
134+
---
135+
apiVersion: tekton.dev/v1beta1
136+
kind: Task
137+
metadata:
138+
name: to-lower
139+
spec:
140+
description: |
141+
This task read and process a file from the workspace and write the result
142+
both to a file in the workspace and as a Task Result
143+
params:
144+
- name: input-path
145+
type: string
146+
results:
147+
- name: message
148+
description: Input message in lower case
149+
steps:
150+
- name: to-lower
151+
image: ubuntu
152+
script: cat $(workspaces.w.path)/$(params.input-path) | tr '[:upper:]' '[:lower:]' | tee $(workspaces.w.path)/lower $(results.message.path)
153+
workspaces:
154+
- name: w
155+
---
156+
apiVersion: tekton.dev/v1beta1
157+
kind: Task
158+
metadata:
159+
name: result-reporter
160+
spec:
161+
description: |
162+
This task is supposed to mimic a service that post data from the Pipeline,
163+
e.g. to an remote HTTP service or a Slack notification.
164+
params:
165+
- name: result-to-report
166+
type: string
167+
steps:
168+
- name: report-result
169+
image: ubuntu
170+
script: echo $(params.result-to-report)
171+
---
172+
apiVersion: tekton.dev/v1beta1
173+
kind: Task
174+
metadata:
175+
name: validator
176+
spec:
177+
steps:
178+
- name: validate-upper
179+
image: ubuntu
180+
script: cat $(workspaces.files.path)/upper | grep HELLO\ TEKTON
181+
- name: validate-lower
182+
image: ubuntu
183+
script: cat $(workspaces.files.path)/lower | grep hello\ tekton
184+
workspaces:
185+
- name: files
186+
---
187+
apiVersion: tekton.dev/v1beta1
188+
kind: PipelineRun
189+
metadata:
190+
generateName: parallel-pipelinerun-
191+
spec:
192+
params:
193+
- name: message
194+
value: Hello Tekton
195+
pipelineRef:
196+
name: parallel-pipeline
197+
workspaces:
198+
- name: ws
199+
volumeClaimTemplate:
200+
spec:
201+
accessModes:
202+
- ReadWriteOnce
203+
resources:
204+
requests:
205+
storage: 1Gi
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: umoci-sa
5+
secrets:
6+
- name: regcred

0 commit comments

Comments
 (0)