Search in sources :

Example 1 with WatchContext

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchContext in project jkube by eclipse.

the class WatchServiceTest method testRestartContainerAndCallPostGoalRestartEnabled.

@Test
public void testRestartContainerAndCallPostGoalRestartEnabled() throws Exception {
    // Given
    AtomicBoolean restarted = new AtomicBoolean(false);
    WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.both).containerRestarter(// Override Restart task to set this value to true
    i -> restarted.set(true)).build();
    WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
    WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
    // When
    watchService.restartContainerAndCallPostGoal(imageWatcher, true);
    // Then
    assertTrue(restarted.get());
}
Also used : WatchMode(org.eclipse.jkube.kit.config.image.WatchMode) Files(java.nio.file.Files) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) WatchImageConfiguration(org.eclipse.jkube.kit.config.image.WatchImageConfiguration) KitLogger(org.eclipse.jkube.kit.common.KitLogger) Mocked(mockit.Mocked) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Test(org.junit.Test)

Example 2 with WatchContext

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchContext in project jkube by eclipse.

the class WatchServiceTest method testRestartContainerAndCallPostGoalRestartDisabled.

@Test
public void testRestartContainerAndCallPostGoalRestartDisabled() throws Exception {
    // Given
    AtomicReference<String> stringAtomicReference = new AtomicReference<>("oldVal");
    String mavenGoalToExecute = "org.apache.maven.plugins:maven-help-plugin:help";
    WatchContext watchContext = WatchContext.builder().watchMode(WatchMode.both).postGoalTask(() -> stringAtomicReference.compareAndSet("oldVal", mavenGoalToExecute)).build();
    WatchService.ImageWatcher imageWatcher = new WatchService.ImageWatcher(imageConfiguration, watchContext, "test-img", "efe1234");
    WatchService watchService = new WatchService(archiveService, buildService, queryService, runService, logger);
    // When
    watchService.restartContainerAndCallPostGoal(imageWatcher, false);
    // Then
    assertEquals(mavenGoalToExecute, stringAtomicReference.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Test(org.junit.Test)

Example 3 with WatchContext

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchContext 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)

Example 4 with WatchContext

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchContext in project jkube by eclipse.

the class DockerImageWatcherTest method setUp.

@Before
public void setUp() {
    dockerImageWatcher = new DockerImageWatcher(watcherContext);
    // @formatter:off
    new Expectations() {

        {
            watcherContext.getWatchContext();
            result = new WatchContext();
            minTimes = 0;
        }
    };
    // @formatter:on
    new MockUp<WatchService>() {

        @Mock
        void watch(WatchContext context, JKubeConfiguration buildContext, List<ImageConfiguration> images) {
            watchContext = context;
        }
    };
}
Also used : Expectations(mockit.Expectations) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) MockUp(mockit.MockUp) List(java.util.List) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext) Before(org.junit.Before)

Example 5 with WatchContext

use of org.eclipse.jkube.kit.build.service.docker.watch.WatchContext in project jkube by eclipse.

the class WatchMojo method getWatcherContext.

private WatcherContext getWatcherContext() throws MojoExecutionException {
    try {
        JKubeConfiguration buildContext = initJKubeConfiguration();
        WatchContext watchContext = jkubeServiceHub.getDockerServiceHub() != null ? getWatchContext() : null;
        return WatcherContext.builder().buildContext(buildContext).watchContext(watchContext).config(extractWatcherConfig()).logger(log).newPodLogger(createLogger("[[C]][NEW][[C]] ")).oldPodLogger(createLogger("[[R]][OLD][[R]] ")).useProjectClasspath(useProjectClasspath).jKubeServiceHub(jkubeServiceHub).build();
    } catch (DependencyResolutionRequiredException dependencyException) {
        throw new MojoExecutionException("Instructed to use project classpath, but cannot. Continuing build if we can: " + dependencyException.getMessage());
    } catch (IOException ioException) {
        throw new MojoExecutionException(ioException.getMessage());
    }
}
Also used : DependencyResolutionRequiredException(org.apache.maven.artifact.DependencyResolutionRequiredException) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) WatchContext(org.eclipse.jkube.kit.build.service.docker.watch.WatchContext)

Aggregations

WatchContext (org.eclipse.jkube.kit.build.service.docker.watch.WatchContext)7 File (java.io.File)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 KitLogger (org.eclipse.jkube.kit.common.KitLogger)4 ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)4 Before (org.junit.Before)4 Test (org.junit.Test)4 Files (java.nio.file.Files)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Mocked (mockit.Mocked)3 WatchImageConfiguration (org.eclipse.jkube.kit.config.image.WatchImageConfiguration)3 WatchMode (org.eclipse.jkube.kit.config.image.WatchMode)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 IOException (java.io.IOException)2 List (java.util.List)2 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)2 Container (io.fabric8.kubernetes.api.model.Container)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1