private async Task AddIngressRouteTriggersAsync()

in src/routingmanager/RoutingManagerApp.cs [395:487]


        private async Task AddIngressRouteTriggersAsync(
            ConcurrentDictionary<V1Service, RoutingStateEstablisherInput> routingStateEstablisherInputMap,
            IEnumerable<IngressRoute> userIngressRoutes,
            IEnumerable<V1Service> userServices,
            IEnumerable<V1Pod> pods,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            foreach (var ingressRoute in userIngressRoutes)
            {
                _log.Info("Processing the ingressRoute named '{0}'", new PII(ingressRoute.Metadata.Name));
                if (string.IsNullOrWhiteSpace(ingressRoute?.Metadata?.Name) || string.IsNullOrWhiteSpace(ingressRoute?.Metadata?.NamespaceProperty))
                {
                    _log.Warning("Encountered ingressRoute with null/empty name or namespace property");
                    continue;
                }

                var routes = ingressRoute.Spec?.Routes;
                if (routes == null)
                {
                    // Nothing to do
                    _log.Warning("No routes for ingressRoute '{0}' in namespace '{1}'", new PII(ingressRoute.Metadata.Name), new PII(ingressRoute.Metadata.NamespaceProperty));
                    continue;
                }

                _log.Info(JsonSerializer.Serialize(routes));
                (var httpReadinessProbe, var httpLivenessProbe) = await GetHttpProbesForServiceNamesAsync("ingressRoute", ingressRoute.Metadata.Name, userServices, pods, routes.SelectMany(r => r.Services == null ? new List<string>() : r.Services.Select(s => s.Name)), cancellationToken);
                foreach (var route in routes)
                {
                    if (route.Services == null || !route.Services.Any())
                    {
                        _log.Warning("No services found for the ingressRoute {0} for the match {1} in namespace {2}", new PII(ingressRoute.Metadata.Name), new PII(route.Match), new PII(ingressRoute.Metadata.NamespaceProperty));
                        continue;
                    }

                    foreach (var ingressRouteService in route.Services)
                    {
                        V1Service serviceToAdd;
                        if ((serviceToAdd = userServices.FirstOrDefault(svc => StringComparer.OrdinalIgnoreCase.Equals(svc.Metadata.Name, ingressRouteService.Name))) == default(V1Service))
                        {
                            //  If we do not find the service in the same namespace, this ingress is broken.
                            _log.Warning("Service '{0}' was not found which was expected to be trigger service. Skipping this ingressRoute trigger with match '{1}'.",
                                new PII(ingressRouteService.Name), new PII(route.Match));
                            continue;
                        }

                        string serviceProtocol;
                        int servicePort_int;
                        try
                        {
                            (serviceProtocol, servicePort_int) = await GetProtocolAndPortNumberFromServiceNamedPort(serviceToAdd, ingressRouteService.Port, ingressRoute.Metadata.Name, cancellationToken);
                        }
                        catch (InvalidOperationException)
                        {
                            _log.Error("Unable to retrieve the integer port for the named port '{0}' for service '{1}' in ingressRoute '{2}'. Ignoring corresponding ingressRoute trigger with match '{3}'",
                                new PII(ingressRouteService.Port.ToString()), new PII(ingressRouteService.Name), new PII(ingressRoute.Metadata.Name), new PII(route.Match));
                            continue;
                        }
                        catch (RoutingException)
                        {
                            // Ignore this ingress route
                            _log.Info("Ignoring ingressroute service '{0}'", new PII(ingressRouteService.Name));
                            continue;
                        }

                        if (!StringComparer.OrdinalIgnoreCase.Equals(serviceProtocol, KubernetesConstants.Protocols.Tcp))
                        {
                            _log.Error("Detected service protocol '{0}'. Routing manager does not support protocols other than TCP. Ignoring corresponding ingressRoute trigger named '{1}' with match '{2}'",
                                serviceProtocol, new PII(ingressRoute.Metadata.Name), new PII(route.Match));
                            continue;
                        }

                        if (!await ReplaceServiceNamedPortsAsync(serviceToAdd, pods, cancellationToken))
                        {
                            _log.Warning("'{0}' Trigger service's underlying pods or pod's corresponding port was not found in order to resolve named target port. Ignoring corresponding ingressRoute trigger named '{1}'",
                                    new PII(serviceToAdd.Metadata.Name), new PII(ingressRoute.Metadata.Name));
                            continue;
                        }

                        var ingressRouteTriggerToAdd =
                                new IngressRouteTriggerConfig(
                                    namespaceName: ingressRoute.Metadata.NamespaceProperty,
                                    triggerService: serviceToAdd,
                                    ingressRouteName: ingressRoute.Metadata.Name,
                                    servicePort: servicePort_int,
                                    host: ingressRoute.GetIngressRouteHostForRoute(route).First(),
                                    httpReadinessProbe: httpReadinessProbe,
                                    httpLivenessProbe: httpLivenessProbe);
                        routingStateEstablisherInputMap.AddOrUpdateWithTrigger(serviceToAdd, ingressRouteTriggerToAdd);
                    }
                }
            }
        }