/*
 * Copyright (C) 2017 Original Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import hudson.AbortException;
import hudson.FilePath;
import hudson.model.Node;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave;
import org.jenkinsci.plugins.workflow.steps.StepContext;

/**
 * helper class for steps running in a kubernetes `node` context
 */
class KubernetesNodeContext {
    private static final transient String HOSTNAME_FILE = "/etc/hostname";
    private StepContext context;
    private FilePath workspace;

    KubernetesNodeContext(StepContext context) throws Exception {
        this.context = context;
        workspace = context.get(FilePath.class);
    }

    String getPodName() throws Exception {
        return workspace.child(HOSTNAME_FILE).readToString().trim();
    }

    public String getNamespace() throws Exception {
        return workspace.child(Config.KUBERNETES_NAMESPACE_PATH).readToString().trim();
    }

    KubernetesClient connectToCloud() throws Exception {
        Node node = context.get(Node.class);
        if (! (node instanceof KubernetesSlave)) {
            throw new AbortException(String.format("Node is not a Kubernetes node: %s", node != null ? node.getNodeName() : null));
        }
        KubernetesSlave slave = (KubernetesSlave) node;
        KubernetesCloud cloud = slave.getKubernetesCloud();
        return cloud.connect();
    }
}

@@ -665,132 +665,8 @@
 e;%0A%0A
-import hudson.AbortException;%0Aimport hudson.FilePath;%0Aimport hudson.model.Node;%0Aimport io.fabric8.kubernetes.client.Config;%0A
 impo
@@ -772,21 +772,21 @@
 bernetes
-Cloud
+Slave
 ;%0Aimport
@@ -822,39 +822,40 @@
 .kubernetes.
-KubernetesSlave
+PodTemplateUtils
 ;%0Aimport org
@@ -902,16 +902,73 @@
 ntext;%0A%0A
+import hudson.AbortException;%0Aimport hudson.model.Node;%0A%0A
 /**%0A * h
@@ -1078,134 +1078,27 @@
 ate 
-static final transient String HOSTNAME_FILE = %22/etc/hostname%22;%0A    private StepContext context;%0A    private FilePath workspace
+StepContext context
 ;%0A%0A 
@@ -1198,57 +1198,8 @@
 xt;%0A
-        workspace = context.get(FilePath.class);%0A
     
@@ -1263,59 +1263,73 @@
 urn 
-workspace.child(HOSTNAME_FILE).readToString().trim(
+PodTemplateUtils.substituteEnv(getKubernetesSlave().getNodeName()
 );%0A 
@@ -1405,77 +1405,173 @@
 urn 
-workspace.child(Config.KUBERNETES_NAMESPACE_PATH).readToString().trim
+getKubernetesSlave().getNamespace();%0A    %7D%0A%0A    KubernetesClient connectToCloud() throws Exception %7B%0A        return getKubernetesSlave().getKubernetesCloud().connect
 ();%0A
@@ -1573,32 +1573,40 @@
 t();%0A    %7D%0A%0A    
+private 
 KubernetesClient
@@ -1603,39 +1603,74 @@
 etes
-Client connectToCloud() throws 
+Slave getKubernetesSlave() throws java.io.IOException, Interrupted
 Exce
@@ -1927,146 +1927,37 @@
     
-KubernetesSlave slave = (KubernetesSlave) node;%0A        KubernetesCloud cloud = slave.getKubernetesCloud();%0A        return cloud.connect()
+return (KubernetesSlave) node
 ;%0A  

Use data we have from KubernetesSlave instead of introspecting