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

Add option to apply caps only on alive pods #252

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
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
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -91,6 +92,8 @@ public class KubernetesCloud extends Cloud {

private boolean skipTlsVerify;

private boolean capOnlyOnAlivePods;

private String namespace;
private String jenkinsUrl;
@CheckForNull
Expand Down Expand Up @@ -220,6 +223,15 @@ public String getJenkinsUrl() {
return jenkinsUrl;
}

@DataBoundSetter
public void setCapOnlyOnAlivePods(boolean capOnlyOnAlivePods) {
this.capOnlyOnAlivePods = capOnlyOnAlivePods;
}

public boolean isCapOnlyOnAlivePods() {
return capOnlyOnAlivePods;
}

/**
* Returns Jenkins URL to be used by agents launched by this cloud. Always ends with a trailing slash.
*
Expand Down Expand Up @@ -424,6 +436,22 @@ private boolean addProvisionedSlave(@Nonnull PodTemplate template, @CheckForNull
PodList namedList = client.pods().inNamespace(templateNamespace).withLabels(labelsMap).list();
List<Pod> namedListItems = namedList.getItems();

if (this.isCapOnlyOnAlivePods()) {
slaveListItems = slaveListItems.stream()
.filter(x -> x.getStatus()
.getPhase().toLowerCase()
.matches("(running|pending)"))
.collect(Collectors.toList());
}

if (template.isCapOnlyOnAlivePods()) {
namedListItems = namedListItems.stream()
.filter(x -> x.getStatus()
.getPhase().toLowerCase()
.matches("(running|pending)"))
.collect(Collectors.toList());
}

if (slaveListItems != null && containerCap <= slaveListItems.size()) {
LOGGER.log(Level.INFO,
"Total container cap of {0} reached, not provisioning: {1} running or errored in namespace {2}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class PodTemplate extends AbstractDescribableImpl<PodTemplate> implements

private boolean privileged;

private boolean capOnlyOnAlivePods;

private boolean alwaysPullImage;

private String command;
Expand Down Expand Up @@ -394,6 +396,15 @@ public boolean isAlwaysPullImage() {
return getFirstContainer().map(ContainerTemplate::isAlwaysPullImage).orElse(false);
}

@DataBoundSetter
public void setCapOnlyOnAlivePods(boolean capOnlyOnAlivePods) {
this.capOnlyOnAlivePods = capOnlyOnAlivePods;
}

public boolean isCapOnlyOnAlivePods() {
return capOnlyOnAlivePods;
}

public List<TemplateEnvVar> getEnvVars() {
if (envVars == null) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
<f:textbox default="10"/>
</f:entry>

<f:entry field="capOnlyOnAlivePods" title="${%Apply cap only on alive pods}">
<f:checkbox/>
</f:entry>

<f:entry title="${%Max connections to Kubernetes API}" field="maxRequestsPerHostStr">
<f:textbox default="32"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<f:textbox/>
</f:entry>

<f:entry field="capOnlyOnAlivePods" title="${%Apply cap only on alive pods}">
<f:checkbox/>
</f:entry>

<f:entry field="idleMinutesStr" title="${%Time in minutes to retain slave when idle}">
<f:textbox/>
</f:entry>
Expand Down