Skip to content

Commit a01e4ec

Browse files
authored
add rbac and fixup broken sample (#109)
* add rbac and fixup broken sample * remove type from triggertemplate
1 parent 3ae9319 commit a01e4ec

File tree

7 files changed

+123
-25
lines changed

7 files changed

+123
-25
lines changed

automerge-example/README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ This example is for more advanced users. Start with the [tekton-example](../tekt
1212
- [`tkn`](https://github.com/tektoncd/cli) if you're not using webhooks
1313
- [`docker`](https://docs.docker.com/get-docker/)
1414

15-
*Note* The 'standalone' code was developed on Docker Desktop and does not yet include the Role-Based Access Control configuration necessary for it to run on OpenShift or other locked-down environments. The 'webhook' code was developed on OpenShift but used a ServiceAccount that had a generous Role attached to it. Full RBAC support should be added to this example under https://github.com/rhd-gitops-example/services/issues/77.
1615

1716
## Setup - both cases
1817

@@ -58,7 +57,14 @@ kubectl apply -f standalone/resources
5857
kubectl apply -f standalone/templates
5958
```
6059

61-
Create or modify an existing ServiceAccount to use your `github-secret` (hint, you can use the provided `sa.yaml` file: this creates `my-sa` which will be used in the `tkn` command)
60+
If you did not follow the [tekton-example](../tekton-example/README.md), you will need to create or modify an existing ServiceAccount to use your `github-secret` (You can use the provided `sa.yaml` file - this creates `my-sa` which will be used in the `tkn` command).
61+
62+
Once you have a service account, you will need to edit `rbac.yaml` and edit the entries `REPLACE_ME.YOUR_SA_NAME` and `REPLACE_ME.YOUR_NAMESPACE`. Once this is done apply the yaml:
63+
64+
```sh
65+
kubectl apply -f standalone/rbac.yaml
66+
```
67+
6268

6369
Finally start the Tekton pipeline referencing your ServiceAccount:
6470

@@ -84,6 +90,7 @@ This secret is used in two related ways. We check the source repository out usin
8490
```yaml
8591
---
8692
apiVersion: v1
93+
type: kubernetes.io/basic-auth
8794
data:
8895
password: [base64-encoded token]
8996
username: [base64-encoded email address]
@@ -93,15 +100,15 @@ metadata:
93100
tekton.dev/git-0: https://github.ibm.com # For example
94101
labels:
95102
serviceAccount: test-sa # As configured in the Webhooks Extension
96-
name: github-repo-access-secret
103+
name: github-secret
97104

98105
---
99106
apiVersion: v1
100107
kind: ServiceAccount
101108
metadata:
102109
name: test-sa
103110
secrets:
104-
- name: github-repo-access-secret
111+
- name: github-secret
105112
```
106113
107114
### Edit templates
@@ -111,7 +118,14 @@ Next edit the `webhooks/templates/*` files.
111118
- In automerge-task.yaml,
112119
- replace `YOUR_DOCKER_HUB_ID` with your DockerHub id.
113120
- replace `YOUR_GHE` with your GitHub Enterprise domain.
114-
- In automerge-tt.yaml, replace YOUR_TEKTON_SERVICE_ACCOUNT with the name of your ServiceAccount used by Tekton.
121+
- In automerge-tt.yaml,
122+
- replace `YOUR_TEKTON_SERVICE_ACCOUNT` with the name of your ServiceAccount used by Tekton.
123+
- In automerge-tb.yaml,
124+
- replace `YOUR-GIT-USER-NAME` with your GitHub user name
125+
- replace `YOUR-GIT-EMAIL` with the associated email address
126+
- In rbac.yaml,
127+
- replace `YOUR_TEKTON_SERVICE_ACCOUNT` as per earlier
128+
- replace `YOUR_NAMESPACE` with the namespace of your serviceaccount
115129

116130
### Apply configuration
117131

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: demo-role-binding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: demo-role
9+
subjects:
10+
- kind: ServiceAccount
11+
name: REPLACE_ME.YOUR_SA_NAME
12+
namespace: REPLACE_ME.YOUR_NAMESPACE
13+
14+
15+
---
16+
17+
apiVersion: rbac.authorization.k8s.io/v1
18+
kind: ClusterRole
19+
metadata:
20+
name: demo-role
21+
rules:
22+
- apiGroups:
23+
- ""
24+
resources:
25+
- services
26+
- serviceaccounts
27+
verbs:
28+
- get
29+
- list
30+
- apiGroups:
31+
- apps
32+
resources:
33+
- deployments
34+
verbs:
35+
- get
36+
- list

automerge-example/webhooks/resources/automerge-pipeline.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ spec:
66
params:
77
- name: github-secret
88
type: string
9-
- name: github-config
10-
type: string
119
- name: event-type
1210
type: string
1311
- name: branch-name
1412
type: string
1513
- name: pull-request-url
1614
type: string
15+
- name: commit-name
16+
type: string
17+
- name: commit-email
18+
type: string
1719
resources:
1820
- name: source-repo
1921
type: git
@@ -28,11 +30,13 @@ spec:
2830
params:
2931
- name: github-secret
3032
value: $(params.github-secret)
31-
- name: github-config
32-
value: $(params.github-config)
3333
- name: event-type
3434
value: $(params.event-type)
3535
- name: branch-name
3636
value: $(params.branch-name)
3737
- name: pull-request-url
38-
value: $(params.pull-request-url)
38+
value: $(params.pull-request-url)
39+
- name: commit-name
40+
value: $(params.commit-name)
41+
- name: commit-email
42+
value: $(params.commit-email)

automerge-example/webhooks/templates/automerge-task.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
name: automerge-task
55
spec:
66
params:
7-
- name: github-secret
7+
- name: github-secret
88
type: string
99
description: Name of the secret containing an access token for Github. Expects Tekton format, 'username' and 'password' keys.
1010
- name: event-type
@@ -16,6 +16,11 @@ spec:
1616
- name: pull-request-url
1717
type: string
1818
description: The URL of the pull request, where applicable
19+
- name: commit-name
20+
type: string
21+
description: GitHub name to author the commit with
22+
- name: commit-email
23+
type: string
1924
inputs:
2025
resources:
2126
- name: git-source
@@ -25,7 +30,7 @@ spec:
2530
image: YOUR_DOCKER_HUB_ID/hub-test
2631
script: |
2732
#!/bin/bash
28-
kubectl apply -k git-source/env --dry-run=client
33+
kubectl apply -k git-source/environments/dev/env --dry-run=client
2934
- name: merge-pr
3035
image: YOUR_DOCKER_HUB_ID/hub-test
3136
script: |
@@ -34,7 +39,7 @@ spec:
3439
if [ $(params.event-type) = "push" ]; then
3540
echo "git push on branch $(params.branch-name)"
3641
if [ $(params.branch-name) = "refs/heads/master" ]; then
37-
echo "kubectl apply -k env"
42+
echo "kubectl apply -k git-source/environments/dev/env"
3843
else
3944
echo "do nothing"
4045
fi

automerge-example/webhooks/resources/automerge-tb.yaml renamed to automerge-example/webhooks/templates/automerge-tb.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ apiVersion: triggers.tekton.dev/v1alpha1
33
kind: TriggerBinding
44
metadata:
55
name: automerge-pipeline-pullrequest-binding
6-
namespace: tekton-pipelines
76
spec:
87
params:
98
- name: pullrequesturl
@@ -14,13 +13,16 @@ spec:
1413
value: $(header.X-GitHub-Event)
1514
- name: branch-name
1615
value: $(body.pull_request.head.ref)
16+
- name: commit-name
17+
value: YOUR-GIT-USER-NAME
18+
- name: commit-email
19+
value: YOUR-GIT-EMAIL
1720
---
1821

1922
apiVersion: triggers.tekton.dev/v1alpha1
2023
kind: TriggerBinding
2124
metadata:
2225
name: automerge-pipeline-push-binding
23-
namespace: tekton-pipelines
2426
spec:
2527
params:
2628
- name: gitrepositoryurl
@@ -30,3 +32,4 @@ spec:
3032
- name: branch-name
3133
value: $(body.ref)
3234

35+

automerge-example/webhooks/templates/automerge-tt.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ apiVersion: triggers.tekton.dev/v1alpha1
22
kind: TriggerTemplate
33
metadata:
44
name: automerge-pipeline-template
5-
namespace: tekton-pipelines
65
spec:
76
params:
87
- name: pullrequesturl
98
description: The pull request url
10-
type: string
119
default: notApplicable
12-
- name: github-configmap-name
13-
description: Name of the configmap that stores the github username and password to be used in commits.
14-
type: string
15-
default: promote-configmap
1610
- name: github-secretname
1711
description: The git secret name
1812
default: github-secret
19-
type: string
2013
- name: github-secret-keyname
2114
description: The git secret key name
2215
default: accessToken
23-
type: string
2416
- name: gitrepositoryurl
2517
description: The url of the Git repository
2618
- name: event-type
2719
description: push, pull_request
2820
- name: branch-name
2921
description: The branch name associated with push events
22+
- name: commit-name
23+
description: GitHub name to author the commit with
24+
default: NA-for-push
25+
- name: commit-email
26+
description: GitHub email of author
27+
default: NA-for-push
3028
resourcetemplates:
3129
- apiVersion: tekton.dev/v1alpha1
3230
kind: PipelineResource
@@ -49,8 +47,6 @@ spec:
4947
name: automerge-pipelinerun-$(uid)
5048
spec:
5149
params:
52-
- name: github-config
53-
value: $(params.github-configmap-name)
5450
- name: github-secret
5551
value: $(params.github-secretname)
5652
- name: branch-name
@@ -59,6 +55,10 @@ spec:
5955
value: $(params.event-type)
6056
- name: pull-request-url
6157
value: $(params.pullrequesturl)
58+
- name: commit-name
59+
value: $(params.commit-name)
60+
- name: commit-email
61+
value: $(params.commit-email)
6262
serviceAccountName: YOUR_TEKTON_SERVICE_ACCOUNT
6363
pipelineRef:
6464
name: automerge-pipeline
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: demo-role-binding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: demo-role
9+
subjects:
10+
- kind: ServiceAccount
11+
name: YOUR_TEKTON_SERVICE_ACCOUNT
12+
namespace: YOUR_NAMESPACE
13+
14+
15+
---
16+
17+
apiVersion: rbac.authorization.k8s.io/v1
18+
kind: ClusterRole
19+
metadata:
20+
name: demo-role
21+
rules:
22+
- apiGroups:
23+
- ""
24+
resources:
25+
- services
26+
- serviceaccounts
27+
verbs:
28+
- get
29+
- list
30+
- apiGroups:
31+
- apps
32+
resources:
33+
- deployments
34+
verbs:
35+
- get
36+
- list

0 commit comments

Comments
 (0)