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

[JENKINS-50664] container cap limit hit due to default labels missing #322

Closed
wants to merge 1 commit into from
Closed
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 @@ -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 @@ -187,6 +187,9 @@ public static Pod combine(Pod parent, Pod template) {
Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(),
template.getMetadata().getAnnotations());

Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(),
template.getMetadata().getLabels());

Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
imagePullSecrets.addAll(template.getSpec().getImagePullSecrets());
Expand Down Expand Up @@ -217,7 +220,9 @@ public static Pod combine(Pod parent, Pod template) {
// toolLocationNodeProperties.addAll(template.getNodeProperties());

MetadataNested<PodBuilder> metadataBuilder = new PodBuilder().withNewMetadataLike(parent.getMetadata()) //
.withAnnotations(podAnnotations);
.withAnnotations(podAnnotations)
.withLabels(podLabels);

if (!Strings.isNullOrEmpty(template.getMetadata().getName())) {
metadataBuilder.withName(template.getMetadata().getName());
}
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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.mockito.junit.MockitoRule;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import io.fabric8.kubernetes.api.model.Container;
Expand Down Expand Up @@ -100,7 +99,9 @@ private void setupStubs() {
}

private void validatePod(Pod pod) {
assertEquals(ImmutableMap.of("some-label", "some-label-value"), pod.getMetadata().getLabels());
assertThat(pod.getMetadata().getLabels(), hasEntry("some-label", "some-label-value"));
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));


// check containers
Map<String, Container> containers = pod.getSpec().getContainers().stream()
Expand Down