Example #1
0
        /**
         * Gets the container configuration as a {@link Blob}.
         *
         * @return the container configuration {@link Blob}
         */
        public ContainerConfigurationTemplate GetContainerConfiguration()
        {
            // ISet up the JSON template.
            ContainerConfigurationTemplate template = new ContainerConfigurationTemplate();

            // Adds the layer diff IDs.
            foreach (ILayer layer in image.GetLayers())
            {
                template.AddLayerDiffId(layer.GetDiffId());
            }

            // Adds the history.
            foreach (HistoryEntry historyObject in image.GetHistory())
            {
                template.AddHistoryEntry(historyObject);
            }

            template.Created      = image.GetCreated()?.ToString();
            template.Architecture = image.GetArchitecture();
            template.Os           = image.GetOs();
            template.SetContainerEnvironment(EnvironmentMapToList(image.GetEnvironment()));
            template.SetContainerEntrypoint(image.GetEntrypoint());
            template.SetContainerCmd(image.GetProgramArguments());
            template.SetContainerExposedPorts(PortSetToMap(image.GetExposedPorts()));
            template.SetContainerVolumes(VolumesSetToMap(image.GetVolumes()));
            template.SetContainerLabels(image.GetLabels());
            template.SetContainerWorkingDir(image.GetWorkingDirectory());
            template.SetContainerUser(image.GetUser());

            // Ignore healthcheck if not Docker/command is empty
            DockerHealthCheck healthCheck = image.GetHealthCheck();

            if (image.GetImageFormat() == ManifestFormat.V22 && healthCheck != null)
            {
                template.SetContainerHealthCheckTest(healthCheck.GetCommand());
                healthCheck
                .GetInterval()
                .IfPresent(interval => template.SetContainerHealthCheckInterval((long)interval.TotalNanoseconds));
                healthCheck
                .GetTimeout()
                .IfPresent(timeout => template.SetContainerHealthCheckTimeout((long)timeout.TotalNanoseconds));
                healthCheck
                .GetStartPeriod()
                .IfPresent(
                    startPeriod => template.SetContainerHealthCheckStartPeriod((long)startPeriod.TotalNanoseconds));
                template.SetContainerHealthCheckRetries(healthCheck.GetRetries().AsNullable());
            }

            return(template);
        }
Example #2
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());
        }
Example #3
0
 public ManifestAndConfig(
     IManifestTemplate manifest, ContainerConfigurationTemplate config)
 {
     this.manifest = manifest;
     this.config   = config;
 }
Example #4
0
        private static void ConfigureBuilderWithContainerConfiguration(
            Image.Builder imageBuilder, ContainerConfigurationTemplate containerConfigurationTemplate)
        {
            foreach (HistoryEntry i in containerConfigurationTemplate.History)
            {
                imageBuilder.AddHistory(i);
            }
            if (containerConfigurationTemplate.Created != null)
            {
                try
                {
                    imageBuilder.SetCreated(
                        Instant.FromDateTimeOffset(
                            DateTimeOffset.Parse(
                                containerConfigurationTemplate.Created,
                                CultureInfo.InvariantCulture)));
                }
                catch (FormatException ex)
                {
                    throw new BadContainerConfigurationFormatException(
                              "Invalid image creation time: " + containerConfigurationTemplate.Created, ex);
                }
            }

            if (containerConfigurationTemplate.Architecture != null)
            {
                imageBuilder.SetArchitecture(containerConfigurationTemplate.Architecture);
            }
            if (containerConfigurationTemplate.Os != null)
            {
                imageBuilder.SetOs(containerConfigurationTemplate.Os);
            }

            imageBuilder.SetEntrypoint(containerConfigurationTemplate.GetContainerEntrypoint());
            imageBuilder.SetProgramArguments(containerConfigurationTemplate.GetContainerCmd());

            IList <string> baseHealthCheckCommand = containerConfigurationTemplate.GetContainerHealthTest();

            if (baseHealthCheckCommand != null)
            {
                DockerHealthCheck.Builder builder = DockerHealthCheck.FromCommand(baseHealthCheckCommand);
                if (containerConfigurationTemplate.GetContainerHealthInterval() != null)
                {
                    builder.SetInterval(
                        Duration.FromNanoseconds(containerConfigurationTemplate.GetContainerHealthInterval().GetValueOrDefault()));
                }
                if (containerConfigurationTemplate.GetContainerHealthTimeout() != null)
                {
                    builder.SetTimeout(
                        Duration.FromNanoseconds(containerConfigurationTemplate.GetContainerHealthTimeout().GetValueOrDefault()));
                }
                if (containerConfigurationTemplate.GetContainerHealthStartPeriod() != null)
                {
                    builder.SetStartPeriod(
                        Duration.FromNanoseconds(containerConfigurationTemplate.GetContainerHealthStartPeriod().GetValueOrDefault()));
                }
                if (containerConfigurationTemplate.GetContainerHealthRetries() != null)
                {
                    builder.SetRetries(containerConfigurationTemplate.GetContainerHealthRetries().GetValueOrDefault());
                }
                imageBuilder.SetHealthCheck(builder.Build());
            }

            if (containerConfigurationTemplate.GetContainerExposedPorts() != null)
            {
                imageBuilder.AddExposedPorts(
                    PortMapToSet(containerConfigurationTemplate.GetContainerExposedPorts()));
            }

            if (containerConfigurationTemplate.GetContainerVolumes() != null)
            {
                imageBuilder.AddVolumes(VolumeMapToSet(containerConfigurationTemplate.GetContainerVolumes()));
            }

            if (containerConfigurationTemplate.GetContainerEnvironment() != null)
            {
                foreach (string environmentVariable in containerConfigurationTemplate.GetContainerEnvironment())
                {
                    Match matcher = EnvironmentPattern.Match(environmentVariable);
                    if (!matcher.Success)
                    {
                        throw new BadContainerConfigurationFormatException(
                                  "Invalid environment variable definition: " + environmentVariable);
                    }
                    imageBuilder.AddEnvironmentVariable(matcher.Groups["name"].Value, matcher.Groups["value"].Value);
                }
            }

            imageBuilder.AddLabels(containerConfigurationTemplate.GetContainerLabels());
            imageBuilder.SetWorkingDirectory(containerConfigurationTemplate.GetContainerWorkingDir());
            imageBuilder.SetUser(containerConfigurationTemplate.GetContainerUser());
        }