From b86c9a665e8dea29440afe3ccfb3971a4c5361ca Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Wed, 17 May 2023 11:28:14 -0500 Subject: [PATCH] Use Harbor Proxy service on CI. For Testcontainers-based images, we need to extend usage of our built-in proxy service with a custom ImageNameSubstitutor. See #2724 --- Jenkinsfile | 2 + .../support/ProxyImageNameSubstitutor.java | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/test/java/org/springframework/data/neo4j/support/ProxyImageNameSubstitutor.java diff --git a/Jenkinsfile b/Jenkinsfile index e40b2f2b0..1b3c19876 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -33,6 +33,7 @@ pipeline { environment { ARTIFACTORY = credentials("${p['artifactory.credentials']}") + TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.neo4j.support.ProxyImageNameSubstitutor' } steps { @@ -61,6 +62,7 @@ pipeline { options { timeout(time: 30, unit: 'MINUTES') } environment { ARTIFACTORY = credentials("${p['artifactory.credentials']}") + TESTCONTAINERS_IMAGE_SUBSTITUTOR = 'org.springframework.data.neo4j.support.ProxyImageNameSubstitutor' } steps { script { diff --git a/src/test/java/org/springframework/data/neo4j/support/ProxyImageNameSubstitutor.java b/src/test/java/org/springframework/data/neo4j/support/ProxyImageNameSubstitutor.java new file mode 100644 index 000000000..82eabf817 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/support/ProxyImageNameSubstitutor.java @@ -0,0 +1,82 @@ +/* + * Copyright 2011-2023 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.data.neo4j.support; + +import ch.qos.logback.classic.Logger; + +import java.util.List; + +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.ImageNameSubstitutor; + +/** + * An {@link ImageNameSubstitutor} only used on CI servers to leverage internal proxy solution, that needs to vary the + * prefix based on which container image is needed. + * + * @author Greg Turnquist + */ +public class ProxyImageNameSubstitutor extends ImageNameSubstitutor { + + private static final Logger LOG = (Logger) org.slf4j.LoggerFactory.getLogger(ProxyImageNameSubstitutor.class); + + private static final List NAMES_TO_PROXY_PREFIX = List.of("ryuk"); + + private static final List NAMES_TO_LIBRARY_PROXY_PREFIX = List.of("neo4j"); + + private static final String PROXY_PREFIX = "harbor-repo.vmware.com/dockerhub-proxy-cache/"; + + private static final String LIBRARY_PROXY_PREFIX = PROXY_PREFIX + "library/"; + + @Override + public DockerImageName apply(DockerImageName dockerImageName) { + + if (NAMES_TO_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) { + + String transformedName = applyProxyPrefix(dockerImageName.asCanonicalNameString()); + LOG.info("Converting " + dockerImageName.asCanonicalNameString() + " to " + transformedName); + return DockerImageName.parse(transformedName); + } + + if (NAMES_TO_LIBRARY_PROXY_PREFIX.stream().anyMatch(s -> dockerImageName.asCanonicalNameString().contains(s))) { + + String transformedName = applyProxyAndLibraryPrefix(dockerImageName.asCanonicalNameString()); + LOG.info("Converting " + dockerImageName.asCanonicalNameString() + " to " + transformedName); + return DockerImageName.parse(transformedName); + } + + LOG.info("Not changing " + dockerImageName.asCanonicalNameString() + "..."); + return dockerImageName; + } + + @Override + protected String getDescription() { + return "Spring Data Proxy Image Name Substitutor"; + } + + /** + * Apply a non-library-based prefix. + */ + private static String applyProxyPrefix(String imageName) { + return PROXY_PREFIX + imageName; + } + + /** + * Apply a library based prefix. + */ + private static String applyProxyAndLibraryPrefix(String imageName) { + return LIBRARY_PROXY_PREFIX + imageName; + } +}