Skip to content

Commit

Permalink
refactor: push registry information read from global JKubeConfiguration
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed May 20, 2024
1 parent ed3d204 commit 46610b5
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() {
.authConfig(kubernetesExtension.authConfig != null ? kubernetesExtension.authConfig.toMap() : null)
.skipExtendedAuth(kubernetesExtension.getSkipExtendedAuth().getOrElse(false))
.passwordDecryptionMethod(s -> s)
.registry(kubernetesExtension.getPushRegistryOrNull())
.registry(kubernetesExtension.getPushRegistryOrNull() != null ?
kubernetesExtension.getPushRegistryOrNull() : kubernetesExtension.getRegistryOrDefault())
.build())
.build())
.clusterAccess(clusterAccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;

import javax.inject.Inject;
import java.util.Collections;

public class KubernetesPushTask extends AbstractJKubeTask {
@Inject
Expand All @@ -41,7 +39,7 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() {
public void run() {
try {
jKubeServiceHub.getBuildService()
.push(resolvedImages, kubernetesExtension.getPushRetriesOrDefault(), initRegistryConfig(), kubernetesExtension.getSkipTagOrDefault());
.push(resolvedImages, kubernetesExtension.getPushRetriesOrDefault(), kubernetesExtension.getSkipTagOrDefault());
} catch (JKubeServiceException e) {
throw new IllegalStateException("Error in pushing image: " + e.getMessage(), e);
}
Expand All @@ -52,16 +50,6 @@ protected boolean shouldSkip() {
return super.shouldSkip() || kubernetesExtension.getSkipPushOrDefault();
}

private RegistryConfig initRegistryConfig() {
final String specificRegistry = kubernetesExtension.getPushRegistryOrNull();
return RegistryConfig.builder()
.settings(Collections.emptyList())
.authConfig(kubernetesExtension.authConfig != null ? kubernetesExtension.authConfig.toMap() : null)
.skipExtendedAuth(kubernetesExtension.getSkipExtendedAuthOrDefault())
.registry(specificRegistry != null ? specificRegistry : kubernetesExtension.getRegistryOrDefault())
.passwordDecryptionMethod(password -> password).build();
}

protected BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBuilder() {
return TaskUtil.buildServiceConfigBuilder(kubernetesExtension);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void run_withImageConfiguration_shouldPushImage() throws JKubeServiceException {
// Then
assertThat(dockerBuildServiceMockedConstruction.constructed()).hasSize(1);
verify(dockerBuildServiceMockedConstruction.constructed().iterator().next(), times(1))
.push(argThat(images -> images.iterator().next().getName().equals("foo/bar:latest")), eq(0), any(), eq(false));
.push(argThat(images -> images.iterator().next().getName().equals("foo/bar:latest")), eq(0), eq(false));
}


Expand All @@ -109,7 +109,7 @@ public Property<Boolean> getSkipPush() {

// Then
assertThat(dockerBuildServiceMockedConstruction.constructed()).isEmpty();
verify(pushTask.jKubeServiceHub.getBuildService(), times(0)).push(any(), anyInt(), any(), anyBoolean());
verify(pushTask.jKubeServiceHub.getBuildService(), times(0)).push(any(), anyInt(), anyBoolean());
verify(taskEnvironment.logger, times(1)).lifecycle(contains("k8s: `k8sPush` task is skipped."));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.mockito.MockedConstruction;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mockConstruction;
Expand Down Expand Up @@ -72,7 +71,7 @@ void run_withImageConfigurationAndS2IBuildStrategy_shouldPushImage() throws JKub
// Then
assertThat(openshiftBuildServiceMockedConstruction.constructed()).hasSize(1);
verify(openshiftBuildServiceMockedConstruction.constructed().iterator().next())
.push(argThat(images -> images.iterator().next().getName().equals("foo/bar:latest")), eq(0), any(), eq(false));
.push(argThat(images -> images.iterator().next().getName().equals("foo/bar:latest")), eq(0), eq(false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static org.eclipse.jkube.kit.build.api.helper.RegistryUtil.getApplicablePushRegistryFrom;

/**
* Allows to interact with registries, eg. to push/pull images.
* Allows to interact with registries, e.g. to push/pull images.
*/
public class RegistryService {

Expand All @@ -54,8 +54,9 @@ public class RegistryService {
* @param skipTag flag to skip pushing tagged images
* @throws IOException exception
*/
public void pushImage(ImageConfiguration imageConfig,
int retries, RegistryConfig registryConfig, boolean skipTag) throws IOException {
public void pushImage(
ImageConfiguration imageConfig, int retries, RegistryConfig registryConfig, boolean skipTag
) throws IOException {
BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
String name = imageConfig.getName();
if (buildConfig != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package org.eclipse.jkube.kit.config.service;

import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;

import java.util.Collection;
Expand All @@ -27,7 +26,7 @@ protected AbstractImageBuildService(JKubeServiceHub jKubeServiceHub) {

protected abstract void buildSingleImage(ImageConfiguration imageConfiguration) throws JKubeServiceException;

protected abstract void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException;
protected abstract void pushSingleImage(ImageConfiguration imageConfiguration, int retries, boolean skipTag) throws JKubeServiceException;

/** {@inheritDoc} */
@Override
Expand All @@ -37,8 +36,8 @@ public final void build(ImageConfiguration... imageConfigurations) throws JKubeS
}

@Override
public final void push(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException {
processImage(imageConfiguration -> pushSingleImage(imageConfiguration, retries, registryConfig, skipTag), "Skipped push", imageConfigs.toArray(new ImageConfiguration[0]));
public final void push(Collection<ImageConfiguration> imageConfigs, int retries, boolean skipTag) throws JKubeServiceException {
processImage(imageConfiguration -> pushSingleImage(imageConfiguration, retries, skipTag), "Skipped push", imageConfigs.toArray(new ImageConfiguration[0]));
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ public interface BuildService {
*
* @param imageConfigs image configurations to process
* @param retries number of retries
* @param registryConfig registry configuration
* @param skipTag boolean value whether skip tagging or not
* @throws JKubeServiceException in case of any error while building image
*/
void push(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException;
void push(Collection<ImageConfiguration> imageConfigs, int retries, boolean skipTag) throws JKubeServiceException;

/**
* Post processing step called after all images has been build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
Expand All @@ -43,9 +43,10 @@ public class BuildPackBuildService extends AbstractImageBuildService {
private static final String PACK_CONFIG_DIR = ".pack";
private static final String PACK_CONFIG_FILE = "config.toml";

private final BuildServiceConfig buildServiceConfig;
private final KitLogger kitLogger;
private final BuildServiceConfig buildServiceConfig;
private final BuildPackCliDownloader buildPackCliDownloader;
private final JKubeConfiguration jKubeConfiguration;
private final DockerServiceHub dockerServiceHub;

public BuildPackBuildService(JKubeServiceHub jKubeServiceHub) {
Expand All @@ -54,15 +55,18 @@ public BuildPackBuildService(JKubeServiceHub jKubeServiceHub) {

BuildPackBuildService(JKubeServiceHub jKubeServiceHub, Properties packProperties) {
super(jKubeServiceHub);
this.kitLogger = Objects.requireNonNull(jKubeServiceHub.getLog());
this.buildServiceConfig = Objects.requireNonNull(jKubeServiceHub.getBuildServiceConfig(),
"BuildServiceConfig is required");
this.kitLogger = Objects.requireNonNull(jKubeServiceHub.getLog());
this.jKubeConfiguration = Objects.requireNonNull(jKubeServiceHub.getConfiguration(),
"JKubeConfiguration is required");
this.dockerServiceHub = Objects.requireNonNull(jKubeServiceHub.getDockerServiceHub(),
"Docker Service Hub is required");
if (packProperties == null) {
this.buildPackCliDownloader = new BuildPackCliDownloader(kitLogger);
} else {
this.buildPackCliDownloader = new BuildPackCliDownloader(kitLogger, packProperties);
}
this.dockerServiceHub = jKubeServiceHub.getDockerServiceHub();
}

@Override
Expand Down Expand Up @@ -91,9 +95,10 @@ protected void buildSingleImage(ImageConfiguration imageConfiguration) {
}

@Override
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException {
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, boolean skipTag) throws JKubeServiceException {
try {
dockerServiceHub.getRegistryService().pushImage(imageConfiguration, retries, registryConfig, skipTag);
dockerServiceHub.getRegistryService()
.pushImage(imageConfiguration, retries, jKubeConfiguration.getPushRegistryConfig(), skipTag);
} catch (IOException ex) {
throw new JKubeServiceException("Error while trying to push the image: " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.AbstractImageBuildService;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
Expand Down Expand Up @@ -66,9 +65,10 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService
}

@Override
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException {
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, boolean skipTag) throws JKubeServiceException {
try {
dockerServices.getRegistryService().pushImage(imageConfiguration, retries, registryConfig, skipTag);
dockerServices.getRegistryService()
.pushImage(imageConfiguration, retries, jKubeConfiguration.getPushRegistryConfig(), skipTag);
} catch (IOException ex) {
throw new JKubeServiceException("Error while trying to push the image: " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public void buildSingleImage(ImageConfiguration imageConfiguration) throws JKube
}

@Override
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) throws JKubeServiceException {
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, boolean skipTag) throws JKubeServiceException {
try {
final RegistryConfig registryConfig = configuration.getPushRegistryConfig();
final ImageConfiguration imageConfigToPush = prependPushRegistry(imageConfiguration, registryConfig);
kitLogger.info("This push refers to: %s", imageConfigToPush.getName());
kitLogger.info("Pushing image: %s", new ImageName(imageConfigToPush.getName()).getFullName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService
}

@Override
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, RegistryConfig registryConfig, boolean skipTag) {
protected void pushSingleImage(ImageConfiguration imageConfiguration, int retries, boolean skipTag) {
// Do nothing. Image is pushed as part of build phase
log.warn("Image is pushed to OpenShift's internal registry during oc:build goal. Skipping...");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,17 @@ class PushSingleImage {
private BuildPackBuildService buildPackBuildService;
private DockerAccess dockerAccess;

private RegistryConfig registryConfig;

@BeforeEach
void setUp() {
dockerAccess = mock(DockerAccess.class);
jKubeServiceHub = jKubeServiceHub.toBuilder()
.dockerServiceHub(DockerServiceHub.newInstance(kitLogger, dockerAccess))
.build();
registryConfig = RegistryConfig.builder()
.registry("example.com")
.settings(Collections.emptyList())
.configuration(JKubeConfiguration.builder()
.pushRegistryConfig(RegistryConfig.builder()
.registry("example.com")
.settings(Collections.emptyList())
.build())
.build())
.build();
buildPackBuildService = new BuildPackBuildService(jKubeServiceHub, new Properties());
}
Expand All @@ -247,7 +247,7 @@ void setUp() {
@DisplayName("push successfully done via docker daemon")
void whenPushSuccessful_thenImagePushedViaDockerAccess() throws JKubeServiceException, DockerAccessException {
// When
buildPackBuildService.pushSingleImage(imageConfiguration, 0, registryConfig, true);
buildPackBuildService.pushSingleImage(imageConfiguration, 0, true);

// Then
verify(dockerAccess).pushImage("foo/bar:latest", null, "example.com", 0);
Expand All @@ -262,7 +262,7 @@ void whenPushFailed_thenThrowException() throws DockerAccessException {

// When + Then
assertThatExceptionOfType(JKubeServiceException.class)
.isThrownBy(() -> buildPackBuildService.pushSingleImage(imageConfiguration, 0, registryConfig, false))
.isThrownBy(() -> buildPackBuildService.pushSingleImage(imageConfiguration, 0, false))
.withMessage("Error while trying to push the image: Push failure");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void build_withFailure_shouldThrowException() throws Exception {
@Test
void push_withDefaults_shouldPush() throws Exception {
// When
new DockerBuildService(mockedJKubeServiceHub).push(Collections.emptyList(), 0, null, false);
new DockerBuildService(mockedJKubeServiceHub).push(Collections.emptyList(), 0, false);
// Then
verify(mockedJKubeServiceHub.getDockerServiceHub().getRegistryService(), times(0))
.pushImage(any(), eq(0), isNull(), eq(false));
Expand Down
Loading

0 comments on commit 46610b5

Please sign in to comment.