Search in sources :

Example 1 with ContainerConfiguration

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

the class JibContainerBuilderTest method testToBuildContext_containerConfigurationSet.

@Test
public void testToBuildContext_containerConfigurationSet() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).setPlatforms(ImmutableSet.of(new Platform("testArchitecture", "testOS"))).setEntrypoint(Arrays.asList("entry", "point")).setEnvironment(ImmutableMap.of("name", "value")).setExposedPorts(ImmutableSet.of(Port.tcp(1234), Port.udp(5678))).setLabels(ImmutableMap.of("key", "value")).setProgramArguments(Arrays.asList("program", "arguments")).setCreationTime(Instant.ofEpochMilli(1000)).setUser("user").setWorkingDirectory(AbsoluteUnixPath.get("/working/directory"));
    BuildContext buildContext = jibContainerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS")), containerConfiguration.getPlatforms());
    Assert.assertEquals(Arrays.asList("entry", "point"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(ImmutableMap.of("name", "value"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(ImmutableMap.of("key", "value"), containerConfiguration.getLabels());
    Assert.assertEquals(Arrays.asList("program", "arguments"), containerConfiguration.getProgramArguments());
    Assert.assertEquals(Instant.ofEpochMilli(1000), containerConfiguration.getCreationTime());
    Assert.assertEquals("user", containerConfiguration.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/working/directory"), containerConfiguration.getWorkingDirectory());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) Test(org.junit.Test)

Example 2 with ContainerConfiguration

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

the class BuildImageStep method call.

@Override
public Image call() throws LayerPropertyNotFoundException {
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building image format", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(buildContext.getEventHandlers(), DESCRIPTION)) {
        // Constructs the image.
        Image.Builder imageBuilder = Image.builder(buildContext.getTargetFormat());
        // Base image layers
        baseImageLayers.forEach(imageBuilder::addLayer);
        // Passthrough config and count non-empty history entries
        int nonEmptyLayerCount = 0;
        for (HistoryEntry historyObject : baseImage.getHistory()) {
            imageBuilder.addHistory(historyObject);
            if (!historyObject.hasCorrespondingLayer()) {
                nonEmptyLayerCount++;
            }
        }
        imageBuilder.setArchitecture(baseImage.getArchitecture()).setOs(baseImage.getOs()).addEnvironment(baseImage.getEnvironment()).addLabels(baseImage.getLabels()).setHealthCheck(baseImage.getHealthCheck()).addExposedPorts(baseImage.getExposedPorts()).addVolumes(baseImage.getVolumes()).setUser(baseImage.getUser()).setWorkingDirectory(baseImage.getWorkingDirectory());
        ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
        // Add history elements for non-empty layers that don't have one yet
        Instant layerCreationTime = containerConfiguration.getCreationTime();
        for (int count = 0; count < baseImageLayers.size() - nonEmptyLayerCount; count++) {
            imageBuilder.addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setComment("auto-generated by Jib").build());
        }
        // Add built layers/configuration
        for (PreparedLayer applicationLayer : applicationLayers) {
            imageBuilder.addLayer(applicationLayer).addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setAuthor("Jib").setCreatedBy(buildContext.getToolName() + ":" + buildContext.getToolVersion()).setComment(applicationLayer.getName()).build());
        }
        imageBuilder.addEnvironment(containerConfiguration.getEnvironmentMap()).setCreated(containerConfiguration.getCreationTime()).setEntrypoint(computeEntrypoint(baseImage, containerConfiguration)).setProgramArguments(computeProgramArguments(baseImage, containerConfiguration)).addExposedPorts(containerConfiguration.getExposedPorts()).addVolumes(containerConfiguration.getVolumes()).addLabels(containerConfiguration.getLabels());
        if (containerConfiguration.getUser() != null) {
            imageBuilder.setUser(containerConfiguration.getUser());
        }
        if (containerConfiguration.getWorkingDirectory() != null) {
            imageBuilder.setWorkingDirectory(containerConfiguration.getWorkingDirectory().toString());
        }
        // Gets the container configuration content descriptor.
        return imageBuilder.build();
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) Instant(java.time.Instant) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) HistoryEntry(com.google.cloud.tools.jib.image.json.HistoryEntry) Image(com.google.cloud.tools.jib.image.Image) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration)

Example 3 with ContainerConfiguration

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

the class BuildImageStep method call.

@Override
public Image call() throws LayerPropertyNotFoundException {
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building image format", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(buildContext.getEventHandlers(), DESCRIPTION)) {
        // Constructs the image.
        Image.Builder imageBuilder = Image.builder(buildContext.getTargetFormat());
        // Base image layers
        baseImageLayers.forEach(imageBuilder::addLayer);
        // Passthrough config and count non-empty history entries
        int nonEmptyLayerCount = 0;
        for (HistoryEntry historyObject : baseImage.getHistory()) {
            imageBuilder.addHistory(historyObject);
            if (!historyObject.hasCorrespondingLayer()) {
                nonEmptyLayerCount++;
            }
        }
        imageBuilder.setArchitecture(baseImage.getArchitecture()).setOs(baseImage.getOs()).addEnvironment(baseImage.getEnvironment()).addLabels(baseImage.getLabels()).setHealthCheck(baseImage.getHealthCheck()).addExposedPorts(baseImage.getExposedPorts()).addVolumes(baseImage.getVolumes()).setUser(baseImage.getUser()).setWorkingDirectory(baseImage.getWorkingDirectory());
        ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
        // Add history elements for non-empty layers that don't have one yet
        Instant layerCreationTime = containerConfiguration.getCreationTime();
        for (int count = 0; count < baseImageLayers.size() - nonEmptyLayerCount; count++) {
            imageBuilder.addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setComment("auto-generated by Jib").build());
        }
        // Add built layers/configuration
        for (PreparedLayer applicationLayer : applicationLayers) {
            imageBuilder.addLayer(applicationLayer).addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setAuthor("Jib").setCreatedBy(buildContext.getToolName() + ":" + buildContext.getToolVersion()).setComment(applicationLayer.getName()).build());
        }
        imageBuilder.addEnvironment(containerConfiguration.getEnvironmentMap()).setCreated(containerConfiguration.getCreationTime()).setEntrypoint(computeEntrypoint(baseImage, containerConfiguration)).setProgramArguments(computeProgramArguments(baseImage, containerConfiguration)).addExposedPorts(containerConfiguration.getExposedPorts()).addVolumes(containerConfiguration.getVolumes()).addLabels(containerConfiguration.getLabels());
        if (containerConfiguration.getUser() != null) {
            imageBuilder.setUser(containerConfiguration.getUser());
        }
        if (containerConfiguration.getWorkingDirectory() != null) {
            imageBuilder.setWorkingDirectory(containerConfiguration.getWorkingDirectory().toString());
        }
        // Gets the container configuration content descriptor.
        return imageBuilder.build();
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) Instant(java.time.Instant) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) HistoryEntry(com.google.cloud.tools.jib.image.json.HistoryEntry) Image(com.google.cloud.tools.jib.image.Image) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration)

Example 4 with ContainerConfiguration

use of com.google.cloud.tools.jib.configuration.ContainerConfiguration 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 5 with ContainerConfiguration

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

the class JibContainerBuilderTest method testToBuildContext_containerConfigurationSet.

@Test
public void testToBuildContext_containerConfigurationSet() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).setPlatforms(ImmutableSet.of(new Platform("testArchitecture", "testOS"))).setEntrypoint(Arrays.asList("entry", "point")).setEnvironment(ImmutableMap.of("name", "value")).setExposedPorts(ImmutableSet.of(Port.tcp(1234), Port.udp(5678))).setLabels(ImmutableMap.of("key", "value")).setProgramArguments(Arrays.asList("program", "arguments")).setCreationTime(Instant.ofEpochMilli(1000)).setUser("user").setWorkingDirectory(AbsoluteUnixPath.get("/working/directory"));
    BuildContext buildContext = jibContainerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS")), containerConfiguration.getPlatforms());
    Assert.assertEquals(Arrays.asList("entry", "point"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(ImmutableMap.of("name", "value"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(ImmutableMap.of("key", "value"), containerConfiguration.getLabels());
    Assert.assertEquals(Arrays.asList("program", "arguments"), containerConfiguration.getProgramArguments());
    Assert.assertEquals(Instant.ofEpochMilli(1000), containerConfiguration.getCreationTime());
    Assert.assertEquals("user", containerConfiguration.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/working/directory"), containerConfiguration.getWorkingDirectory());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) Test(org.junit.Test)

Aggregations

ContainerConfiguration (com.google.cloud.tools.jib.configuration.ContainerConfiguration)8 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)6 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)6 Test (org.junit.Test)6 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)2 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)2 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)2 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)2 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)2 Image (com.google.cloud.tools.jib.image.Image)2 HistoryEntry (com.google.cloud.tools.jib.image.json.HistoryEntry)2 Instant (java.time.Instant)2