Skip to content

Commit 43c5345

Browse files
committed
Create a Testcontainers image name resolver to support CI proxy usage.
Backported to Spring Data JPA 2.7.x. Resolves #2941. Original pull request: #2937.
1 parent b007b12 commit 43c5345

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Jenkinsfile

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pipeline {
3333
environment {
3434
DOCKER_HUB = credentials("${p['docker.credentials']}")
3535
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
36+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
3637
}
3738
steps {
3839
script {
@@ -61,6 +62,7 @@ pipeline {
6162
options { timeout(time: 30, unit: 'MINUTES') }
6263
environment {
6364
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
65+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
6466
}
6567
steps {
6668
script {
@@ -78,6 +80,7 @@ pipeline {
7880
options { timeout(time: 30, unit: 'MINUTES') }
7981
environment {
8082
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
83+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
8184
}
8285
steps {
8386
script {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.support;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.testcontainers.utility.DockerImageName;
22+
import org.testcontainers.utility.ImageNameSubstitutor;
23+
24+
/**
25+
* An {@link ImageNameSubstitutor} only used on CI servers to leverage internal proxy solution, that needs to vary the
26+
* prefix based on which container image is needed.
27+
*
28+
* @author Greg Turnquist
29+
*/
30+
public class ProxyImageNameSubstitutor extends ImageNameSubstitutor {
31+
32+
private static final List<String> NAMES_TO_PROXY_PREFIX = Arrays.asList("ryuk");
33+
34+
private static final List<String> NAMES_TO_LIBRARY_PROXY_PREFIX = Arrays.asList("mysql", "postgres");
35+
36+
private static final String PROXY_PREFIX = "harbor-repo.vmware.com/dockerhub-proxy-cache/";
37+
38+
private static final String LIBRARY_PROXY_PREFIX = PROXY_PREFIX + "library/";
39+
40+
@Override
41+
public DockerImageName apply(DockerImageName dockerImageName) {
42+
43+
if (NAMES_TO_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
44+
return DockerImageName.parse(applyProxyPrefix(dockerImageName.asCanonicalNameString()));
45+
}
46+
47+
if (NAMES_TO_LIBRARY_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
48+
return DockerImageName.parse(applyProxyAndLibraryPrefix(dockerImageName.asCanonicalNameString()));
49+
}
50+
51+
return dockerImageName;
52+
}
53+
54+
@Override
55+
protected String getDescription() {
56+
return "Spring Data Proxy Image Name Substitutor";
57+
}
58+
59+
/**
60+
* Apply a non-library-based prefix.
61+
*/
62+
private static String applyProxyPrefix(String imageName) {
63+
return PROXY_PREFIX + imageName;
64+
}
65+
66+
/**
67+
* Apply a library based prefix.
68+
*/
69+
private static String applyProxyAndLibraryPrefix(String imageName) {
70+
return LIBRARY_PROXY_PREFIX + imageName;
71+
}
72+
}

0 commit comments

Comments
 (0)