Skip to content

Commit 6907440

Browse files
authored
[ISV-3340] Disable the concurrent pipeline runs for operator release pipeline. (#431)
* [ISV-3340] Add the locking mechanism to operator release pipeline. * Add the description. * Update the task steps. * Fix the tests. * Update the acquire-lock task. * Update the image in tasks. * Update the image in tasks. --------- Co-authored-by: haripate <>
1 parent 63327dc commit 6907440

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

ansible/roles/operator-pipeline/templates/openshift/pipelines/operator-release-pipeline.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ spec:
419419
- name: connect_registry
420420
value: "$(tasks.set-env.results.connect_registry)"
421421

422-
# call IIB to add the bundle to index
423-
- name: add-bundle-to-index
422+
# acquire/lease the resource to resolve the conflict of concurrent pipelineruns
423+
- name: acquire-lease
424424
runAfter:
425425
- publish-resources
426426
- get-supported-versions
@@ -429,6 +429,18 @@ spec:
429429
operator: notin
430430
values:
431431
- undistributed
432+
taskRef:
433+
name: acquire-lease
434+
params:
435+
- name: pipeline_image
436+
value: "$(params.pipeline_image)"
437+
- name: lease-name
438+
value: *operatorDistribution
439+
440+
# call IIB to add the bundle to index
441+
- name: add-bundle-to-index
442+
runAfter:
443+
- acquire-lease
432444
taskRef:
433445
name: add-bundle-to-index
434446
params:
@@ -548,6 +560,16 @@ spec:
548560

549561
finally:
550562

563+
# Release the acquired resource
564+
- name: release-lease
565+
taskRef:
566+
name: release-lease
567+
params:
568+
- name: pipeline_image
569+
value: "$(params.pipeline_image)"
570+
- name: lease-name
571+
value: *operatorDistribution
572+
551573
# Upload the logs of this pipeline.
552574
# Dependencies on other task results should be minimized. If any
553575
# of those tasks fail, it'll prevent this task from executing.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: acquire-lease
5+
spec:
6+
params:
7+
- name: pipeline_image
8+
- name: lease-name
9+
type: string
10+
description: The name of the resource which will be acquired.
11+
- name: timeout
12+
type: string
13+
default: 90m
14+
description: The timeout for the wait period of leased resource.
15+
steps:
16+
- name: create-lease
17+
image: "$(params.pipeline_image)"
18+
script: |
19+
calculate_duration_in_seconds() {
20+
if [ "${1: -1}" == "m" ]; then
21+
TOTAL_DURATION_IN_SECONDS=$((${1:: -1} * 60))
22+
# If it is "h", then the token is in hours
23+
elif [ "${1: -1}" == "h" ]; then
24+
TOTAL_DURATION_IN_SECONDS=$((${1:: -1} * 3600))
25+
elif [ "${1: -1}" == "s" ]; then
26+
TOTAL_DURATION_IN_SECONDS=$((${1:: -1}))
27+
# Otherwise, the token is in seconds
28+
else
29+
TOTAL_DURATION_IN_SECONDS=${1}
30+
fi
31+
export TOTAL_DURATION_IN_SECONDS
32+
}
33+
acquire_lease() {
34+
LEASE_NAME=$(echo -n $(params.lease-name))
35+
echo "Attempting to create lease $LEASE_NAME"
36+
while true
37+
do
38+
if oc create -f e2e-lease.yaml; then
39+
break
40+
else
41+
echo "Waiting for lease..."
42+
acquiredTime=$(oc get lease $LEASE_NAME -o jsonpath='{.spec.acquireTime}')
43+
echo "acquiredTime: ${acquiredTime}"
44+
acquiredTimeInSeconds=$(date +%s --date="${acquiredTime}")
45+
echo "acquiredTimeInSeconds: ${acquiredTimeInSeconds}"
46+
c=$(date +"%D %T")
47+
echo "currenttime: ${c}"
48+
currentTimeinSeconds=$(date +%s)
49+
echo "currentTimeinSeconds: ${currentTimeinSeconds}"
50+
elapsedSeconds=$(($currentTimeinSeconds-$acquiredTimeInSeconds))
51+
echo "TOTAL_DURATION_IN_SECONDS: ${TOTAL_DURATION_IN_SECONDS}"
52+
echo "elapsedSeconds: ${elapsedSeconds}"
53+
if (( elapsedSeconds > TOTAL_DURATION_IN_SECONDS)); then
54+
echo "Lease abandoned, deleting"
55+
oc delete lease $LEASE_NAME
56+
oc create -f e2e-lease.yaml
57+
break
58+
else
59+
oc wait --for=delete lease $LEASE_NAME --timeout=$(params.timeout) || continue
60+
fi
61+
fi
62+
done
63+
leaseOwner=$(oc get lease $LEASE_NAME -o jsonpath='{.spec.holderIdentity}')
64+
echo "The owner of the lease is ${leaseOwner}"
65+
echo "Acquired lease $LEASE_NAME"
66+
}
67+
create_lease_yaml() {
68+
microTime=$(date -u +"%Y-%m-%dT%H:%M:%S.000000Z")
69+
# EOF in yaml is hard, so make a file the simple way
70+
echo "apiVersion: coordination.k8s.io/v1" > e2e-lease.yaml
71+
echo "kind: Lease" >> e2e-lease.yaml
72+
echo "metadata:" >> e2e-lease.yaml
73+
echo " name: $(params.lease-name)" >> e2e-lease.yaml
74+
echo "spec:" >> e2e-lease.yaml
75+
echo " acquireTime: ${microTime}" >> e2e-lease.yaml
76+
echo " leaseDurationSeconds: ${TOTAL_DURATION_IN_SECONDS}" >> e2e-lease.yaml
77+
echo " holderIdentity: $(params.lease-name)" >> e2e-lease.yaml
78+
}
79+
# Calculate duration in seconds to store in lease
80+
calculate_duration_in_seconds $(params.timeout)
81+
# Create yaml that will be used for the lease object
82+
create_lease_yaml
83+
# Acquire Lease
84+
acquire_lease
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: release-lease
5+
spec:
6+
params:
7+
- name: pipeline_image
8+
- name: lease-name
9+
type: string
10+
description: The name of the resource which will be acquired.
11+
steps:
12+
- name: delete-lease
13+
image: "$(params.pipeline_image)"
14+
script: |
15+
LEASE_NAME=$(echo -n $(params.lease-name))
16+
oc delete lease $LEASE_NAME --ignore-not-found=true

0 commit comments

Comments
 (0)