Using Lease Resources to Manage Concurrency in Tekton Builds

Hand drawn threads
An OpenShift Pipelines build in flight
This is what the final pipeline looked like, with concurrency gates.
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: leases.somegroup.org
spec:
group: somegroup.org
version: v1
scope: Namespaced
names:
plural: leases
singular: lease
kind: Lease
shortNames:
- le
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-leases-role
rules:
- apiGroups:
- somegroup.org
resources:
- leases
verbs:
- get
- create
- update
- patch
- delete
- watch
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: tekton-leases-rolebinding
subjects:
- kind: ServiceAccount
name: pipeline
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: tekton-leases-role
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: acquire-lease-task
spec:
params:
- name: label
steps:
- name: create-lease
image: bitnami/kubectl:1.16.3
script: |
export TIMEOUT=”20m”
# EOF in yaml is hard, so make a file the simple way
echo ‘apiVersion: “somegroup.org/v1”’ > e2e-lease.yaml
echo ‘kind: Lease’ >> e2e-lease.yaml
echo ‘metadata:’ >> e2e-lease.yaml
echo ‘ name: e2e-lease’ >> e2e-lease.yaml
echo ‘ label: $(inputs.params.label)’ >> e2e-lease.yaml
#Try to create a lease — either it succeeds, and we are good, or it fails, and then we wait for the lease to be deleted or a timeout, and then we make the lease if there was a deletion # In the event of a timeout, clear out the dead lease so it doesn’t mess up future builds kubectl create -f e2e-test-lease.yaml || (echo Waiting for lease && kubectl wait --for=delete lease.somegroup.org/e2e-test-lease --timeout=$TIMEOUT || ( echo “Grabbing abandoned lease.” && kubectl delete lease.somegroup.org/e2e-test-lease )) # We could be here for three reasons;
# either we successfully created a lease,
# we waited and another run’s lease got deleted,
# or we waited and the other lease is still there.
# Run an apply to make sure a lease with our label now exists.
kubectl apply -f e2e-lease.yaml
Output of the acquire-lease task
In this run, the acquire-lease task tried to get a lease, failed, waited for the lease to be freed, and then proceeded normally.
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
name: release-lease-task
spec:
params:
- name: label
steps:
- name: delete-lease
image: linkyard/kubectl:1.14.2
script: kubectl delete lease.somegroup.org --ignore-not-found=true --field-selector metadata.label=$(inputs.params.label) --field-selector metadata.name=e2e-test-lease
Output of the release-lease task
The release-lease task deletes the lease associated with this pipeline run.
- name: acquire-lease
taskRef:
name: acquire-lease-task
params:
- name: label
value: $(params.label)
finally:
- name: final-release-lease
taskRef:
name: release-lock
params:
- name: label
value: $(params.label)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store