Search in sources :

Example 21 with ImageConfiguration

use of com.google.cloud.tools.jib.configuration.ImageConfiguration in project jib by google.

the class JibContainerBuilderTest method testToContainerBuildPlan.

@Test
public void testToContainerBuildPlan() throws InvalidImageReferenceException, IOException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder containerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).setPlatforms(ImmutableSet.of(new Platform("testArchitecture", "testOS"))).setCreationTime(Instant.ofEpochMilli(1000)).setFormat(ImageFormat.OCI).setEnvironment(ImmutableMap.of("env", "var")).setLabels(ImmutableMap.of("com.example.label", "value")).setVolumes(AbsoluteUnixPath.get("/mnt/vol"), AbsoluteUnixPath.get("/media/data")).setExposedPorts(ImmutableSet.of(Port.tcp(1234), Port.udp(5678))).setUser("user").setWorkingDirectory(AbsoluteUnixPath.get("/working/directory")).setEntrypoint(Arrays.asList("entry", "point")).setProgramArguments(Arrays.asList("program", "arguments")).addLayer(Arrays.asList(Paths.get("/non/existing/foo")), "/into/this");
    ContainerBuildPlan buildPlan = containerBuilder.toContainerBuildPlan();
    Assert.assertEquals("base/image", buildPlan.getBaseImage());
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS")), buildPlan.getPlatforms());
    Assert.assertEquals(Instant.ofEpochMilli(1000), buildPlan.getCreationTime());
    Assert.assertEquals(ImageFormat.OCI, buildPlan.getFormat());
    Assert.assertEquals(ImmutableMap.of("env", "var"), buildPlan.getEnvironment());
    Assert.assertEquals(ImmutableMap.of("com.example.label", "value"), buildPlan.getLabels());
    Assert.assertEquals(ImmutableSet.of(AbsoluteUnixPath.get("/mnt/vol"), AbsoluteUnixPath.get("/media/data")), buildPlan.getVolumes());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678)), buildPlan.getExposedPorts());
    Assert.assertEquals("user", buildPlan.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/working/directory"), buildPlan.getWorkingDirectory());
    Assert.assertEquals(Arrays.asList("entry", "point"), buildPlan.getEntrypoint());
    Assert.assertEquals(Arrays.asList("program", "arguments"), buildPlan.getCmd());
    Assert.assertEquals(1, buildPlan.getLayers().size());
    MatcherAssert.assertThat(buildPlan.getLayers().get(0), CoreMatchers.instanceOf(FileEntriesLayer.class));
    Assert.assertEquals(Arrays.asList(new FileEntry(Paths.get("/non/existing/foo"), AbsoluteUnixPath.get("/into/this/foo"), FilePermissions.fromOctalString("644"), Instant.ofEpochSecond(1))), ((FileEntriesLayer) buildPlan.getLayers().get(0)).getEntries());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 22 with ImageConfiguration

use of com.google.cloud.tools.jib.configuration.ImageConfiguration in project jib by google.

the class JibContainerBuilderTest method testToBuildContext_containerConfigurationAdd.

@Test
public void testToBuildContext_containerConfigurationAdd() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).addPlatform("testArchitecture", "testOS").setEntrypoint("entry", "point").setEnvironment(ImmutableMap.of("name", "value")).addEnvironmentVariable("environment", "variable").setExposedPorts(Port.tcp(1234), Port.udp(5678)).addExposedPort(Port.tcp(1337)).setLabels(ImmutableMap.of("key", "value")).addLabel("added", "label").setProgramArguments("program", "arguments");
    BuildContext buildContext = jibContainerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS"), new Platform("amd64", "linux")), containerConfiguration.getPlatforms());
    Assert.assertEquals(Arrays.asList("entry", "point"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(ImmutableMap.of("name", "value", "environment", "variable"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678), Port.tcp(1337)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(ImmutableMap.of("key", "value", "added", "label"), containerConfiguration.getLabels());
    Assert.assertEquals(Arrays.asList("program", "arguments"), containerConfiguration.getProgramArguments());
    Assert.assertEquals(Instant.EPOCH, containerConfiguration.getCreationTime());
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) Test(org.junit.Test)

Example 23 with ImageConfiguration

use of com.google.cloud.tools.jib.configuration.ImageConfiguration in project jib by google.

the class JibContainerTest method testCreation_withBuildContextAndBuildResult.

@Test
public void testCreation_withBuildContextAndBuildResult() {
    BuildResult buildResult = Mockito.mock(BuildResult.class);
    BuildContext buildContext = Mockito.mock(BuildContext.class);
    ImageConfiguration mockTargetConfiguration = Mockito.mock(ImageConfiguration.class);
    when(buildResult.getImageDigest()).thenReturn(digest1);
    when(buildResult.getImageId()).thenReturn(digest1);
    when(buildResult.isImagePushed()).thenReturn(true);
    when(mockTargetConfiguration.getImage()).thenReturn(targetImage1);
    when(buildContext.getTargetImageConfiguration()).thenReturn(mockTargetConfiguration);
    when(buildContext.getAllTargetImageTags()).thenReturn(ImmutableSet.copyOf(tags1));
    JibContainer container = JibContainer.from(buildContext, buildResult);
    Assert.assertEquals(targetImage1, container.getTargetImage());
    Assert.assertEquals(digest1, container.getDigest());
    Assert.assertEquals(digest1, container.getImageId());
    Assert.assertEquals(tags1, container.getTags());
    Assert.assertTrue(container.isImagePushed());
}
Also used : BuildResult(com.google.cloud.tools.jib.builder.steps.BuildResult) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) Test(org.junit.Test)

Example 24 with ImageConfiguration

use of com.google.cloud.tools.jib.configuration.ImageConfiguration in project jib by google.

the class ContainerizerTest method testGetImageConfiguration_tarImage.

@Test
public void testGetImageConfiguration_tarImage() throws InvalidImageReferenceException {
    Containerizer containerizer = Containerizer.to(TarImage.at(Paths.get("output/file")).named("tar/image"));
    ImageConfiguration imageConfiguration = containerizer.getImageConfiguration();
    Assert.assertEquals("tar/image", imageConfiguration.getImage().toString());
    Assert.assertEquals(0, imageConfiguration.getCredentialRetrievers().size());
}
Also used : ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) Test(org.junit.Test)

Example 25 with ImageConfiguration

use of com.google.cloud.tools.jib.configuration.ImageConfiguration in project jib by google.

the class PullBaseImageStep method pullBaseImages.

/**
 * Pulls the base images specified in the platforms list.
 *
 * @param registryClient to communicate with remote registry
 * @param progressDispatcherFactory the {@link ProgressEventDispatcher.Factory} for emitting
 *     {@link ProgressEvent}s
 * @return the list of pulled base images and a registry client
 * @throws IOException when an I/O exception occurs during the pulling
 * @throws RegistryException if communicating with the registry caused a known error
 * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
 *     information
 * @throws LayerPropertyNotFoundException if adding image layers fails
 * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
 *     format
 */
private List<Image> pullBaseImages(RegistryClient registryClient, ProgressEventDispatcher.Factory progressDispatcherFactory) throws IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException {
    Cache cache = buildContext.getBaseImageLayersCache();
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    ImageConfiguration baseImageConfig = buildContext.getBaseImageConfiguration();
    try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("pulling base image manifest and container config", 2)) {
        ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest(baseImageConfig.getImageQualifier());
        eventHandlers.dispatch(LogEvent.lifecycle("Using base image with digest: " + manifestAndDigest.getDigest()));
        progressDispatcher1.dispatchProgress(1);
        ProgressEventDispatcher.Factory childProgressDispatcherFactory = progressDispatcher1.newChildProducer();
        ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
        if (manifestTemplate instanceof V21ManifestTemplate) {
            V21ManifestTemplate v21Manifest = (V21ManifestTemplate) manifestTemplate;
            cache.writeMetadata(baseImageConfig.getImage(), v21Manifest);
            return Collections.singletonList(JsonToImageTranslator.toImage(v21Manifest));
        } else if (manifestTemplate instanceof BuildableManifestTemplate) {
            // V22ManifestTemplate or OciManifestTemplate
            BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) manifestTemplate;
            ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(manifestAndDigest, registryClient, childProgressDispatcherFactory);
            PlatformChecker.checkManifestPlatform(buildContext, containerConfig);
            cache.writeMetadata(baseImageConfig.getImage(), imageManifest, containerConfig);
            return Collections.singletonList(JsonToImageTranslator.toImage(imageManifest, containerConfig));
        }
        // TODO: support OciIndexTemplate once AbstractManifestPuller starts to accept it.
        Verify.verify(manifestTemplate instanceof V22ManifestListTemplate);
        List<ManifestAndConfigTemplate> manifestsAndConfigs = new ArrayList<>();
        ImmutableList.Builder<Image> images = ImmutableList.builder();
        Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
        try (ProgressEventDispatcher progressDispatcher2 = childProgressDispatcherFactory.create("pulling platform-specific manifests and container configs", 2L * platforms.size())) {
            // If a manifest list, search for the manifests matching the given platforms.
            for (Platform platform : platforms) {
                String message = "Searching for architecture=%s, os=%s in the base image manifest list";
                eventHandlers.dispatch(LogEvent.info(String.format(message, platform.getArchitecture(), platform.getOs())));
                String manifestDigest = lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestTemplate, platform);
                // TODO: pull multiple manifests (+ container configs) in parallel.
                ManifestAndDigest<?> imageManifestAndDigest = registryClient.pullManifest(manifestDigest);
                progressDispatcher2.dispatchProgress(1);
                BuildableManifestTemplate imageManifest = (BuildableManifestTemplate) imageManifestAndDigest.getManifest();
                ContainerConfigurationTemplate containerConfig = pullContainerConfigJson(imageManifestAndDigest, registryClient, progressDispatcher2.newChildProducer());
                manifestsAndConfigs.add(new ManifestAndConfigTemplate(imageManifest, containerConfig, manifestDigest));
                images.add(JsonToImageTranslator.toImage(imageManifest, containerConfig));
            }
        }
        cache.writeMetadata(baseImageConfig.getImage(), new ImageMetadataTemplate(manifestTemplate, /* manifest list */
        manifestsAndConfigs));
        return images.build();
    }
}
Also used : ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ManifestAndConfigTemplate(com.google.cloud.tools.jib.image.json.ManifestAndConfigTemplate) Image(com.google.cloud.tools.jib.image.Image) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageMetadataTemplate(com.google.cloud.tools.jib.image.json.ImageMetadataTemplate) V21ManifestTemplate(com.google.cloud.tools.jib.image.json.V21ManifestTemplate) Cache(com.google.cloud.tools.jib.cache.Cache)

Aggregations

ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)49 Test (org.junit.Test)41 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)21 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)12 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)8 Path (java.nio.file.Path)8 ExecutorService (java.util.concurrent.ExecutorService)8 BuildResult (com.google.cloud.tools.jib.builder.steps.BuildResult)7 ContainerizerTestProxy (com.google.cloud.tools.jib.api.ContainerizerTestProxy)6 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)6 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)6 StepsRunner (com.google.cloud.tools.jib.builder.steps.StepsRunner)6 ContainerConfiguration (com.google.cloud.tools.jib.configuration.ContainerConfiguration)6 DockerClient (com.google.cloud.tools.jib.docker.DockerClient)6 XdgDirectories (com.google.cloud.tools.jib.filesystem.XdgDirectories)6 Preconditions (com.google.common.base.Preconditions)6 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)6 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 ListMultimap (com.google.common.collect.ListMultimap)6