CreateServiceAsync(Kubernetes k8sClient, V1Service yamlModel, MTAServiceModel serviceModel) { try { var namespaceParams = PrepareNamespaceParams(_groupName); var serviceParams = PrepareServiceParams(_serviceName); yamlModel.Metadata.Name = serviceParams; yamlModel.Spec.Selector = serviceModel.Selectors; var v1ServicePorts = new List <V1ServicePort>(); foreach (var port in serviceModel.Ports) { var v1ServicePort = new V1ServicePort(port); v1ServicePorts.Add(v1ServicePort); } yamlModel.Spec.Ports = v1ServicePorts; var v1Service = await k8sClient.CreateNamespacedServiceAsync (yamlModel, namespaceParams); serviceModel = new MTAServiceModel(v1Service); return(new Tuple <MTAServiceModel, MTAErrorModel>(serviceModel, null)); } catch (HttpOperationException ex) { var errorModel = new MTAErrorModel(ex); return(new Tuple <MTAServiceModel, MTAErrorModel>(null, errorModel)); } catch (Exception) { throw; } }
static void AssertPort(V1ServicePort expected, V1ServicePort actual) { Assert.NotNull(actual); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.Port, actual.Port); Assert.Equal(expected.NodePort, actual.NodePort); Assert.Equal(expected.TargetPort, actual.TargetPort); Assert.Equal(expected.Protocol, actual.Protocol); }
public static ServicePortModel ToModel(this V1ServicePort port, string serviceName, string serviceNamespace) { return(new ServicePortModel { ServiceName = serviceName, Namespace = serviceNamespace, PortName = port.Name, Protocol = port.Protocol, Port = port.Port }); }
private static bool MatchesPort(V1ServicePort port1, V1ServiceBackendPort port2) { if (port1 == null || port2 == null) { return(false); } if (port2.Number != null && port2.Number == port1.Port) { return(true); } if (port2.Name != null && string.Equals(port2.Name, port1.Name, StringComparison.Ordinal)) { return(true); } return(false); }
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 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); }
private static void HandleIngressRulePath(YarpIngressContext ingressContext, V1ServicePort servicePort, List <Endpoints> endpoints, IList <V1EndpointSubset> defaultSubsets, V1IngressRule rule, V1HTTPIngressPath path, YarpConfigContext configContext) { var backend = path.Backend; var ingressServiceBackend = backend.Service; var subsets = defaultSubsets; var clusters = configContext.ClusterTransfers; var routes = configContext.Routes; if (!string.IsNullOrEmpty(ingressServiceBackend?.Name)) { subsets = endpoints.SingleOrDefault(x => x.Name == ingressServiceBackend?.Name).Subsets; } // Each ingress rule path can only be for one service var key = ingressServiceBackend.Port.Number.HasValue ? $"{ingressServiceBackend?.Name}:{ingressServiceBackend?.Port.Number}" : $"{ingressServiceBackend?.Name}:{ingressServiceBackend?.Port.Name}"; if (!clusters.ContainsKey(key)) { clusters.Add(key, new ClusterTransfer()); } var cluster = clusters[key]; cluster.ClusterId = key; // make sure cluster is present foreach (var subset in subsets ?? Enumerable.Empty <V1EndpointSubset>()) { foreach (var port in subset.Ports ?? Enumerable.Empty <V1EndpointPort>()) { if (!MatchesPort(port, servicePort.TargetPort)) { continue; } var pathMatch = FixupPathMatch(path); var host = rule.Host; routes.Add(new RouteConfig() { Match = new RouteMatch() { Hosts = host != null ? new[] { host } : Array.Empty <string>(), Path = pathMatch }, ClusterId = cluster.ClusterId, RouteId = $"{ingressContext.Ingress.Metadata.Name}:{path.Path}" }); // Add destination for every endpoint address foreach (var address in subset.Addresses ?? Enumerable.Empty <V1EndpointAddress>()) { var protocol = ingressContext.Options.Https ? "https" : "http"; var uri = $"{protocol}://{address.Ip}:{port.Port}"; cluster.Destinations[uri] = new DestinationConfig() { Address = uri }; } } } }