mirror of https://github.com/apache/jclouds.git
JCLOUDS-1158 fix Docker find image Predicate for cases where repoTags field contains 'docker.io/' registry host prefix
This commit is contained in:
parent
71dd766227
commit
a4e2110a63
|
@ -22,6 +22,7 @@ import static com.google.common.collect.Iterables.find;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.inject.Inject;
|
||||
|
@ -67,6 +68,21 @@ import org.jclouds.logging.Logger;
|
|||
public class DockerComputeServiceAdapter implements
|
||||
ComputeServiceAdapter<Container, Hardware, Image, Location> {
|
||||
|
||||
|
||||
/**
|
||||
* Some Docker versions returns host prefix even for images from Docker hub in repoTags field. We use this constant
|
||||
* to correctly identify requested image name.
|
||||
*/
|
||||
public static final String PREFIX_DOCKER_HUB_HOST = "docker.io/";
|
||||
|
||||
/**
|
||||
* (Optional) Suffix used, when image version is not used during searching images.
|
||||
*/
|
||||
public static final String SUFFIX_LATEST_VERSION = ":latest";
|
||||
|
||||
private static final String PATTERN_IMAGE_PREFIX = "^(" + Pattern.quote(PREFIX_DOCKER_HUB_HOST) + ")?";
|
||||
private static final String PATTERN_IMAGE_SUFFIX = "(" + Pattern.quote(SUFFIX_LATEST_VERSION) + ")?$";
|
||||
|
||||
@Resource
|
||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
@ -266,17 +282,7 @@ public class DockerComputeServiceAdapter implements
|
|||
api.getImageApi().createImage(CreateImageOptions.Builder.fromImage(imageIdOrName));
|
||||
|
||||
// as above this ensure repotags are returned
|
||||
return find(listImages(), new Predicate<Image>() {
|
||||
@Override
|
||||
public boolean apply(Image input) {
|
||||
for (String tag : input.repoTags()) {
|
||||
if (tag.equals(imageIdOrName) || tag.equals(imageIdOrName + ":latest")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, null);
|
||||
return find(listImages(), createPredicateMatchingRepoTags(imageIdOrName), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -329,4 +335,19 @@ public class DockerComputeServiceAdapter implements
|
|||
api.getContainerApi().pause(id);
|
||||
}
|
||||
|
||||
protected static Predicate<Image> createPredicateMatchingRepoTags(final String imageIdOrName) {
|
||||
final Pattern imgPattern = Pattern
|
||||
.compile(PATTERN_IMAGE_PREFIX + Pattern.quote(imageIdOrName) + PATTERN_IMAGE_SUFFIX);
|
||||
return new Predicate<Image>() {
|
||||
@Override
|
||||
public boolean apply(Image input) {
|
||||
for (String tag : input.repoTags()) {
|
||||
if (imgPattern.matcher(tag).matches()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,7 +148,9 @@ public class DockerComputeServiceAdapterLiveTest extends BaseDockerApiLiveTest {
|
|||
@Override
|
||||
public boolean apply(Image input) {
|
||||
for (String tag : input.repoTags()) {
|
||||
if (tag.equals(image) || tag.equals(CHUANWEN_COWSAY + ":latest")) {
|
||||
if (tag.equals(CHUANWEN_COWSAY + DockerComputeServiceAdapter.SUFFIX_LATEST_VERSION)
|
||||
|| tag.equals(DockerComputeServiceAdapter.PREFIX_DOCKER_HUB_HOST + CHUANWEN_COWSAY
|
||||
+ DockerComputeServiceAdapter.SUFFIX_LATEST_VERSION)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jclouds.docker.compute.strategy;
|
||||
|
||||
import static org.testng.Assert.assertFalse;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.jclouds.docker.domain.Config;
|
||||
import org.jclouds.docker.domain.Image;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
/**
|
||||
* Unit tests for finding images logic used in
|
||||
* {@link DockerComputeServiceAdapter#getImage(String)} method. It's mainly a
|
||||
* regression test for issue
|
||||
* <a href="https://issues.apache.org/jira/browse/JCLOUDS-1158">JCLOUDS-1158</a>
|
||||
* .
|
||||
*/
|
||||
@Test(groups = "unit", testName = "PredicateLocateImageByNameTest")
|
||||
public class PredicateLocateImageByNameTest {
|
||||
|
||||
private static final Image IMAGE_REPO_TAGS_MULTI = Image.create("id", // id
|
||||
"author", "comment", Config.builder().image("imageId").build(), Config.builder().image("imageId").build(),
|
||||
"parent", // parent
|
||||
new Date(), // created
|
||||
"containerId", // container
|
||||
"1.3.1", // dockerVersion
|
||||
"x86_64", // architecture
|
||||
"os", // os
|
||||
0l, // size
|
||||
0l, // virtualSize
|
||||
ImmutableList.<String> of("kwart/alpine-ext:3.3-ssh", "kwart/alpine-ext:latest", "my-tag:latestdock") // repoTags
|
||||
);
|
||||
|
||||
private static final Image IMAGE_REPO_TAGS_EMPTY = Image.create("id", // id
|
||||
"author", "comment", Config.builder().image("imageId").build(), Config.builder().image("imageId").build(),
|
||||
"parent", // parent
|
||||
new Date(), // created
|
||||
"containerId", // container
|
||||
"1.3.1", // dockerVersion
|
||||
"x86_64", // architecture
|
||||
"os", // os
|
||||
0l, // size
|
||||
0l, // virtualSize
|
||||
ImmutableList.<String> of() // repoTags
|
||||
);
|
||||
|
||||
private static final Image IMAGE_REPO_TAGS_WITH_HOST = Image.create("id", // id
|
||||
"author", "comment", Config.builder().image("imageId").build(), Config.builder().image("imageId").build(),
|
||||
"parent", // parent
|
||||
new Date(), // created
|
||||
"containerId", // container
|
||||
"1.3.1", // dockerVersion
|
||||
"x86_64", // architecture
|
||||
"os", // os
|
||||
0l, // size
|
||||
0l, // virtualSize
|
||||
ImmutableList.<String> of("docker.io/kwart/alpine-ext:3.3-ssh", "docker.io/kwart/alpine-ext:latest") // repoTags
|
||||
);
|
||||
|
||||
public void testRepoTagVersion() {
|
||||
final Predicate<Image> predicate = DockerComputeServiceAdapter
|
||||
.createPredicateMatchingRepoTags("kwart/alpine-ext:3.3-ssh");
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_MULTI));
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_EMPTY));
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_WITH_HOST));
|
||||
}
|
||||
|
||||
public void testRepoTagLatest() {
|
||||
final Predicate<Image> predicate = DockerComputeServiceAdapter.createPredicateMatchingRepoTags("kwart/alpine-ext");
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_MULTI));
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_EMPTY));
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_WITH_HOST));
|
||||
}
|
||||
|
||||
public void testRepoTagVersionWithHost() {
|
||||
final Predicate<Image> predicate = DockerComputeServiceAdapter
|
||||
.createPredicateMatchingRepoTags("docker.io/kwart/alpine-ext:3.3-ssh");
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_MULTI));
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_EMPTY));
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_WITH_HOST));
|
||||
}
|
||||
|
||||
public void testRepoTagLatestWithHost() {
|
||||
final Predicate<Image> predicate = DockerComputeServiceAdapter
|
||||
.createPredicateMatchingRepoTags("docker.io/kwart/alpine-ext");
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_MULTI));
|
||||
assertFalse(predicate.apply(IMAGE_REPO_TAGS_EMPTY));
|
||||
assertTrue(predicate.apply(IMAGE_REPO_TAGS_WITH_HOST));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue