Search in sources :

Example 31 with BuildConfiguration

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

the class BuildService method buildImage.

/**
 * Build an image
 *
 * @param imageConfig the image configuration
 * @param params mojo params for the project
 * @param noCache if not null, dictate the caching behaviour. Otherwise its taken from the build configuration
 * @param buildArgs maven build context
 * @throws DockerAccessException docker access exception
 * @throws IOException in case of any I/O exception
 */
protected void buildImage(ImageConfiguration imageConfig, JKubeConfiguration params, boolean noCache, Map<String, String> buildArgs) throws IOException {
    String imageName = imageConfig.getName();
    ImageName.validate(imageName);
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String oldImageId = null;
    CleanupMode cleanupMode = buildConfig.cleanupMode();
    if (cleanupMode.isRemove()) {
        oldImageId = queryService.getImageId(imageName);
    }
    long time = System.currentTimeMillis();
    if (buildConfig.getDockerArchive() != null) {
        docker.loadImage(imageName, buildConfig.getAbsoluteDockerTarPath(params.getSourceDirectory(), params.getProject().getBaseDirectory() != null ? params.getProject().getBaseDirectory().toString() : null));
        log.info("%s: Loaded tarball in %s", buildConfig.getDockerArchive(), EnvUtil.formatDurationTill(time));
        return;
    }
    File dockerArchive = archiveService.createArchive(imageName, buildConfig, params, log);
    log.info("%s: Created %s in %s", imageConfig.getDescription(), dockerArchive.getName(), EnvUtil.formatDurationTill(time));
    Map<String, String> mergedBuildMap = prepareBuildArgs(buildArgs, buildConfig);
    // auto is now supported by docker, consider switching?
    BuildOptions opts = new BuildOptions(buildConfig.getBuildOptions()).dockerfile(getDockerfileName(buildConfig)).forceRemove(cleanupMode.isRemove()).noCache(noCache).cacheFrom(buildConfig.getCacheFrom()).buildArgs(mergedBuildMap);
    String newImageId = doBuildImage(imageName, dockerArchive, opts);
    if (newImageId == null) {
        throw new IllegalStateException("Failure in building image, unable to find image built with name " + imageName);
    }
    log.info("%s: Built image %s", imageConfig.getDescription(), newImageId);
    if (oldImageId != null && !oldImageId.equals(newImageId)) {
        try {
            docker.removeImage(oldImageId, true);
            log.info("%s: Removed old image %s", imageConfig.getDescription(), oldImageId);
        } catch (DockerAccessException exp) {
            if (cleanupMode == CleanupMode.TRY_TO_REMOVE) {
                log.warn("%s: %s (old image)%s", imageConfig.getDescription(), exp.getMessage(), (exp.getCause() != null ? " [" + exp.getCause().getMessage() + "]" : ""));
            } else {
                throw exp;
            }
        }
    }
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) BuildOptions(org.eclipse.jkube.kit.build.service.docker.access.BuildOptions) DockerAccessException(org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException) CleanupMode(org.eclipse.jkube.kit.config.image.build.CleanupMode) File(java.io.File)

Example 32 with BuildConfiguration

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

the class RegistryService method pushImage.

/**
 * Push a set of images to a registry
 *
 * @param imageConfig image to push (but only if they have a build configuration)
 * @param retries how often to retry
 * @param registryConfig a global registry configuration
 * @param skipTag flag to skip pushing tagged images
 * @throws IOException exception
 */
public void pushImage(ImageConfiguration imageConfig, int retries, RegistryConfig registryConfig, boolean skipTag) throws IOException {
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    String name = imageConfig.getName();
    if (buildConfig != null) {
        String configuredRegistry = EnvUtil.firstRegistryOf(new ImageName(imageConfig.getName()).getRegistry(), imageConfig.getRegistry(), registryConfig.getRegistry());
        AuthConfig authConfig = createAuthConfig(true, new ImageName(name).getUser(), configuredRegistry, registryConfig);
        long start = System.currentTimeMillis();
        docker.pushImage(name, authConfig, configuredRegistry, retries);
        log.info("Pushed %s in %s", name, EnvUtil.formatDurationTill(start));
        if (!skipTag) {
            for (String tag : imageConfig.getBuildConfiguration().getTags()) {
                if (tag != null) {
                    docker.pushImage(new ImageName(name, tag).getFullName(), authConfig, configuredRegistry, retries);
                }
            }
        }
    }
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageName(org.eclipse.jkube.kit.config.image.ImageName) AuthConfig(org.eclipse.jkube.kit.build.api.auth.AuthConfig)

Example 33 with BuildConfiguration

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

the class AssemblyManagerTest method testCopyMultipleInvalidVerifyGivenDockerfile.

@Test
public void testCopyMultipleInvalidVerifyGivenDockerfile(@Injectable final KitLogger logger) throws IOException {
    BuildConfiguration buildConfig = createBuildConfig().toBuilder().assembly(AssemblyConfiguration.builder().name("other-layer").build()).build();
    AssemblyManager.verifyAssemblyReferencedInDockerfile(new File(getClass().getResource("/docker/Dockerfile_assembly_verify_copy_valid.test").getPath()), buildConfig, new Properties(), logger);
    // @formatter:off
    new Verifications() {

        {
            logger.warn(anyString, (Object[]) any);
            times = 1;
        }
    };
// @formatter:on
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) Properties(java.util.Properties) Verifications(mockit.Verifications) File(java.io.File) Test(org.junit.Test)

Example 34 with BuildConfiguration

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

the class AssemblyManagerTest method testCopyChownValidVerifyGivenDockerfile.

@Test
public void testCopyChownValidVerifyGivenDockerfile(@Injectable final KitLogger logger) throws IOException {
    BuildConfiguration buildConfig = createBuildConfig();
    AssemblyManager.verifyAssemblyReferencedInDockerfile(new File(getClass().getResource("/docker/Dockerfile_assembly_verify_copy_chown_valid.test").getPath()), buildConfig, new Properties(), logger);
    // @formatter:off
    new Verifications() {

        {
            logger.warn(anyString, (Object[]) any);
            times = 0;
        }
    };
// @formatter:on
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) Properties(java.util.Properties) Verifications(mockit.Verifications) File(java.io.File) Test(org.junit.Test)

Example 35 with BuildConfiguration

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

the class AssemblyManagerTest method testCopyMultipleValidVerifyGivenDockerfile.

@Test
public void testCopyMultipleValidVerifyGivenDockerfile(@Injectable final KitLogger logger) throws IOException {
    BuildConfiguration buildConfig = createBuildConfig().toBuilder().assembly(AssemblyConfiguration.builder().name("other-layer").build()).build();
    AssemblyManager.verifyAssemblyReferencedInDockerfile(new File(getClass().getResource("/docker/Dockerfile_assembly_verify_copy_multiple_valid.test").getPath()), buildConfig, new Properties(), logger);
    // @formatter:off
    new Verifications() {

        {
            logger.warn(anyString, (Object[]) any);
            times = 0;
        }
    };
// @formatter:on
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) Properties(java.util.Properties) Verifications(mockit.Verifications) File(java.io.File) Test(org.junit.Test)

Aggregations

BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)58 Test (org.junit.Test)39 ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)24 File (java.io.File)23 Properties (java.util.Properties)11 AssemblyFile (org.eclipse.jkube.kit.common.AssemblyFile)10 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)10 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)8 GroupArtifactVersion (org.eclipse.jkube.kit.config.resource.GroupArtifactVersion)8 Before (org.junit.Before)8 AssemblyConfiguration (org.eclipse.jkube.kit.common.AssemblyConfiguration)7 ArrayList (java.util.ArrayList)6 Expectations (mockit.Expectations)5 VolumeConfig (org.eclipse.jkube.kit.config.resource.VolumeConfig)5 Verifications (mockit.Verifications)4 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assembly (org.eclipse.jkube.kit.common.Assembly)3 AssemblyFileEntry (org.eclipse.jkube.kit.common.AssemblyFileEntry)3 JavaProject (org.eclipse.jkube.kit.common.JavaProject)3