From 70c16f56f7c574810dfe60a7192d7b9a94e666d2 Mon Sep 17 00:00:00 2001 From: Yuxiang Zhu Date: Jun 19 2019 08:18:56 +0000 Subject: Remove openshift.withCluster() calls from Builder and Deployer This will allow users to customize the OpenShift cluster and project that they want to interact with. Users are expected to call `c3i.foo()` functions within a `openshift.withCluster()` closure. We need to ensure our Jenkinsfile are doing this way before merging this. --- diff --git a/src/com/redhat/c3i/util/Builder.groovy b/src/com/redhat/c3i/util/Builder.groovy index 0f6b098..09ed01f 100644 --- a/src/com/redhat/c3i/util/Builder.groovy +++ b/src/com/redhat/c3i/util/Builder.groovy @@ -4,18 +4,14 @@ package com.redhat.c3i.util def build(List models, boolean wait, Object... args) { - openshift.withCluster() { - def objects = openshift.apply(models) - def bc = objects.narrow("bc") - return _build(bc, wait, args) - } + def objects = openshift.apply(models) + def bc = objects.narrow("bc") + return _build(bc, wait, args) } def build(bcname, boolean wait, Object... args) { - openshift.withCluster() { - def bc = openshift.selector(bcname) - return _build(bc, wait, args) - } + def bc = openshift.selector(bcname) + return _build(bc, wait, args) } def _build(bc, boolean wait, Object... args) { @@ -27,10 +23,8 @@ def _build(bc, boolean wait, Object... args) { } def wait(buildsel) { - openshift.withCluster() { - def build = openshift.selector(buildsel) - return _wait(build) - } + def build = openshift.selector(buildsel) + return _wait(build) } def _wait(build) { diff --git a/src/com/redhat/c3i/util/Deployer.groovy b/src/com/redhat/c3i/util/Deployer.groovy index 6be4006..a91f0ca 100644 --- a/src/com/redhat/c3i/util/Deployer.groovy +++ b/src/com/redhat/c3i/util/Deployer.groovy @@ -6,39 +6,35 @@ package com.redhat.c3i.util import java.text.* def run(models) { - openshift.withCluster() { - def objects = openshift.apply(models) - def dcs = objects.narrow('dc') - def rm = dcs.rollout() - def replicas = 0 - dcs.withEach { - replicas += it.object().spec.replicas - } - return replicas + def objects = openshift.apply(models) + def dcs = objects.narrow('dc') + def rm = dcs.rollout() + def replicas = 0 + dcs.withEach { + replicas += it.object().spec.replicas } + return replicas } def wait(num, selector) { echo "Waiting for ${num} test pods matching ${selector} to become Ready" - openshift.withCluster() { - def pods = openshift.selector('pods', selector) - timeout(10) { - pods.untilEach(num) { - def pod = it.object() - if (pod.status.phase in ["New", "Pending", "Unknown"]) { - return false - } - if (pod.status.phase == "Running") { - for (cond in pod.status.conditions) { - if (cond.type == 'Ready' && cond.status == 'True') { - echo "Pod ${pod.metadata.name} is ready" - return true - } + def pods = openshift.selector('pods', selector) + timeout(10) { + pods.untilEach(num) { + def pod = it.object() + if (pod.status.phase in ["New", "Pending", "Unknown"]) { + return false + } + if (pod.status.phase == "Running") { + for (cond in pod.status.conditions) { + if (cond.type == 'Ready' && cond.status == 'True') { + echo "Pod ${pod.metadata.name} is ready" + return true } - return false } - error("Test pod ${pod.metadata.name} is not running. Current phase is ${pod.status.phase}.") + return false } + error("Test pod ${pod.metadata.name} is not running. Current phase is ${pod.status.phase}.") } } } @@ -50,25 +46,23 @@ def runUntilReady(models, selector) { def cleanup(int age=60, String... apps) { // age is specified in minutes - openshift.withCluster() { - def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") - df.setTimeZone(TimeZone.getTimeZone("UTC")) - def oldobjs = [] - for (app in apps) { - def selected = openshift.selector("all,pvc,configmap,secret", ["app": app]) - oldobjs.addAll(selected.objects()) + def df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + df.setTimeZone(TimeZone.getTimeZone("UTC")) + def oldobjs = [] + for (app in apps) { + def selected = openshift.selector("all,pvc,configmap,secret", ["app": app]) + oldobjs.addAll(selected.objects()) + } + def now = new Date() + // Delete all objects that are older than 1 hour + for (obj in oldobjs) { + if (!obj.metadata.creationTimestamp) { + continue } - def now = new Date() - // Delete all objects that are older than 1 hour - for (obj in oldobjs) { - if (!obj.metadata.creationTimestamp) { - continue - } - def creationTime = df.parse(obj.metadata.creationTimestamp) - if (now.getTime() - creationTime.getTime() > (1000 * 60 * age)) { - echo "Deleting ${obj.kind} ${obj.metadata.name}..." - openshift.delete(obj.kind, obj.metadata.name, "--ignore-not-found=true") - } + def creationTime = df.parse(obj.metadata.creationTimestamp) + if (now.getTime() - creationTime.getTime() > (1000 * 60 * age)) { + echo "Deleting ${obj.kind} ${obj.metadata.name}..." + openshift.delete(obj.kind, obj.metadata.name, "--ignore-not-found=true") } } }