Skip to content

Instantly share code, notes, and snippets.

@tzolov
Created January 31, 2020 08:43
Show Gist options
  • Save tzolov/0bf0509a1f439715aa8896763925cbd1 to your computer and use it in GitHub Desktop.
Save tzolov/0bf0509a1f439715aa8896763925cbd1 to your computer and use it in GitHub Desktop.
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
import com.google.cloud.tools.jib.api.Credential;
import com.google.cloud.tools.jib.api.DescriptorDigest;
import com.google.cloud.tools.jib.api.ImageReference;
import com.google.cloud.tools.jib.api.RegistryException;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.builder.ProgressEventDispatcher;
import com.google.cloud.tools.jib.configuration.BuildConfiguration;
import com.google.cloud.tools.jib.configuration.ImageConfiguration;
import com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer;
import com.google.cloud.tools.jib.image.Image;
import com.google.cloud.tools.jib.image.LayerCountMismatchException;
import com.google.cloud.tools.jib.image.json.BadContainerConfigurationFormatException;
import com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate;
import com.google.cloud.tools.jib.image.json.JsonToImageTranslator;
import com.google.cloud.tools.jib.image.json.ManifestTemplate;
import com.google.cloud.tools.jib.image.json.V21ManifestTemplate;
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
import com.google.cloud.tools.jib.json.JsonTemplateMapper;
import com.google.cloud.tools.jib.registry.ManifestAndDigest;
import com.google.cloud.tools.jib.registry.RegistryAuthenticator;
import com.google.cloud.tools.jib.registry.RegistryClient;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
/**
* <dependency>
* <groupId>com.google.cloud.tools</groupId>
* <artifactId>jib-core</artifactId>
* <version>0.12.0</version>
* </dependency>
* @author Christian Tzolov
*/
public class JibDockerImageLabelsReader {
public static class ThrottledProgressEventDispatcherWrapper implements Closeable {
private final ProgressEventDispatcher.Factory progressEventDispatcherFactory;
private final String description;
@Nullable
private ProgressEventDispatcher progressEventDispatcher;
@Nullable
private ThrottledAccumulatingConsumer throttledDispatcher;
ThrottledProgressEventDispatcherWrapper(
ProgressEventDispatcher.Factory progressEventDispatcherFactory, String description) {
this.progressEventDispatcherFactory = progressEventDispatcherFactory;
this.description = description;
}
public void dispatchProgress(Long progressUnits) {
Preconditions.checkNotNull(throttledDispatcher);
throttledDispatcher.accept(progressUnits);
}
@Override
public void close() {
Preconditions.checkNotNull(progressEventDispatcher);
Preconditions.checkNotNull(throttledDispatcher);
throttledDispatcher.close();
progressEventDispatcher.close();
}
void setProgressTarget(long allocationUnits) {
Preconditions.checkState(progressEventDispatcher == null);
progressEventDispatcher = progressEventDispatcherFactory.create(description, allocationUnits);
throttledDispatcher =
new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress);
}
}
// Check StepsRunner.begin() and StepsRunner.pullBaseImage() and PullBaseImageStep()
public static void main(String[] args) throws Exception {
Path path = Files.createTempDir().toPath();
ImageConfiguration imageConfiguration =
ImageConfiguration.builder(ImageReference.parse("tzolov/my-log-sink:latest")).build();
//ImageConfiguration.builder(ImageReference.parse("tzolov/credit-card-transaction-generator:latest")).build();
BuildConfiguration buildConfiguration = BuildConfiguration.builder()
.setBaseImageConfiguration(imageConfiguration)
// we don't need the target image but still need to provide non-null value.
.setTargetImageConfiguration(ImageConfiguration.builder(ImageReference.parse("fake/fake:latest")).build())
.setBaseImageLayersCacheDirectory(Files.createTempDir().toPath())
.setApplicationLayersCacheDirectory(Files.createTempDir().toPath())
.setExecutorService(Executors.newSingleThreadExecutor())
.build();
System.out.println("Format:" + buildConfiguration.getTargetFormat().getSimpleName());
RegistryAuthenticator registryAuthenticator = buildConfiguration
.newBaseImageRegistryClientFactory()
.newRegistryClient()
.getRegistryAuthenticator().get();
RegistryClient registryClient = buildConfiguration.newBaseImageRegistryClientFactory()
.setAuthorization(registryAuthenticator.authenticatePull(Credential.from("XXXXXX", "XXXXXX")))
.newRegistryClient();
labelsManifestV21(registryClient);
labelsManifestV22(buildConfiguration, registryClient);
////RegistryImage image = RegistryImage
//// .named("tzolov/credit-card-transaction-generator:latest")
//// .addCredential("XXXXX", "XXXXXX");
////
////Containerizer containerizer = Containerizer.to(image);
////
////
////JibContainer container = Jib.from("tzolov/credit-card-transaction-generator:latest")
//// .containerize(containerizer);
//
//
//System.out.println(container);
//System.out.println(container);
}
public static Map<String, String> labelsManifestV21(RegistryClient registryClient) throws IOException, RegistryException, BadContainerConfigurationFormatException {
ManifestAndDigest<V21ManifestTemplate> manifestAndDigest = registryClient.pullManifest("latest", V21ManifestTemplate.class);
ManifestTemplate manifestTemplate = manifestAndDigest.getManifest();
System.out.println(manifestTemplate.getSchemaVersion());
Image imageA = JsonToImageTranslator.toImage((V21ManifestTemplate) manifestTemplate);
ImmutableMap<String, String> labels = imageA.getLabels();
System.out.println("Image V21: " + labels);
return labels;
}
public static Map<String, String> labelsManifestV22(BuildConfiguration buildConfiguration, RegistryClient registryClient) throws IOException, RegistryException, LayerCountMismatchException, BadContainerConfigurationFormatException {
ManifestAndDigest<V22ManifestTemplate> manifestV22AndDigest = registryClient.pullManifest("latest", V22ManifestTemplate.class);
DescriptorDigest containerConfigurationDigest =
manifestV22AndDigest.getManifest().getContainerConfiguration().getDigest();
//ProgressEventDispatcher.Factory childProgressDispatcherFactory =
// Verify.verifyNotNull(progressEventDispatcher).newChildProducer();
//new PullBaseImageStep(buildConfiguration, childProgressDispatcherFactory)
try (ProgressEventDispatcher progressEventDispatcher =
ProgressEventDispatcher.newRoot(
buildConfiguration.getEventHandlers(), "rootProgressDescription", 1);
ThrottledProgressEventDispatcherWrapper progressEventDispatcherWrapper =
new ThrottledProgressEventDispatcherWrapper(
progressEventDispatcher.newChildProducer(),
"pull container configuration " + containerConfigurationDigest)) {
String containerConfigurationString =
Blobs.writeToString(
registryClient.pullBlob(
containerConfigurationDigest,
progressEventDispatcherWrapper::setProgressTarget,
progressEventDispatcherWrapper::dispatchProgress));
ContainerConfigurationTemplate containerConfigurationTemplate =
JsonTemplateMapper.readJson(
containerConfigurationString, ContainerConfigurationTemplate.class);
buildConfiguration
.getBaseImageLayersCache()
.writeMetadata(
buildConfiguration.getBaseImageConfiguration().getImage(),
manifestV22AndDigest.getManifest(),
containerConfigurationTemplate);
Image image = JsonToImageTranslator.toImage(
manifestV22AndDigest.getManifest(), containerConfigurationTemplate);
System.out.println("image V22 labels: " + image.getLabels());
return image.getLabels();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment