title: Kubernetes deployment status in Jenkins
body: <p>I am writing a Jenkins Global pipeline library where I have a stage to deploy my docker image to K8s cluster.
  So after building my docker image during CI process I am promoting(deploying) the image to multiple environments(sequentially lower to higher).
  So, to get the correct status of deployment after running </p>
  
  <pre><code>kubectl apply -f Application-k8s-file.yaml
  </code></pre>
  
  <p>I used following command in a shell step.</p>
  
  <pre><code>kubectl rollout status deployment deployment_name
  </code></pre>
  
  <p>Things goes well if my deployment does not have error but if my deployment has some error(might be some code bug, application does not start) then this command <code>kubectl rollout status deployment &lt;deployment name&gt;</code> runs infinitely(as k8s retries again and again to redeploy) and my Jenkins job runs infinitely(till the Job timeout).</p>
  
  <p>So to find a hack I tried a logic to put the timeout on this command and calculations are something like this:</p>
  
  <p>timeout = (number of pods * liveness probe time + number of pods* 10) seconds</p>
  
  <p>Not sure if this calculation is correct or not.</p>
  
  <p>My code snippet looks like this</p>
  
  <pre><code>        sh(returnStdout: true,script:"#!/bin/sh +e\n timeout --preserve-status ${timeout_value} kubectl rollout status deployment ${deploymentName} --kubeconfig='/opt/kubernetes-secrets/${env}/kubeconfig' 2&gt;tmpfile; echo \$? &gt; tmpfile1")
      def readThisFile = readFile "tmpfile1.txt"
  
  def var=readThisFile.toInteger()
  
             if(var==0)
             {
               echo "deployment successful"
             }
         else{"do something else"}
  </code></pre>
  
  <p>This works well initially but later I find that k8s "kubectl rollout status deployment " command doesn't give exit code 0 until all the pods get scheduled and old get terminated completely which sometimes take time.</p>
  
  <p>What I basically want is a minimal calculated timeout value.</p>
  
  <p>My K8s file have parameters like this:</p>
  
  <pre><code>   spec:
       minReadySeconds: 30
  
      livenessProbe:
        httpGet:
          path: /ping
          port: 80
        initialDelaySeconds: 45
        periodSeconds: 5
        timeoutSeconds: 60
      name: test-dummyservice
      ports:
      - containerPort: 80
      readinessProbe:
        httpGet:
          path: /health
          port: 80
        initialDelaySeconds: 60
        periodSeconds: 120
        timeoutSeconds: 60
  </code></pre>
  
  <p>I did not find anything specific related to this in K8s documentation. Anyone facing same challenge?</p>