Exemplo n.º 1
0
        private static void HandleIngressAdded(Extensionsv1beta1Ingress item, NodesInfo nodesInfo)
        {
            item.Spec.Rules.ToList().ForEach(rule =>
            {
                rule.Http.Paths.ToList().ForEach(path =>
                {
                    var targetPort = LookupPort(path, item.Metadata.NamespaceProperty);

                    var addRule = new Rule
                    {
                        Name     = item.Metadata.Name,
                        Host     = rule.Host,
                        Port     = 443,
                        Protocol = "https" // look up from annotation
                    };

                    addRule.Targets.AddRange(nodesInfo.NodeAddresses.Select(nodeAddress => $"{nodeAddress}:{targetPort}"));

                    try
                    {
                        var pushRule = _client.PushRule(addRule);
                        Console.WriteLine($"Rule pushed {pushRule.Accepted}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                });
            });
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Wait untill IP address of Ingress LoadBalancer is available.
        /// </summary>
        /// <param name="ingress"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <IEnumerable <V1LoadBalancerIngress> > WaitForIngressIPAsync(
            Extensionsv1beta1Ingress extensionsv1beta1Ingress,
            CancellationToken cancellationToken = default
            )
        {
            if (extensionsv1beta1Ingress is null)
            {
                throw new ArgumentNullException(nameof(extensionsv1beta1Ingress));
            }

            Exception exception = null;

            try {
                var tmpIngress = extensionsv1beta1Ingress;

                var namespaceProperty = tmpIngress.Metadata.NamespaceProperty;
                var labels            = tmpIngress.Metadata.Labels
                                        .Select(kvp => $"{kvp.Key}={kvp.Value}")
                                        .ToList();
                var labelsStr = string.Join(",", labels);

                const int secondsDelay = 5;
                const int secondsRetry = 900;

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                // Now we will loop untill LoadBalancer Ingress IP address is available.
                while (null == tmpIngress.Status ||
                       null == tmpIngress.Status.LoadBalancer ||
                       null == tmpIngress.Status.LoadBalancer.Ingress ||
                       0 == tmpIngress.Status.LoadBalancer.Ingress.Count()
                       )
                {
                    if (stopwatch.Elapsed >= TimeSpan.FromSeconds(secondsRetry))
                    {
                        exception = new Exception($"Ingress IP address is not available after " +
                                                  $"{secondsRetry} seconds for: {tmpIngress.Metadata.Name}");
                        break;
                    }

                    await Task.Delay(secondsDelay * 1000, cancellationToken);

                    var ingresses = await _k8sClient
                                    .ListNamespacedIngressAsync(
                        namespaceProperty,
                        labelSelector : labelsStr,
                        cancellationToken : cancellationToken
                        );

                    tmpIngress = ingresses
                                 .Items
                                 .Where(ingress => ingress.Metadata.Name == tmpIngress.Metadata.Name)
                                 .FirstOrDefault();
                }

                if (null == exception)
                {
                    return(tmpIngress.Status.LoadBalancer.Ingress.ToList());
                }
            } catch (Exception ex) {
                Log.Error(ex, $"Failure while waiting for IP address of Ingress LoadBalancer");
                throw;
            }

            throw exception;
        }
Exemplo n.º 4
0
        private async Task <string> DescribeObject(Kubernetes client, V1Namespace ns, Extensionsv1beta1Ingress o, StringBuilder buffer)
        {
            var fetched = await client.ReadNamespacedIngressAsync(o.Metadata.Name, ns.Metadata.Name).ConfigureAwait(false);

            buffer.AppendLine($"API Veresion: {fetched.ApiVersion}");
            buffer.AppendLine($"Kind: {fetched.Kind}");
            buffer.AppendLine(DescribeMetadata(fetched.Metadata));
            return($"Ingress - {fetched.Metadata.Name}");
        }
        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();
        }
Exemplo n.º 6
0
        private async Task Update()
        {
            Item = await State.Client.ReadNamespacedIngressAsync(Name, Namespace);

            StateHasChanged();
        }
Exemplo n.º 7
0
 private async Task Delete(Extensionsv1beta1Ingress item)
 {
     await State.Client.DeleteNamespacedIngressAsync(item.Metadata.Name, item.Metadata.NamespaceProperty);
 }