Example #1
0
        public void PortBindingOverrideExposedPort()
        {
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                ["10/TCP"] = default(EmptyStruct)
            };
            var hostConfig = new DockerModels.HostConfig
            {
                PortBindings = new Dictionary <string, IList <DockerModels.PortBinding> >
                {
                    ["10/TCP"] = new List <DockerModels.PortBinding> {
                        new DockerModels.PortBinding {
                            HostPort = "10"
                        }
                    }
                }
            };
            var createOptions = CreatePodParameters.Create(exposedPorts: exposedPorts, hostConfig: hostConfig);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId      = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.Equal(1, service.Spec.Ports.Count);
            AssertPort(new V1ServicePort(10, "hostport-10-tcp", null, "TCP", 10), service.Spec.Ports.First());
            Assert.Equal("ClusterIP", service.Spec.Type);
        }
        public void DockerLabelsConvertedAsAnnotations()
        {
            var createOptions = new Docker.Models.CreateContainerParameters
            {
                // Add a port to be exposed
                ExposedPorts = new Dictionary <string, EmptyStruct>
                {
                    ["10/TCP"] = default(EmptyStruct)
                },
                Labels = new Dictionary <string, string>
                {
                    ["GPU"] = "Enabled"
                }
            };
            var config       = new CombinedDockerConfig("image", createOptions, Option.None <AuthConfig>());
            var docker       = new DockerModule("module1", "v1", ModuleStatus.Running, Core.RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module       = new KubernetesModule(docker, config);
            var moduleLabels = new Dictionary <string, string>();
            var mapper       = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId     = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.Equal(1, service.Spec.Ports.Count);
            Assert.Equal(2, service.Metadata.Annotations.Count);
            Assert.NotNull(service.Metadata.Annotations[KubernetesConstants.CreationString]);
            Assert.Equal("Enabled", service.Metadata.Annotations["GPU"]);
            Assert.Equal(0, service.Metadata.Labels.Count);
            Assert.Equal(0, service.Spec.Selector.Count);
        }
        public void PortBindingsCreatesAServiceWithPorts()
        {
            var createOptions = new Docker.Models.CreateContainerParameters
            {
                // Add a port binding to host
                HostConfig = new DockerModels.HostConfig
                {
                    PortBindings = new Dictionary <string, IList <DockerModels.PortBinding> >
                    {
                        ["10/TCP"] = new List <DockerModels.PortBinding>
                        {
                            new DockerModels.PortBinding
                            {
                                HostPort = "10"
                            }
                        }
                    }
                }
            };
            var config       = new CombinedDockerConfig("image", createOptions, Option.None <AuthConfig>());
            var docker       = new DockerModule("module1", "v1", ModuleStatus.Running, Core.RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module       = new KubernetesModule(docker, config);
            var moduleLabels = new Dictionary <string, string>();
            var mapper       = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId     = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.Equal(1, service.Spec.Ports.Count);
            AssertPort(new V1ServicePort(10, "hostport-10-tcp", null, "TCP", 10), service.Spec.Ports.First());
            Assert.Equal("ClusterIP", service.Spec.Type);
        }
Example #4
0
        public void LabelsConvertedAsLabelsAndSelectors()
        {
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                ["10/TCP"] = default(EmptyStruct)
            };
            var createOptions = CreatePodParameters.Create(exposedPorts: exposedPorts);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string> {
                { "Label1", "VaLue1" }
            };
            var mapper   = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.Equal(1, service.Spec.Ports.Count);
            Assert.Equal(1, service.Metadata.Annotations.Count);
            Assert.NotNull(service.Metadata.Annotations[KubernetesConstants.CreationString]);
            Assert.Equal(1, service.Metadata.Labels.Count);
            Assert.Equal("VaLue1", service.Metadata.Labels["Label1"]);
            Assert.Equal("ClusterIP", service.Spec.Type);
            Assert.Equal(1, service.Spec.Selector.Count);
            Assert.Equal("VaLue1", service.Spec.Selector["Label1"]);
        }
        public void NoPortInOptionsCreatesNoService()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create());
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.False(result.HasValue);
        }
        public void CreateServiceHostPortsCreatesDefaultServiceType()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create(hostConfig: HostPorts));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var service = result.OrDefault();

            Assert.Equal(PortMapServiceType.LoadBalancer.ToString(), service.Spec.Type);
        }
        public void ServiceNameIsModuleName()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create(exposedPorts: ExposedPorts, hostConfig: HostPorts));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var service = result.OrDefault();

            Assert.Equal("module1", service.Metadata.Name);
        }
        public void CreateServiceExposedPortsOnlyCreatesClusterIP()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create(exposedPorts: ExposedPorts));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var service = result.OrDefault();

            Assert.Equal(PortMapServiceType.ClusterIP.ToString(), service.Spec.Type);
        }
        public void CreateServiceSetsServiceOptionsOverridesSetDefault()
        {
            var serviceOptions = new KubernetesServiceOptions("loadBalancerIP", "clusterIP");
            var module         = CreateKubernetesModule(CreatePodParameters.Create(hostConfig: HostPorts, serviceOptions: serviceOptions));
            var mapper         = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);

            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var service = result.OrDefault();

            Assert.Equal(PortMapServiceType.ClusterIP.ToString(), service.Spec.Type);
            Assert.Equal("loadBalancerIP", service.Spec.LoadBalancerIP);
        }
        public void NoPortsExposedMeansNoServiceCreated()
        {
            var config       = new CombinedDockerConfig("image", new Docker.Models.CreateContainerParameters(), Option.None <AuthConfig>());
            var moduleId     = new ModuleIdentity("hub", "gateway", "deviceId", "moduleid", Mock.Of <ICredentials>());
            var docker       = new DockerModule("module1", "v1", ModuleStatus.Running, Core.RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module       = new KubernetesModule(docker, config);
            var moduleLabels = new Dictionary <string, string>();

            var mapper = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            var service = mapper.CreateService(moduleId, module, moduleLabels);

            Assert.False(service.HasValue);
        }
Example #11
0
        public void NoPortsExposedMeansNoServiceCreated()
        {
            var createOptions = CreatePodParameters.Create();
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var moduleId      = new ModuleIdentity("hub", "gateway", "deviceId", "moduleid", Mock.Of <ICredentials>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();

            var mapper = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            var service = mapper.CreateService(moduleId, module, moduleLabels);

            Assert.False(service.HasValue);
        }
Example #12
0
        public void EmptyIsNotAllowedAsServiceAnnotation()
        {
            // string.Empty is an invalid label name
            var labels = new Dictionary <string, string> {
                { string.Empty, "test" }
            };
            var createOptions = CreatePodParameters.Create(labels: labels);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var moduleId      = new ModuleIdentity("hub", "gateway", "deviceId", "moduleid", Mock.Of <ICredentials>());
            var docker        = new DockerModule(moduleId.ModuleId, "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            Assert.Throws <InvalidKubernetesNameException>(() => mapper.CreateService(moduleId, module, moduleLabels));
        }
        public void EmptyIsNotAllowedAsServiceAnnotation()
        {
            var config = new CombinedDockerConfig("image", new Docker.Models.CreateContainerParameters(), Option.None <AuthConfig>());

            config.CreateOptions.Labels = new Dictionary <string, string>
            {
                // string.Empty is an invalid label name
                { string.Empty, "test" }
            };
            var moduleId     = new ModuleIdentity("hub", "gateway", "deviceId", "moduleid", Mock.Of <ICredentials>());
            var docker       = new DockerModule(moduleId.ModuleId, "v1", ModuleStatus.Running, Core.RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module       = new KubernetesModule(docker, config);
            var moduleLabels = new Dictionary <string, string>();
            var mapper       = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            Assert.Throws <InvalidKubernetesNameException>(() => mapper.CreateService(moduleId, module, moduleLabels));
        }
        public void CreateServiceExposedPortsOnlyCreatesExposedPortService()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create(exposedPorts: ExposedPorts));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var           service = result.OrDefault();
            V1ServicePort port80  = service.Spec.Ports.Single(p => p.Port == 80);

            Assert.Equal("exposedport-80-tcp", port80.Name);
            Assert.Equal("TCP", port80.Protocol);
            V1ServicePort port5000 = service.Spec.Ports.Single(p => p.Port == 5000);

            Assert.Equal("exposedport-5000-udp", port5000.Name);
            Assert.Equal("UDP", port5000.Protocol);
        }
        public void ServiceAnnotationsAreLabels()
        {
            var dockerLabels = new Dictionary <string, string>
            {
                ["Complicated Value that doesn't fit in k8s label name"] = "Complicated Value that doesn't fit in k8s label value",
                ["Label2"] = "Value2"
            };
            var module = CreateKubernetesModule(CreatePodParameters.Create(exposedPorts: ExposedPorts, hostConfig: HostPorts, labels: dockerLabels));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var service = result.OrDefault();

            Assert.Equal("Complicated Value that doesn't fit in k8s label value", service.Metadata.Annotations["ComplicatedValuethatdoesntfitink8slabelname"]);
            Assert.Equal("Value2", service.Metadata.Annotations["Label2"]);
        }
Example #16
0
        public void InvalidPortBindingDoesNotCreateAService()
        {
            // Add invalid port
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                { "aa/TCP", default(EmptyStruct) }
            };
            var createOptions = new CreatePodParameters(null, exposedPorts, null, null, null, null);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var moduleId      = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            var service = mapper.CreateService(moduleId, module, moduleLabels);

            Assert.False(service.HasValue);
        }
Example #17
0
        public void UnknownProtocolDoesNotCreateService()
        {
            // Add unknown protocol
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                { "123/XXX", default(EmptyStruct) }
            };
            var createOptions = CreatePodParameters.Create(exposedPorts: exposedPorts);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var moduleId      = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            var service = mapper.CreateService(moduleId, module, moduleLabels);

            Assert.False(service.HasValue);
        }
        public void UnknownProtocolDoesNotCreateService()
        {
            var config   = new CombinedDockerConfig("image", new Docker.Models.CreateContainerParameters(), Option.None <AuthConfig>());
            var moduleId = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            config.CreateOptions.ExposedPorts = new Dictionary <string, EmptyStruct>
            {
                // Add unknown protocol
                { "123/XXX", default(EmptyStruct) }
            };
            var docker       = new DockerModule("module1", "v1", ModuleStatus.Running, Core.RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, DefaultConfigurationInfo, EnvVars);
            var module       = new KubernetesModule(docker, config);
            var moduleLabels = new Dictionary <string, string>();
            var mapper       = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);

            var service = mapper.CreateService(moduleId, module, moduleLabels);

            Assert.False(service.HasValue);
        }
        public void CreateServiceHostPortsCreatesHostportService()
        {
            var module = CreateKubernetesModule(CreatePodParameters.Create(hostConfig: HostPorts));
            var mapper = new KubernetesServiceMapper(PortMapServiceType.LoadBalancer);
            Option <V1Service> result = mapper.CreateService(CreateIdentity, module, DefaultLabels);

            Assert.True(result.HasValue);
            var           service = result.OrDefault();
            V1ServicePort port80  = service.Spec.Ports.Single(p => p.Port == 8080);

            Assert.Equal("hostport-80-tcp", port80.Name);
            Assert.Equal("TCP", port80.Protocol);
            Assert.Equal(80, (int)port80.TargetPort);
            V1ServicePort port5000 = service.Spec.Ports.Single(p => p.Port == 5050);

            Assert.Equal("hostport-5000-udp", port5000.Name);
            Assert.Equal("UDP", port5000.Protocol);
            Assert.Equal(5000, (int)port5000.TargetPort);
        }
        public void ExposingPortsCreatesAServiceWithPorts()
        {
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                ["10/TCP"] = default(EmptyStruct)
            };
            var createOptions = CreatePodParameters.Create(exposedPorts: exposedPorts);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultStartupOrder, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId      = new ModuleIdentity("hostname", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.Equal(1, service.Spec.Ports.Count);
            AssertPort(new V1ServicePort(10, "exposedport-10-tcp", null, "TCP"), service.Spec.Ports.First());
            Assert.Equal("ClusterIP", service.Spec.Type);
        }
Example #21
0
        public void SimpleServiceCreationHappyPath()
        {
            var exposedPorts = new Dictionary <string, EmptyStruct> {
                ["10/TCP"] = default(EmptyStruct)
            };
            var createOptions = CreatePodParameters.Create(exposedPorts: exposedPorts);
            var config        = new KubernetesConfig("image", createOptions, Option.None <AuthConfig>());
            var docker        = new DockerModule("module1", "v1", ModuleStatus.Running, RestartPolicy.Always, Config1, ImagePullPolicy.OnCreate, Constants.DefaultPriority, DefaultConfigurationInfo, EnvVars);
            var module        = new KubernetesModule(docker, config, EdgeletModuleOwner);
            var moduleLabels  = new Dictionary <string, string>();
            var mapper        = new KubernetesServiceMapper(PortMapServiceType.ClusterIP);
            var moduleId      = new ModuleIdentity("hostname", "gatewayhost", "deviceid", "moduleid", Mock.Of <ICredentials>());

            var service = mapper.CreateService(moduleId, module, moduleLabels).OrDefault();

            Assert.NotNull(service);
            Assert.Equal(1, service.Metadata.OwnerReferences.Count);
            Assert.Equal(V1Deployment.KubeKind, service.Metadata.OwnerReferences[0].Kind);
            Assert.Equal(EdgeletModuleOwner.Name, service.Metadata.OwnerReferences[0].Name);
            Assert.Equal(1, service.Spec.Ports.Count);
            Assert.Equal(0, service.Spec.Selector.Count);
        }