Skip to content

Commit

Permalink
- re-enable ability to use a random container name
Browse files Browse the repository at this point in the history
  • Loading branch information
jgangemi committed Dec 9, 2020
1 parent e7fefda commit 51ec870
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/main/asciidoc/inc/image/_naming.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ When specifying the container name pattern the following placeholders can be use
| *%i*
| An index which is incremented if a container has already been created. With this parameter it is easily possible to have multiple, similar containers. See the example below for more details.

| *%r*
| A random container name choosen by the docker engine.

|===

You can combine the placeholders in any combination and will be resolved during `docker:start`, `docker:stop` and `docker:watch`.
Expand Down
33 changes: 22 additions & 11 deletions src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public static String formatContainerName(final ImageConfiguration image,
String containerNamePattern = extractContainerNamePattern(image, defaultContainerNamePattern);
Set<String> existingContainersNames = extractContainerNames(existingContainers);

final String partiallyApplied =
replacePlaceholders(
containerNamePattern,
image.getName(),
image.getAlias(),
buildTimestamp);


if (partiallyApplied.contains(INDEX_PLACEHOLDER)) {
// don't replace if we want a random container name
String partiallyApplied = containerNamePattern == null ? null :
replacePlaceholders(
containerNamePattern,
image.getName(),
image.getAlias(),
buildTimestamp);

if (partiallyApplied != null && partiallyApplied.contains(INDEX_PLACEHOLDER)) {
for (long i = 1; i < Long.MAX_VALUE; i++) {
final String withIndexApplied = partiallyApplied.replaceAll(INDEX_PLACEHOLDER, String.valueOf(i));
if (!existingContainersNames.contains(withIndexApplied)) {
Expand Down Expand Up @@ -143,16 +143,27 @@ private static String extractContainerNamePattern(ImageConfiguration image, Stri
RunImageConfiguration runConfig = image.getRunConfiguration();
if (runConfig != null) {
if (runConfig.getContainerNamePattern() != null) {
return runConfig.getContainerNamePattern();
return getContainerNamingPattern(runConfig.getContainerNamePattern());
}
if (runConfig.getNamingStrategy() == RunImageConfiguration.NamingStrategy.alias) {
return "%a";
}
}
return defaultContainerNamePattern != null ? defaultContainerNamePattern : DEFAULT_CONTAINER_NAME_PATTERN;
}

private static String cleanImageName(final String imageName) {
return new ImageName(imageName).getSimpleName().replaceAll("[^a-zA-Z0-9_.-]+", "_");
}

private static String getContainerNamingPattern(String pattern) {
if (pattern.contains("%r")) {
if (pattern.length() > 2) {
throw new IllegalArgumentException("Invalid use of container naming pattern '%r'");
}
return null;
}

return pattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public void testAlias() {
Collections.emptySet()));
}

@Test
public void testRandom() {
Assert.assertEquals(null,
ContainerNamingUtil.formatContainerName(
imageConfiguration("jolokia/jolokia_demo","nameAlias", "%r"),
null,
new Date(123456),
Collections.emptySet()));
}

@Test
public void testTimestamp() {
Assert.assertEquals("123456",
Expand Down

0 comments on commit 51ec870

Please sign in to comment.