#19 Remove openshift.withCluster() calls from Builder and Deployer
Closed 4 years ago by mikeb. Opened 4 years ago by rayson.
rayson/c3i-library remove-with-cluster  into  master

@@ -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 wait(buildsel) {

-   openshift.withCluster() {

-     def build = openshift.selector(buildsel)

-     return _wait(build)

-   }

+   def build = openshift.selector(buildsel)

+   return _wait(build)

  }

  

  def _wait(build) {

@@ -6,39 +6,35 @@ 

  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 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")

      }

    }

  }

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.

Since this change is backwards-incompatible: Is it possible to use git tags or branches so that future changes won't break existing Jenkins jobs?

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.

+1, after we are sure this is addressed.

This change should be obsoleted by #25.

Closing this PR, since it is now obsolete.

Pull-Request has been closed by mikeb

4 years ago