github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/SidecarUtils.groovy (about)

     1  package com.sap.piper
     2  
     3  class SidecarUtils implements Serializable {
     4  
     5      private static Script script
     6  
     7      SidecarUtils(Script script) {
     8          this.script = script
     9      }
    10  
    11      void waitForSidecarReadyOnDocker(String containerId, String command) {
    12          String dockerCommand = "docker exec ${containerId} ${command}"
    13          waitForSidecarReady(dockerCommand)
    14      }
    15  
    16      void waitForSidecarReadyOnKubernetes(String containerName, String command) {
    17          script.container(name: containerName) {
    18              waitForSidecarReady(command)
    19          }
    20      }
    21  
    22      void waitForSidecarReady(String command) {
    23          int sleepTimeInSeconds = 10
    24          int timeoutInSeconds = 5 * 60
    25          int maxRetries = timeoutInSeconds / sleepTimeInSeconds
    26          int retries = 0
    27          while (true) {
    28              script.echo "Waiting for sidecar container"
    29              String status = script.sh script: command, returnStatus: true
    30              if (status == "0") {
    31                  return
    32              }
    33              if (retries > maxRetries) {
    34                  script.error("Timeout while waiting for sidecar container to be ready")
    35              }
    36  
    37              sleep sleepTimeInSeconds
    38              retries++
    39          }
    40      }
    41  }