Skip to content

Commit

Permalink
- restore ability to use a random container name (#1410)
Browse files Browse the repository at this point in the history
* - re-enable ability to use a random container name

* changed to %e for an "empty container name" placeholder.

Co-authored-by: Rohan Kumar <rohaan@redhat.com>
Co-authored-by: Roland Huß <roland@ro14nd.de>
  • Loading branch information
3 people authored Mar 8, 2021
1 parent b2f287d commit 3d9cdbf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/main/asciidoc/inc/image/_naming.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ When specifying the container name pattern the following placeholders can be use
| *%a*
| The `<alias>` of an image which must be set. The alias is set in the top-level image configuration

| *%e*
| Choose an empty container name, which will let the docker engine chose a random container name automatically. This placeholder must be given as single value to `containerNamePattern` when used.

| *%n*
| A sanitized version of the imag's short name from which this container is created. "Sanitized" means that any non letter, digit, dot or dash is replaced by an underscore.
| A sanitized version of the image's short name from which this container is created. "Sanitized" means that any non letter, digit, dot or dash is replaced by an underscore.

| *%t*
| The build time stamp. This is the timestamp which created during the building of an image and locally cached. A rebuild of the image will update the timestamp.

| *%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.

|===

You can combine the placeholders in any combination and will be resolved during `docker:start`, `docker:stop` and `docker:watch`.
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
public class ContainerNamingUtil {

private static final String INDEX_PLACEHOLDER = "%i";
static final String INDEX_PLACEHOLDER = "%i";
static final String EMPTY_NAME_PLACEHOLDER = "%e";

public static final String DEFAULT_CONTAINER_NAME_PATTERN = "%n-%i";

Expand All @@ -35,13 +36,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 (shouldUseEmptyName(containerNamePattern)) {
return null;
}

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

if (partiallyApplied.contains(INDEX_PLACEHOLDER)) {
for (long i = 1; i < Long.MAX_VALUE; i++) {
Expand Down Expand Up @@ -70,20 +73,18 @@ public static Collection<Container> getContainersToStop(final ImageConfiguration

String containerNamePattern = extractContainerNamePattern(image, defaultContainerNamePattern);

// Only special treatment for indexed container names
if (!containerNamePattern.contains(INDEX_PLACEHOLDER)) {
if (shouldUseEmptyName(containerNamePattern) || !containerNamePattern.contains(INDEX_PLACEHOLDER)) {
return containers;
}

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

return keepOnlyLastIndexedContainer(containers, partiallyApplied);

}

// ========================================================================================================
Expand Down Expand Up @@ -155,4 +156,12 @@ private static String extractContainerNamePattern(ImageConfiguration image, Stri
private static String cleanImageName(final String imageName) {
return new ImageName(imageName).getSimpleName().replaceAll("[^a-zA-Z0-9_.-]+", "_");
}

private static boolean shouldUseEmptyName(String pattern) {
if (pattern.contains(EMPTY_NAME_PLACEHOLDER) && !pattern.equals(EMPTY_NAME_PLACEHOLDER)) {
throw new IllegalArgumentException("Invalid use of container naming pattern " + EMPTY_NAME_PLACEHOLDER);
}
return pattern.equals(EMPTY_NAME_PLACEHOLDER);
}

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

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

@Test
public void testMixedRandom() {
Assert.assertThrows(IllegalArgumentException.class, () -> {
Assert.assertEquals(null,
ContainerNamingUtil.formatContainerName(
imageConfiguration("jolokia/jolokia_demo","nameAlias", ContainerNamingUtil.EMPTY_NAME_PLACEHOLDER + "-extra"),
null,
new Date(123456),
Collections.emptySet()));
});
}

@Test
public void testTimestamp() {
Assert.assertEquals("123456",
Expand Down Expand Up @@ -74,7 +96,7 @@ public void testIndex() {
public void testAll() {
Assert.assertEquals("prefix-1-nameAlias-jolokia_demo-123456-postfix",
ContainerNamingUtil.formatContainerName(
imageConfiguration("jolokia/jolokia_demo","nameAlias", "prefix-%i-%a-%n-%t-postfix"),
imageConfiguration("jolokia/jolokia_demo","nameAlias", "prefix-" + ContainerNamingUtil.INDEX_PLACEHOLDER + "-%a-%n-%t-postfix"),
null,
new Date(123456),
Collections.emptySet()));
Expand Down

0 comments on commit 3d9cdbf

Please sign in to comment.