use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.
the class RegistryCredentialRetrieverTest method makeFakeBuildContext.
private BuildContext makeFakeBuildContext(List<CredentialRetriever> baseCredentialRetrievers, List<CredentialRetriever> targetCredentialRetrievers) throws CacheDirectoryCreationException {
ImageReference baseImage = ImageReference.of("baseregistry", "baserepo", null);
ImageReference targetImage = ImageReference.of("targetregistry", "targetrepo", null);
return BuildContext.builder().setEventHandlers(mockEventHandlers).setBaseImageConfiguration(ImageConfiguration.builder(baseImage).setCredentialRetrievers(baseCredentialRetrievers).build()).setTargetImageConfiguration(ImageConfiguration.builder(targetImage).setCredentialRetrievers(targetCredentialRetrievers).build()).setContainerConfiguration(ContainerConfiguration.builder().build()).setBaseImageLayersCacheDirectory(Paths.get("ignored")).setApplicationLayersCacheDirectory(Paths.get("ignored")).setExecutorService(MoreExecutors.newDirectExecutorService()).build();
}
use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.
the class ConfigurationPropertyValidatorTest method testGetGeneratedTargetDockerTag.
@Test
public void testGetGeneratedTargetDockerTag() throws InvalidImageReferenceException {
HelpfulSuggestions helpfulSuggestions = new HelpfulSuggestions("", "", "to", "--to", "build.txt");
// Target configured
ProjectProperties mockProjectProperties = mock(ProjectProperties.class);
when(mockProjectProperties.getName()).thenReturn("project-name");
when(mockProjectProperties.getVersion()).thenReturn("project-version");
ImageReference result = ConfigurationPropertyValidator.getGeneratedTargetDockerTag("a/b:c", mockProjectProperties, helpfulSuggestions);
assertThat(result.getRepository()).isEqualTo("a/b");
assertThat(result.getTag()).hasValue("c");
verify(mockLogger, never()).accept(LogEvent.lifecycle(any()));
// Target not configured
result = ConfigurationPropertyValidator.getGeneratedTargetDockerTag(null, mockProjectProperties, helpfulSuggestions);
assertThat(result.getRepository()).isEqualTo("project-name");
assertThat(result.getTag()).hasValue("project-version");
verify(mockProjectProperties).log(LogEvent.lifecycle("Tagging image with generated image reference project-name:project-version. If you'd " + "like to specify a different tag, you can set the to parameter in your " + "build.txt, or use the --to=<MY IMAGE> commandline flag."));
// Generated tag invalid
when(mockProjectProperties.getName()).thenReturn("%#&///*@(");
when(mockProjectProperties.getVersion()).thenReturn("%$#//&*@($");
assertThrows(InvalidImageReferenceException.class, () -> ConfigurationPropertyValidator.getGeneratedTargetDockerTag(null, mockProjectProperties, helpfulSuggestions));
}
use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.
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.ImageReference in project jib by google.
the class Containerizers method create.
private static Containerizer create(CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
String imageSpec = commonCliOptions.getTargetImage();
if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
// TODO: allow setting docker env and docker executable (along with path/env)
return Containerizer.to(DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
}
if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
return Containerizer.to(TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, ""))).named(commonCliOptions.getName()));
}
ImageReference imageReference = ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
RegistryImage registryImage = RegistryImage.named(imageReference);
DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
Credentials.getToCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
return Containerizer.to(registryImage);
}
use of com.google.cloud.tools.jib.api.ImageReference in project jib by google.
the class PluginConfigurationProcessor method createJibBuildRunnerForRegistryImage.
/**
* Generate a runner for image builds to registries.
*
* @param rawConfiguration the raw configuration from the plugin
* @param inferredAuthProvider the plugin specific auth provider
* @param projectProperties an plugin specific implementation of {@link ProjectProperties}
* @param globalConfig the Jib global config
* @param helpfulSuggestions a plugin specific instance of {@link HelpfulSuggestions}
* @return new {@link JibBuildRunner} to execute a build
* @throws InvalidImageReferenceException if the image reference is invalid
* @throws MainClassInferenceException if a main class could not be found
* @throws InvalidAppRootException if the specific path for application root is invalid
* @throws IOException if an error occurs creating the container builder
* @throws InvalidWorkingDirectoryException if the working directory specified for the build is
* invalid
* @throws InvalidPlatformException if there exists a {@link PlatformConfiguration} in the
* specified platforms list that is missing required fields or has invalid values
* @throws InvalidContainerVolumeException if a specific container volume is invalid
* @throws IncompatibleBaseImageJavaVersionException if the base image java version cannot support
* this build
* @throws NumberFormatException if a string to number conversion operation fails
* @throws InvalidContainerizingModeException if an invalid {@link ContainerizingMode} was
* specified
* @throws InvalidFilesModificationTimeException if configured modification time could not be
* parsed
* @throws InvalidCreationTimeException if configured creation time could not be parsed
* @throws JibPluginExtensionException if an error occurred while running plugin extensions
* @throws ExtraDirectoryNotFoundException if the extra directory specified for the build is not
* found
*/
public static JibBuildRunner createJibBuildRunnerForRegistryImage(RawConfiguration rawConfiguration, InferredAuthProvider inferredAuthProvider, ProjectProperties projectProperties, GlobalConfig globalConfig, HelpfulSuggestions helpfulSuggestions) throws InvalidImageReferenceException, MainClassInferenceException, InvalidAppRootException, IOException, InvalidWorkingDirectoryException, InvalidPlatformException, InvalidContainerVolumeException, IncompatibleBaseImageJavaVersionException, NumberFormatException, InvalidContainerizingModeException, InvalidFilesModificationTimeException, InvalidCreationTimeException, JibPluginExtensionException, ExtraDirectoryNotFoundException {
Optional<String> image = rawConfiguration.getToImage();
Preconditions.checkArgument(image.isPresent());
ImageReference targetImageReference = ImageReference.parse(image.get());
RegistryImage targetImage = RegistryImage.named(targetImageReference);
configureCredentialRetrievers(rawConfiguration, projectProperties, targetImage, targetImageReference, PropertyNames.TO_AUTH_USERNAME, PropertyNames.TO_AUTH_PASSWORD, rawConfiguration.getToAuth(), inferredAuthProvider, rawConfiguration.getToCredHelper());
boolean alwaysCacheBaseImage = Boolean.parseBoolean(rawConfiguration.getProperty(PropertyNames.ALWAYS_CACHE_BASE_IMAGE).orElse("false"));
Containerizer containerizer = Containerizer.to(targetImage).setAlwaysCacheBaseImage(alwaysCacheBaseImage);
Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
JibContainerBuilder jibContainerBuilder = processCommonConfiguration(rawConfiguration, inferredAuthProvider, projectProperties, containerizer);
JibContainerBuilder updatedContainerBuilder = projectProperties.runPluginExtensions(rawConfiguration.getPluginExtensions(), jibContainerBuilder);
return JibBuildRunner.forBuildImage(updatedContainerBuilder, containerizer, projectProperties::log, helpfulSuggestions, targetImageReference, rawConfiguration.getToTags()).writeImageDigest(rawConfiguration.getDigestOutputPath()).writeImageId(rawConfiguration.getImageIdOutputPath()).writeImageJson(rawConfiguration.getImageJsonOutputPath());
}
Aggregations