Skip to content

Commit

Permalink
Fix eclipse-jkube#240: No Ports exposed inside Deployment in case of …
Browse files Browse the repository at this point in the history
…Zero Config Dockerfile Mode

Add ports to ImageConfiguration generated in DockerFileUtil.createSimpleDockerfileConfig
by parsing contents of Dockerfile
  • Loading branch information
rohanKanojia committed Nov 4, 2020
1 parent 881c315 commit f654d81
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Usage:
### 1.1.0-SNAPSHOT
* Fix #467: Upgrade assertj-core to 3.18.0
* Added a Quickstart for implementing and using a Custom Enricher based on Eclipse JKube Kit Enricher API
* Fix #240: No Ports exposed inside Deployment in case of Zero Config Dockerfile Mode

### 1.0.2 (2020-10-30)
* Fix #429: Added quickstart for Micronaut framework
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -27,15 +28,18 @@
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Utility class for dealing with dockerfiles
Expand Down Expand Up @@ -250,6 +254,7 @@ public static ImageConfiguration createSimpleDockerfileConfig(File dockerFile, S

final BuildConfiguration buildConfig = BuildConfiguration.builder()
.dockerFile(dockerFile.getPath())
.ports(extractPorts(dockerFile))
.build();

return ImageConfiguration.builder()
Expand All @@ -261,6 +266,7 @@ public static ImageConfiguration createSimpleDockerfileConfig(File dockerFile, S
public static ImageConfiguration addSimpleDockerfileConfig(ImageConfiguration image, File dockerfile) {
final BuildConfiguration buildConfig = BuildConfiguration.builder()
.dockerFile(dockerfile.getPath())
.ports(extractPorts(dockerfile))
.build();
return image.toBuilder().build(buildConfig).build();
}
Expand Down Expand Up @@ -312,4 +318,23 @@ private static String trimSurroundingQuotes(String argStringValue) {
}
return argStringValue;
}

static List<String> extractPorts(File dockerFile) {
Properties properties = new Properties();
try {
return extractPorts(extractLines(dockerFile, "EXPOSE", properties, null));
} catch (IOException ioException) {
throw new IllegalArgumentException("Error in reading Dockerfile", ioException);
}
}

static List<String> extractPorts(List<String[]> dockerLinesContainingExpose) {
Set<String> ports = new HashSet<>();
dockerLinesContainingExpose.forEach(line -> Arrays.stream(line)
.skip(1)
.filter(Objects::nonNull)
.filter(StringUtils::isNotBlank)
.forEach(ports::add));
return new ArrayList<>(ports);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
Expand Down Expand Up @@ -217,6 +218,48 @@ public void testResolveArgValueFromStrContainingArgKey() {
assertEquals("test", DockerFileUtil.resolveArgValueFromStrContainingArgKey("${project.scope}", Collections.singletonMap("project.scope", "test")));
}

@Test
public void testCreateSimpleDockerfileConfigWithPorts() {
// Given
File dockerFile = new File(getClass().getResource("/docker/Dockerfile_expose_ports").getFile());
// When
ImageConfiguration imageConfiguration1 = DockerFileUtil.createSimpleDockerfileConfig(dockerFile, null);
// Then
assertNotNull(imageConfiguration1);
assertEquals("%g/%a:%l", imageConfiguration1.getName());
assertEquals(dockerFile.getPath(), imageConfiguration1.getBuild().getDockerFileRaw());
assertNotNull(imageConfiguration1.getBuild().getPorts());
assertEquals(5, imageConfiguration1.getBuild().getPorts().size());
assertEquals("80/tcp", imageConfiguration1.getBuild().getPorts().get(0));
assertEquals("8080/udp", imageConfiguration1.getBuild().getPorts().get(1));
assertEquals("80", imageConfiguration1.getBuild().getPorts().get(2));
assertEquals("8080", imageConfiguration1.getBuild().getPorts().get(3));
assertEquals("99/udp", imageConfiguration1.getBuild().getPorts().get(4));
}

@Test(expected = IllegalArgumentException.class)
public void testGetPortsFromDockerFileShouldThrowExceptionOnInvalidFile() {
DockerFileUtil.extractPorts(new File("iDoNotExist"));
}

@Test
public void testGetPortsFromDockerFileLines() {
// Given
List<String[]> input1 = Arrays.asList(new String[]{"EXPOSE", "8080", "9090", "9999"} , new String[]{"EXPOSE", "9010"});
List<String[]> input2 = Arrays.asList(new String[]{"EXPOSE", "9001"}, new String[]{"EXPOSE", null});
List<String[]> input3 = Arrays.asList(new String[]{"EXPOSE", ""}, new String[]{"EXPOSE", "8001"});

// When
List<String> result1 = DockerFileUtil.extractPorts(input1);
List<String> result2 = DockerFileUtil.extractPorts(input2);
List<String> result3 = DockerFileUtil.extractPorts(input3);

// Then
assertEquals(Arrays.asList("9090", "8080", "9999", "9010"), result1);
assertEquals(Collections.singletonList("9001"), result2);
assertEquals(Collections.singletonList("8001"), result3);
}

private File getDockerfilePath(String dir) {
ClassLoader classLoader = getClass().getClassLoader();
return new File(Objects.requireNonNull(classLoader.getResource(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ARG VERSION=latest
FROM openjdk:$VERSION
EXPOSE 80
COPY maven/target/docker-file-simple.jar /deployments/docker-file-simple.jar
# Copying a file inside project root directory
COPY maven/static-dir-in-project-root/my-file.txt /deployments/my-file.txt
EXPOSE 8080
EXPOSE 80/tcp
EXPOSE 8080/udp
EXPOSE 99/udp
CMD ["java", "-jar", "/deployments/docker-file-simple.jar"]
EXPOSE 8080
1 change: 1 addition & 0 deletions quickstarts/maven/docker-file-simple/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ FROM openjdk:$VERSION
COPY maven/target/docker-file-simple.jar /deployments/docker-file-simple.jar
# Copying a file inside project root directory
COPY maven/static-dir-in-project-root/my-file.txt /deployments/my-file.txt
EXPOSE 8080
CMD ["java", "-jar", "/deployments/docker-file-simple.jar"]
1 change: 0 additions & 1 deletion quickstarts/maven/docker-file-simple/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<properties>
<java.version>11</java.version>
<jkube.version>${project.version}</jkube.version>
<jkube.enricher.jkube-service.port>8080</jkube.enricher.jkube-service.port>
<jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
</properties>

Expand Down

0 comments on commit f654d81

Please sign in to comment.