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

Wait for complete disconnection with pod slaves #248

Merged
Changes from all commits
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
@@ -1,5 +1,25 @@
package org.csanchez.jenkins.plugins.kubernetes;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateEncodingException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.durabletask.executors.Messages;
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy;
import org.jvnet.localizer.Localizable;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.Util;
import hudson.model.Computer;
Expand All @@ -14,22 +34,6 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import jenkins.model.Jenkins;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.durabletask.executors.Messages;
import org.jenkinsci.plugins.durabletask.executors.OnceRetentionStrategy;
import org.jvnet.localizer.Localizable;
import org.jvnet.localizer.ResourceBundleHolder;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Carlos Sanchez carlos@apache.org
Expand All @@ -38,6 +42,9 @@ public class KubernetesSlave extends AbstractCloudSlave {

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

private static final Integer DISCONNECTION_TIMEOUT = Integer
.getInteger(KubernetesSlave.class.getName() + ".disconnectionTimeout", 5);

private static final long serialVersionUID = -8642936855413034232L;
private static final String DEFAULT_AGENT_PREFIX = "jenkins-agent";

Expand Down Expand Up @@ -166,7 +173,15 @@ protected void _terminate(TaskListener listener) throws IOException, Interrupted
}

OfflineCause offlineCause = OfflineCause.create(new Localizable(HOLDER, "offline"));
computer.disconnect(offlineCause);

Future<?> disconnected = computer.disconnect(offlineCause);
// wait a bit for disconnection to avoid stack traces in logs
try {
disconnected.get(DISCONNECTION_TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
String msg = String.format("Ignoring error waiting for agent disconnection %s: %s", name, e.getMessage());
LOGGER.log(Level.INFO, msg, e);
}

if (getCloudName() == null) {
String msg = String.format("Cloud name is not set for agent, can't terminate: %s", name);
Expand Down