Skip to content

Commit 785999e

Browse files
committed
Use Harbor Proxy service on CI.
When run on CI servers, leverage an internal proxy service using Testcontainers ability to plugin a custom ImageNameSubstitor. See #1518 Original Pull Request: #1516
1 parent e9304b4 commit 785999e

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ target/
1111

1212
#prevent license accepting file to get accidentially commited to git
1313
container-license-acceptance.txt
14+
spring-data-jdbc/src/test/java/org/springframework/data/ProxyImageNameSubstitutor.java
15+
spring-data-r2dbc/src/test/java/org/springframework/data/ProxyImageNameSubstitutor.java

Jenkinsfile

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pipeline {
3434
environment {
3535
DOCKER_HUB = credentials("${p['docker.credentials']}")
3636
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
37+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.ProxyImageNameSubstitutor'
3738
}
3839

3940
steps {
@@ -65,6 +66,7 @@ pipeline {
6566
environment {
6667
DOCKER_HUB = credentials("${p['docker.credentials']}")
6768
ARTIFACTORY = credentials("${p['artifactory.credentials']}")
69+
TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.ProxyImageNameSubstitutor'
6870
}
6971

7072
steps {

ci/accept-third-party-license.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
{
44
echo "mcr.microsoft.com/mssql/server:2017-CU12"
55
echo "ibmcom/db2:11.5.0.0a"
6-
} > spring-data-jdbc/src/test/resources/container-license-acceptance.txt
6+
echo "harbor-repo.vmware.com/dockerhub-proxy-cache/ibmcom/db2:11.5.0.0a"
7+
} > spring-data-jdbc/src/test/resources/container-license-acceptance.txt

ci/test.sh

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
set -euo pipefail
44

55
ci/accept-third-party-license.sh
6+
7+
echo "Copying ProxyImageNameSubstitutor into JDBC and R2DBC..."
8+
cp spring-data-relational/src/test/java/org/springframework/data/ProxyImageNameSubstitutor.java spring-data-jdbc/src/test/java/org/springframework/data
9+
cp spring-data-relational/src/test/java/org/springframework/data/ProxyImageNameSubstitutor.java spring-data-r2dbc/src/test/java/org/springframework/data
10+
611
mkdir -p /tmp/jenkins-home
712
chown -R 1001:1001 .
813
MAVEN_OPTS="-Duser.name=jenkins -Duser.home=/tmp/jenkins-home" \

spring-data-relational/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<scope>test</scope>
8585
</dependency>
8686

87+
<dependency>
88+
<groupId>org.testcontainers</groupId>
89+
<artifactId>testcontainers</artifactId>
90+
<version>${testcontainers}</version>
91+
<scope>test</scope>
92+
</dependency>
93+
8794
</dependencies>
8895

8996
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 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;
17+
18+
import java.util.List;
19+
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.testcontainers.utility.DockerImageName;
23+
import org.testcontainers.utility.ImageNameSubstitutor;
24+
25+
/**
26+
* An {@link ImageNameSubstitutor} only used on CI servers to leverage internal proxy solution, that needs to vary the
27+
* prefix based on which container image is needed.
28+
*
29+
* @author Greg Turnquist
30+
*/
31+
public class ProxyImageNameSubstitutor extends ImageNameSubstitutor {
32+
33+
private static final Logger LOG = LoggerFactory.getLogger(ProxyImageNameSubstitutor.class);
34+
35+
private static final List<String> NAMES_TO_PROXY_PREFIX = List.of("ryuk", "arm64v8/mariadb", "ibmcom/db2",
36+
"gvenzl/oracle-xe");
37+
38+
private static final List<String> NAMES_TO_LIBRARY_PROXY_PREFIX = List.of("mariadb", "mysql", "postgres");
39+
40+
private static final String PROXY_PREFIX = "harbor-repo.vmware.com/dockerhub-proxy-cache/";
41+
42+
private static final String LIBRARY_PROXY_PREFIX = PROXY_PREFIX + "library/";
43+
44+
@Override
45+
public DockerImageName apply(DockerImageName dockerImageName) {
46+
47+
if (NAMES_TO_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
48+
49+
String transformedName = applyProxyPrefix(dockerImageName.asCanonicalNameString());
50+
LOG.info("Converting " + dockerImageName.asCanonicalNameString() + " to " + transformedName);
51+
return DockerImageName.parse(transformedName);
52+
}
53+
54+
if (NAMES_TO_LIBRARY_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) {
55+
56+
String transformedName = applyProxyAndLibraryPrefix(dockerImageName.asCanonicalNameString());
57+
LOG.info("Converting " + dockerImageName.asCanonicalNameString() + " to " + transformedName);
58+
return DockerImageName.parse(transformedName);
59+
}
60+
61+
LOG.info("Not changing " + dockerImageName.asCanonicalNameString() + "...");
62+
return dockerImageName;
63+
}
64+
65+
@Override
66+
protected String getDescription() {
67+
return "Spring Data Proxy Image Name Substitutor";
68+
}
69+
70+
/**
71+
* Apply a non-library-based prefix.
72+
*/
73+
private static String applyProxyPrefix(String imageName) {
74+
return PROXY_PREFIX + imageName;
75+
}
76+
77+
/**
78+
* Apply a library based prefix.
79+
*/
80+
private static String applyProxyAndLibraryPrefix(String imageName) {
81+
return LIBRARY_PROXY_PREFIX + imageName;
82+
}
83+
}

0 commit comments

Comments
 (0)