예제 #1
0
        public async void CrdCommandExecuteEdgeAgentGetsCurrentImage()
        {
            IModule        dockerModule         = new DockerModule("edgeAgent", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            IRuntimeModule currentModule        = new EdgeAgentDockerRuntimeModule(AgentConfig1, ModuleStatus.Running, 0, "description", DateTime.Today, DateTime.Today, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var            dockerConfigProvider = new Mock <ICombinedConfigProvider <CombinedDockerConfig> >();

            dockerConfigProvider.Setup(cp => cp.GetCombinedConfig(dockerModule, Runtime))
            .Returns(() => new CombinedDockerConfig("test-image:1", Config1.CreateOptions, Option.Maybe(DockerAuth)));
            var configProvider = new Mock <ICombinedConfigProvider <CombinedKubernetesConfig> >();

            configProvider.Setup(cp => cp.GetCombinedConfig(dockerModule, Runtime))
            .Returns(() => new CombinedKubernetesConfig("test-image:1", CreatePodParameters.Create(image: "test-image:1"), Option.Maybe(ImagePullSecret)));
            configProvider.Setup(cp => cp.GetCombinedConfig(currentModule, Runtime))
            .Returns(() => new CombinedKubernetesConfig(AgentConfig1.Image, CreatePodParameters.Create(image: AgentConfig1.Image), Option.Maybe(ImagePullSecret)));
            var edgeDefinition              = Option.None <EdgeDeploymentDefinition>();
            KubernetesConfig kc             = new KubernetesConfig(AgentConfig1.Image, CreatePodParameters.Create(), Option.None <AuthConfig>());
            var edgeDefinitionCurrentModule = new EdgeDeploymentDefinition("v1", "EdgeDeployment", new V1ObjectMeta(name: ResourceName), new List <KubernetesModule>()
            {
                new KubernetesModule(currentModule, kc, EdgeletModuleOwner)
            }, null);

            using (var server = new KubernetesApiServer(
                       resp: string.Empty,
                       shouldNext: httpContext =>
            {
                string pathStr = httpContext.Request.Path.Value;
                string method = httpContext.Request.Method;
                if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase))
                {
                    httpContext.Response.StatusCode = 404;
                }
                else if (string.Equals(method, "PUT", StringComparison.OrdinalIgnoreCase))
                {
                    httpContext.Response.Body = httpContext.Request.Body;
                    if (pathStr.Contains($"namespaces/{Namespace}/{Constants.EdgeDeployment.Plural}"))
                    {
                        StreamReader reader = new StreamReader(httpContext.Request.Body);
                        string bodyText = reader.ReadToEnd();
                        var body = JsonConvert.DeserializeObject <EdgeDeploymentDefinition>(bodyText);
                        edgeDefinition = Option.Maybe(body);
                    }
                }

                return(Task.FromResult(false));
            }))
            {
                var client = new Kubernetes(new KubernetesClientConfiguration {
                    Host = server.Uri
                });
                var cmd = new EdgeDeploymentCommand(Namespace, ResourceName, client, new[] { dockerModule }, Option.Maybe(edgeDefinitionCurrentModule), Runtime, configProvider.Object, EdgeletModuleOwner);

                await cmd.ExecuteAsync(CancellationToken.None);

                Assert.True(edgeDefinition.HasValue);
                var receivedEdgeDefinition = edgeDefinition.OrDefault();
                var agentModule            = receivedEdgeDefinition.Spec[0];
                Assert.Equal(AgentConfig1.Image, agentModule.Config.Image);
            }
        }
예제 #2
0
        public void TestJsonSerialize()
        {
            // Arrange
            DateTime lastStartTimeUtc = DateTime.Parse("2017-11-13T23:44:35.127381Z", null, DateTimeStyles.RoundtripKind);
            DateTime lastExitTimeUtc  = DateTime.Parse("2017-11-13T23:49:35.127381Z", null, DateTimeStyles.RoundtripKind);
            var      module           = new EdgeAgentDockerRuntimeModule(
                new DockerReportedConfig("booyah:latest", string.Empty, "someSha", Option.None <string>()),
                ModuleStatus.Running,
                0,
                string.Empty,
                lastStartTimeUtc,
                lastExitTimeUtc,
                ImagePullPolicy.OnCreate,
                null,
                new Dictionary <string, EnvVal>());

            // Act
            JToken json = JToken.Parse(JsonConvert.SerializeObject(module));

            // Assert
            JToken expected = JToken.FromObject(
                new
            {
                runtimeStatus     = "running",
                exitCode          = 0,
                lastStartTimeUtc  = lastStartTimeUtc,
                lastExitTimeUtc   = lastExitTimeUtc,
                statusDescription = string.Empty,
                type            = "docker",
                imagePullPolicy = "on-create",
                startupOrder    = Constants.HighestPriority,
                settings        = new
                {
                    image         = "booyah:latest",
                    imageHash     = "someSha",
                    createOptions = "{}"
                },
                env = new { }
            });

            Assert.True(JToken.DeepEquals(expected, json));
        }
예제 #3
0
        public void TestWithRuntimeStatus()
        {
            DateTime lastStartTimeUtc = DateTime.Parse("2017-11-13T23:44:35.127381Z", null, DateTimeStyles.RoundtripKind);
            DateTime lastExitTimeUtc  = DateTime.Parse("2017-11-13T23:49:35.127381Z", null, DateTimeStyles.RoundtripKind);
            var      module           = new EdgeAgentDockerRuntimeModule(
                new DockerReportedConfig("booyah", string.Empty, "someSha"),
                ModuleStatus.Running,
                0,
                string.Empty,
                lastStartTimeUtc,
                lastExitTimeUtc,
                new ConfigurationInfo("bing"),
                new Dictionary <string, EnvVal>());
            var updatedModule1 = (EdgeAgentDockerRuntimeModule)module.WithRuntimeStatus(ModuleStatus.Running);
            var updatedModule2 = (EdgeAgentDockerRuntimeModule)module.WithRuntimeStatus(ModuleStatus.Unknown);

            Assert.Equal(module, updatedModule1);
            Assert.NotEqual(module, updatedModule2);
            Assert.Equal(updatedModule2.RuntimeStatus, ModuleStatus.Unknown);
        }
        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, Core.RestartPolicy.Unknown, new DockerConfig(Constants.UnknownImage, new CreateContainerParameters(), Option.None <string>()), ImagePullPolicy.OnCreate, Core.Constants.HighestPriority, new ConfigurationInfo(), null, null);
                }

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

                ModuleState moduleState = moduleStateOption.GetOrElse(new ModuleState(0, moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue)));

                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,
                        moduleRuntimeInfo.Description,
                        moduleRuntimeInfo.StartTime.GetOrElse(DateTime.MinValue),
                        moduleRuntimeInfo.ExitTime.GetOrElse(DateTime.MinValue),
                        moduleState.RestartCount,
                        moduleState.LastRestartTimeUtc,
                        moduleRuntimeInfo.ModuleStatus,
                        dockerModule.ImagePullPolicy,
                        dockerModule.Priority,
                        dockerModule.ConfigurationInfo,
                        dockerModule.Env);
                    break;

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

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

                modules.Add(module);
            }

            return(ModuleSet.Create(modules.ToArray()));
        }