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-60537] Sanitize label #677

Merged
merged 3 commits into from
Jan 9, 2020
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 @@ -379,7 +379,17 @@ public Set<LabelAtom> getLabelSet() {
}

public Map<String, String> getLabelsMap() {
return ImmutableMap.of("jenkins/label", label == null ? DEFAULT_ID : label);
return ImmutableMap.of("jenkins/label", label == null ? DEFAULT_ID : sanitizeLabel(label));
}

static String sanitizeLabel(String input) {
String label = input;
int max = 63; // Kubernetes limit
if (label.length() > max) {
label = label.substring(label.length() - max);
}
label = label.replaceAll("[^_.a-zA-Z0-9-]", "_").replaceFirst("^[^a-zA-Z0-9]", "x");
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
return label;
}

@DataBoundSetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public void singleLabel() {
public void multiLabel() {
PodTemplate podTemplate = new PodTemplate();
podTemplate.setLabel("foo bar");
assertEquals("foo bar", podTemplate.getLabelsMap().get("jenkins/label"));
assertEquals("foo_bar", podTemplate.getLabelsMap().get("jenkins/label"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hudson.util.XStream2;
import org.junit.Test;

import static org.csanchez.jenkins.plugins.kubernetes.PodTemplate.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -31,4 +32,12 @@ public void copyConstructor() throws Exception {
assertEquals(xs.toXML(pt), xs.toXML(new PodTemplate(pt)));
}

@Test
public void sanitizeLabels() {
assertEquals("label1", sanitizeLabel("label1"));
assertEquals("label1_label2", sanitizeLabel("label1 label2"));
assertEquals("el1_label2_verylooooooooooooooooooooooooooooonglabelover63chars", sanitizeLabel("label1 label2 verylooooooooooooooooooooooooooooonglabelover63chars"));
assertEquals("xfoo_bar", sanitizeLabel(":foo:bar"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.junit.Assume.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -41,11 +42,13 @@
import com.gargoylesoftware.htmlunit.html.DomNodeUtil;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.Label;
import hudson.slaves.SlaveComputer;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClientException;
import jenkins.model.Jenkins;
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave;
import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
Expand Down Expand Up @@ -382,6 +385,31 @@ public void runInPodWithLivenessProbe() throws Exception {
r.assertLogContains("Still alive", b);
}

@Test
public void podTemplateWithMultipleLabels() throws Exception {
PodTemplate pt = new PodTemplate();
pt.setName("podTemplate");
pt.setLabel("label1 label2");
ContainerTemplate jnlp = new ContainerTemplate("jnlp", "jenkins/jnlp-slave:3.35-5-alpine");
pt.setContainers(Collections.singletonList(jnlp));
cloud.addTemplate(pt);
SemaphoreStep.waitForStart("pod/1", b);
Map<String, String> labels = getLabels(cloud, this, name);
Vlatombe marked this conversation as resolved.
Show resolved Hide resolved
labels.put("jenkins/label","label1_label2");
KubernetesSlave node = r.jenkins.getNodes().stream()
.filter(KubernetesSlave.class::isInstance)
.map(KubernetesSlave.class::cast)
.findAny().get();
assertTrue(node.getAssignedLabels().containsAll(Label.parse("label1 label2")));
PodList pods = cloud.connect().pods().withLabels(labels).list();
assertThat(
"Expected one pod with labels " + labels + " but got: "
+ pods.getItems().stream().map(pod -> pod.getMetadata()).collect(Collectors.toList()),
pods.getItems(), hasSize(1));
SemaphoreStep.success("pod/1", null);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
}

@Test
public void runWithActiveDeadlineSeconds() throws Exception {
SemaphoreStep.waitForStart("podTemplate/1", b);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node('label1') {
semaphore 'pod'
sh 'echo "It works"'
}