Skip to content
Open
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 @@ -28,6 +28,11 @@ public final class DockerImageName {

private static final String LIBRARY_PREFIX = "library/";

/* Registry names that refer to Docker Hub, which is also used when no registry is given */
private static final String DOCKER_HUB_REGISTRY = "docker.io";

private static final String LEGACY_DOCKER_HUB_REGISTRY = "index.docker.io";

private final String rawName;

@With
Expand Down Expand Up @@ -259,20 +264,18 @@ public DockerImageName asCompatibleSubstituteFor(DockerImageName otherImageName)
* <p>
* If a version tag part is present in the <code>other</code> image name, the tags must exactly match, unless it
* is 'latest'. If a version part is not present in the <code>other</code> image name, the tag contents are ignored.
* <p>
* Docker Hub names are compared in their normalized form: an explicit Docker Hub registry (<code>docker.io</code> or
* <code>index.docker.io</code>) is treated the same as no registry, and single-segment official image names are
* treated the same with or without the <code>library/</code> prefix. For example,
* <code>docker.io/library/postgres:16</code>, <code>docker.io/postgres:16</code> and <code>library/postgres:16</code>
* are all compatible with <code>postgres</code>.
*
* @param other the other image that we are trying to test compatibility with
* @return whether this image has declared compatibility.
*/
public boolean isCompatibleWith(DockerImageName other) {
// Make sure we always compare against a version of the image name containing the LIBRARY_PREFIX
String finalImageName;
if (this.repository.startsWith(LIBRARY_PREFIX)) {
finalImageName = this.repository;
} else {
finalImageName = LIBRARY_PREFIX + this.repository;
}
DockerImageName imageWithLibraryPrefix = DockerImageName.parse(finalImageName);
if (other.equals(this) || imageWithLibraryPrefix.equals(this)) {
if (normalizeDockerHubName(other).equals(normalizeDockerHubName(this))) {
return true;
}

Expand All @@ -283,6 +286,21 @@ public boolean isCompatibleWith(DockerImageName other) {
return this.compatibleSubstituteFor.isCompatibleWith(other);
}

/**
* Returns the name in the form Docker resolves it to on Docker Hub: without a Docker Hub registry and, for
* single-segment official image names, with the <code>library/</code> prefix. Other names are returned unchanged.
*/
private static DockerImageName normalizeDockerHubName(DockerImageName image) {
DockerImageName normalized = image;
if (DOCKER_HUB_REGISTRY.equals(image.registry) || LEGACY_DOCKER_HUB_REGISTRY.equals(image.registry)) {
normalized = normalized.withRegistry("");
}
if (normalized.registry.isEmpty() && normalized.repository.indexOf('/') == -1) {
normalized = normalized.withRepository(LIBRARY_PREFIX + normalized.repository);
}
return normalized;
}

/**
* Behaves as {@link DockerImageName#isCompatibleWith(DockerImageName)} but throws an exception
* rather than returning false if a mismatch is detected.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,88 @@ void testTagAndDigestImageWithRegistryIsCompatible() {
.isTrue();
}

@Test
void testDockerHubRegistryPrefixIsCompatible() {
assertThat(
DockerImageName.parse("docker.io/postgres:16-alpine").isCompatibleWith(DockerImageName.parse("postgres"))
)
.as("docker.io/postgres:16-alpine ~= postgres")
.isTrue();
assertThat(
DockerImageName
.parse("docker.io/library/postgres:16-alpine")
.isCompatibleWith(DockerImageName.parse("postgres"))
)
.as("docker.io/library/postgres:16-alpine ~= postgres")
.isTrue();
assertThat(
DockerImageName.parse("index.docker.io/postgres:16").isCompatibleWith(DockerImageName.parse("postgres"))
)
.as("index.docker.io/postgres:16 ~= postgres")
.isTrue();
assertThat(
DockerImageName
.parse("docker.io/confluentinc/cp-kafka:7.4.0")
.isCompatibleWith(DockerImageName.parse("confluentinc/cp-kafka"))
)
.as("docker.io/confluentinc/cp-kafka:7.4.0 ~= confluentinc/cp-kafka")
.isTrue();
assertThat(DockerImageName.parse("postgres:16").isCompatibleWith(DockerImageName.parse("docker.io/postgres")))
.as("postgres:16 ~= docker.io/postgres")
.isTrue();
}

@Test
void testDockerHubRegistryPrefixStillChecksRepositoryAndTag() {
assertThat(DockerImageName.parse("docker.io/mysql:8").isCompatibleWith(DockerImageName.parse("postgres")))
.as("docker.io/mysql:8 != postgres")
.isFalse();
assertThat(
DockerImageName.parse("docker.io/postgres:16").isCompatibleWith(DockerImageName.parse("postgres:15"))
)
.as("docker.io/postgres:16 != postgres:15")
.isFalse();
assertThat(DockerImageName.parse("quay.io/postgres:16").isCompatibleWith(DockerImageName.parse("postgres")))
.as("quay.io/postgres:16 != postgres")
.isFalse();
assertThat(
DockerImageName.parse("docker.io/library/mysql:8").isCompatibleWith(DockerImageName.parse("postgres"))
)
.as("docker.io/library/mysql:8 != postgres")
.isFalse();
assertThat(
DockerImageName
.parse("docker.io/library/postgres:16")
.isCompatibleWith(DockerImageName.parse("postgres:15"))
)
.as("docker.io/library/postgres:16 != postgres:15")
.isFalse();
}

@Test
void testLibraryPrefixStillChecksRepositoryAndTag() {
assertThat(DockerImageName.parse("library/postgres:16").isCompatibleWith(DockerImageName.parse("postgres")))
.as("library/postgres:16 ~= postgres")
.isTrue();
assertThat(DockerImageName.parse("postgres:16").isCompatibleWith(DockerImageName.parse("library/postgres")))
.as("postgres:16 ~= library/postgres")
.isTrue();
assertThat(DockerImageName.parse("library/mysql:8").isCompatibleWith(DockerImageName.parse("postgres")))
.as("library/mysql:8 != postgres")
.isFalse();
assertThat(DockerImageName.parse("library/postgres:16").isCompatibleWith(DockerImageName.parse("postgres:15")))
.as("library/postgres:16 != postgres:15")
.isFalse();
assertThat(DockerImageName.parse("library/foo/bar").isCompatibleWith(DockerImageName.parse("foo/bar")))
.as("library/foo/bar != foo/bar (library/ only applies to single-segment names)")
.isFalse();
}

@Test
void testAssertMethodAcceptsDockerHubRegistryPrefix() {
DockerImageName.parse("docker.io/postgres:16-alpine").assertCompatibleWith(DockerImageName.parse("postgres"));
}

@Test
void testAssertMethodAcceptsCompatible() {
DockerImageName subject = DockerImageName.parse("foo").asCompatibleSubstituteFor("bar");
Expand Down