From 14cef06f6a719166fe4133a0d2b4d3b1e1f402c8 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Thu, 16 May 2024 18:43:13 +0200 Subject: [PATCH] refactor: introduce JKubeConfiguration#pushRegistryConfig settings Signed-off-by: Marc Nuri --- .../jkube/gradle/plugin/task/AbstractJKubeTask.java | 9 ++++++++- .../jkube/kit/build/service/docker/BuildService.java | 2 +- .../org/eclipse/jkube/kit/common/JKubeConfiguration.java | 3 ++- .../eclipse/jkube/kit/common/JKubeConfigurationTest.java | 8 ++++---- .../config/service/kubernetes/JibImageBuildService.java | 6 +++--- .../config/service/openshift/OpenshiftBuildService.java | 7 +++---- .../kubernetes/JibImageBuildServiceIntegrationTest.java | 5 +++-- .../service/kubernetes/JibImageBuildServiceTest.java | 2 +- .../openshift/OpenshiftBuildServiceIntegrationTest.java | 2 +- .../org/eclipse/jkube/kit/resource/helm/HelmService.java | 5 ++--- .../eclipse/jkube/kit/resource/helm/HelmServiceTest.java | 2 +- .../jkube/kit/resource/helm/HelmServiceUploadIT.java | 2 +- .../maven/plugin/mojo/build/AbstractDockerMojo.java | 7 ++++++- .../jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java | 8 +++++++- .../eclipse/jkube/maven/plugin/mojo/build/PushMojo.java | 4 ---- .../jkube/maven/plugin/mojo/build/HelmPushMojoTest.java | 2 +- 16 files changed, 44 insertions(+), 30 deletions(-) diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java index 7656ba377a..e9e5d57c4c 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java @@ -127,13 +127,20 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() { .reactorProjects(Collections.singletonList(kubernetesExtension.javaProject)) .sourceDirectory(kubernetesExtension.getBuildSourceDirectoryOrDefault()) .outputDirectory(kubernetesExtension.getBuildOutputDirectoryOrDefault()) - .registryConfig(RegistryConfig.builder() + .pullRegistryConfig(RegistryConfig.builder() .settings(Collections.emptyList()) .authConfig(kubernetesExtension.authConfig != null ? kubernetesExtension.authConfig.toMap() : null) .skipExtendedAuth(kubernetesExtension.getSkipExtendedAuth().getOrElse(false)) .passwordDecryptionMethod(s -> s) .registry(kubernetesExtension.getPullRegistryOrDefault()) .build()) + .pushRegistryConfig(RegistryConfig.builder() + .settings(Collections.emptyList()) + .authConfig(kubernetesExtension.authConfig != null ? kubernetesExtension.authConfig.toMap() : null) + .skipExtendedAuth(kubernetesExtension.getSkipExtendedAuth().getOrElse(false)) + .passwordDecryptionMethod(s -> s) + .registry(kubernetesExtension.getPushRegistryOrNull()) + .build()) .build()) .clusterAccess(clusterAccess) .offline(kubernetesExtension.getOfflineOrDefault()) diff --git a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java index ad0ed0c05f..8897cbe217 100644 --- a/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java +++ b/jkube-kit/build/service/docker/src/main/java/org/eclipse/jkube/kit/build/service/docker/BuildService.java @@ -191,7 +191,7 @@ private void autoPullBaseImage(ImageConfiguration imageConfig, ImagePullManager } for (String fromImage : fromImages) { if (fromImage != null && !AssemblyManager.SCRATCH_IMAGE.equals(fromImage)) { - registryService.pullImageWithPolicy(fromImage, imagePullManager, configuration.getRegistryConfig(), buildConfig); + registryService.pullImageWithPolicy(fromImage, imagePullManager, configuration.getPullRegistryConfig(), buildConfig); } } } diff --git a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeConfiguration.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeConfiguration.java index 6674cce26b..975d3ae50d 100644 --- a/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeConfiguration.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeConfiguration.java @@ -41,7 +41,8 @@ public class JKubeConfiguration implements Serializable { private String sourceDirectory; private String outputDirectory; private Map buildArgs; - private RegistryConfig registryConfig; + private RegistryConfig pullRegistryConfig; + private RegistryConfig pushRegistryConfig; private List reactorProjects; public File getBasedir() { diff --git a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/JKubeConfigurationTest.java b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/JKubeConfigurationTest.java index d5c2e06380..6cf0693a42 100644 --- a/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/JKubeConfigurationTest.java +++ b/jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/JKubeConfigurationTest.java @@ -100,9 +100,8 @@ void builder() { .sourceDirectory("src/main/jkube") .outputDirectory("target") .buildArgs(Collections.singletonMap("foo", "bar")) - .registryConfig(RegistryConfig.builder() - .registry("r.example.com") - .build()); + .pullRegistryConfig(RegistryConfig.builder().registry("pull.example.com").build()) + .pushRegistryConfig(RegistryConfig.builder().registry("push.example.com").build()); // When JKubeConfiguration jKubeConfiguration = builder.build(); @@ -113,7 +112,8 @@ void builder() { .hasFieldOrPropertyWithValue("sourceDirectory", "src/main/jkube") .hasFieldOrPropertyWithValue("outputDirectory", "target") .hasFieldOrPropertyWithValue("buildArgs", Collections.singletonMap("foo", "bar")) - .hasFieldOrPropertyWithValue("registryConfig.registry", "r.example.com"); + .hasFieldOrPropertyWithValue("pullRegistryConfig.registry", "pull.example.com") + .hasFieldOrPropertyWithValue("pushRegistryConfig.registry", "push.example.com"); } /** diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildService.java index 36d36e305f..9e2dc77c3e 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildService.java @@ -89,11 +89,11 @@ public void buildSingleImage(ImageConfiguration imageConfiguration) throws JKube if (imageConfiguration.getBuildConfiguration().isDockerFileMode()) { throw new JKubeServiceException("Dockerfile mode is not supported with JIB build strategy"); } - final ImageConfiguration imageConfigToBuild = prependPushRegistry(imageConfiguration, configuration.getRegistryConfig()); + final ImageConfiguration imageConfigToBuild = prependPushRegistry(imageConfiguration, configuration.getPullRegistryConfig()); final BuildDirs buildDirs = new BuildDirs(imageConfigToBuild.getName(), configuration); - final String pullRegistry = getPullRegistry(imageConfigToBuild, configuration.getRegistryConfig()); + final String pullRegistry = getPullRegistry(imageConfigToBuild, configuration.getPullRegistryConfig()); final Credential pullRegistryCredential = getRegistryCredentials( - configuration.getRegistryConfig(), false, pullRegistry); + configuration.getPullRegistryConfig(), false, pullRegistry); final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfigToBuild, pullRegistry, pullRegistryCredential); final Map> layers = AssemblyManager.getInstance() diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java index dcc3282df6..dfea83f7d6 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java @@ -39,7 +39,6 @@ import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.eclipse.jkube.kit.common.JKubeConfiguration; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; -import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.RuntimeMode; import org.eclipse.jkube.kit.config.service.AbstractImageBuildService; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; @@ -131,7 +130,7 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService initClient(); String buildName = null; try { - final ImageConfiguration applicableImageConfig = getApplicableImageConfiguration(imageConfig, jKubeConfiguration.getRegistryConfig()); + final ImageConfiguration applicableImageConfig = getApplicableImageConfiguration(imageConfig, jKubeConfiguration.getPullRegistryConfig()); ImageName imageName = new ImageName(applicableImageConfig.getName()); File dockerTar = createBuildArchive(jKubeServiceHub, applicableImageConfig); @@ -343,10 +342,10 @@ private boolean checkOrCreatePullSecret(OpenShiftClient client, KubernetesListBu fromImage = extractBaseFromConfiguration(buildConfig); } - String pullRegistry = getApplicablePullRegistryFrom(fromImage, jKubeConfiguration.getRegistryConfig()); + String pullRegistry = getApplicablePullRegistryFrom(fromImage, jKubeConfiguration.getPullRegistryConfig()); if (pullRegistry != null) { - RegistryConfig registryConfig = jKubeConfiguration.getRegistryConfig(); + RegistryConfig registryConfig = jKubeConfiguration.getPullRegistryConfig(); final AuthConfig authConfig = new AuthConfigFactory(log).createAuthConfig(false, registryConfig.isSkipExtendedAuth(), registryConfig.getAuthConfig(), registryConfig.getSettings(), null, pullRegistry, registryConfig.getPasswordDecryptionMethod()); diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceIntegrationTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceIntegrationTest.java index 8f3db63bf9..538e2bbb2e 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceIntegrationTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceIntegrationTest.java @@ -85,7 +85,8 @@ void setUp(@TempDir Path projectRoot) throws IOException { .outputDirectory(targetDirectory.toFile()) .properties(new Properties()) .build()) - .registryConfig(RegistryConfig.builder().settings(Collections.emptyList()).build()) + .pullRegistryConfig(RegistryConfig.builder().settings(Collections.emptyList()).build()) + .pushRegistryConfig(RegistryConfig.builder().settings(Collections.emptyList()).build()) .build()) .build(); jibBuildService = new JibImageBuildService(hub); @@ -188,7 +189,7 @@ void setUp() { .registry("gcr.io") .build(); hub = hub.toBuilder().configuration(hub.getConfiguration().toBuilder() - .registryConfig(RegistryConfig.builder() + .pullRegistryConfig(RegistryConfig.builder() .registry("gcr.io") .authConfig(Collections.emptyMap()) .settings(Collections.emptyList()) diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceTest.java index 10e84072f3..356e060c44 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/JibImageBuildServiceTest.java @@ -251,7 +251,7 @@ void build_shouldCallPluginServiceAddFiles() throws JKubeServiceException { @Test void build_withRegistryConfig_shouldPrependRegistryToImageName() throws JKubeServiceException { // Given - when(mockedServiceHub.getConfiguration().getRegistryConfig()) + when(mockedServiceHub.getConfiguration().getPullRegistryConfig()) .thenReturn(RegistryConfig.builder().registry("quay.io").settings(Collections.emptyList()).build()); when(mockedServiceHub.getConfiguration().getProject()) .thenReturn(JavaProject.builder().baseDirectory(temporaryFolder.toFile()).build()); diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java index aad97335fa..def6bfb197 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java @@ -135,7 +135,7 @@ void init(@TempDir Path temporaryFolder) throws Exception { .baseDirectory(baseDirectory) .buildDirectory(target) .build()) - .registryConfig(RegistryConfig.builder().build()) + .pullRegistryConfig(RegistryConfig.builder().build()) .build()); image = ImageConfiguration.builder() diff --git a/jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmService.java b/jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmService.java index 634d4ab1c6..3be77bdc40 100644 --- a/jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmService.java +++ b/jkube-kit/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmService.java @@ -16,7 +16,6 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -154,10 +153,10 @@ public void uploadHelmChart(HelmConfig helm) throws BadUploadException, IOExcept final HelmRepository helmRepository = selectHelmRepository(helm); if (isRepositoryValid(helmRepository)) { final List registryServerConfigurations = Optional - .ofNullable(jKubeConfiguration).map(JKubeConfiguration::getRegistryConfig).map(RegistryConfig::getSettings) + .ofNullable(jKubeConfiguration).map(JKubeConfiguration::getPullRegistryConfig).map(RegistryConfig::getSettings) .orElse(Collections.emptyList()); final UnaryOperator passwordDecryptor = Optional.ofNullable(jKubeConfiguration) - .map(JKubeConfiguration::getRegistryConfig).map(RegistryConfig::getPasswordDecryptionMethod) + .map(JKubeConfiguration::getPullRegistryConfig).map(RegistryConfig::getPasswordDecryptionMethod) .orElse(s -> s); setAuthentication(helmRepository, logger, registryServerConfigurations, passwordDecryptor); uploadHelmChart(helm, helmRepository); diff --git a/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceTest.java b/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceTest.java index 7a200c75c4..8f815601b5 100644 --- a/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceTest.java +++ b/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceTest.java @@ -67,7 +67,7 @@ void setUp(@TempDir Path tempDir) throws IOException { .tarballOutputDir(helmOutputDirectory.toFile().getAbsolutePath()); jKubeConfiguration = JKubeConfiguration.builder() .project(JavaProject.builder().properties(new Properties()).build()) - .registryConfig(RegistryConfig.builder().settings(new ArrayList<>()).build()).build(); + .pullRegistryConfig(RegistryConfig.builder().settings(new ArrayList<>()).build()).build(); resourceServiceConfig = new ResourceServiceConfig(); helmService = new HelmService(jKubeConfiguration, resourceServiceConfig, new KitLogger.SilentLogger()); } diff --git a/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceUploadIT.java b/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceUploadIT.java index 44b4f1aeee..4038c0c14c 100644 --- a/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceUploadIT.java +++ b/jkube-kit/helm/src/test/java/org/eclipse/jkube/kit/resource/helm/HelmServiceUploadIT.java @@ -99,7 +99,7 @@ void setUp(@TempDir Path temporaryFolder) throws IOException { helmService = new HelmService( JKubeConfiguration.builder() .project(JavaProject.builder().properties(new Properties()).build()) - .registryConfig(RegistryConfig.builder() + .pullRegistryConfig(RegistryConfig.builder() .settings(Collections.singletonList(registryServerConfiguration)).build()) .build(), new ResourceServiceConfig(), diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java index 542e9bd4bd..6ac0898f91 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java @@ -177,6 +177,10 @@ public abstract class AbstractDockerMojo extends AbstractMojo @Parameter(property = "jkube.docker.pull.registry") protected String pullRegistry; + // Registry to use for push operations if no registry is specified + @Parameter(property = "jkube.docker.push.registry") + protected String pushRegistry; + /** * Build mode when build is performed. * Can be either "s2i" for an s2i binary build mode (in case of OpenShift) or @@ -428,7 +432,8 @@ protected JKubeConfiguration initJKubeConfiguration() throws DependencyResolutio .outputDirectory(outputDirectory) .reactorProjects(Collections.singletonList(javaProject)) .buildArgs(buildArgs) - .registryConfig(getRegistryConfig(pullRegistry)) + .pullRegistryConfig(getRegistryConfig(pullRegistry)) + .pushRegistryConfig(getRegistryConfig(pushRegistry)) .build(); } diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java index b4fb6c83f1..9bc37db986 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java @@ -233,7 +233,13 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder(Java .configuration(JKubeConfiguration.builder() .project(javaProject) .reactorProjects(Collections.singletonList(javaProject)) - .registryConfig(RegistryConfig.builder() + // TODO: Should provide same values as AbstractDockerMojo#getRegistryConfig + // AbstractDockerMojo should be eventually removed in favor of AbstractJKubeMojo + .pullRegistryConfig(RegistryConfig.builder() + .settings(MavenUtil.getRegistryServerFromMavenSettings(settings)) + .passwordDecryptionMethod(this::decrypt) + .build()) + .pushRegistryConfig(RegistryConfig.builder() .settings(MavenUtil.getRegistryServerFromMavenSettings(settings)) .passwordDecryptionMethod(this::decrypt) .build()) diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/PushMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/PushMojo.java index 393be8ca53..5f6f9d9388 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/PushMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/PushMojo.java @@ -31,10 +31,6 @@ public class PushMojo extends AbstractDockerMojo { @Parameter(property = "jkube.skip.push", defaultValue = "false") protected boolean skipPush; - // Registry to use for push operations if no registry is specified - @Parameter(property = "jkube.docker.push.registry") - private String pushRegistry; - /** * Skip building tags */ diff --git a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojoTest.java b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojoTest.java index 8268795cc1..5a3037f62d 100644 --- a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojoTest.java +++ b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/HelmPushMojoTest.java @@ -166,7 +166,7 @@ void execute_withValidMavenSettings_shouldUpload() throws Exception { // When helmPushMojo.execute(); // Then - assertThat(helmPushMojo.jkubeServiceHub.getConfiguration().getRegistryConfig().getSettings()).singleElement() + assertThat(helmPushMojo.jkubeServiceHub.getConfiguration().getPullRegistryConfig().getSettings()).singleElement() .isEqualTo(RegistryServerConfiguration.builder() .id("SNAP-REPO").username("mavenUser").password("mavenPassword").configuration(new HashMap<>()).build()); assertThat(helmServiceMockedConstruction.constructed()).hasSize(1);