//private static DescriptorDigest fakeDigest = DescriptorDigest.fromHash(new string('a', 64));

        private void SetUp(ManifestFormat imageFormat)
        {
            Image.Builder testImageBuilder =
                Image.CreateBuilder(imageFormat)
                .SetCreated(Instant.FromUnixTimeSeconds(20))
                .SetArchitecture("wasm")
                .SetOs("js")
                .AddEnvironmentVariable("VAR1", "VAL1")
                .AddEnvironmentVariable("VAR2", "VAL2")
                .SetEntrypoint(new[] { "some", "entrypoint", "command" })
                .SetProgramArguments(new[] { "arg1", "arg2" })
                .SetHealthCheck(
                    DockerHealthCheck.FromCommand(ImmutableArray.Create("CMD-SHELL", "/checkhealth"))
                    .SetInterval(Duration.FromSeconds(3))
                    .SetTimeout(Duration.FromSeconds(1))
                    .SetStartPeriod(Duration.FromSeconds(2))
                    .SetRetries(3)
                    .Build())
                .AddExposedPorts(ImmutableHashSet.Create(Port.Tcp(1000), Port.Tcp(2000), Port.Udp(3000)))
                .AddVolumes(
                    ImmutableHashSet.Create(
                        AbsoluteUnixPath.Get("/var/job-result-data"),
                        AbsoluteUnixPath.Get("/var/log/my-app-logs")))
                .AddLabels(ImmutableDic.Of("key1", "value1", "key2", "value2"))
                .SetWorkingDirectory("/some/workspace")
                .SetUser("tomcat");

            DescriptorDigest fakeDigest =
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");

            testImageBuilder.AddLayer(
                new FakeLayer(fakeDigest));
            testImageBuilder.AddHistory(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(0))
                .SetAuthor("Bazel")
                .SetCreatedBy("bazel build ...")
                .SetEmptyLayer(true)
                .Build());
            testImageBuilder.AddHistory(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(20))
                .SetAuthor("Fib")
                .SetCreatedBy("fib")
                .Build());
            imageToJsonTranslator = new ImageToJsonTranslator(testImageBuilder.Build());
        }
예제 #2
0
        /**
         * Translates {@link V21ManifestTemplate} to {@link Image}.
         *
         * @param manifestTemplate the template containing the image layers.
         * @return the translated {@link Image}.
         * @throws LayerPropertyNotFoundException if adding image layers fails.
         * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
         *     format
         */
        public static Image ToImage(V21ManifestTemplate manifestTemplate)
        {
            manifestTemplate = manifestTemplate ?? throw new ArgumentNullException(nameof(manifestTemplate));
            Image.Builder imageBuilder = Image.CreateBuilder(ManifestFormat.V21);

            // V21 layers are in reverse order of V22. (The first layer is the latest one.)
            foreach (DescriptorDigest digest in manifestTemplate.GetLayerDigests().ToImmutableList().Reverse())
            {
                imageBuilder.AddLayer(new DigestOnlyLayer(digest));
            }

            if (manifestTemplate.GetContainerConfiguration().IsPresent())
            {
                ConfigureBuilderWithContainerConfiguration(
                    imageBuilder, manifestTemplate.GetContainerConfiguration().Get());
            }
            return(imageBuilder.Build());
        }
예제 #3
0
        /**
         * Translates {@link BuildableManifestTemplate} to {@link Image}. Uses the corresponding {@link
         * ContainerConfigurationTemplate} to get the layer diff IDs.
         *
         * @param manifestTemplate the template containing the image layers.
         * @param containerConfigurationTemplate the template containing the diff IDs and container
         *     configuration properties.
         * @return the translated {@link Image}.
         * @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
         */
        public static Image ToImage <T>(
            T manifestTemplate,
            ContainerConfigurationTemplate containerConfigurationTemplate) where T : IBuildableManifestTemplate
        {
            containerConfigurationTemplate =
                containerConfigurationTemplate
                ?? throw new ArgumentNullException(nameof(containerConfigurationTemplate));
            IList <ReferenceNoDiffIdLayer> layers = new List <ReferenceNoDiffIdLayer>();

            foreach (ContentDescriptorTemplate layerObjectTemplate in
                     manifestTemplate.Layers)
            {
                if (layerObjectTemplate.Digest == null)
                {
                    throw new ArgumentException(Resources.JsonToImageTranslatorMissingDigestExceptionMessage);
                }

                layers.Add(new ReferenceNoDiffIdLayer(
                               new BlobDescriptor(layerObjectTemplate.Size, layerObjectTemplate.Digest)));
            }

            IList <DescriptorDigest> diffIds = containerConfigurationTemplate.GetDiffIds();

            if (layers.Count != diffIds.Count)
            {
                throw new LayerCountMismatchException(Resources.JsonToImageTranslatorDiffIdMismatchExceptionMessage);
            }

            Image.Builder imageBuilder = Image.CreateBuilder(manifestTemplate.GetFormat());

            for (int layerIndex = 0; layerIndex < layers.Count; layerIndex++)
            {
                ReferenceNoDiffIdLayer noDiffIdLayer = layers[layerIndex];
                DescriptorDigest       diffId        = diffIds[layerIndex];

                imageBuilder.AddLayer(new ReferenceLayer(noDiffIdLayer.GetBlobDescriptor(), diffId));
            }

            ConfigureBuilderWithContainerConfiguration(imageBuilder, containerConfigurationTemplate);
            return(imageBuilder.Build());
        }
예제 #4
0
        public async Task <Image> CallAsync()
        {
            BaseImageWithAuthorization baseImageWithAuthorization = await pullBaseImageStep.GetFuture().ConfigureAwait(false);

            IReadOnlyList <ICachedLayer> baseImageLayers = await pullAndCacheBaseImageLayersStep.GetFuture().ConfigureAwait(false);

            IReadOnlyList <ICachedLayer> applicationLayers = await buildAndCacheApplicationLayersStep.GetFuture().ConfigureAwait(false);

            using (progressEventDispatcherFactory.Create("building image format", 1))
                using (new TimerEventDispatcher(buildConfiguration.GetEventHandlers(), DESCRIPTION))
                {
                    // Constructs the image.
                    Image.Builder           imageBuilder           = Image.CreateBuilder(buildConfiguration.GetTargetFormat());
                    Image                   baseImage              = baseImageWithAuthorization.GetBaseImage();
                    IContainerConfiguration containerConfiguration =
                        buildConfiguration.GetContainerConfiguration();

                    // Base image layers
                    foreach (ICachedLayer pullAndCacheBaseImageLayer in baseImageLayers)
                    {
                        imageBuilder.AddLayer(pullAndCacheBaseImageLayer);
                    }

                    // Passthrough config and count non-empty history entries
                    int nonEmptyLayerCount = 0;
                    foreach (HistoryEntry historyObject in 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())
                    .SetWorkingDirectory(baseImage.GetWorkingDirectory());

                    // Add history elements for non-empty layers that don't have one yet
                    Instant layerCreationTime =
                        containerConfiguration == null
                        ? ContainerConfiguration.DefaultCreationTime
                        : containerConfiguration.GetCreationTime();
                    for (int count = 0; count < baseImageLayers.Count - nonEmptyLayerCount; count++)
                    {
                        imageBuilder.AddHistory(
                            HistoryEntry.CreateBuilder()
                            .SetCreationTimestamp(layerCreationTime)
                            .SetComment("auto-generated by Fib")
                            .Build());
                    }

                    // Add built layers/configuration
                    foreach (ICachedLayer applicationLayer in applicationLayers)
                    {
                        HistoryEntry.Builder historyBuilder = HistoryEntry.CreateBuilder();
                        if (buildConfiguration.GetToolName() != null)
                        {
                            historyBuilder.SetCreatedBy(buildConfiguration.GetToolName() + ":" + (buildConfiguration.GetToolVersion() ?? "null"));
                        }
                        else
                        {
                            historyBuilder.SetCreatedBy(ProjectInfo.TOOL_NAME + ":" + ProjectInfo.VERSION);
                        }
                        imageBuilder
                        .AddLayer(applicationLayer)
                        .AddHistory(
                            historyBuilder
                            .SetCreationTimestamp(layerCreationTime)
                            .SetAuthor("Fib")
                            .SetComment(applicationLayer.GetLayerType())
                            .Build());
                    }
                    if (containerConfiguration != null)
                    {
                        imageBuilder
                        .AddEnvironment(containerConfiguration.GetEnvironmentMap())
                        .SetCreated(containerConfiguration.GetCreationTime())
                        .SetUser(containerConfiguration.GetUser())
                        .SetEntrypoint(ComputeEntrypoint(baseImage, containerConfiguration))
                        .SetProgramArguments(ComputeProgramArguments(baseImage, containerConfiguration))
                        .AddExposedPorts(containerConfiguration.GetExposedPorts())
                        .AddVolumes(containerConfiguration.GetVolumes())
                        .AddLabels(containerConfiguration.GetLabels());
                        if (containerConfiguration.GetWorkingDirectory() != null)
                        {
                            imageBuilder.SetWorkingDirectory(containerConfiguration.GetWorkingDirectory().ToString());
                        }
                    }

                    // Gets the container configuration content descriptor.
                    return(imageBuilder.Build());
                }
        }