Exemplo n.º 1
0
        internal static ModuleRuntimeInfo InspectResponseToModule(ContainerInspectResponse inspectResponse)
        {
            // Get the following runtime state:
            //  - name
            //  - exit code
            //  - exit status description
            //  - last start time
            //  - last exit time
            //  - image hash
            (
                string name,
                int exitCode,
                string statusDescription,
                DateTime lastStartTime,
                DateTime lastExitTime,
                string imageHash
            ) = ExtractModuleRuntimeState(inspectResponse);

            var dockerConfig = new DockerReportedConfig(string.Empty, string.Empty, imageHash);

            // Figure out module stats and runtime status
            ModuleStatus runtimeStatus = ToRuntimeStatus(inspectResponse.State);

            var reportedConfig    = new DockerReportedConfig(string.Empty, string.Empty, imageHash);
            var moduleRuntimeInfo = new ModuleRuntimeInfo <DockerReportedConfig>(name, "docker", runtimeStatus, statusDescription, exitCode, Option.Some(lastStartTime), Option.Some(lastExitTime), reportedConfig);

            return(moduleRuntimeInfo);
        }
Exemplo n.º 2
0
        public static ModuleRuntimeInfo ConvertToRuntime(this V1Pod pod, string name)
        {
            Option <V1ContainerStatus> containerStatus = GetContainerByName(name, pod);
            ReportedModuleStatus       moduleStatus    = ConvertPodStatusToModuleStatus(containerStatus);
            RuntimeData runtimeData = GetRuntimeData(containerStatus.OrDefault());

            string moduleName = string.Empty;

            if (!(pod.Metadata?.Annotations?.TryGetValue(KubernetesConstants.K8sEdgeOriginalModuleId, out moduleName) ?? false))
            {
                moduleName = name;
            }

            var reportedConfig = new AgentDocker.DockerReportedConfig(runtimeData.ImageName, string.Empty, string.Empty);

            return(new ModuleRuntimeInfo <AgentDocker.DockerReportedConfig>(
                       moduleName,
                       "docker",
                       moduleStatus.Status,
                       moduleStatus.Description,
                       runtimeData.ExitStatus,
                       runtimeData.StartTime,
                       runtimeData.EndTime,
                       reportedConfig));
        }
Exemplo n.º 3
0
        public async Task <ModuleSet> GetModulesAsync(CancellationToken token)
        {
            IEnumerable <ModuleRuntimeInfo> moduleStatuses = await this.moduleStatusProvider.GetModules(token);

            var       modules   = new List <IModule>();
            ModuleSet moduleSet = this.deploymentConfig.GetModuleSet();

            foreach (ModuleRuntimeInfo moduleRuntimeInfo in moduleStatuses)
            {
                if (moduleRuntimeInfo.Type != "docker" || !(moduleRuntimeInfo is ModuleRuntimeInfo <DockerReportedConfig> dockerRuntimeInfo))
                {
                    Events.InvalidModuleType(moduleRuntimeInfo);
                    continue;
                }

                if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
                {
                    dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, RestartPolicy.Unknown, new DockerConfig(string.Empty), new ConfigurationInfo(), null);
                }

                Option <ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));
                // compute module state based on restart policy
                DateTime     lastExitTime        = moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue);
                ModuleStatus moduleRuntimeStatus = this.restartManager.ComputeModuleStatusFromRestartPolicy(moduleRuntimeInfo.ModuleStatus, dockerModule.RestartPolicy, moduleState.RestartCount, lastExitTime);
                string       image = !string.IsNullOrWhiteSpace(dockerRuntimeInfo.Config.Image) ? dockerRuntimeInfo.Config.Image : dockerModule.Config.Image;
                var          dockerReportedConfig = new DockerReportedConfig(image, dockerModule.Config.CreateOptions, dockerRuntimeInfo.Config.ImageHash);
                IModule      module;
                switch (moduleRuntimeInfo.Name)
                {
                case Core.Constants.EdgeHubModuleName:
                    module = new EdgeHubDockerRuntimeModule(
                        dockerModule.DesiredStatus, dockerModule.RestartPolicy, dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode, dockerRuntimeInfo.Description, dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime, moduleState.RestartCount, moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;

                case Core.Constants.EdgeAgentModuleName:
                    module = new EdgeAgentDockerRuntimeModule(dockerReportedConfig, moduleRuntimeStatus,
                                                              (int)dockerRuntimeInfo.ExitCode, dockerRuntimeInfo.Description, dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                                                              lastExitTime, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;

                default:
                    module = new DockerRuntimeModule(
                        moduleRuntimeInfo.Name, dockerModule.Version, dockerModule.DesiredStatus, dockerModule.RestartPolicy, dockerReportedConfig,
                        (int)moduleRuntimeInfo.ExitCode, moduleRuntimeInfo.Description, moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue), lastExitTime,
                        moduleState.RestartCount, moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus, dockerModule.ConfigurationInfo, dockerModule.Env);
                    break;
                }

                modules.Add(module);
            }
            return(new ModuleSet(modules.ToDictionary(m => m.Name, m => m)));
        }
Exemplo n.º 4
0
        public async Task <ModuleSet> GetModulesAsync(CancellationToken token)
        {
            IEnumerable <ModuleRuntimeInfo> moduleStatuses = await this.moduleStatusProvider.GetModules(token);

            var       modules   = new List <IModule>();
            ModuleSet moduleSet = this.deploymentConfig.GetModuleSet();

            foreach (ModuleRuntimeInfo moduleRuntimeInfo in moduleStatuses)
            {
                if (moduleRuntimeInfo.Type != "docker" || !(moduleRuntimeInfo is ModuleRuntimeInfo <DockerReportedConfig> dockerRuntimeInfo))
                {
                    Events.InvalidModuleType(moduleRuntimeInfo);
                    continue;
                }

                if (!moduleSet.Modules.TryGetValue(dockerRuntimeInfo.Name, out IModule configModule) || !(configModule is DockerModule dockerModule))
                {
                    // This is given the highest priority so that it's removal is prioritized first before other known modules are processed.
                    dockerModule = new DockerModule(dockerRuntimeInfo.Name, string.Empty, ModuleStatus.Unknown, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters(), Option.None <string>()), ImagePullPolicy.OnCreate, Core.Constants.HighestPriority, new ConfigurationInfo(), null);
                }

                Option <ModuleState> moduleStateOption = await this.moduleStateStore.Get(moduleRuntimeInfo.Name);

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));
                // compute module state based on restart policy
                DateTime     lastExitTime        = moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue);
                ModuleStatus moduleRuntimeStatus = dockerModule.DesiredStatus == ModuleStatus.Running
                    ? this.restartManager.ComputeModuleStatusFromRestartPolicy(moduleRuntimeInfo.ModuleStatus, dockerModule.RestartPolicy, moduleState.RestartCount, lastExitTime)
                    : moduleRuntimeInfo.ModuleStatus;

                string  image = !string.IsNullOrWhiteSpace(dockerRuntimeInfo.Config.Image) ? dockerRuntimeInfo.Config.Image : dockerModule.Config.Image;
                var     dockerReportedConfig = new DockerReportedConfig(image, dockerModule.Config.CreateOptions, dockerRuntimeInfo.Config.ImageHash, dockerModule.Config.Digest);
                IModule module;
                switch (moduleRuntimeInfo.Name)
                {
                case Core.Constants.EdgeHubModuleName:
                    module = new EdgeHubDockerRuntimeModule(
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)dockerRuntimeInfo.ExitCode,
                        dockerRuntimeInfo.Description,
                        dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.StartupOrder,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;

                case Core.Constants.EdgeAgentModuleName:
                    var labels = dockerRuntimeInfo.Config.CreateOptions?.Labels ?? new Dictionary <string, string>();
                    var env    = labels.TryGetValue(Core.Constants.Labels.Env, out string envStr)
                            ? JsonConvert.DeserializeObject <Dictionary <string, EnvVal> >(envStr)
                            : new Dictionary <string, EnvVal>();

                    module = new EdgeAgentDockerRuntimeModule(
                        dockerRuntimeInfo.Config,
                        moduleRuntimeStatus,
                        (int)dockerRuntimeInfo.ExitCode,
                        dockerRuntimeInfo.Description,
                        dockerRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        dockerModule.ImagePullPolicy,
                        dockerModule.ConfigurationInfo,
                        env);
                    break;

                default:
                    module = new DockerRuntimeModule(
                        moduleRuntimeInfo.Name,
                        dockerModule.Version,
                        dockerModule.DesiredStatus,
                        dockerModule.RestartPolicy,
                        dockerReportedConfig,
                        (int)moduleRuntimeInfo.ExitCode,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        lastExitTime,
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.StartupOrder,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;
                }

                modules.Add(module);
            }

            return(new ModuleSet(modules.ToDictionary(m => m.Name, m => m)));
        }