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());
}
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());
}
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());
}
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());
}
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();
}
}
Aggregations