Skip to content

Commit

Permalink
[VBV-1949] Registries are incorrectly filtered if they start with a t…
Browse files Browse the repository at this point in the history
…he same path

- changed the logic of filtering not to rely on startsWith method. The idea now
is the split the image path into segments and to check for existing registry.

Change-Id: I4d930baddb708144b15cb4d66c81feae8095b757
Reviewed-on: https://bellevue-ci.eng.vmware.com:8080/31046
Closures-Verified: jenkins <[email protected]>
Upgrade-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
CS-Verified: jenkins <[email protected]>
Reviewed-by: Stanislav Hadjiiski <[email protected]>
Reviewed-by: Georgi Muleshkov <[email protected]>
  • Loading branch information
afilipov1 committed Apr 11, 2018
1 parent d722608 commit d95b28b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void testFilterRegistriesByPath() throws Throwable {
createRegistry("https://test.registry.com:5000/vmware", null),
createRegistry("https://test.registry.com:5000/vmware/test", null),
createRegistry("https://test.registry.com:5000/test", null),
createRegistry("https://test.registry.com:5000/test2", null),
createRegistry("https://test.registry.com:5001/vmware", null),
createRegistry("https://test.registry.com:5001", null)
);
Expand Down Expand Up @@ -142,6 +143,15 @@ public void testFilterRegistriesByPath() throws Throwable {
filteredRegistries.stream().forEach(r -> {
assertTrue(r.address.contains("test.registry.com:5000/vmware"));
});

host.log("Test registry path");
image = DockerImage.fromParts("test.registry.com:5000", "test", "vmware", "latest");
filteredRegistries = RegistryUtil.filterRegistriesByPath(host, registries, image);
assertNotNull(filteredRegistries);
assertEquals(1, filteredRegistries.size());
filteredRegistries.stream().forEach(r -> {
assertTrue(r.address.contains("test.registry.com:5000/test"));
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,32 @@ public static void findRegistriesByHostname(ServiceHost serviceHost, String host
*/
public static List<RegistryState> filterRegistriesByPath(ServiceHost host, Collection<RegistryState> registries, DockerImage image) {
String imageHost = nonNullValue(image.getHost(), DEFAULT_DOCKER_REGISTRY_ADDRESS);
String path = image.getNamespace();
String imagePath = image.getNamespace();
host.log(Level.FINE, "Image path: %s.", imagePath);

String hostPath = imageHost;
if (path != null && !path.isEmpty()) {
hostPath = hostPath + "/" + path;
List<String> imagePathSegments = new ArrayList<>();
if (imagePath != null && !imagePath.isEmpty()) {
imagePathSegments = Arrays.asList(imagePath.split("/"));
}

final String address = hostPath;
host.log(Level.INFO, "Perform filtering of registries by path: %s.", address);

if (registries != null && !registries.isEmpty()) {
List<String> finalPathSegments = imagePathSegments;
return registries.stream().filter(r -> {
if (r.address != null && !r.address.isEmpty()) {
try {
String addressPart = new URI(r.address).getSchemeSpecificPart().replace("//", "");
return address.startsWith(addressPart);
String registryAddress = new URI(r.address).getSchemeSpecificPart().replace("//", "");
String imageAddress = imageHost;

if (imageAddress.equals(registryAddress)) {
return true;
}

for (String segment: finalPathSegments) {
imageAddress = imageAddress + "/" + segment;
if (imageAddress.equals(registryAddress)) {
return true;
}
}
} catch (URISyntaxException e) {
host.log(Level.SEVERE, "Failed to build URI. Exception [%s]", e.getMessage());
}
Expand Down

0 comments on commit d95b28b

Please sign in to comment.