Skip to content

Commit

Permalink
[JENKINS-45910] Delete pods in the cloud namespace when pod namespace…
Browse files Browse the repository at this point in the history
… is not defined

Warn if pods have not been found, checking the correct way
  • Loading branch information
carlossg committed Aug 11, 2017
1 parent de6846b commit 6c6f3b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Label;
Expand Down Expand Up @@ -82,9 +83,8 @@ public KubernetesSlave(PodTemplate template, String nodeDescription, String clou
rs,
template.getNodeProperties());

// this.pod = pod;
this.cloudName = cloudName;
this.namespace = template.getNamespace();
this.namespace = Util.fixEmpty(template.getNamespace());
}

public String getCloudName() {
Expand Down Expand Up @@ -164,17 +164,25 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted
return;
}

String actualNamespace = getNamespace() == null ? client.getNamespace() : getNamespace();
try {
client.pods().inNamespace(namespace).withName(name).delete();
Boolean delete = client.pods().inNamespace(actualNamespace).withName(name).delete();
if (delete == null) {
String msg = String.format("Failed to delete pod for agent %s/%s: not found", actualNamespace, name);
LOGGER.log(Level.WARNING, msg);
listener.error(msg);
return;
}
} catch (KubernetesClientException e) {
String msg = String.format("Failed to delete pod for agent %s: %s", name, e.getMessage());
String msg = String.format("Failed to delete pod for agent %s/%s: %s", actualNamespace, name,
e.getMessage());
LOGGER.log(Level.WARNING, msg, e);
listener.error(msg);
computer.disconnect(OfflineCause.create(new Localizable(HOLDER, "offline")));
return;
}

String msg = String.format("Terminated Kubernetes instance for agent %s", name);
String msg = String.format("Terminated Kubernetes instance for agent %s/%s", actualNamespace, name);
LOGGER.log(Level.INFO, msg);
listener.getLogger().println(msg);
computer.disconnect(OfflineCause.create(new Localizable(HOLDER, "offline")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ public void runWithOverriddenNamespace() throws Exception {
}

@Test
/**
* Step namespace should have priority over anything else.
*/
public void runWithOverriddenNamespace2() throws Exception {
String overriddenNamespace = "kubernetes-plugin-overridden-namespace";
KubernetesClient client = cloud.connect();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Step namespace should have priority over anything else.
podTemplate(cloud: 'kubernetes-plugin-test', namespace: 'testns2', label: 'mypod', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [
containerTemplate(name: 'jnlp', image: 'jenkinsci/jnlp-slave:2.62-alpine', args: '${computer.jnlpmac} ${computer.name}')
]) {
Expand Down

0 comments on commit 6c6f3b4

Please sign in to comment.