Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-53297] Use data we have from KubernetesSlave instead of introspecting #388

Merged
merged 4 commits into from
Oct 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,41 @@

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.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils;
import org.jenkinsci.plugins.workflow.steps.StepContext;

import hudson.AbortException;
import hudson.model.Node;

/**
* 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();
return PodTemplateUtils.substituteEnv(getKubernetesSlave().getNodeName());
}

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

KubernetesClient connectToCloud() throws Exception {
return getKubernetesSlave().getKubernetesCloud().connect();
}

private KubernetesSlave getKubernetesSlave() throws java.io.IOException, InterruptedException {
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();
return (KubernetesSlave) node;
}
}