Skip to content

Commit a97a0d7

Browse files
authored
Split tekton demo. (#669)
Signed-off-by: Michal Pryc <mpryc@redhat.com> Signed-off-by: Michal Pryc <mpryc@redhat.com>
1 parent 5b5142b commit a97a0d7

7 files changed

Lines changed: 126 additions & 93 deletions

demo/demo-tekton.sh

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
#Assumes User is logged in to cluster
33
set -euo pipefail
44

5+
app_name="basic-python-tekton"
6+
app_namespace="basic-python-tekton"
7+
58
Help()
69
{
710
# Display Help
811
echo "Execute a tekton pipeline with various build types."
912
echo
1013
echo "Syntax: scriptTemplate [-h|g|b|n]"
1114
echo "options:"
15+
echo "a application name, default ${app_name}"
16+
echo "n namespace to be used, default ${app_namespace}"
1217
echo "g the git url"
1318
echo "r git branch reference, use this for Pull Requests. e.g. refs/pull/587/head"
1419
echo "h Print this Help."
1520
echo "b build type [buildconfig, binary, s2i]"
16-
echo "n If set then no human interaction is required for subsequent deployments"
17-
echo "t Time in seconds to sleep between subsequent deployments, default 300 (no human only)"
18-
echo "c Number of deployments, default 1 (no human only)"
21+
echo "t Time in seconds to sleep between subsequent deployments, default 300 (enables non-interaction mode)"
22+
echo "c Number of deployments, default 1 (enables non-interaction mode)"
1923
echo
2024
}
2125

@@ -29,7 +33,7 @@ sleep_between=300
2933
no_deployments=1
3034

3135
# Get the options
32-
while getopts ":hg:b:r:nt:c:" option; do
36+
while getopts ":hg:b:r:n:t:c:a:" option; do
3337
case $option in
3438
h) # display Help
3539
Help
@@ -40,12 +44,16 @@ while getopts ":hg:b:r:nt:c:" option; do
4044
current_branch=$OPTARG;;
4145
b) # Enter the build type
4246
build_type=$OPTARG;;
43-
n) # No human interaction
44-
NO_HUMAN=true;;
4547
t) # Sleep between subsequent deployments
46-
sleep_between=$OPTARG;;
48+
sleep_between=$OPTARG && \
49+
NO_HUMAN=true;;
4750
c) # Number of subsequent deployments
48-
no_deployments=$OPTARG;;
51+
no_deployments=$OPTARG && \
52+
NO_HUMAN=true;;
53+
a) # Application name
54+
app_name=$OPTARG;;
55+
n) # Application namespace
56+
app_namespace=$OPTARG;;
4957
\?) # Invalid option
5058
echo "Error: Invalid option"
5159
exit;;
@@ -59,12 +67,19 @@ if [[ $(git status --porcelain --untracked-files=no) ]]; then
5967
fi
6068

6169
echo "============================"
62-
echo "Executing the basic-python-tekton demo for Pelorus..."
70+
echo "Executing the ${app_name} demo for Pelorus..."
6371
echo ""
6472
echo "*** Current Options used ***"
73+
echo "App name: ${app_name}"
74+
echo "Used namespace: ${app_namespace}"
6575
echo "Git URL: $url"
6676
echo "Git ref: $current_branch"
6777
echo "Build Type: $build_type"
78+
echo "No interaction mode: ${NO_HUMAN}"
79+
if [ "${NO_HUMAN}" == true ]; then
80+
echo "Number of deployments: ${no_deployments}"
81+
echo "Time between deployments: ${sleep_between}[s]"
82+
fi
6883
echo "============================"
6984
echo ""
7085

@@ -77,57 +92,57 @@ for cmd in oc tkn; do
7792
done
7893
if ! [[ $all_cmds_found ]]; then exit 1; fi
7994

80-
8195
tekton_setup_dir="$(dirname "${BASH_SOURCE[0]}")/tekton-demo-setup"
8296
python_example_txt="$(dirname "${BASH_SOURCE[0]}")/python-example/response.txt"
8397

84-
echo "Switch to the basic-python-tekton namespace"
85-
oc get ns basic-python-tekton || oc create ns basic-python-tekton
86-
oc project basic-python-tekton
98+
# Create namespace if one doesn't exist
99+
if oc get namespace "${app_namespace}" >/dev/null 2>&1 ; then
100+
echo "1. Namespace '${app_namespace}' already exists"
101+
else
102+
echo "1. Create namespace: ${app_namespace}"
103+
oc process -f "$tekton_setup_dir/01-new-project-request_template.yaml" -p PROJECT_NAME="${app_namespace}" | oc create -f -
104+
fi
87105

88106
echo "Clean up resources prior to execution:"
89107
# cleaning resources vs. deleting the namespace to preserve pipeline run history
90108
# resources are cleaned to ensure that the new running artifact is from the latest build
91-
oc delete --all imagestream -n basic-python-tekton &> /dev/null || true
92-
oc scale dc/basic-python-tekton --replicas=0 &> /dev/null || true
93-
oc delete dc/basic-python-tekton -n basic-python-tekton &> /dev/null || true
94-
oc delete buildConfig basic-python-tekton &> /dev/null || true
95-
oc delete buildconfig.build.openshift.io/basic-python-tekton &> /dev/null || true
96-
oc delete -all pods -n basic-python-tekton &> /dev/null || true
97-
oc delete --all replicationcontroller -n basic-python-tekton &> /dev/null || true
109+
oc delete --all imagestream -n "${app_namespace}" &> /dev/null || true
110+
oc scale "dc/${app_name}" --replicas=0 -n "${app_namespace}" &> /dev/null || true
111+
oc delete "dc/${app_name}" -n "${app_namespace}" &> /dev/null || true
112+
oc delete buildConfig "${app_name}" -n "${app_namespace}" &> /dev/null || true
113+
oc delete "buildconfig.build.openshift.io/${app_name}" -n "${app_namespace}" &> /dev/null || true
114+
oc delete --all pods -n "${app_namespace}" &> /dev/null || true
115+
oc delete --all replicationcontroller -n "${app_namespace}" &> /dev/null || true
116+
oc delete --all template.template.openshift.io -n "${app_namespace}" &> /dev/null || true
117+
oc delete "clusterrolebinding.rbac.authorization.k8s.io/pipeline-role-binding-${app_namespace}" &> /dev/null || true
98118

99119
echo "Setting up resources:"
100120

101-
echo "1. Installing tekton operator"
102-
oc apply -f "$tekton_setup_dir/01-tekton-operator.yaml"
121+
echo "2. Installing tekton operator"
122+
oc apply -f "$tekton_setup_dir/02-tekton-operator.yaml"
103123

104-
echo "2. Setting up python tekton project"
105-
if ! project_setup_output="$(oc apply -f "$tekton_setup_dir/02-project.yaml" 2>&1)"; then
106-
if echo "$project_setup_output" | grep -q "AlreadyExists"; then
107-
echo "Project already exists"
108-
else
109-
echo "$project_setup_output" >&2
110-
exit 1
111-
fi
112-
else
113-
echo "$project_setup_output"
114-
fi
124+
echo "3. Creating pipeline-role ClusterRole"
125+
oc apply -f "$tekton_setup_dir/03-rbac-pipeline-role.yaml"
115126

127+
echo "4. Creating ClusterRoleBinding with ServiceAccount"
128+
oc process -f "$tekton_setup_dir/04-service-account_template.yaml" -p PROJECT_NAMESPACE="${app_namespace}" -n default | oc create -f -
116129

117-
echo "3. Setting up build and deployment information"
118-
oc process -f "$tekton_setup_dir/03-build-and-deploy.yaml" > /tmp/03-build-and-deploy.yaml.out 2>/tmp/03-build-and-deploy.yaml.err
119-
oc apply -f /tmp/03-build-and-deploy.yaml.out
130+
echo "5. Setting up build and deployment information"
131+
oc process -f "$tekton_setup_dir/05-build-and-deploy.yaml" -p NAMESPACE="${app_namespace}" -p APPLICATION_NAME="${app_name}" -n default > /tmp/05-build-and-deploy.yaml.out 2>/tmp/05-build-and-deploy.yaml.err
132+
oc apply -n "${app_namespace}" -f /tmp/05-build-and-deploy.yaml.out
120133

121-
route="$(oc get -n basic-python-tekton route/basic-python-tekton --output=go-template='http://{{.spec.host}}')"
134+
route=$(oc get -n "${app_namespace}" "route/${app_name}" --output=go-template='http://{{.spec.host}}')
122135

123136
counter=1
124137

125138
function run_pipeline {
126-
tkn pipeline start -n basic-python-tekton --showlog basic-python-tekton-pipeline \
127-
-w name=repo,claimName=basic-python-tekton-build-pvc \
139+
set -x
140+
tkn pipeline start -n "${app_namespace}" --showlog "${app_name}-pipeline" \
141+
-w name=repo,claimName="${app_name}-build-pvc" \
128142
-p git-url="$url" -p git-revision="$current_branch" \
129-
-l app.kubernetes.io/name=basic-python-tekton \
143+
-l app.kubernetes.io/name="${app_name}" \
130144
-p BUILD_TYPE="$build_type"
145+
set +x
131146
}
132147

133148
echo -e "\nRunning pipeline\n"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: template.openshift.io/v1
2+
kind: Template
3+
metadata:
4+
creationTimestamp: null
5+
name: project-request
6+
objects:
7+
- apiVersion: project.openshift.io/v1
8+
kind: Project
9+
displayName: Basic Python Tekton App
10+
metadata:
11+
creationTimestamp: null
12+
name: ${PROJECT_NAME}
13+
spec: {}
14+
status: {}
15+
parameters:
16+
- name: PROJECT_NAME
17+
description: "Project Name"

demo/tekton-demo-setup/02-project.yaml

Lines changed: 0 additions & 39 deletions
This file was deleted.
File renamed without changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRole
4+
metadata:
5+
name: pipeline-role
6+
rules:
7+
- apiGroups:
8+
- image.openshift.io
9+
resources:
10+
- "images"
11+
verbs:
12+
- "get"
13+
- "patch"
14+
- apiGroups:
15+
- route.openshift.io
16+
resources:
17+
- "routes"
18+
verbs:
19+
- "get"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: template.openshift.io/v1
2+
kind: Template
3+
metadata:
4+
creationTimestamp: null
5+
name: service-account-request
6+
objects:
7+
- apiVersion: rbac.authorization.k8s.io/v1
8+
kind: ClusterRoleBinding
9+
metadata:
10+
name: pipeline-role-binding-${PROJECT_NAMESPACE}
11+
roleRef:
12+
apiGroup: rbac.authorization.k8s.io
13+
kind: ClusterRole
14+
name: pipeline-role
15+
subjects:
16+
- kind: ServiceAccount
17+
name: pipeline
18+
namespace: ${PROJECT_NAMESPACE}
19+
parameters:
20+
- name: PROJECT_NAMESPACE
21+
description: "Namespace"

demo/tekton-demo-setup/03-build-and-deploy.yaml renamed to demo/tekton-demo-setup/05-build-and-deploy.yaml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ objects:
3333
output:
3434
to:
3535
kind: ImageStreamTag
36-
name: "basic-python-tekton:latest"
36+
name: "${APPLICATION_NAME}:latest"
3737

3838
- kind: ImageStream
3939
apiVersion: image.openshift.io/v1
@@ -134,8 +134,8 @@ objects:
134134
params:
135135
- name: SCRIPT
136136
value: |
137-
oc import-image python-39 --from=registry.access.redhat.com/ubi9/python-39:latest --confirm -n basic-python-tekton
138-
oc import-image toolbox --from=registry.access.redhat.com/ubi9/toolbox:latest --confirm -n basic-python-tekton
137+
oc import-image python-39 --from=registry.access.redhat.com/ubi9/python-39:latest --confirm -n ${NAMESPACE}
138+
oc import-image toolbox --from=registry.access.redhat.com/ubi9/toolbox:latest --confirm -n ${NAMESPACE}
139139
taskRef:
140140
kind: ClusterTask
141141
name: openshift-client
@@ -191,7 +191,7 @@ objects:
191191
params:
192192
- name: SCRIPT
193193
value: |
194-
oc label image "$(tasks.builds2i.results.IMAGE_DIGEST)" app.kubernetes.io/name=basic-python-tekton
194+
oc label image "$(tasks.builds2i.results.IMAGE_DIGEST)" app.kubernetes.io/name=${APPLICATION_NAME}
195195
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.commit.id="$(tasks.checkout.results.commit)"
196196
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.source-location="$(tasks.checkout.results.url)"
197197
oc annotate image "$(tasks.builds2i.results.IMAGE_DIGEST)" io.openshift.build.commit.date="$(tasks.listfiles.results.commit-time)"
@@ -208,8 +208,8 @@ objects:
208208
params:
209209
- name: SCRIPT
210210
value: |
211-
oc patch bc/basic-python-tekton -p '{"spec":{"source":{"git":{"uri": "$(tasks.checkout.results.url)"}}}}'
212-
oc start-build basic-python-tekton --wait
211+
oc patch bc/${APPLICATION_NAME} -p '{"spec":{"source":{"git":{"uri": "$(tasks.checkout.results.url)"}}}}'
212+
oc start-build ${APPLICATION_NAME} --wait
213213
- name: VERSION
214214
value: latest
215215
runAfter: [listfiles]
@@ -228,23 +228,23 @@ objects:
228228
params:
229229
- name: SCRIPT
230230
value: |
231-
oc delete buildConfig basic-python-tekton || true
231+
oc delete buildConfig ${APPLICATION_NAME} || true
232232
#ls -la $(workspaces.manifest-dir.path)
233233
cd $(workspaces.manifest-dir.path)
234234
pwd
235235
# create build
236-
echo "oc new-build python --name=basic-python-tekton --binary=true"
237-
oc new-build python --name=basic-python-tekton --binary=true
236+
echo "oc new-build python --name=${APPLICATION_NAME} --binary=true"
237+
oc new-build python --name=${APPLICATION_NAME} --binary=true
238238
# label build name
239-
echo "oc label bc basic-python-tekton app.kubernetes.io/name=basic-python-tekton"
240-
oc label bc basic-python-tekton app.kubernetes.io/name=basic-python-tekton
239+
echo "oc label bc ${APPLICATION_NAME} app.kubernetes.io/name=${APPLICATION_NAME}"
240+
oc label bc ${APPLICATION_NAME} app.kubernetes.io/name=${APPLICATION_NAME}
241241
# start build
242-
oc start-build bc/basic-python-tekton --from-dir=$(workspaces.manifest-dir.path)/demo/python-example --wait
242+
oc start-build bc/${APPLICATION_NAME} --from-dir=$(workspaces.manifest-dir.path)/demo/python-example --wait
243243
# annotate build
244244
echo "annotate binary build with git data: "
245245
echo "git-url: $(params.git-url)"
246246
echo "commit.id: $(tasks.checkout.results.commit)"
247-
oc annotate build basic-python-tekton-1 --overwrite \
247+
oc annotate build ${APPLICATION_NAME}-1 --overwrite \
248248
io.openshift.build.commit.id="$(tasks.checkout.results.commit)" \
249249
io.openshift.build.source-location="$(params.git-url)"
250250
- name: VERSION
@@ -261,7 +261,7 @@ objects:
261261
- name: SCRIPT
262262
value: |
263263
printf "Test the basic-python app is running:\n"
264-
curl $(oc get route -n basic-python-tekton basic-python-tekton -o=template='http://{{.spec.host}}') 2>&1 | grep "Hello world!" || exit 2
264+
curl $(oc get route -n ${NAMESPACE} ${APPLICATION_NAME} -o=template='http://{{.spec.host}}') 2>&1 | grep "Hello world!" || exit 2
265265
printf "\n"
266266
printf "Test that Pelorus has found the latest git commit:\n"
267267
printf "Build Type is $(params.BUILD_TYPE)\n"

0 commit comments

Comments
 (0)