Skip to content

Commit

Permalink
Merge pull request #435 from jenkinsci/JENKINS-55392-2
Browse files Browse the repository at this point in the history
[JENKINS-55392] Do not close Kubernetes client after containerLog step
  • Loading branch information
carlossg authored Mar 4, 2019
2 parents c6c3894 + e9b52de commit ccd5079
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ final class KubernetesClientProvider {

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

private static final Integer CACHE_SIZE = Integer.getInteger("org.csanchez.jenkins.plugins.kubernetes.clients.cacheSize", 10);
/**
* How many clouds can we connect to, default to 10
*/
private static final Integer CACHE_SIZE = Integer
.getInteger(KubernetesClientProvider.class.getPackage().getName() + ".clients.cacheSize", 10);

/**
* Client expiration in seconds, default to one day
*/
private static final Integer CACHE_EXPIRATION = Integer
.getInteger("org.csanchez.jenkins.plugins.kubernetes.clients.cacheExpiration", 24 * 60 * 60);
private static final Integer CACHE_EXPIRATION = Integer.getInteger(
KubernetesClientProvider.class.getPackage().getName() + ".clients.cacheExpiration", 24 * 60 * 60);

private static final List<KubernetesClient> expiredClients = Collections.synchronizedList(new ArrayList());

private static final Cache<String, Client> clients = CacheBuilder
.newBuilder()
.maximumSize(CACHE_SIZE)
.expireAfterWrite(CACHE_EXPIRATION, TimeUnit.SECONDS)
.newBuilder() //
.maximumSize(CACHE_SIZE) //
.expireAfterWrite(CACHE_EXPIRATION, TimeUnit.SECONDS) //
.removalListener(rl -> {
LOGGER.log(Level.FINE, "{0} cache : Removing entry for {1}", new Object[] {KubernetesClient.class.getSimpleName(), rl.getKey()});
KubernetesClient client = ((Client) rl.getValue()).getClient();
Expand All @@ -65,7 +70,7 @@ final class KubernetesClientProvider {
}
}

})
}) //
.build();

private KubernetesClientProvider() {
Expand All @@ -80,6 +85,7 @@ static KubernetesClient createClient(KubernetesCloud cloud) throws NoSuchAlgorit
cloud.getServerCertificate(), cloud.getCredentialsId(), cloud.isSkipTlsVerify(),
cloud.getConnectTimeout(), cloud.getReadTimeout(), cloud.getMaxRequestsPerHost()).createClient();
clients.put(displayName, new Client(getValidity(cloud), client));
LOGGER.log(Level.INFO, "Created new Kubernetes client: {0} {1}", new Object[] { displayName, client });
return client;
}
return c.getClient();
Expand Down Expand Up @@ -123,7 +129,7 @@ public long getRecurrencePeriod() {

@Override
protected Level getNormalLoggingLevel() {
return Level.FINE;
return Level.FINEST;
}

@Override
Expand Down Expand Up @@ -170,6 +176,9 @@ public void onChange(Saveable o, XmlFile file) {
Client client = clients.getIfPresent(displayName);
if (client != null && client.getValidity() == getValidity(cloud)) {
cloudDisplayNames.remove(displayName);
} else {
LOGGER.log(Level.INFO, "Invalidating Kubernetes client: {0} {1}",
new Object[] { displayName, client });
}
}
// Remove missing / invalid clients
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,12 @@ protected String run() throws Exception {
logger().println(message);
LOGGER.log(Level.WARNING, message, e);
return "";
} finally {
closeQuietly(getContext(), client);
}
}

@Override
public void stop(Throwable cause) throws Exception {
LOGGER.log(Level.FINE, "Stopping container log step.");
try {
super.stop(cause);
} finally {
closeQuietly(getContext(), client);
}
super.stop(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public boolean start() throws Exception {
@Override
public void stop(@Nonnull Throwable cause) throws Exception {
LOGGER.log(Level.FINE, "Stopping container step.");
closeQuietly(getContext(), client, decorator);
closeQuietly(getContext(), decorator);
}

private static class ContainerExecCallback extends BodyExecutionCallback.TailCall {
Expand Down

0 comments on commit ccd5079

Please sign in to comment.