private IEnumerable <IngressModel> FindIngressesForService(ServicePortModel sp, IEnumerable <IngressModel> ingresses)
        {
            var matchingIngresses = new List <IngressModel>();

            // Match ingresses in the right namespace
            foreach (var ingress in ingresses.Where(i => i.Namespace == sp.Namespace))
            {
                foreach (var rule in ingress.Rules)
                {
                    // Match paths with the right service name
                    foreach (var path in rule.Paths.Where(p => p.BackendServiceName == sp.ServiceName && p.PathType != IngressPath.IngressPathType.ImplementationSpecific))
                    {
                        if (int.TryParse(path.BackendServicePort, out var portValue) && portValue == sp.Port)
                        {
                            // Port number match
                            matchingIngresses.Add(ingress);
                        }
                        else if (string.Equals(path.BackendServicePort, sp.PortName, StringComparison.OrdinalIgnoreCase))
                        {
                            // Matched based on the port name in the service
                            matchingIngresses.Add(ingress);
                        }
                    }
                }
            }

            return(matchingIngresses);
        }
        private Cluster BuildCluster(string clusterId, ServicePortModel sp, V1Endpoints endpoints)
        {
            var destinations = new Dictionary <string, Destination>();

            var destinationIndex = 0;

            foreach (var subset in endpoints.Subsets)
            {
                foreach (var port in subset.Ports)
                {
                    if (string.IsNullOrWhiteSpace(sp.PortName))
                    {
                        // Compare based on port number
                        if (sp.Port == port.Port)
                        {
                            // TODO - Check the scheme! Currently assuming http. Also figure out what to do with unavailable addresses
                            var addresses = subset.Addresses.Select(a => $"http://{a.Ip}:{port.Port}").ToArray();
                            for (var i = 0; i < addresses.Length; i++)
                            {
                                var dest = new Destination()
                                {
                                    Address = addresses[i],
                                };

                                destinations.Add($"{clusterId}/{destinationIndex}", dest);
                                destinationIndex++;
                            }
                        }
                    }
                    else
                    {
                        if (sp.PortName == port.Name)
                        {
                            // TODO - Check the scheme! Currently assuming http. Also figure out what to do with unavailable addresses
                            var addresses = subset.Addresses.Select(a => $"http://{a.Ip}:{port.Port}").ToArray();
                            for (var i = 0; i < addresses.Length; i++)
                            {
                                var dest = new Destination()
                                {
                                    Address = addresses[i],
                                };

                                destinations.Add($"{clusterId}/{destinationIndex}", dest);
                                destinationIndex++;
                            }
                        }
                    }
                }
            }

            return(new Cluster
            {
                Id = clusterId,
                Destinations = destinations,
            });
        }