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();
}
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));
}
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();
}
}
}
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);
}
}
};
}
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);
}
}
Aggregations