Search in sources :

Example 1 with Credential

use of com.google.cloud.tools.jib.api.Credential in project docker-maven-plugin by fabric8io.

the class JibBuildService method build.

public void build(String jibImageFormat, ImageConfiguration imageConfig, RegistryService.RegistryConfig registryConfig) throws MojoExecutionException {
    try {
        log.info("[[B]]JIB[[B]] image build started");
        if (imageConfig.getBuildConfiguration().isDockerFileMode()) {
            throw new MojoExecutionException("Dockerfile mode is not supported with JIB build strategy");
        }
        prependRegistry(imageConfig, mojoParameters.getProject().getProperties().getProperty(PUSH_REGISTRY));
        BuildDirs buildDirs = new BuildDirs(imageConfig.getName(), mojoParameters);
        final Credential pullRegistryCredential = getRegistryCredentials(registryConfig, false, imageConfig, log);
        final JibContainerBuilder containerBuilder = containerFromImageConfiguration(jibImageFormat, imageConfig, pullRegistryCredential);
        File dockerTarArchive = getAssemblyTarArchive(imageConfig, serviceHub, mojoParameters, log);
        for (AssemblyConfiguration assemblyConfiguration : imageConfig.getBuildConfiguration().getAssemblyConfigurations()) {
            // TODO: Improve Assembly Manager so that the effective assemblyFileEntries computed can be properly shared
            // the call to DockerAssemblyManager.getInstance().createDockerTarArchive should not be necessary,
            // files should be added using the AssemblyFileEntry list. DockerAssemblyManager, 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.
            AssemblyFiles assemblyFiles = serviceHub.getDockerAssemblyManager().getAssemblyFiles(imageConfig.getName(), assemblyConfiguration, mojoParameters, log);
            final Map<File, AssemblyFiles.Entry> files = assemblyFiles.getUpdatedEntriesAndRefresh().stream().collect(Collectors.toMap(AssemblyFiles.Entry::getDestFile, Function.identity(), (oldV, newV) -> newV));
            JibServiceUtil.copyToContainer(containerBuilder, buildDirs.getOutputDirectory(), buildDirs.getOutputDirectory().getAbsolutePath(), files);
        }
        JibServiceUtil.buildContainer(containerBuilder, TarImage.at(dockerTarArchive.toPath()).named(imageConfig.getName()), log);
        log.info(" %s successfully built", dockerTarArchive.getAbsolutePath());
    } catch (Exception ex) {
        throw new MojoExecutionException("Error when building JIB image", ex);
    }
}
Also used : Arrays(java.util.Arrays) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) ImageName(io.fabric8.maven.docker.util.ImageName) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Logger(io.fabric8.maven.docker.util.Logger) Function(java.util.function.Function) Credential(com.google.cloud.tools.jib.api.Credential) JibServiceUtil(io.fabric8.maven.docker.util.JibServiceUtil) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) Map(java.util.Map) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) AuthConfigFactory(io.fabric8.maven.docker.util.AuthConfigFactory) Collection(java.util.Collection) JibServiceUtil.containerFromImageConfiguration(io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration) TarImage(com.google.cloud.tools.jib.api.TarImage) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ArchiveCompression(io.fabric8.maven.docker.config.ArchiveCompression) JibServiceUtil.getBaseImage(io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage) EnvUtil(io.fabric8.maven.docker.util.EnvUtil) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) Credential(com.google.cloud.tools.jib.api.Credential) AssemblyConfiguration(io.fabric8.maven.docker.config.AssemblyConfiguration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AssemblyFiles(io.fabric8.maven.docker.assembly.AssemblyFiles) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) BuildDirs(io.fabric8.maven.docker.assembly.BuildDirs) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 2 with Credential

use of com.google.cloud.tools.jib.api.Credential in project docker-maven-plugin by fabric8io.

the class JibBuildServiceTest method testGetRegistryCredentialsForPush.

@Test
@java.lang.SuppressWarnings("squid:S00112")
public void testGetRegistryCredentialsForPush() throws MojoExecutionException {
    // Given
    ImageConfiguration imageConfiguration = getImageConfiguration();
    RegistryService.RegistryConfig registryConfig = new RegistryService.RegistryConfig.Builder().authConfig(Collections.emptyMap()).authConfigFactory(authConfigFactory).settings(settings).build();
    mockAuthConfigFactory(true, registryConfig);
    // When
    Credential credential = JibBuildService.getRegistryCredentials(registryConfig, true, imageConfiguration, logger);
    // Then
    assertNotNull(credential);
    assertEquals("testuserpush", credential.getUsername());
    assertEquals("testpass", credential.getPassword());
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Example 3 with Credential

use of com.google.cloud.tools.jib.api.Credential in project docker-maven-plugin by fabric8io.

the class JibBuildService method getRegistryCredentials.

static Credential getRegistryCredentials(RegistryService.RegistryConfig registryConfig, boolean isPush, ImageConfiguration imageConfiguration, Logger log) throws MojoExecutionException {
    String registry;
    if (isPush) {
        registry = EnvUtil.firstRegistryOf(new ImageName(imageConfiguration.getName()).getRegistry(), imageConfiguration.getRegistry(), registryConfig.getRegistry());
    } else {
        registry = EnvUtil.firstRegistryOf(new ImageName(getBaseImage(imageConfiguration)).getRegistry(), registryConfig.getRegistry());
    }
    if (registry == null || DEFAULT_DOCKER_REGISTRIES.contains(registry)) {
        // Let's assume docker is default registry.
        registry = DOCKER_LOGIN_DEFAULT_REGISTRY;
    }
    AuthConfigFactory authConfigFactory = registryConfig.getAuthConfigFactory();
    AuthConfig standardAuthConfig = authConfigFactory.createAuthConfig(isPush, registryConfig.isSkipExtendedAuth(), registryConfig.getAuthConfig(), registryConfig.getSettings(), null, registry);
    Credential credentials = null;
    if (standardAuthConfig != null) {
        credentials = Credential.from(standardAuthConfig.getUsername(), standardAuthConfig.getPassword());
    }
    return credentials;
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) Credential(com.google.cloud.tools.jib.api.Credential) AuthConfigFactory(io.fabric8.maven.docker.util.AuthConfigFactory) AuthConfig(io.fabric8.maven.docker.access.AuthConfig)

Example 4 with Credential

use of com.google.cloud.tools.jib.api.Credential in project docker-maven-plugin by fabric8io.

the class JibBuildServiceTest method testGetRegistryCredentialsForPull.

@Test
@java.lang.SuppressWarnings("squid:S00112")
public void testGetRegistryCredentialsForPull() throws MojoExecutionException {
    // Given
    ImageConfiguration imageConfiguration = getImageConfiguration();
    RegistryService.RegistryConfig registryConfig = new RegistryService.RegistryConfig.Builder().authConfig(Collections.emptyMap()).authConfigFactory(authConfigFactory).settings(settings).build();
    mockAuthConfigFactory(false, registryConfig);
    // When
    Credential credential = JibBuildService.getRegistryCredentials(registryConfig, false, imageConfiguration, logger);
    // Then
    assertNotNull(credential);
    assertEquals("testuserpull", credential.getUsername());
    assertEquals("testpass", credential.getPassword());
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) Test(org.junit.Test)

Aggregations

Credential (com.google.cloud.tools.jib.api.Credential)4 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)3 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)2 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)2 AuthConfigFactory (io.fabric8.maven.docker.util.AuthConfigFactory)2 ImageName (io.fabric8.maven.docker.util.ImageName)2 Test (org.junit.Test)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 TarImage (com.google.cloud.tools.jib.api.TarImage)1 AssemblyFiles (io.fabric8.maven.docker.assembly.AssemblyFiles)1 BuildDirs (io.fabric8.maven.docker.assembly.BuildDirs)1 ArchiveCompression (io.fabric8.maven.docker.config.ArchiveCompression)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 EnvUtil (io.fabric8.maven.docker.util.EnvUtil)1 JibServiceUtil (io.fabric8.maven.docker.util.JibServiceUtil)1 JibServiceUtil.containerFromImageConfiguration (io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration)1 JibServiceUtil.getBaseImage (io.fabric8.maven.docker.util.JibServiceUtil.getBaseImage)1 Logger (io.fabric8.maven.docker.util.Logger)1 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)1 File (java.io.File)1