From 36f86f9d55cd969c1c4f21cf8ec1807b04980af3 Mon Sep 17 00:00:00 2001 From: Yuxiang Zhu Date: Oct 25 2018 05:51:38 +0000 Subject: CI/CD: Cleanup unused test environments Fix https://pagure.io/waiverdb/issue/248 This PR will cleanup all test environments that were created 1 hour ago before creating a new test environment. The following Java methods must be approved to allow the pipeline script to access the Java DateTime API. For more information about Jenkins In-process Script Approval, see . ``` method java.text.DateFormat parse java.lang.String method java.text.DateFormat setTimeZone java.util.TimeZone method java.util.Date getTime ``` --- diff --git a/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile b/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile index 233aec5..62a6aa3 100644 --- a/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile +++ b/openshift/pipelines/templates/waiverdb-integration-test.Jenkinsfile @@ -1,3 +1,4 @@ +import java.text.* pipeline { agent { kubernetes { @@ -48,6 +49,35 @@ pipeline { ]) } } + stage('Cleanup') { + // Cleanup all test environments that were created 1 hour ago in case of failures of previous cleanups. + steps { + script { + openshift.withCluster() { + def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + df.setTimeZone(TimeZone.getTimeZone('UTC')) + // Get all OpenShift objects of previous test environments + def oldObjs = openshift.selector('dc,deploy,configmap,secret,svc,route', + ['template': 'waiverdb-test', 'app':'waiverdb']) + def now = new Date() + // Delete all objects that are older than 1 hour + for (objName in oldObjs.names()) { + def obj = openshift.selector(objName) + def objData = obj.object() + if (!objData.metadata.creationTimestamp) + continue + def creationTime = df.parse(objData.metadata.creationTimestamp) + // 1 hour = 1000 * 60 * 60 ms + if (now.getTime() - creationTime.getTime() < 1000 * 60 * 60) + continue + echo "Deleting ${objName}..." + obj.delete() + echo "Deleted ${objName}" + } + } + } + } + } stage('Run functional tests') { environment { // Jenkins BUILD_TAG could be too long (> 63 characters) for OpenShift to consume diff --git a/openshift/waiverdb-test-template.yaml b/openshift/waiverdb-test-template.yaml index 6fc1e65..f200a80 100644 --- a/openshift/waiverdb-test-template.yaml +++ b/openshift/waiverdb-test-template.yaml @@ -13,6 +13,8 @@ apiVersion: v1 kind: Template metadata: name: waiverdb-test-template +labels: + template: waiverdb-test parameters: - name: TEST_ID displayName: Test id