Search in sources :

Example 1 with JKubeServiceHub

use of org.eclipse.jkube.kit.config.service.JKubeServiceHub in project jkube by eclipse.

the class AbstractDockerMojo method doExecute.

protected void doExecute() throws MojoExecutionException {
    final boolean ansiRestore = Ansi.isEnabled();
    try {
        DockerAccess dockerAccess = null;
        try {
            javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session);
            resources = updateResourceConfigNamespace(namespace, resources);
            // The 'real' images configuration to use (configured images + externally resolved images)
            if (isDockerAccessRequired()) {
                DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
                dockerAccess = dockerAccessFactory.createDockerAccess(dockerAccessContext);
            }
            jkubeServiceHub = JKubeServiceHub.builder().log(log).configuration(initJKubeConfiguration()).clusterAccess(clusterAccess).platformMode(getConfiguredRuntimeMode()).dockerServiceHub(DockerServiceHub.newInstance(log, dockerAccess, logOutputSpecFactory)).buildServiceConfig(buildServiceConfigBuilder().build()).offline(offline).build();
            resolvedImages = ConfigHelper.initImageConfiguration(apiVersion, getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP), images, imageConfigResolver, log, filter, this, jkubeServiceHub.getConfiguration());
            executeInternal();
        } catch (IOException | DependencyResolutionRequiredException exp) {
            logException(exp);
            throw new MojoExecutionException(exp.getMessage());
        } catch (MojoExecutionException exp) {
            logException(exp);
            throw exp;
        } finally {
            Optional.ofNullable(jkubeServiceHub).ifPresent(JKubeServiceHub::close);
        }
    } finally {
        Ansi.setEnabled(ansiRestore);
    }
}
Also used : DockerAccess(org.eclipse.jkube.kit.build.service.docker.access.DockerAccess) DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) DockerAccessFactory(org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub)

Example 2 with JKubeServiceHub

use of org.eclipse.jkube.kit.config.service.JKubeServiceHub in project jkube by eclipse.

the class OpenShiftResourceMojoTest method setUp.

@Before
public void setUp() {
    mockedClusterAccess = mock(ClusterAccess.class, RETURNS_DEEP_STUBS);
    mockedImageConfigResolver = mock(ImageConfigResolver.class, RETURNS_DEEP_STUBS);
    MavenProject mockedMavenProject = mock(MavenProject.class, RETURNS_DEEP_STUBS);
    JavaProject mockedJavaProject = mock(JavaProject.class, RETURNS_DEEP_STUBS);
    JKubeServiceHub mockedJKubeServiceHub = mock(JKubeServiceHub.class, RETURNS_DEEP_STUBS);
    Properties properties = new Properties();
    this.openShiftResourceMojo = new OpenshiftResourceMojo();
    this.openShiftResourceMojo.project = mockedMavenProject;
    this.openShiftResourceMojo.settings = mock(Settings.class, RETURNS_DEEP_STUBS);
    this.openShiftResourceMojo.jkubeServiceHub = mockedJKubeServiceHub;
    this.openShiftResourceMojo.clusterAccess = mockedClusterAccess;
    this.openShiftResourceMojo.log = mock(KitLogger.class, RETURNS_DEEP_STUBS);
    this.openShiftResourceMojo.skipResourceValidation = true;
    this.openShiftResourceMojo.projectHelper = mock(MavenProjectHelper.class, RETURNS_DEEP_STUBS);
    this.openShiftResourceMojo.imageConfigResolver = mockedImageConfigResolver;
    this.openShiftResourceMojo.javaProject = mockedJavaProject;
    when(mockedMavenProject.getProperties()).thenReturn(properties);
    when(mockedJavaProject.getArtifactId()).thenReturn("test-project");
    when(mockedJavaProject.getGroupId()).thenReturn("org.eclipse.jkube");
    when(mockedJavaProject.getProperties()).thenReturn(properties);
    when(mockedJKubeServiceHub.getConfiguration().getProject()).thenReturn(mockedJavaProject);
    when(mockedJKubeServiceHub.getConfiguration().getBasedir()).thenReturn(temporaryFolder.getRoot());
}
Also used : KitLogger(org.eclipse.jkube.kit.common.KitLogger) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) JavaProject(org.eclipse.jkube.kit.common.JavaProject) MavenProject(org.apache.maven.project.MavenProject) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) ImageConfigResolver(org.eclipse.jkube.kit.build.service.docker.config.handler.ImageConfigResolver) Properties(java.util.Properties) Settings(org.apache.maven.settings.Settings) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Before(org.junit.Before)

Example 3 with JKubeServiceHub

use of org.eclipse.jkube.kit.config.service.JKubeServiceHub in project jkube by eclipse.

the class OpenshiftBuildServiceIntegrationTest method init.

@Before
public void init() throws Exception {
    final KitLogger logger = new KitLogger.StdoutLogger();
    baseDirectory = temporaryFolder.newFolder("openshift-build-service");
    target = temporaryFolder.newFolder("openshift-build-service", "target");
    final File emptyDockerBuildTar = new File(target, "docker-build.tar");
    FileUtils.touch(emptyDockerBuildTar);
    baseDir = baseDirectory.getAbsolutePath();
    projectName = "myapp";
    FileUtils.deleteDirectory(new File(baseDir, projectName));
    final File dockerFile = new File(baseDir, "Docker.tar");
    FileUtils.touch(dockerFile);
    openshiftHelper = mockStatic(OpenshiftHelper.class);
    openshiftHelper.when(() -> OpenshiftHelper.isOpenShift(eq(mockServer.getOpenshiftClient()))).thenReturn(true);
    jKubeBuildTarArchiver = mockConstruction(JKubeBuildTarArchiver.class, (mock, ctx) -> when(mock.createArchive(any(File.class), any(BuildDirs.class), eq(ArchiveCompression.none))).thenReturn(emptyDockerBuildTar));
    mockedResourceConfig = mock(ResourceConfig.class, RETURNS_DEEP_STUBS);
    when(mockedResourceConfig.getNamespace()).thenReturn("ns1");
    jKubeServiceHub = mock(JKubeServiceHub.class, RETURNS_DEEP_STUBS);
    when(jKubeServiceHub.getClusterAccess().createDefaultClient()).thenReturn(mockServer.getOpenshiftClient());
    when(jKubeServiceHub.getLog()).thenReturn(logger);
    when(jKubeServiceHub.getDockerServiceHub().getArchiveService()).thenReturn(new ArchiveService(AssemblyManager.getInstance(), logger));
    when(jKubeServiceHub.getBuildServiceConfig().getBuildDirectory()).thenReturn(target.getAbsolutePath());
    when(jKubeServiceHub.getConfiguration()).thenReturn(JKubeConfiguration.builder().outputDirectory(target.getAbsolutePath()).project(JavaProject.builder().baseDirectory(baseDirectory).buildDirectory(target).build()).registryConfig(RegistryConfig.builder().build()).build());
    image = ImageConfiguration.builder().name(projectName).build(BuildConfiguration.builder().from(projectName).build()).build();
    defaultConfig = BuildServiceConfig.builder().buildDirectory(baseDir).buildRecreateMode(BuildRecreateMode.none).s2iBuildNameSuffix("-s2i-suffix2").resourceConfig(mockedResourceConfig).jKubeBuildStrategy(JKubeBuildStrategy.s2i);
    defaultConfigSecret = defaultConfig.build().toBuilder().openshiftPullSecret("pullsecret-fabric8");
    dockerImageConfig = defaultConfig.build().toBuilder().buildOutputKind("DockerImage");
    dockerImageConfigSecret = defaultConfig.build().toBuilder().openshiftPullSecret("pullsecret-fabric8").openshiftPushSecret("pushsecret-fabric8").buildOutputKind("DockerImage");
    Serialization.jsonMapper().disable(SerializationFeature.INDENT_OUTPUT);
}
Also used : BuildServiceConfig(org.eclipse.jkube.kit.config.service.BuildServiceConfig) Quantity(io.fabric8.kubernetes.api.model.Quantity) BuildStrategyBuilder(io.fabric8.openshift.api.model.BuildStrategyBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) AssemblyManager(org.eclipse.jkube.kit.build.api.assembly.AssemblyManager) Map(java.util.Map) After(org.junit.After) Mockito.mockConstruction(org.mockito.Mockito.mockConstruction) Build(io.fabric8.openshift.api.model.Build) MockedConstruction(org.mockito.MockedConstruction) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) ImmutableMap(com.google.common.collect.ImmutableMap) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) BuildRecreateMode(org.eclipse.jkube.kit.config.resource.BuildRecreateMode) RegistryConfig(org.eclipse.jkube.kit.common.RegistryConfig) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) StandardCharsets(java.nio.charset.StandardCharsets) MockedStatic(org.mockito.MockedStatic) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) JKubeServiceException(org.eclipse.jkube.kit.config.service.JKubeServiceException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) JKubeBuildStrategy(org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) Assert.assertThrows(org.junit.Assert.assertThrows) Mockito.mockStatic(org.mockito.Mockito.mockStatic) HashMap(java.util.HashMap) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) Serialization(io.fabric8.kubernetes.client.utils.Serialization) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) RETURNS_DEEP_STUBS(org.mockito.Mockito.RETURNS_DEEP_STUBS) JavaProject(org.eclipse.jkube.kit.common.JavaProject) ImageStream(io.fabric8.openshift.api.model.ImageStream) Before(org.junit.Before) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) JKubeBuildTarArchiver(org.eclipse.jkube.kit.build.api.assembly.JKubeBuildTarArchiver) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Assert.assertTrue(org.junit.Assert.assertTrue) ArchiveService(org.eclipse.jkube.kit.build.service.docker.ArchiveService) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) File(java.io.File) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Rule(org.junit.Rule) OpenShiftServer(io.fabric8.openshift.client.server.mock.OpenShiftServer) Assembly(org.eclipse.jkube.kit.common.Assembly) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) OpenshiftBuildConfig(org.eclipse.jkube.kit.config.resource.OpenshiftBuildConfig) TemporaryFolder(org.junit.rules.TemporaryFolder) Assert.assertEquals(org.junit.Assert.assertEquals) KitLogger(org.eclipse.jkube.kit.common.KitLogger) ArchiveService(org.eclipse.jkube.kit.build.service.docker.ArchiveService) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) JKubeBuildTarArchiver(org.eclipse.jkube.kit.build.api.assembly.JKubeBuildTarArchiver) ResourceConfig(org.eclipse.jkube.kit.config.resource.ResourceConfig) File(java.io.File) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub) Before(org.junit.Before)

Example 4 with JKubeServiceHub

use of org.eclipse.jkube.kit.config.service.JKubeServiceHub in project jkube by eclipse.

the class Main method main.

public static void main(String[] args) {
    final KitLogger kitLogger = new KitLogger.StdoutLogger();
    kitLogger.info("Initiating default JKube configuration and required services...");
    kitLogger.info(" - Creating Docker Build Service Configuration");
    final BuildServiceConfig dockerBuildServiceConfig = BuildServiceConfig.builder().build();
    kitLogger.info(" - Creating configuration for JKube");
    kitLogger.info(" - Current working directory is: %s", getProjectDir().toFile().toString());
    final JKubeConfiguration configuration = JKubeConfiguration.builder().project(JavaProject.builder().baseDirectory(getProjectDir().toFile()).build()).outputDirectory("target").build();
    kitLogger.info("Creating configuration for example Docker Image");
    final ImageConfiguration imageConfiguration = ImageConfiguration.builder().name("jkube-example").run(RunImageConfiguration.builder().user("1000").build()).build(BuildConfiguration.builder().putEnv("MY_VAR", "value").putEnv("MY_OTHER_VAR", "true").label("maintainer", "JKube Devs").port("80/tcp").maintainer("JKube Devs").from("busybox").assembly(AssemblyConfiguration.builder().targetDir("/").layer(Assembly.builder().fileSet(AssemblyFileSet.builder().directory(new File("static")).build()).build()).build()).cmd(Arguments.builder().shell("/bin/sh").build()).build()).build();
    try (JKubeServiceHub jKubeServiceHub = JKubeServiceHub.builder().log(kitLogger).configuration(configuration).platformMode(RuntimeMode.KUBERNETES).dockerServiceHub(DockerServiceHub.newInstance(kitLogger)).buildServiceConfig(dockerBuildServiceConfig).build()) {
        jKubeServiceHub.getBuildService().build(imageConfiguration);
        final String imageId = jKubeServiceHub.getDockerServiceHub().getDockerAccess().getImageId("jkube-example");
        kitLogger.info("Docker image built successfully (%s)!", imageId);
        System.exit(0);
    } catch (Exception ex) {
        kitLogger.error("Error occurred: '%s'", ex.getMessage());
    }
}
Also used : BuildServiceConfig(org.eclipse.jkube.kit.config.service.BuildServiceConfig) KitLogger(org.eclipse.jkube.kit.common.KitLogger) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) RunImageConfiguration(org.eclipse.jkube.kit.config.image.RunImageConfiguration) File(java.io.File) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub)

Example 5 with JKubeServiceHub

use of org.eclipse.jkube.kit.config.service.JKubeServiceHub in project jkube by eclipse.

the class Main method main.

public static void main(String[] args) {
    final KitLogger kitLogger = new KitLogger.StdoutLogger();
    kitLogger.info("Initiating default JKube configuration and required services...");
    kitLogger.info(" - Creating Docker Service Hub");
    final DockerServiceHub dockerServiceHub = DockerServiceHub.newInstance(kitLogger);
    kitLogger.info(" - Creating Docker Build Service Configuration");
    final BuildServiceConfig dockerBuildServiceConfig = BuildServiceConfig.builder().build();
    kitLogger.info(" - Creating configuration for JKube");
    kitLogger.info(" - Current working directory is: %s", getProjectDir().toFile().toString());
    final JKubeConfiguration configuration = JKubeConfiguration.builder().project(JavaProject.builder().baseDirectory(getProjectDir().toFile()).build()).outputDirectory("target").sourceDirectory(getProjectDir().toFile().getAbsolutePath()).build();
    JKubeServiceHub.JKubeServiceHubBuilder buildBuilder = JKubeServiceHub.builder().log(kitLogger).configuration(configuration).platformMode(RuntimeMode.KUBERNETES).dockerServiceHub(dockerServiceHub).buildServiceConfig(dockerBuildServiceConfig);
    try (JKubeServiceHub jKubeServiceHub = buildBuilder.build()) {
        jKubeServiceHub.getBuildService().build(new DynamicDockerfileGenerator(kitLogger, getProjectDir(), IMAGE_NAME).generateImageConfiguration());
        final String imageId = jKubeServiceHub.getDockerServiceHub().getDockerAccess().getImageId(IMAGE_NAME);
        kitLogger.info("Docker image built successfully (%s)!", imageId);
        System.exit(0);
    } catch (Exception ex) {
        ex.printStackTrace();
        kitLogger.error("Error occurred: '%s'", ex.getMessage());
    }
}
Also used : BuildServiceConfig(org.eclipse.jkube.kit.config.service.BuildServiceConfig) KitLogger(org.eclipse.jkube.kit.common.KitLogger) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) DockerServiceHub(org.eclipse.jkube.kit.build.service.docker.DockerServiceHub) JKubeServiceHub(org.eclipse.jkube.kit.config.service.JKubeServiceHub)

Aggregations

JKubeServiceHub (org.eclipse.jkube.kit.config.service.JKubeServiceHub)5 KitLogger (org.eclipse.jkube.kit.common.KitLogger)4 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)3 BuildServiceConfig (org.eclipse.jkube.kit.config.service.BuildServiceConfig)3 File (java.io.File)2 JavaProject (org.eclipse.jkube.kit.common.JavaProject)2 ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)2 Before (org.junit.Before)2 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)1 Serialization (io.fabric8.kubernetes.client.utils.Serialization)1 Build (io.fabric8.openshift.api.model.Build)1 BuildBuilder (io.fabric8.openshift.api.model.BuildBuilder)1 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)1 BuildStrategyBuilder (io.fabric8.openshift.api.model.BuildStrategyBuilder)1