public void TestToBuildConfiguration_containerConfigurationAdd()
        {
            FibContainerBuilder fibContainerBuilder =
                new FibContainerBuilder(RegistryImage.Named("base/image"), buildConfigurationBuilder)
                .SetEntrypoint("entry", "point")
                .SetEnvironment(ImmutableDic.Of("name", "value"))
                .AddEnvironmentVariable("environment", "variable")
                .SetExposedPorts(Port.Tcp(1234), Port.Udp(5678))
                .AddExposedPort(Port.Tcp(1337))
                .SetLabels(ImmutableDic.Of("key", "value"))
                .AddLabel("added", "label")
                .SetProgramArguments("program", "arguments");

            BuildConfiguration buildConfiguration =
                fibContainerBuilder.ToBuildConfiguration(
                    Containerizer.To(RegistryImage.Named("target/image")));
            IContainerConfiguration containerConfiguration = buildConfiguration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "entry", "point" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(
                ImmutableDic.Of("name", "value", "environment", "variable"),
                containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(
                ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678), Port.Tcp(1337)),
                containerConfiguration.GetExposedPorts());
            Assert.AreEqual(
                ImmutableDic.Of("key", "value", "added", "label"), containerConfiguration.GetLabels());
            Assert.AreEqual(
                new[] { "program", "arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(Instant.FromUnixTimeSeconds(0), containerConfiguration.GetCreationTime());
        }
Exemple #2
0
        public void TestGetContainerBuilder_BaseImage()
        {
            LayerConfiguration[] layerConfigurations = new[] { new LayerConfiguration("appLayer", ImmutableArray <LayerEntry> .Empty) };
            var cliConfiguration = new FibCliConfiguration
            {
                ImageFormat = ImageFormat.OCI,
                ImageLayers = layerConfigurations,
                BaseImage   = "reg.io/base-image:tag",
                Entrypoint  = new[] { "Entrypoint" },
                Cmd         = new[] { "Program", "Arguments" },
                Environment = new Dictionary <string, string> {
                    ["Var"] = "Value"
                },
                ImageWorkingDirectory = AbsoluteUnixPath.Get("/working/dir"),
                ImageUser             = "******",
                Ports   = new[] { Port.Tcp(5000) },
                Volumes = new[] { AbsoluteUnixPath.Get("/volume") },
                Labels  = new Dictionary <string, string> {
                    ["Label"] = "data"
                },
                ReproducableBuild = true
            };

            var builder = cliConfiguration.GetContainerBuilder();

            var configuration = builder.ToBuildConfiguration(Containerizer.To(DockerDaemonImage.Named("target-image")));

            Assert.AreEqual(ManifestFormat.OCI, configuration.GetTargetFormat());
            Assert.AreEqual(layerConfigurations, configuration.GetLayerConfigurations());

            ImageConfiguration imageConfiguration = configuration.GetBaseImageConfiguration();

            Assert.AreEqual("reg.io", imageConfiguration.GetImageRegistry());
            Assert.AreEqual("base-image", imageConfiguration.GetImageRepository());
            Assert.AreEqual("tag", imageConfiguration.GetImageTag());

            IContainerConfiguration containerConfiguration = configuration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "Entrypoint" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(new[] { "Program", "Arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(new Dictionary <string, string> {
                ["Var"] = "Value"
            }, containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(AbsoluteUnixPath.Get("/working/dir"), containerConfiguration.GetWorkingDirectory());
            Assert.AreEqual("user", containerConfiguration.GetUser());
            Assert.AreEqual(new[] { Port.Tcp(5000) }, containerConfiguration.GetExposedPorts());
            Assert.AreEqual(new[] { AbsoluteUnixPath.Get("/volume") }, containerConfiguration.GetVolumes());
            Assert.AreEqual(new Dictionary <string, string> {
                ["Label"] = "data"
            }, containerConfiguration.GetLabels());
            Assert.AreEqual(ContainerConfiguration.DefaultCreationTime, containerConfiguration.GetCreationTime());
        }
        public void TestToBuildConfiguration_containerConfigurationSet()
        {
            FibContainerBuilder fibContainerBuilder =
                new FibContainerBuilder(RegistryImage.Named("base/image"), buildConfigurationBuilder)
                .SetEntrypoint(new[] { "entry", "point" })
                .SetEnvironment(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["name"] = "value"
            }))
                .SetExposedPorts(ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678)))
                .SetLabels(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["key"] = "value"
            }))
                .SetProgramArguments(new[] { "program", "arguments" })
                .SetCreationTime(DateTimeOffset.FromUnixTimeMilliseconds(1000))
                .SetUser("user")
                .SetWorkingDirectory(AbsoluteUnixPath.Get("/working/directory"));

            BuildConfiguration buildConfiguration =
                fibContainerBuilder.ToBuildConfiguration(
                    Containerizer.To(RegistryImage.Named("target/image")));
            IContainerConfiguration containerConfiguration = buildConfiguration.GetContainerConfiguration();

            Assert.AreEqual(new[] { "entry", "point" }, containerConfiguration.GetEntrypoint());
            Assert.AreEqual(
                ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["name"] = "value"
            }), containerConfiguration.GetEnvironmentMap());
            Assert.AreEqual(
                ImmutableHashSet.Create(Port.Tcp(1234), Port.Udp(5678)), containerConfiguration.GetExposedPorts());
            Assert.AreEqual(ImmutableDictionary.CreateRange(new Dictionary <string, string> {
                ["key"] = "value"
            }), containerConfiguration.GetLabels());
            Assert.AreEqual(
                new[] { "program", "arguments" }, containerConfiguration.GetProgramArguments());
            Assert.AreEqual(Instant.FromUnixTimeMilliseconds(1000), containerConfiguration.GetCreationTime());
            Assert.AreEqual("user", containerConfiguration.GetUser());
            Assert.AreEqual(
                AbsoluteUnixPath.Get("/working/directory"), containerConfiguration.GetWorkingDirectory());
        }
Exemple #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());
                }
        }