Example #1
0
        private async Task UpdateJsonBlob(string serviceName)
        {
            bool shouldWrite;

            lock (_sync)
            {
                shouldWrite = _ipMappingList.ContainsKey(serviceName);
                if (shouldWrite)
                {
                    _ipMappingList[serviceName].IpAddresses = _serviceToIp[serviceName];
                }
            }

            if (shouldWrite)
            {
                // We want to make sure the ingress file is present before updating it.
                // Use a tcs for now to update it.
                await _tcs.Task;
                var ingressFile = "/app/Ingress/ingress.json";
                var fileStream  = File.Open(ingressFile, FileMode.Create);
                var json        = new IngressBindingOptions()
                {
                    IpMappings = _ipMappingList.Values.ToList()
                };
                await JsonSerializer.SerializeAsync(fileStream, json, typeof(IngressBindingOptions));

                fileStream.Close();
            }
        }
Example #2
0
        private async ValueTask CreateJsonBlob(Extensionsv1beta1Ingress ingress)
        {
            // Get IP and port from k8s.
            var ingressFile = "/app/Ingress/ingress.json";

            var fileStream = File.Open(ingressFile, FileMode.Create);

            foreach (var i in ingress.Spec.Rules)
            {
                foreach (var path in i.Http.Paths)
                {
                    _logger.LogInformation("Querying for endpoints");
                    var endpoints = await _klient.ListNamespacedEndpointsAsync(namespaceParameter : ingress.Metadata.NamespaceProperty);

                    var service = await _klient.ReadNamespacedServiceAsync(path.Backend.ServiceName, ingress.Metadata.NamespaceProperty);

                    // TODO can there be multiple ports here?
                    var targetPort = service.Spec.Ports.Where(e => e.Port == path.Backend.ServicePort).Select(e => e.TargetPort).Single();

                    UpdateServiceToEndpointDictionary(endpoints);
                    lock (_sync)
                    {
                        // From what it looks like, scheme is always http unless the tls section is specified,
                        _ipMappingList[path.Backend.ServiceName] = new IpMapping {
                            IpAddresses = _serviceToIp[path.Backend.ServiceName], Port = targetPort, Path = path.Path, Scheme = "http"
                        };
                    }
                }
            }

            var json = new IngressBindingOptions()
            {
                IpMappings = _ipMappingList.Values.ToList()
            };
            await JsonSerializer.SerializeAsync(fileStream, json, typeof(IngressBindingOptions));

            fileStream.Close();

            _tcs.SetResult(null);
        }
        private async ValueTask CreateJsonBlob(Extensionsv1beta1Ingress ingress)
        {
            // Get IP and port from k8s.
            var ingressFile = "/app/Ingress/ingress.json";

            var fileStream    = File.Open(ingressFile, FileMode.Create);
            var ipMappingList = new List <IpMapping>();

            if (ingress.Spec.Backend != null)
            {
                // TODO
            }
            else
            {
                // TODO maybe check that a host is present:
                // An optional host. In this example, no host is specified, so the rule applies to all
                // inbound HTTP traffic through the IP address specified. If a host is provided
                // (for example, foo.bar.com), the rules apply to that host.
                foreach (var i in ingress.Spec.Rules)
                {
                    foreach (var path in i.Http.Paths)
                    {
                        bool          exists;
                        List <string> ipList;

                        lock (_sync)
                        {
                            exists = _serviceToIp.TryGetValue(path.Backend.ServiceName, out ipList);
                            _logger.LogInformation(path.Backend.ServiceName);
                        }

                        if (exists)
                        {
                            _logger.LogInformation("IP mapping exists, use it.");

                            ipMappingList.Add(new IpMapping {
                                IpAddresses = ipList, Port = path.Backend.ServicePort, Path = path.Path
                            });
                        }
                        else
                        {
                            _logger.LogInformation("querying for endpoints");
                            var endpoints = await _klient.ListNamespacedEndpointsAsync(namespaceParameter : ingress.Metadata.NamespaceProperty);

                            var service = await _klient.ReadNamespacedServiceAsync(path.Backend.ServiceName, ingress.Metadata.NamespaceProperty);

                            // TODO can there be multiple ports here?
                            var targetPort = service.Spec.Ports.Where(e => e.Port == path.Backend.ServicePort).Select(e => e.TargetPort).Single();

                            UpdateServiceToEndpointDictionary(endpoints);
                            lock (_sync)
                            {
                                // From what it looks like, scheme is always http unless the tls section is specified,
                                ipMappingList.Add(new IpMapping {
                                    IpAddresses = _serviceToIp[path.Backend.ServiceName], Port = targetPort, Path = path.Path, Scheme = "http"
                                });
                            }
                        }
                    }
                }
            }

            var json = new IngressBindingOptions()
            {
                IpMappings = ipMappingList
            };
            await JsonSerializer.SerializeAsync(fileStream, json, typeof(IngressBindingOptions));

            fileStream.Close();
        }