-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
… Kubernetes Gradle Plugin Kubernetes Push Task ported to gradle Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
ab72bed
commit 39eabff
Showing
8 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
gradle-plugin/doc/src/main/asciidoc/inc/tasks/build/_jkube-push.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[[jkube:push]] | ||
=== *{task-prefix}Push* | ||
|
||
This task uploads images to the registry which have a `build` configuration section. The images to push can be restricted with | ||
the global option `filter` (see <<build-configuration,Build Goal Configuration>> for details). The registry to push is by | ||
default `docker.io` but can be specified as part of the images's `name` the Docker way. | ||
E.g. `docker.test.org:5000/data:1.5` will push the image `data` with tag `1.5` to the registry `docker.test.org` at port | ||
`5000`. Registry credentials (i.e. username and password) can be specified in multiple ways as described in section <<authentication,Authentication>>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...gin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesPushTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import org.eclipse.jkube.gradle.plugin.KubernetesExtension; | ||
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 | ||
public KubernetesPushTask(Class<? extends KubernetesExtension> extensionClass) { | ||
super(extensionClass); | ||
setDescription("Uploads the built Docker images to a Docker registry"); | ||
} | ||
|
||
|
||
@Override | ||
protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() { | ||
return TaskUtil.addDockerServiceHubToJKubeServiceHubBuilder(super.initJKubeServiceHubBuilder(), kubernetesExtension, javaProject, kitLogger) | ||
.buildServiceConfig(buildServiceConfigBuilder().build()); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (kubernetesExtension.getSkipPushOrDefault()) { | ||
return; | ||
} | ||
|
||
try { | ||
jKubeServiceHub.getBuildService().push(resolvedImages, kubernetesExtension.getPushRetriesOrDefault(), initRegistryConfig(kubernetesExtension.getPushRegistry().getOrNull()), kubernetesExtension.getSkipTagOrDefault()); | ||
} catch (JKubeServiceException e) { | ||
throw new IllegalStateException("Error in pushing image: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
private RegistryConfig initRegistryConfig(String specificRegistry) { | ||
return RegistryConfig.builder() | ||
.settings(Collections.emptyList()) | ||
.authConfig(kubernetesExtension.authConfig != null ? kubernetesExtension.authConfig.toMap() : null) | ||
.skipExtendedAuth(kubernetesExtension.getSkipExtendedAuthOrDefault()) | ||
.registry(specificRegistry != null ? specificRegistry : kubernetesExtension.getRegistry().getOrNull()) | ||
.passwordDecryptionMethod(password -> password).build(); | ||
} | ||
|
||
protected BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBuilder() { | ||
return TaskUtil.buildServiceConfigBuilder(kubernetesExtension, javaProject); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesPushTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.gradle.plugin.task; | ||
|
||
import org.eclipse.jkube.gradle.plugin.KubernetesExtension; | ||
import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension; | ||
import org.eclipse.jkube.kit.config.image.ImageConfiguration; | ||
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; | ||
import org.eclipse.jkube.kit.config.service.JKubeServiceException; | ||
import org.eclipse.jkube.kit.config.service.kubernetes.DockerBuildService; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.logging.Logger; | ||
import org.gradle.api.plugins.JavaPluginConvention; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.mockito.MockedConstruction; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockConstruction; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class KubernetesPushTaskTest { | ||
@Rule | ||
public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
private MockedConstruction<DefaultTask> defaultTaskMockedConstruction; | ||
private MockedConstruction<DockerBuildService> dockerBuildServiceMockedConstruction; | ||
private List<ImageConfiguration> images; | ||
private Project project; | ||
private Logger logger; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
project = mock(Project.class, Mockito.RETURNS_DEEP_STUBS); | ||
logger = mock(Logger.class); | ||
|
||
defaultTaskMockedConstruction = mockConstruction(DefaultTask.class, (mock, ctx) -> { | ||
when(mock.getProject()).thenReturn(project); | ||
when(mock.getLogger()).thenReturn(logger); | ||
}); | ||
dockerBuildServiceMockedConstruction = mockConstruction(DockerBuildService.class, (mock, ctx) -> { | ||
when(mock.isApplicable()).thenReturn(true); | ||
}); | ||
when(project.getConfigurations().stream()).thenAnswer(i -> Stream.empty()); | ||
when(project.getBuildscript().getConfigurations().stream()).thenAnswer(i -> Stream.empty()); | ||
when(project.getProjectDir()).thenReturn(temporaryFolder.getRoot()); | ||
when(project.getBuildDir()).thenReturn(temporaryFolder.newFolder("build")); | ||
KubernetesExtension extension = new TestKubernetesExtension() { | ||
@Override | ||
public boolean isDockerAccessRequired() { return false; } | ||
}; | ||
images = Collections.singletonList(ImageConfiguration.builder() | ||
.name("foo/bar:latest") | ||
.build(BuildConfiguration.builder() | ||
.dockerFile("Dockerfile") | ||
.build()) | ||
.build()); | ||
extension.images = images; | ||
when(project.getExtensions().getByType(KubernetesExtension.class)).thenReturn(extension); | ||
when(project.getConvention().getPlugin(JavaPluginConvention.class)).thenReturn(mock(JavaPluginConvention.class)); | ||
} | ||
|
||
@Test | ||
public void run_withImageConfiguration_shouldPushImage() throws JKubeServiceException { | ||
// Given | ||
final KubernetesPushTask kubernetesPushTask = new KubernetesPushTask(KubernetesExtension.class); | ||
|
||
// When | ||
kubernetesPushTask.runTask(); | ||
|
||
// Then | ||
assertThat(dockerBuildServiceMockedConstruction.constructed()).hasSize(1); | ||
verify(dockerBuildServiceMockedConstruction.constructed().iterator().next(), times(1)) | ||
.push(eq(images), eq(0), any(), eq(false)); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
defaultTaskMockedConstruction.close(); | ||
dockerBuildServiceMockedConstruction.close(); | ||
} | ||
} |