Ejemplo n.º 1
0
        public void AddIngress(IKubernetes client, Networkingv1beta1Ingress ingress, string nspace)
        {
            string regex = "";

            if (ingress.Metadata.Annotations.ContainsKey("regex"))
            {
                regex = ingress.Metadata.Annotations["regex"];
            }
            // Adding rewrite annotation; getting associated regex value

            // Creating a custom path for each service. Path format is arbitrary and could be changed if needed

            foreach (var rule in ingress.Spec.Rules)
            {
                foreach (var path in rule.Http.Paths)
                {
                    path.Path = "/" + nspace + path.Path;
                }
            }
            client.CreateNamespacedIngress1(ingress, nspace);
        }
        /// <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(
            Networkingv1beta1Ingress networkingv1beta1Ingress,
            CancellationToken cancellationToken = default
            )
        {
            if (networkingv1beta1Ingress is null)
            {
                throw new ArgumentNullException(nameof(networkingv1beta1Ingress));
            }

            Exception exception = null;

            try {
                var tmpIngress = networkingv1beta1Ingress;

                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
                                    .ListNamespacedIngress1Async(
                        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;
        }