Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-68896

Pod Retention on JOB FAILURE STATUS Implementation

      For the existing functionality, We have Pod Retention policies as given below:

      • never
      • always
      • On failure
      • default

      All these above retention policies have been designed on the basis of status of the POD.
      But when it  comes to retain a pod on the basis of status of the build, We are not having any way to retain a pod for sometime and debug the issue that might have caused the failure of the Job.

      Hence, I am trying to implement a solution( a new retention policy "onJobFailure"  ) which will check the status of the last executed build on the dynamic node and will return the result of the run, based on which if it comes out to be a FAILURE, We can retain a pod and if it has been SUCCESS, We can go on to delete the pod. 
       
      Implementation queries:

      While trying to implement the solution, I tried to fetch the Status of the last executed job from the plugin code!

      I followed 2 approaches:

      1.) Using Computer method ( toComputer )  
      As KubernetesSlave.java is having toComputer() and the below function, 

      public PodRetention getPodRetention(KubernetesCloud cloud) {
          PodRetention retentionPolicy = cloud.getPodRetention();
          PodTemplate template = getTemplateOrNull();
          if (template != null) {
              PodRetention pr = template.getPodRetention();
              if(pr instanceof OnJobFailure){
                  ((OnJobFailure) pr).setComputer(toComputer());
              }  
      
      

      is deciding the pod retention policy, I used it to refer my new OnJobFailure.java class which have the inherited method, shouldDeletePod(), where I am getting the status of the last build ran on that computer, ( say computer.getBuilds().getLastBuild().getResult().equals(Result.FAILURE) )

      But Somehow, getLastBuild() never returned anything.

      2.) Using Jenkins Object

      I tried to fetch the result as follows,

      public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {
           
              Jenkins jenkins = Jenkins.getInstanceOrNull();
              if(jenkins!= null){
                List<Job> jobs = jenkins.getItems(Job.class); // This worked and returned the list of jobs which are present in that jenkins
                if( jobs!= null) {
                  Result result = jobs.get(2).getLastBuild().getResult(); // Used (2) to fetch the kubernetes job result specifically
                    if (result!=null && result.equals(Result.FAILURE)) {
                        return false;
                    }
                  }
                }
          return true

      With this approach, I am having the list of all the jobs in list<Job>jobs, but there is no way I can be specific about my run and if I don't have the exact build, I will not be able to fetch the status of the job.
       
      I tried to search the plugin code as well, Is there any specific technical reason why we have not used anywhere the build status in the Kubernetes Plugin code ?

          [JENKINS-68896] Pod Retention on JOB FAILURE STATUS Implementation

          Tim Ploss added a comment -

          I would also like this feature and would help with the implementation but don't have any additional ideas how to get the build status.

          Tim Ploss added a comment - I would also like this feature and would help with the implementation but don't have any additional ideas how to get the build status.

          Wang Ruxun added a comment - - edited

          I think  if you want to find the result of build ,should use the PodStatus.phase

          because  zhe  Master  jenkins  give the   PodStatus'phase value equals the build status

           

           

          public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {

                  if (pod == null || pod.getStatus() == null)

           

          {             return false;         }

                  boolean hasErrors = pod.getStatus().getPhase().toLowerCase().matches("(failed|unknown)");

                  return !hasErrors;

              }

          Wang Ruxun added a comment - - edited I think  if you want to find the result of build ,should use the  PodStatus.phase because  zhe  Master  jenkins  give the    PodStatus'phase  value equals the build status     public boolean shouldDeletePod(KubernetesCloud cloud, Pod pod) {         if (pod == null || pod.getStatus() == null)   {             return false;         }         boolean hasErrors = pod.getStatus() . getPhase(). toLowerCase().matches("(failed|unknown)");         return !hasErrors;     }

            Unassigned Unassigned
            sbajaj23 Sagar Bajaj
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: