private async Task AddLoadBalancerTriggersAsync()

in src/routingmanager/RoutingManagerApp.cs [684:745]


        private async Task AddLoadBalancerTriggersAsync(
            ConcurrentDictionary<V1Service, RoutingStateEstablisherInput> routingStateEstablisherInputMap,
            IEnumerable<V1Service> userServices,
            IEnumerable<V1Pod> pods,
            CancellationToken cancellationToken)
        {
            try
            {
                foreach (var service in userServices)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (service.Spec?.Selector == null)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(service.Spec.Type) && StringComparer.OrdinalIgnoreCase.Equals(service.Spec.Type, KubernetesConstants.TypeStrings.LoadBalancer))
                    {
                        if (!await ReplaceServiceNamedPortsAsync(service, pods, cancellationToken))
                        {
                            _log.Warning("'{0}' Trigger service's underlying pods not found in order to resolve named target port. Ignoring corresponding load balancer trigger", service?.Metadata?.Name ?? string.Empty);
                            continue;
                        }

                        // the below logic checks of the service of type load balancer belongs to an ingress controller
                        var isLoadBalancerIngressController = false;
                        var serviceSelectedPod =
                            pods.FirstOrDefault(pod =>
                                                    pod.Metadata.Labels != null
                                                    && service.Spec.Selector.All(selector =>
                                                                                pod.Metadata.Labels.TryGetValue(selector.Key, out var labelValue)
                                                                                && StringComparer.OrdinalIgnoreCase.Equals(labelValue, selector.Value)));
                        if (serviceSelectedPod?.Spec?.Containers != null)
                        {
                            if (serviceSelectedPod.Spec.Containers.Any(container => !string.IsNullOrEmpty(container.Image) && Constants.IngressControllerImageNames.Any(icImageName => container.Image.Contains(icImageName))))
                            {
                                _log.Info("Service {0} of type load balancer is an ingress controller.", new PII(service.Metadata.Name));
                                isLoadBalancerIngressController = true;
                            }
                            else
                            {
                                _log.Info("Service {0} of type load balancer is not an ingress controller. Image: {1}",
                                    new PII(service.Metadata.Name),
                                    new PII(string.Join(",", serviceSelectedPod.Spec.Containers.Select(c => string.IsNullOrEmpty(c.Image) ? string.Empty : c.Image))));
                            }
                        }

                        var loadBalancerTriggerToAdd = new LoadBalancerTriggerConfig(
                            namespaceName: service.Metadata.NamespaceProperty,
                            triggerService: service,
                            triggerEntityName: service.Metadata.Name,
                            isLoadBalancerIngressController);
                        routingStateEstablisherInputMap.AddOrUpdateWithTrigger(service, loadBalancerTriggerToAdd);
                    }
                }
            }
            catch (NullReferenceException e)
            {
                _log.Error("Null ref in AddLoadBalancerTriggersAsync - message : {0}. Call stack: {1}. Data: {2}", e.Message, e.StackTrace, JsonSerializer.Serialize(e.Data));
                throw;
            }
        }