Search in sources :

Example 1 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class AssemblyManager method createDockerTarArchive.

/**
 * Create a docker tar archive from the given configuration which can be sent to the Docker host for
 * creating the image.
 *
 * @param imageName Name of the image to create (used for creating build directories)
 * @param configuration Mojos parameters (used for finding the directories)
 * @param buildConfig configuration for how to build the image
 * @param log KitLogger used to display warning if permissions are to be normalized
 * @param finalCustomizer finalCustomizer to be applied to the tar archive
 * @return file holding the path to the created assembly tar file
 * @throws IOException IO exception
 */
public File createDockerTarArchive(String imageName, final JKubeConfiguration configuration, final BuildConfiguration buildConfig, KitLogger log, ArchiverCustomizer finalCustomizer) throws IOException {
    final BuildDirs buildDirs = createBuildDirs(imageName, configuration);
    final List<ArchiverCustomizer> archiveCustomizers = new ArrayList<>();
    final AssemblyConfiguration assemblyConfig = getAssemblyConfiguration(buildConfig, configuration);
    final Map<Assembly, List<AssemblyFileEntry>> layers = copyFilesToFinalTarballDirectory(configuration, buildDirs, assemblyConfig);
    try {
        if (buildConfig.isDockerFileMode()) {
            createDockerTarArchiveForDockerFile(buildConfig, assemblyConfig, configuration, buildDirs, log, archiveCustomizers);
        } else {
            createAssemblyArchive(assemblyConfig, configuration, buildDirs, buildConfig.getCompression(), layers);
            createDockerTarArchiveForGeneratorMode(buildConfig, buildDirs, archiveCustomizers, assemblyConfig, layers);
        }
        archiveCustomizers.addAll(getDefaultCustomizers(configuration, assemblyConfig, finalCustomizer, layers));
        return createBuildTarBall(configuration, buildDirs, archiveCustomizers, assemblyConfig, buildConfig.getCompression());
    } catch (IOException e) {
        throw new IOException(String.format("Cannot create %s in %s", DOCKERFILE_NAME, buildDirs.getOutputDirectory()), e);
    }
}
Also used : AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Assembly(org.eclipse.jkube.kit.common.Assembly)

Example 2 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class AssemblyManager method createAssemblyArchive.

private void createAssemblyArchive(AssemblyConfiguration assemblyConfig, JKubeConfiguration params, BuildDirs buildDirs, ArchiveCompression compression, Map<Assembly, List<AssemblyFileEntry>> layers) throws IOException {
    if (layers.isEmpty()) {
        return;
    }
    AssemblyConfigurationSource source = new AssemblyConfigurationSource(params, buildDirs, assemblyConfig);
    JKubeBuildTarArchiver jkubeTarArchiver = new JKubeBuildTarArchiver();
    AssemblyMode buildMode = assemblyConfig.getMode();
    try {
        layers.values().stream().flatMap(List::stream).filter(afe -> StringUtils.isNotBlank(afe.getFileMode())).forEach(jkubeTarArchiver::setFileMode);
        jkubeTarArchiver.createArchive(source.getOutputDirectory(), buildDirs, compression);
    } catch (IOException e) {
        String error = "Failed to create assembly for docker image " + " (with mode '" + buildMode + "'): " + e.getMessage() + ".";
        if (params.getProject().getArtifact() == null) {
            error += " If you include the build artifact please ensure that you have " + "built the artifact before with 'mvn package' (should be available in the target/ dir). " + "Please see the documentation (section \"Assembly\") for more information.";
        }
        throw new IOException(error, e);
    }
}
Also used : JKubeProjectUtil(org.eclipse.jkube.kit.common.util.JKubeProjectUtil) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) AssemblyConfigurationUtils.createDockerFileBuilder(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder) DockerFileUtil(org.eclipse.jkube.kit.build.api.helper.DockerFileUtil) StringUtils(org.apache.commons.lang3.StringUtils) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) LinkedHashMap(java.util.LinkedHashMap) AssemblyConfigurationUtils.getJKubeAssemblyFiles(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) Nonnull(javax.annotation.Nonnull) JavaProject(org.eclipse.jkube.kit.common.JavaProject) DockerFileBuilder(org.eclipse.jkube.kit.config.image.build.DockerFileBuilder) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Properties(java.util.Properties) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) Files(java.nio.file.Files) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) BufferedWriter(java.io.BufferedWriter) AssemblyFileUtils.getAssemblyFileOutputDirectory(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.getAssemblyFileOutputDirectory) FileWriter(java.io.FileWriter) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) IOException(java.io.IOException) AssemblyConfigurationUtils.getJKubeAssemblyFileSets(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets) Collectors(java.util.stream.Collectors) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault) KitLogger(org.eclipse.jkube.kit.common.KitLogger) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assembly(org.eclipse.jkube.kit.common.Assembly) JKubeTarArchiver(org.eclipse.jkube.kit.common.archive.JKubeTarArchiver) Optional(java.util.Optional) AssemblyFileSetUtils.processAssemblyFileSet(org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 3 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class AssemblyManager method copyFilesToFinalTarballDirectory.

public Map<Assembly, List<AssemblyFileEntry>> copyFilesToFinalTarballDirectory(JKubeConfiguration configuration, BuildDirs buildDirs, AssemblyConfiguration assemblyConfiguration) throws IOException {
    final Map<Assembly, List<AssemblyFileEntry>> entries = new LinkedHashMap<>();
    FileUtil.createDirectory(new File(buildDirs.getOutputDirectory(), assemblyConfiguration.getTargetDir()));
    final List<Assembly> layers = assemblyConfiguration.getProcessedLayers(configuration);
    if (layers.size() > 1 && layers.stream().anyMatch(l -> StringUtils.isBlank(l.getId()))) {
        throw new IllegalStateException("Assemblies with more than one layer require a proper id for each layer");
    }
    for (Assembly layer : layers) {
        entries.put(layer, copyLayerFilesToFinalTarballDirectory(configuration.getProject(), buildDirs, assemblyConfiguration, layer));
    }
    return entries;
}
Also used : JKubeProjectUtil(org.eclipse.jkube.kit.common.util.JKubeProjectUtil) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) AssemblyConfigurationUtils.createDockerFileBuilder(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder) DockerFileUtil(org.eclipse.jkube.kit.build.api.helper.DockerFileUtil) StringUtils(org.apache.commons.lang3.StringUtils) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) LinkedHashMap(java.util.LinkedHashMap) AssemblyConfigurationUtils.getJKubeAssemblyFiles(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles) JKubeConfiguration(org.eclipse.jkube.kit.common.JKubeConfiguration) Map(java.util.Map) FileUtil(org.eclipse.jkube.kit.common.util.FileUtil) AssemblyMode(org.eclipse.jkube.kit.common.AssemblyMode) Nonnull(javax.annotation.Nonnull) JavaProject(org.eclipse.jkube.kit.common.JavaProject) DockerFileBuilder(org.eclipse.jkube.kit.config.image.build.DockerFileBuilder) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) Properties(java.util.Properties) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) Files(java.nio.file.Files) ArchiveCompression(org.eclipse.jkube.kit.common.archive.ArchiveCompression) BufferedWriter(java.io.BufferedWriter) AssemblyFileUtils.getAssemblyFileOutputDirectory(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.getAssemblyFileOutputDirectory) FileWriter(java.io.FileWriter) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) IOException(java.io.IOException) AssemblyConfigurationUtils.getJKubeAssemblyFileSets(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets) Collectors(java.util.stream.Collectors) File(java.io.File) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault(org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault) KitLogger(org.eclipse.jkube.kit.common.KitLogger) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assembly(org.eclipse.jkube.kit.common.Assembly) JKubeTarArchiver(org.eclipse.jkube.kit.common.archive.JKubeTarArchiver) Optional(java.util.Optional) AssemblyFileSetUtils.processAssemblyFileSet(org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet) ArrayList(java.util.ArrayList) List(java.util.List) AssemblyFileUtils.resolveSourceFile(org.eclipse.jkube.kit.common.archive.AssemblyFileUtils.resolveSourceFile) AssemblyFile(org.eclipse.jkube.kit.common.AssemblyFile) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class AssemblyConfigurationUtilsTest method getJKubeAssemblyFileSetsNullFileSetsShouldReturnEmptyList.

@Test
public void getJKubeAssemblyFileSetsNullFileSetsShouldReturnEmptyList() {
    // Given
    final Assembly assembly = new Assembly();
    // When
    final List<AssemblyFileSet> result = getJKubeAssemblyFileSets(assembly);
    // Then
    assertThat(result).isNotNull().isEmpty();
}
Also used : AssemblyFileSet(org.eclipse.jkube.kit.common.AssemblyFileSet) Assembly(org.eclipse.jkube.kit.common.Assembly) Test(org.junit.Test)

Example 5 with Assembly

use of org.eclipse.jkube.kit.common.Assembly in project jkube by eclipse.

the class JibBuildService method buildSingleImage.

@Override
public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeServiceException {
    try {
        log.info("[[B]]JIB[[B]] image build started");
        if (imageConfig.getBuildConfiguration().isDockerFileMode()) {
            throw new JKubeServiceException("Dockerfile mode is not supported with JIB build strategy");
        }
        prependRegistry(imageConfig, configuration.getProperties().getProperty(PUSH_REGISTRY));
        BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), configuration);
        final Credential pullRegistryCredential = getRegistryCredentials(configuration.getRegistryConfig(), false, imageConfig, log);
        final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfig, pullRegistryCredential);
        final Map<Assembly, List<AssemblyFileEntry>> layers = AssemblyManager.getInstance().copyFilesToFinalTarballDirectory(configuration, buildDirs, AssemblyManager.getAssemblyConfiguration(imageConfig.getBuildConfiguration(), configuration));
        JibServiceUtil.layers(buildDirs, layers).forEach(containerBuilder::addFileEntriesLayer);
        // TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
        // the call to AssemblyManager.getInstance().createDockerTarArchive should not be necessary,
        // files should be added using the AssemblyFileEntry list. AssemblyManager, should provide
        // a common way to achieve this so that both the tar builder and any other builder could get a hold of
        // archive customizers, file entries, etc.
        File dockerTarArchive = getAssemblyTarArchive(imageConfig, configuration, log);
        JibServiceUtil.buildContainer(containerBuilder, TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), log);
        log.info(" %s successfully built", dockerTarArchive.getAbsolutePath());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    } catch (Exception ex) {
        throw new JKubeServiceException("Error when building JIB image", ex);
    }
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) List(java.util.List) JKubeServiceException(org.eclipse.jkube.kit.config.service.JKubeServiceException) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) BuildDirs(org.eclipse.jkube.kit.build.api.assembly.BuildDirs) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) IOException(java.io.IOException) JKubeServiceException(org.eclipse.jkube.kit.config.service.JKubeServiceException)

Aggregations

Assembly (org.eclipse.jkube.kit.common.Assembly)36 AssemblyConfiguration (org.eclipse.jkube.kit.common.AssemblyConfiguration)31 File (java.io.File)29 Test (org.junit.Test)29 AssemblyFileSet (org.eclipse.jkube.kit.common.AssemblyFileSet)22 AssemblyFileEntry (org.eclipse.jkube.kit.common.AssemblyFileEntry)21 AssemblyFileSetUtils.processAssemblyFileSet (org.eclipse.jkube.kit.common.archive.AssemblyFileSetUtils.processAssemblyFileSet)18 AssemblyFile (org.eclipse.jkube.kit.common.AssemblyFile)12 List (java.util.List)9 Map (java.util.Map)7 IOException (java.io.IOException)6 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)6 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 Nonnull (javax.annotation.Nonnull)5 AssemblyConfigurationUtils.createDockerFileBuilder (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.createDockerFileBuilder)4 AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getAssemblyConfigurationOrCreateDefault)4 AssemblyConfigurationUtils.getJKubeAssemblyFileSets (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFileSets)4 AssemblyConfigurationUtils.getJKubeAssemblyFiles (org.eclipse.jkube.kit.build.api.assembly.AssemblyConfigurationUtils.getJKubeAssemblyFiles)4