Skip to content

Commit b3cb1f7

Browse files
committed
Create a Testcontainers image name resolver to support CI proxy usage.
Ported the solution to leveraging Docker proxy services to Spring Data JPA 3.1. Related: #2941. Original pull request: #2937.
1 parent 87bd578 commit b3cb1f7

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Jenkinsfile

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pipeline {
3232
options { timeout(time: 30, unit: 'MINUTES') }
3333
environment {
3434
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
35+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
3536
}
3637
steps {
3738
script {
@@ -60,6 +61,7 @@ pipeline {
6061
options { timeout(time: 30, unit: 'MINUTES')}
6162
environment {
6263
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
64+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
6365
}
6466
steps {
6567
script {
@@ -77,6 +79,7 @@ pipeline {
7779
options { timeout(time: 30, unit: 'MINUTES')}
7880
environment {
7981
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
82+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
8083
}
8184
steps {
8285
script {
@@ -94,6 +97,7 @@ pipeline {
9497
options { timeout(time: 30, unit: 'MINUTES')}
9598
environment {
9699
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
100+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.jpa.support.ProxyImageNameSubstitutor'
97101
}
98102
steps {
99103
script {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.List;
19+
20+
import org.testcontainers.utility.DockerImageName;
21+
import org.testcontainers.utility.ImageNameSubstitutor;
22+
23+
/**
24+
* An {@link ImageNameSubstitutor} only used on CI servers to leverage internal proxy solution, that needs to vary the
25+
* prefix based on which container image is needed.
26+
*
27+
* @author Greg Turnquist
28+
*/
29+
public class ProxyImageNameSubstitutor extends ImageNameSubstitutor {
30+
31+
private static final List<String> NAMES_TO_PROXY_PREFIX = List.of("ryuk");
32+
33+
private static final List<String> NAMES_TO_LIBRARY_PROXY_PREFIX = List.of("mysql", "postgres");
34+
35+
private static final String PROXY_PREFIX = "harbor-repo.vmware.com/dockerhub-proxy-cache/";
36+
37+
private static final String LIBRARY_PROXY_PREFIX = PROXY_PREFIX + "library/";
38+
39+
@Override
40+
public DockerImageName apply(DockerImageName dockerImageName) {
41+
42+
if (NAMES_TO_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
43+
return DockerImageName.parse(applyProxyPrefix(dockerImageName.asCanonicalNameString()));
44+
}
45+
46+
if (NAMES_TO_LIBRARY_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
47+
return DockerImageName.parse(applyProxyAndLibraryPrefix(dockerImageName.asCanonicalNameString()));
48+
}
49+
50+
return dockerImageName;
51+
}
52+
53+
@Override
54+
protected String getDescription() {
55+
return "Spring Data Proxy Image Name Substitutor";
56+
}
57+
58+
/**
59+
* Apply a non-library-based prefix.
60+
*/
61+
private static String applyProxyPrefix(String imageName) {
62+
return PROXY_PREFIX + imageName;
63+
}
64+
65+
/**
66+
* Apply a library based prefix.
67+
*/
68+
private static String applyProxyAndLibraryPrefix(String imageName) {
69+
return LIBRARY_PROXY_PREFIX + imageName;
70+
}
71+
}

0 commit comments

Comments
 (0)