use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.
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.api.buildplan.Platform in project jib by GoogleContainerTools.
the class PlatformChecker method checkManifestPlatform.
/**
* Assuming the base image is not a manifest list, checks and warns misconfigured platforms.
*
* @param buildContext the {@link BuildContext}
* @param containerConfig container configuration JSON of the base image
*/
static void checkManifestPlatform(BuildContext buildContext, ContainerConfigurationTemplate containerConfig) {
EventHandlers eventHandlers = buildContext.getEventHandlers();
Optional<Path> path = buildContext.getBaseImageConfiguration().getTarPath();
String baseImageName = path.map(Path::toString).orElse(buildContext.getBaseImageConfiguration().getImage().toString());
Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
Verify.verify(!platforms.isEmpty());
if (platforms.size() != 1) {
eventHandlers.dispatch(LogEvent.warn("platforms configured, but '" + baseImageName + "' is not a manifest list"));
} else {
Platform platform = platforms.iterator().next();
if (!platform.getArchitecture().equals(containerConfig.getArchitecture()) || !platform.getOs().equals(containerConfig.getOs())) {
// configure it. Skip reporting to suppress false alarm.
if (!(platform.getArchitecture().equals("amd64") && platform.getOs().equals("linux"))) {
String warning = "the configured platform (%s/%s) doesn't match the platform (%s/%s) of the base " + "image (%s)";
eventHandlers.dispatch(LogEvent.warn(String.format(warning, platform.getArchitecture(), platform.getOs(), containerConfig.getArchitecture(), containerConfig.getOs(), baseImageName)));
}
}
}
}
use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.
the class ContainerBuilders method create.
/**
* Creates a {@link JibContainerBuilder} depending on the base image specified.
*
* @param baseImageReference base image reference
* @param platforms platforms for multi-platform support in build command
* @param commonCliOptions common cli options
* @param logger console logger
* @return a {@link JibContainerBuilder}
* @throws InvalidImageReferenceException if the baseImage reference cannot be parsed
* @throws FileNotFoundException if credential helper file cannot be found
*/
public static JibContainerBuilder create(String baseImageReference, Set<Platform> platforms, CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
if (baseImageReference.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
return Jib.from(DockerDaemonImage.named(baseImageReference.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
}
if (baseImageReference.startsWith(TAR_IMAGE_PREFIX)) {
return Jib.from(TarImage.at(Paths.get(baseImageReference.replaceFirst(TAR_IMAGE_PREFIX, ""))));
}
ImageReference imageReference = ImageReference.parse(baseImageReference.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
RegistryImage registryImage = RegistryImage.named(imageReference);
DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
Credentials.getFromCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
JibContainerBuilder containerBuilder = Jib.from(registryImage);
if (!platforms.isEmpty()) {
containerBuilder.setPlatforms(platforms);
}
return containerBuilder;
}
use of com.google.cloud.tools.jib.api.buildplan.Platform in project jib by GoogleContainerTools.
the class PluginConfigurationProcessorTest method testGetPlatformsSet.
@Test
public void testGetPlatformsSet() throws InvalidPlatformException {
Mockito.<List<?>>when(rawConfiguration.getPlatforms()).thenReturn(Arrays.asList(new TestPlatformConfiguration("testArchitecture", "testOs")));
assertThat(PluginConfigurationProcessor.getPlatformsSet(rawConfiguration)).containsExactly(new Platform("testArchitecture", "testOs"));
}
use of com.google.cloud.tools.jib.api.buildplan.Platform in project quarkus by quarkusio.
the class PlatformHelper method parse.
public static Platform parse(String specifier) {
Platform platform;
String[] elements = specifier.split("/", 2);
if (elements.length == 1) {
Optional<String> os = normalizeOs(elements[0]);
if (os.filter(PlatformHelper::isKnownOs).isPresent()) {
platform = new Platform(ARCH_DEFAULT, os.get());
} else {
platform = new Platform(normalizeArch(elements[0]).orElse(ARCH_DEFAULT), OS_DEFAULT);
}
} else {
Optional<String> arch = normalizeArch(specifier);
if (arch.filter(PlatformHelper::isKnownArch).isPresent()) {
platform = new Platform(arch.get(), OS_DEFAULT);
} else {
platform = new Platform(normalizeArch(elements[1]).orElse(ARCH_DEFAULT), normalizeOs(elements[0]).orElse(OS_DEFAULT));
}
}
log.debug("Platform Added=" + PlatformHelper.platformToString(platform) + " from specifier: " + specifier);
return platform;
}
Aggregations