Search in sources :

Example 6 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.

the class RegistryService method pullImageWithPolicy.

/**
 *  Check an image, and, if <code>autoPull</code> is set to true, fetch it. Otherwise if the image
 *  is not existent, throw an error
 *
 * @param image image which is required to be pulled
 * @param pullManager image pull manager
 * @param registryConfig registry configuration
 * @param buildImageConfiguration image build configuration
 * @throws DockerAccessException in case of error in contacting docker daemon
 * @throws MojoExecutionException in case of any other misc failure
 */
public void pullImageWithPolicy(String image, ImagePullManager pullManager, RegistryConfig registryConfig, BuildImageConfiguration buildImageConfiguration) throws DockerAccessException, MojoExecutionException {
    // Already pulled, so we don't need to take care
    if (pullManager.hasAlreadyPulled(image)) {
        return;
    }
    // Check if a pull is required
    if (!imageRequiresPull(queryService.hasImage(image), pullManager.getImagePullPolicy(), image)) {
        return;
    }
    final ImageName imageName = new ImageName(image);
    final long pullStartTime = System.currentTimeMillis();
    final String actualRegistry = EnvUtil.firstRegistryOf(imageName.getRegistry(), registryConfig.getRegistry());
    final CreateImageOptions createImageOptions = new CreateImageOptions(buildImageConfiguration != null ? buildImageConfiguration.getCreateImageOptions() : Collections.emptyMap()).fromImage(imageName.getNameWithoutTag(actualRegistry)).tag(imageName.getDigest() != null ? imageName.getDigest() : imageName.getTag());
    docker.pullImage(imageName.getFullName(), createAuthConfig(false, null, actualRegistry, registryConfig), actualRegistry, createImageOptions);
    log.info("Pulled %s in %s", imageName.getFullName(), EnvUtil.formatDurationTill(pullStartTime));
    pullManager.pulled(image);
    if (actualRegistry != null && !imageName.hasRegistry()) {
        // If coming from a registry which was not contained in the original name, add a tag from the
        // full name with the registry to the short name with no-registry.
        docker.tag(imageName.getFullName(actualRegistry), image, false);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) CreateImageOptions(io.fabric8.maven.docker.access.CreateImageOptions)

Example 7 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig 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 8 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.

the class CopyMojoTest method thenImageIsPulled.

private void thenImageIsPulled(ImageConfiguration image, String pullRegistry, boolean imageExists) throws DockerAccessException, MojoExecutionException {
    new Verifications() {

        {
            final String imageName;
            final RegistryConfig registryConfig;
            registryService.pullImageWithPolicy(imageName = withCapture(), (ImagePullManager) any, registryConfig = withCapture(), image.getBuildConfiguration());
            times = 1;
            assertEquals(image.getName(), imageName);
            assertNotNull(registryConfig);
            assertEquals(pullRegistry, registryConfig.getRegistry());
        }
    };
}
Also used : RegistryConfig(io.fabric8.maven.docker.service.RegistryService.RegistryConfig) Verifications(mockit.Verifications)

Example 9 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig 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 10 with RegistryConfig

use of io.fabric8.maven.docker.service.RegistryService.RegistryConfig in project docker-maven-plugin by fabric8io.

the class RegistryService method pushImages.

/**
 * Push a set of images to a registry
 *
 * @param imageConfigs images 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 DockerAccessException
 * @throws MojoExecutionException
 */
public void pushImages(Collection<ImageConfiguration> imageConfigs, int retries, RegistryConfig registryConfig, boolean skipTag) throws DockerAccessException, MojoExecutionException {
    for (ImageConfiguration imageConfig : imageConfigs) {
        BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
        String name = imageConfig.getName();
        if (buildConfig != null) {
            if (buildConfig.skipPush()) {
                log.info("%s : Skipped pushing", imageConfig.getDescription());
                continue;
            }
            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 : ImageName(io.fabric8.maven.docker.util.ImageName) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) AuthConfig(io.fabric8.maven.docker.access.AuthConfig) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Aggregations

ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)7 ImageName (io.fabric8.maven.docker.util.ImageName)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)5 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 Credential (com.google.cloud.tools.jib.api.Credential)4 Test (org.junit.Test)3 RegistryConfig (io.fabric8.maven.docker.service.RegistryService.RegistryConfig)2 AuthConfigFactory (io.fabric8.maven.docker.util.AuthConfigFactory)2 JibServiceUtil.containerFromImageConfiguration (io.fabric8.maven.docker.util.JibServiceUtil.containerFromImageConfiguration)2 File (java.io.File)2 Verifications (mockit.Verifications)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 TarImage (com.google.cloud.tools.jib.api.TarImage)1 CreateImageOptions (io.fabric8.maven.docker.access.CreateImageOptions)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 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)1