Search in sources :

Example 1 with HistoryEntry

use of com.google.cloud.tools.jib.image.json.HistoryEntry in project jib by GoogleContainerTools.

the class BuildImageStepTest method test_generateHistoryObjects.

@Test
public void test_generateHistoryObjects() {
    Image image = new BuildImageStep(mockBuildContext, mockProgressEventDispatcherFactory, baseImage, baseImageLayers, applicationLayers).call();
    // Make sure history is as expected
    HistoryEntry expectedAddedBaseLayerHistory = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setComment("auto-generated by Jib").build();
    HistoryEntry expectedApplicationLayerHistoryDependencies = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("dependencies").build();
    HistoryEntry expectedApplicationLayerHistoryResources = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("resources").build();
    HistoryEntry expectedApplicationLayerHistoryClasses = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("classes").build();
    HistoryEntry expectedApplicationLayerHistoryExtrafiles = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("extra files").build();
    // Base layers (1 non-empty propagated, 2 empty propagated, 2 non-empty generated)
    Assert.assertEquals(nonEmptyLayerHistory, image.getHistory().get(0));
    Assert.assertEquals(emptyLayerHistory, image.getHistory().get(1));
    Assert.assertEquals(emptyLayerHistory, image.getHistory().get(2));
    Assert.assertEquals(expectedAddedBaseLayerHistory, image.getHistory().get(3));
    Assert.assertEquals(expectedAddedBaseLayerHistory, image.getHistory().get(4));
    // Application layers (4 generated)
    Assert.assertEquals(expectedApplicationLayerHistoryDependencies, image.getHistory().get(5));
    Assert.assertEquals(expectedApplicationLayerHistoryResources, image.getHistory().get(6));
    Assert.assertEquals(expectedApplicationLayerHistoryClasses, image.getHistory().get(7));
    Assert.assertEquals(expectedApplicationLayerHistoryExtrafiles, image.getHistory().get(8));
    // Should be exactly 9 total
    Assert.assertEquals(9, image.getHistory().size());
}
Also used : HistoryEntry(com.google.cloud.tools.jib.image.json.HistoryEntry) Image(com.google.cloud.tools.jib.image.Image) Test(org.junit.Test)

Example 2 with HistoryEntry

use of com.google.cloud.tools.jib.image.json.HistoryEntry 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 HistoryEntry

use of com.google.cloud.tools.jib.image.json.HistoryEntry 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 HistoryEntry

use of com.google.cloud.tools.jib.image.json.HistoryEntry in project jib by google.

the class BuildImageStepTest method test_generateHistoryObjects.

@Test
public void test_generateHistoryObjects() {
    Image image = new BuildImageStep(mockBuildContext, mockProgressEventDispatcherFactory, baseImage, baseImageLayers, applicationLayers).call();
    // Make sure history is as expected
    HistoryEntry expectedAddedBaseLayerHistory = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setComment("auto-generated by Jib").build();
    HistoryEntry expectedApplicationLayerHistoryDependencies = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("dependencies").build();
    HistoryEntry expectedApplicationLayerHistoryResources = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("resources").build();
    HistoryEntry expectedApplicationLayerHistoryClasses = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("classes").build();
    HistoryEntry expectedApplicationLayerHistoryExtrafiles = HistoryEntry.builder().setCreationTimestamp(Instant.EPOCH).setAuthor("Jib").setCreatedBy("jib:null").setComment("extra files").build();
    // Base layers (1 non-empty propagated, 2 empty propagated, 2 non-empty generated)
    Assert.assertEquals(nonEmptyLayerHistory, image.getHistory().get(0));
    Assert.assertEquals(emptyLayerHistory, image.getHistory().get(1));
    Assert.assertEquals(emptyLayerHistory, image.getHistory().get(2));
    Assert.assertEquals(expectedAddedBaseLayerHistory, image.getHistory().get(3));
    Assert.assertEquals(expectedAddedBaseLayerHistory, image.getHistory().get(4));
    // Application layers (4 generated)
    Assert.assertEquals(expectedApplicationLayerHistoryDependencies, image.getHistory().get(5));
    Assert.assertEquals(expectedApplicationLayerHistoryResources, image.getHistory().get(6));
    Assert.assertEquals(expectedApplicationLayerHistoryClasses, image.getHistory().get(7));
    Assert.assertEquals(expectedApplicationLayerHistoryExtrafiles, image.getHistory().get(8));
    // Should be exactly 9 total
    Assert.assertEquals(9, image.getHistory().size());
}
Also used : HistoryEntry(com.google.cloud.tools.jib.image.json.HistoryEntry) Image(com.google.cloud.tools.jib.image.Image) Test(org.junit.Test)

Aggregations

Image (com.google.cloud.tools.jib.image.Image)4 HistoryEntry (com.google.cloud.tools.jib.image.json.HistoryEntry)4 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)2 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)2 ContainerConfiguration (com.google.cloud.tools.jib.configuration.ContainerConfiguration)2 Instant (java.time.Instant)2 Test (org.junit.Test)2