package org.csanchez.jenkins.plugins.kubernetes;

import hudson.Extension;
import hudson.model.TaskListener;
import hudson.model.Descriptor;
import hudson.model.Node;
import hudson.slaves.AbstractCloudSlave;
import hudson.slaves.NodeProperty;

import java.io.IOException;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy;
import org.kohsuke.stapler.DataBoundConstructor;

import com.github.kubernetes.java.client.model.Pod;
import com.google.common.base.MoreObjects;
import com.nirima.jenkins.plugins.docker.DockerTemplate;

/**
 * @author Carlos Sanchez <carlos@apache.org>
 */
public class KubernetesSlave extends AbstractCloudSlave {

    private static final Logger LOGGER = Logger.getLogger(KubernetesSlave.class.getName());

    private static final long serialVersionUID = -8642936855413034232L;

    private final Pod pod;

    private final KubernetesCloud cloud;

    @DataBoundConstructor
    public KubernetesSlave(Pod pod, DockerTemplate template, String nodeDescription, KubernetesCloud cloud)
            throws Descriptor.FormException, IOException {
        super(pod.getId(), //
                nodeDescription, //
                "/", //
                template.getNumExecutors(), //
                Node.Mode.NORMAL, //
                pod.getLabels().getName(), //
                new KubernetesComputerLauncher(pod, template), //
                new OnceRetentionStrategy(0), //
                Collections.<NodeProperty<Node>> emptyList());

        this.pod = pod;
        this.cloud = cloud;
    }

    @Override
    public KubernetesComputer createComputer() {
        return new KubernetesComputer(this);
    }

    @Override
    protected void _terminate(TaskListener listener) throws IOException, InterruptedException {
        LOGGER.log(Level.INFO, "Terminating Kubernetes instance for slave {0}", name);

        try {
            cloud.connect().deletePod(this.pod.getId());
            LOGGER.log(Level.INFO, "Terminated Kubernetes instance for slave {0}", name);
            toComputer().disconnect(null);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Failure to terminate instance for slave " + name, e);
        }
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this).add("name", name).toString();
    }

    @Extension
    public static final class DescriptorImpl extends SlaveDescriptor {

        @Override
        public String getDisplayName() {
            return "Kubernetes Slave";
        };

        @Override
        public boolean isInstantiable() {
            return false;
        }

    }
}

@@ -235,16 +235,56 @@
 roperty;
+%0Aimport hudson.slaves.RetentionStrategy;
 %0A%0Aimport
@@ -620,16 +620,98 @@
 bjects;%0A
+import com.google.common.base.Optional;%0Aimport com.google.common.primitives.Ints;%0A
 import c
@@ -1608,16 +1608,11 @@
     
-new Once
+get
 Rete
@@ -1625,17 +1625,24 @@
 trategy(
-0
+template
 ), //%0A  
@@ -1727,16 +1727,16 @@
  = pod;%0A
-
         
@@ -1754,32 +1754,243 @@
 = cloud;%0A    %7D%0A%0A
+    private static RetentionStrategy getRetentionStrategy(DockerTemplate template) %7B%0A        return new OnceRetentionStrategy(Optional.fromNullable(Ints.tryParse(template.idleTerminationMinutes)).or(0));%0A    %7D%0A%0A
     @Override%0A  

Use the idleTerminationMinutes for the retention strategy