Search in sources :

Example 56 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class AssemblyManagerTest method assemblyFiles.

@Test
public void assemblyFiles(@Injectable final JKubeConfiguration configuration, @Injectable final JavaProject project) throws Exception {
    // Given
    final File buildDirs = temporaryFolder.newFolder("buildDirs");
    // @formatter:off
    new Expectations() {

        {
            configuration.getProject();
            result = project;
            project.getBaseDirectory();
            result = buildDirs;
            project.getBuildDirectory();
            result = targetDirectory;
        }
    };
    // @formatter:on
    ImageConfiguration imageConfiguration = ImageConfiguration.builder().name("testImage").build(createBuildConfig()).build();
    // When
    AssemblyFiles assemblyFiles = assemblyManager.getAssemblyFiles(imageConfiguration, configuration);
    // Then
    assertThat(assemblyFiles).isNotNull().hasFieldOrPropertyWithValue("assemblyDirectory", buildDirs.toPath().resolve("testImage").resolve("build").toFile()).extracting(AssemblyFiles::getUpdatedEntriesAndRefresh).asList().isEmpty();
}
Also used : Expectations(mockit.Expectations) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) File(java.io.File) Test(org.junit.Test)

Example 57 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class DockerFileUtilTest method testCreateSimpleDockerfileConfigWithPorts.

@Test
public void testCreateSimpleDockerfileConfigWithPorts() {
    // Given
    File dockerFile = new File(getClass().getResource("/docker/Dockerfile_expose_ports").getFile());
    // When
    ImageConfiguration imageConfiguration1 = DockerFileUtil.createSimpleDockerfileConfig(dockerFile, null);
    // Then
    assertNotNull(imageConfiguration1);
    assertEquals("%g/%a:%l", imageConfiguration1.getName());
    assertEquals(dockerFile.getPath(), imageConfiguration1.getBuild().getDockerFileRaw());
    assertNotNull(imageConfiguration1.getBuild().getPorts());
    assertEquals(5, imageConfiguration1.getBuild().getPorts().size());
    assertEquals("80/tcp", imageConfiguration1.getBuild().getPorts().get(0));
    assertEquals("8080/udp", imageConfiguration1.getBuild().getPorts().get(1));
    assertEquals("80", imageConfiguration1.getBuild().getPorts().get(2));
    assertEquals("8080", imageConfiguration1.getBuild().getPorts().get(3));
    assertEquals("99/udp", imageConfiguration1.getBuild().getPorts().get(4));
}
Also used : ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) File(java.io.File) Test(org.junit.Test)

Example 58 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class WatchService method watch.

public synchronized void watch(WatchContext context, JKubeConfiguration buildContext, List<ImageConfiguration> images) throws IOException {
    // Important to be be a single threaded scheduler since watch jobs must run serialized
    ScheduledExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadScheduledExecutor();
        for (ImageConfiguration imageConfig : runService.getImagesConfigsInOrder(queryService, images)) {
            String imageId = queryService.getImageId(imageConfig.getName());
            String containerId = runService.lookupContainer(imageConfig.getName());
            ImageWatcher watcher = new ImageWatcher(imageConfig, context, imageId, containerId);
            long interval = watcher.getInterval();
            WatchMode watchMode = watcher.getWatchMode(imageConfig);
            log.info("Watching %s %s", imageConfig.getName(), (watchMode != null ? " using " + watchMode.getDescription() : ""));
            ArrayList<String> tasks = new ArrayList<>();
            if (imageConfig.getBuildConfiguration() != null && imageConfig.getBuildConfiguration().getAssembly() != null) {
                if (watcher.isCopy()) {
                    schedule(executor, createCopyWatchTask(watcher, context.getBuildContext()), interval);
                    tasks.add("copying artifacts");
                }
                if (watcher.isBuild()) {
                    schedule(executor, createBuildWatchTask(watcher, context.getBuildContext(), watchMode == WatchMode.both, buildContext), interval);
                    tasks.add("rebuilding");
                }
            }
            if (watcher.isRun() && watcher.getContainerId() != null) {
                schedule(executor, createRestartWatchTask(watcher), interval);
                tasks.add("restarting");
            }
            if (!tasks.isEmpty()) {
                log.info("%s: Watch for %s", imageConfig.getDescription(), String.join(" and ", tasks));
            }
        }
        log.info("Waiting ...");
        if (!context.isKeepRunning()) {
            runService.addShutdownHookForStoppingContainers(context.isKeepContainer(), context.isRemoveVolumes(), context.isAutoCreateCustomNetworks());
        }
        wait();
    } catch (InterruptedException e) {
        log.warn("Interrupted");
        Thread.currentThread().interrupt();
    } finally {
        if (executor != null) {
            executor.shutdownNow();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) RunImageConfiguration(org.eclipse.jkube.kit.config.image.RunImageConfiguration) WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) ArrayList(java.util.ArrayList)

Example 59 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class WatchService method createBuildWatchTask.

Runnable createBuildWatchTask(final ImageWatcher watcher, final JKubeConfiguration mojoParameters, final boolean doRestart, final JKubeConfiguration buildContext) throws IOException {
    final ImageConfiguration imageConfig = watcher.getImageConfiguration();
    final AssemblyFiles files = archiveService.getAssemblyFiles(imageConfig, mojoParameters);
    if (files.isEmpty()) {
        log.error("No assembly files for %s. Are you sure you invoked together with the `package` goal?", imageConfig.getDescription());
        throw new IOException("No files to watch found for " + imageConfig);
    }
    return () -> {
        List<AssemblyFileEntry> entries = files.getUpdatedEntriesAndRefresh();
        if (entries != null && !entries.isEmpty()) {
            try {
                log.info("%s: Assembly changed. Rebuild ...", imageConfig.getDescription());
                if (watcher.getWatchContext().getImageCustomizer() != null) {
                    log.info("%s: Customizing the image ...", imageConfig.getDescription());
                    watcher.getWatchContext().getImageCustomizer().execute(imageConfig);
                }
                buildService.buildImage(imageConfig, null, buildContext);
                String name = imageConfig.getName();
                watcher.setImageId(queryService.getImageId(name));
                restartContainerAndCallPostGoal(watcher, doRestart);
            } catch (Exception e) {
                log.error("%s: Error when rebuilding - %s", imageConfig.getDescription(), e);
            }
        }
    };
}
Also used : AssemblyFiles(org.eclipse.jkube.kit.build.api.assembly.AssemblyFiles) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) RunImageConfiguration(org.eclipse.jkube.kit.config.image.RunImageConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) IOException(java.io.IOException) WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException)

Example 60 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class DockerImageWatcher method watch.

@Override
public void watch(List<ImageConfiguration> configs, String namespace, final Collection<HasMetadata> resources, PlatformMode mode) {
    WatchContext watchContext = getContext().getWatchContext();
    watchContext = watchContext.toBuilder().imageCustomizer(this::buildImage).containerRestarter(imageWatcher -> restartContainer(imageWatcher, resources)).containerCommandExecutor(command -> executeCommandInPod(command, resources)).containerCopyTask(f -> copyFileToPod(f, resources)).build();
    DockerServiceHub hub = getContext().getJKubeServiceHub().getDockerServiceHub();
    try {
        hub.getWatchService().watch(watchContext, getContext().getBuildContext(), configs);
    } catch (Exception ex) {
        throw new RuntimeException("Error while watching", ex);
    }
}
Also used : ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet) OpenshiftHelper(org.eclipse.jkube.kit.common.util.OpenshiftHelper) ClusterAccess(org.eclipse.jkube.kit.config.access.ClusterAccess) Container(io.fabric8.kubernetes.api.model.Container) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec) Date(java.util.Date) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) WatcherContext(org.eclipse.jkube.watcher.api.WatcherContext) PlatformMode(org.eclipse.jkube.kit.config.resource.PlatformMode) ReplicaSetSpec(io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec) PipedInputStream(java.io.PipedInputStream) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Duration(java.time.Duration) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ImageNameFormatter(org.eclipse.jkube.kit.build.service.docker.helper.ImageNameFormatter) BaseWatcher(org.eclipse.jkube.watcher.api.BaseWatcher) PrintWriter(java.io.PrintWriter) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Collection(java.util.Collection) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) IOException(java.io.IOException) KubernetesHelper(org.eclipse.jkube.kit.common.util.KubernetesHelper) FileInputStream(java.io.FileInputStream) PipedOutputStream(java.io.PipedOutputStream) WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File) DockerServiceHub(org.eclipse.jkube.kit.build.service.docker.DockerServiceHub) KitLogger(org.eclipse.jkube.kit.common.KitLogger) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) WatchService(org.eclipse.jkube.kit.build.service.docker.WatchService) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DockerServiceHub(org.eclipse.jkube.kit.build.service.docker.DockerServiceHub) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) WatchException(org.eclipse.jkube.kit.build.service.docker.watch.WatchException) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext)

Aggregations

ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)109 Test (org.junit.Test)75 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)29 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)28 File (java.io.File)26 ArrayList (java.util.ArrayList)19 Expectations (mockit.Expectations)15 GroupArtifactVersion (org.eclipse.jkube.kit.config.resource.GroupArtifactVersion)11 Before (org.junit.Before)11 IOException (java.io.IOException)9 Properties (java.util.Properties)8 KitLogger (org.eclipse.jkube.kit.common.KitLogger)8 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)7 JKubeServiceException (org.eclipse.jkube.kit.config.service.JKubeServiceException)7 WatchImageConfiguration (org.eclipse.jkube.kit.config.image.WatchImageConfiguration)6 ResourceConfig (org.eclipse.jkube.kit.config.resource.ResourceConfig)6 List (java.util.List)5 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)5 VolumeConfig (org.eclipse.jkube.kit.config.resource.VolumeConfig)5 Container (io.fabric8.kubernetes.api.model.Container)4