Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for docker.io #1560

Merged
merged 6 commits into from
Mar 19, 2019
Merged
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 @@ -91,7 +91,7 @@ public Authorization call()
RegistryAuthenticator registryAuthenticator =
RegistryAuthenticator.initializer(
buildConfiguration.getEventDispatcher(),
buildConfiguration.getTargetImageConfiguration().getImageRegistry(),
registry,
buildConfiguration.getTargetImageConfiguration().getImageRepository())
.setAllowInsecureRegistries(buildConfiguration.getAllowInsecureRegistries())
.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.tools.jib.image;

import com.google.cloud.tools.jib.registry.RegistryAliasGroup;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.regex.Matcher;
Expand All @@ -32,7 +33,7 @@
*/
public class ImageReference {

private static final String DOCKER_HUB_REGISTRY = "registry.hub.docker.com";
private static final String DOCKER_HUB_REGISTRY = "registry-1.docker.io";
private static final String DEFAULT_TAG = "latest";
private static final String LIBRARY_REPOSITORY_PREFIX = "library/";

Expand Down Expand Up @@ -120,6 +121,7 @@ public static ImageReference parse(String reference) throws InvalidImageReferenc
repository = registry + "/" + repository;
registry = DOCKER_HUB_REGISTRY;
}

/*
* For Docker Hub, if the repository is only one component, then it should be prefixed with
* 'library/'.
Expand Down Expand Up @@ -230,15 +232,15 @@ public static boolean isDefaultTag(String tag) {

/** Construct with {@link #parse}. */
private ImageReference(String registry, String repository, String tag) {
this.registry = registry;
this.registry = RegistryAliasGroup.getHost(registry);
this.repository = repository;
this.tag = tag;
}

/**
* Gets the registry portion of the {@link ImageReference}.
*
* @return the registry
* @return the registry host
*/
public String getRegistry() {
return registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@
package com.google.cloud.tools.jib.registry;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Provides known aliases for a given registry. */
/** Provides known aliases and alternative hosts for a given registry. */
public class RegistryAliasGroup {

private static final ImmutableList<ImmutableSet<String>> REGISTRY_ALIAS_GROUPS =
ImmutableList.of(
// Docker Hub alias group
ImmutableSet.of("registry.hub.docker.com", "index.docker.io"));
// Docker Hub alias group (https://github.com/moby/moby/pull/28100)
ImmutableSet.of(
"registry.hub.docker.com", "index.docker.io", "registry-1.docker.io", "docker.io"));

/** Some registry names are symbolic. */
private static final ImmutableMap<String, String> REGISTRY_HOST_MAP =
ImmutableMap.of(
// https://github.com/docker/hub-feedback/issues/1767
"docker.io", "registry-1.docker.io");

/**
* Returns the list of registry aliases for the given {@code registry}, including {@code registry}
Expand All @@ -50,4 +58,14 @@ public static List<String> getAliasesGroup(String registry) {

return Collections.singletonList(registry);
}

/**
* Returns the server host name to use for the given registry.
*
* @param registry the name of the registry
* @return the registry host
*/
public static String getHost(String registry) {
return REGISTRY_HOST_MAP.getOrDefault(registry, registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void testParse_dockerHub_official() throws InvalidImageReferenceException
String imageReferenceString = "busybox";
ImageReference imageReference = ImageReference.parse(imageReferenceString);

Assert.assertEquals("registry.hub.docker.com", imageReference.getRegistry());
Assert.assertEquals("registry-1.docker.io", imageReference.getRegistry());
Assert.assertEquals("library/busybox", imageReference.getRepository());
Assert.assertEquals("latest", imageReference.getTag());
}
Expand All @@ -76,7 +76,7 @@ public void testParse_dockerHub_user() throws InvalidImageReferenceException {
String imageReferenceString = "someuser/someimage";
ImageReference imageReference = ImageReference.parse(imageReferenceString);

Assert.assertEquals("registry.hub.docker.com", imageReference.getRegistry());
Assert.assertEquals("registry-1.docker.io", imageReference.getRegistry());
Assert.assertEquals("someuser/someimage", imageReference.getRepository());
Assert.assertEquals("latest", imageReference.getTag());
}
Expand Down Expand Up @@ -109,10 +109,10 @@ public void testOf_smoke() {
Assert.assertEquals(
expectedTag, ImageReference.of(expectedRegistry, expectedRepository, expectedTag).getTag());
Assert.assertEquals(
"registry.hub.docker.com",
"registry-1.docker.io",
ImageReference.of(null, expectedRepository, expectedTag).getRegistry());
Assert.assertEquals(
"registry.hub.docker.com", ImageReference.of(null, expectedRepository, null).getRegistry());
"registry-1.docker.io", ImageReference.of(null, expectedRepository, null).getRegistry());
Assert.assertEquals(
"latest", ImageReference.of(expectedRegistry, expectedRepository, null).getTag());
Assert.assertEquals("latest", ImageReference.of(null, expectedRepository, null).getTag());
Expand Down Expand Up @@ -187,15 +187,29 @@ public void testIsScratch() {
Assert.assertFalse(ImageReference.of(null, "scratch", null).isScratch());
}

@Test
public void testGetRegistry() {
Assert.assertEquals(
"registry-1.docker.io", ImageReference.of(null, "someimage", null).getRegistry());
Assert.assertEquals(
"registry-1.docker.io", ImageReference.of("docker.io", "someimage", null).getRegistry());
Assert.assertEquals(
"index.docker.io", ImageReference.of("index.docker.io", "someimage", null).getRegistry());
Assert.assertEquals(
"registry.hub.docker.com",
ImageReference.of("registry.hub.docker.com", "someimage", null).getRegistry());
Assert.assertEquals("gcr.io", ImageReference.of("gcr.io", "someimage", null).getRegistry());
}

private void verifyParse(String registry, String repository, String tagSeparator, String tag)
throws InvalidImageReferenceException {
// Gets the expected parsed components.
String expectedRegistry = registry;
if (Strings.isNullOrEmpty(expectedRegistry)) {
expectedRegistry = "registry.hub.docker.com";
expectedRegistry = "registry-1.docker.io";
}
String expectedRepository = repository;
if ("registry.hub.docker.com".equals(expectedRegistry) && repository.indexOf('/') < 0) {
if ("registry-1.docker.io".equals(expectedRegistry) && repository.indexOf('/') < 0) {
expectedRepository = "library/" + expectedRepository;
}
String expectedTag = tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package com.google.cloud.tools.jib.registry;

import java.util.Arrays;
import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -32,16 +34,24 @@ public void testGetAliasesGroup_noKnownAliases() {
}

@Test
public void testGetAliasesGroup_registryHubDockerCom() {
Assert.assertEquals(
Arrays.asList("registry.hub.docker.com", "index.docker.io"),
RegistryAliasGroup.getAliasesGroup("registry.hub.docker.com"));
public void testGetAliasesGroup_dockerHub() {
Set<String> aliases =
Sets.newHashSet(
"registry.hub.docker.com", "index.docker.io", "registry-1.docker.io", "docker.io");
for (String alias : aliases) {
Assert.assertEquals(aliases, new HashSet<>(RegistryAliasGroup.getAliasesGroup(alias)));
}
}

@Test
public void testGetAliasesGroup_indexDockerIo() {
Assert.assertEquals(
Arrays.asList("index.docker.io", "registry.hub.docker.com"),
RegistryAliasGroup.getAliasesGroup("index.docker.io"));
public void testGetHost_noAlias() {
String host = RegistryAliasGroup.getHost("something.gcr.io");
Assert.assertEquals("something.gcr.io", host);
}

@Test
public void testGetHost_dockerIo() {
String host = RegistryAliasGroup.getHost("docker.io");
Assert.assertEquals("registry-1.docker.io", host);
}
}
2 changes: 1 addition & 1 deletion jib-maven-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ For example, to build the image `my-docker-id/my-app`, the configuration would b
```xml
<configuration>
<to>
<image>registry.hub.docker.com/my-docker-id/my-app</image>
<image>docker.io/my-docker-id/my-app</image>
</to>
</configuration>
```
Expand Down