Skip to content

Commit

Permalink
Fix for JENKINS-51286: container cap limit
Browse files Browse the repository at this point in the history
Issue : Setting container cap limit doesn't work correctly without Jenkins restart. Once user set container cap limit configuration for given namespace, kubernetes plugin consider all pods/containers from the namespace which are not started by the plugin (ideally it should query slave pods/container which are started by the plugin).
This patch fixes this issue by setting default slave labels to query pods/containers while checking the container cap limit.
  • Loading branch information
hrishin committed May 14, 2018
1 parent 908cd0f commit 624e6ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public class KubernetesCloud extends Cloud {
private static final Logger LOGGER = Logger.getLogger(KubernetesCloud.class.getName());

public static final String JNLP_NAME = "jnlp";

/** label for all pods started by the plugin */
@Deprecated
public static final Map<String, String> DEFAULT_POD_LABELS = ImmutableMap.of("jenkins", "slave");

/** Default timeout for idle workers that don't correctly indicate exit. */
Expand Down Expand Up @@ -325,7 +325,7 @@ public int getConnectTimeout() {
* Labels for all pods started by the plugin
*/
public Map<String, String> getLabels() {
return labels == null ? Collections.emptyMap() : labels;
return (labels == null || labels.isEmpty()) ? DEFAULT_POD_LABELS : labels;
}

public void setLabels(Map<String, String> labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ public Map<String, String> getLabelsMap() {
builder.put(label == null ? DEFAULT_ID : "jenkins/" + label.getName(), "true");
}
}
builder.putAll(KubernetesCloud.DEFAULT_POD_LABELS);
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import jenkins.model.JenkinsLocationConfiguration;
import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume;
Expand All @@ -12,6 +13,7 @@
import org.jvnet.hudson.test.JenkinsRule;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

public class KubernetesCloudTest {
Expand Down Expand Up @@ -76,5 +78,17 @@ public void getJenkinsUrlOrDie_UrlInLocation() {
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrDie());
}

@Test
public void has_default_labels_for_slave_pods() {
// GIVEN
KubernetesCloud cloud = new KubernetesCloud("name");

// WHEN
Map<String, String> defaultLabels = cloud.getLabels();

// THEN
assertNotNull(defaultLabels);
assertNotNull(defaultLabels.get("jenkins"));
}

}

0 comments on commit 624e6ae

Please sign in to comment.