public async Task CreatePodAsync(Corev1Pod pod)
        {
            if (this.IsEdgeDeployment(pod))
            {
                // get list of config maps in this namespace
                Corev1ConfigMapList configMapList = await this.kubeClient.ListNamespacedConfigMapAsync(pod.Metadata.NamespaceProperty);

                // build configuration
                var configuration = new Configuration(pod.Metadata.Name)
                {
                    TargetCondition = pod.Metadata.Annotations["targetCondition"],
                    Priority        = ParseInt32(pod.Metadata.Annotations["priority"], 10),
                    Labels          = new Dictionary <string, string>(),
                    Content         = new ConfigurationContent
                    {
                        ModuleContent = new Dictionary <string, TwinContent>()
                    }
                };

                // copy over the labels set on the pod as deployment labels
                foreach (KeyValuePair <string, string> label in pod.Metadata.Labels)
                {
                    configuration.Labels.Add(label.Key, label.Value);
                }

                EdgeAgent edgeAgent = this.GetDefaultEdgeAgentConfig();
                this.PopulateEdgeAgentConfig(configMapList, edgeAgent);
                foreach (Corev1Container container in pod.Spec.Containers)
                {
                    var module = new EdgeModule
                    {
                        Settings = new EdgeModuleSettings
                        {
                            Image = container.Image
                        }
                    };
                    this.PopulateModuleConfig(configMapList, container.Name, module);
                    edgeAgent.Modules.Add(container.Name, module);
                }

                EdgeHub edgeHub = this.GetDefaultEdgeHubConfig();
                this.PopulateEdgeHubConfig(configMapList, edgeHub);

                // create module twins
                IList <(string Name, JObject Twin)> moduleTwins = this.PopulateModuleTwins(configMapList, edgeAgent.Modules.Keys);

                // now we have everything we need to build the configuration content
                AddModuleTwin(configuration, "$edgeAgent", edgeAgent);
                AddModuleTwin(configuration, "$edgeHub", edgeHub);
                foreach ((string Name, JObject Twin) in moduleTwins)
                {
                    AddModuleTwin(configuration, Name, Twin);
                }

                await this.registryManager.AddConfigurationAsync(configuration);
            }

            pod.Status            = pod.Status ?? new Corev1PodStatus();
            pod.Status.Phase      = "Running";
            pod.Status.Conditions = new List <Corev1PodCondition>
            {
                new Corev1PodCondition("True", "Initialized", null, DateTime.UtcNow),
                new Corev1PodCondition("True", "Ready", null, DateTime.UtcNow),
                new Corev1PodCondition("True", "PodScheduled", null, DateTime.UtcNow)
            };
            this.podsMap.AddOrUpdate(pod.Metadata?.Name ?? string.Empty, pod, (k, v) => pod);
        }
        void PopulateModuleConfig(Corev1ConfigMapList configMapList, string moduleName, EdgeModule module)
        {
            // see if there is an entry for this module in the config maps
            Corev1ConfigMap configMap = configMapList.Items.Where(cm => cm.Metadata.Name == moduleName).FirstOrDefault();

            module.RestartPolicy          = configMap?.Data?["restartPolicy"] ?? "on-unhealthy";
            module.Version                = configMap?.Data?["version"] ?? "1.0";
            module.Status                 = configMap?.Data?["status"] ?? "running";
            module.Settings.CreateOptions = configMap?.Data?["createOptions"] ?? "{}";
        }
Exemple #3
0
        void PopulateModuleConfig(Corev1ConfigMapList configMapList, string moduleName, EdgeModule module)
        {
            // see if there is an entry for this module in the config maps
            Corev1ConfigMap configMap = configMapList.Items.Where(cm => cm.Metadata.Name == moduleName).FirstOrDefault();

            if (configMap != null && configMap.Data != null)
            {
                if (configMap.Data.TryGetValue("restartPolicy", out string configMapRestartPolicy))
                {
                    module.RestartPolicy = configMapRestartPolicy ?? "on-unhealthy";
                }
                else
                {
                    module.RestartPolicy = "on-unhealthy";
                }
                if (configMap.Data.TryGetValue("version", out string configMapVersion))
                {
                    module.Version = configMapVersion ?? "1.0";
                }
                else
                {
                    module.Version = "1.0";
                }
                if (configMap.Data.TryGetValue("status", out string configMapStatus))
                {
                    module.Status = configMapStatus ?? "running";
                }
                else
                {
                    module.Status = "running";
                }
                if (configMap.Data.TryGetValue("createOptions", out string configMapCreateOptions))
                {
                    module.Settings.CreateOptions = configMapCreateOptions ?? "{}";
                }
                else
                {
                    module.Settings.CreateOptions = "{}";
                }
            }
            else
            {
                module.RestartPolicy          = "on-unhealthy";
                module.Version                = "1.0";
                module.Status                 = "running";
                module.Settings.CreateOptions = "{}";
            }
        }