Skip to content

Commit

Permalink
Use configured registry for jenkins-agent when running in agent-injec…
Browse files Browse the repository at this point in the history
…tion mode (#1613)
  • Loading branch information
iandrewt authored Oct 17, 2024
1 parent 6d5d0b1 commit 7fa01b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ public Pod build() {
pod.getSpec().getContainers().stream()
.filter(c -> c.getWorkingDir() == null)
.forEach(c -> c.setWorkingDir(workingDir));
String agentImage = DEFAULT_AGENT_IMAGE;
if (cloud != null && StringUtils.isNotEmpty(cloud.getJnlpregistry())) {
agentImage = Util.ensureEndsWith(cloud.getJnlpregistry(), "/") + agentImage;
} else if (StringUtils.isNotEmpty(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX)) {
agentImage = Util.ensureEndsWith(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX, "/") + agentImage;
}
if (StringUtils.isBlank(agentContainer.getImage())) {
String agentImage = DEFAULT_AGENT_IMAGE;
if (cloud != null && StringUtils.isNotEmpty(cloud.getJnlpregistry())) {
agentImage = Util.ensureEndsWith(cloud.getJnlpregistry(), "/") + agentImage;
} else if (StringUtils.isNotEmpty(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX)) {
agentImage = Util.ensureEndsWith(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX, "/") + agentImage;
}
agentContainer.setImage(agentImage);
}
Map<String, EnvVar> envVars = new HashMap<>();
Expand All @@ -351,7 +351,7 @@ public Pod build() {
var oldInitContainers = pod.getSpec().getInitContainers();
var jenkinsAgentInitContainer = new ContainerBuilder()
.withName("set-up-jenkins-agent")
.withImage(DEFAULT_AGENT_IMAGE)
.withImage(agentImage)
.withCommand(
"/bin/sh",
"-c",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,34 @@ public void testValidateDockerRegistryPrefixOverrideWithSlashSuffix(boolean dire
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));
}

@Test
@TestCaseName("{method}(directConnection={0})")
@Parameters({"true", "false"})
public void testValidateDockerRegistryPrefixOverrideForInitContainer(boolean directConnection) throws Exception {
cloud.setDirectConnection(directConnection);
DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX = "jenkins.docker.com/docker-hub";
PodTemplate template = new PodTemplate();
template.setYaml(loadYamlFile("pod-busybox.yaml"));
template.setAgentContainer("busybox");
template.setAgentInjection(true);
setupStubs();
Pod pod = new PodTemplateBuilder(template, slave).build();
// check containers
Map<String, Container> containers = toContainerMap(pod);
assertEquals(1, containers.size());
Map<String, Container> initContainers = toInitContainerMap(pod);
assertEquals(1, initContainers.size());

assertEquals("busybox", containers.get("busybox").getImage());
assertEquals(
List.of("/jenkins-agent/jenkins-agent"),
containers.get("busybox").getCommand());
assertEquals(
DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX + "/" + DEFAULT_AGENT_IMAGE,
initContainers.get("set-up-jenkins-agent").getImage());
assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));
}

@Test
@Issue("JENKINS-50525")
public void testBuildWithCustomWorkspaceVolume() throws Exception {
Expand Down Expand Up @@ -989,6 +1017,11 @@ private Map<String, Container> toContainerMap(Pod pod) {
.collect(Collectors.toMap(Container::getName, Function.identity()));
}

private Map<String, Container> toInitContainerMap(Pod pod) {
return pod.getSpec().getInitContainers().stream()
.collect(Collectors.toMap(Container::getName, Function.identity()));
}

private String loadYamlFile(String s) throws IOException {
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(s)));
}
Expand Down

0 comments on commit 7fa01b3

Please sign in to comment.