private IActionResult GetRoutesForGateway()

in sfintegration/Controllers/v1.cs [201:294]


        private IActionResult GetRoutesForGateway(string name, List<EnvoyVirtualHost> ret)
        {
            var segments = name.Split("|");
            if (segments.Length != 2 ||
                EnvoyDefaults.gateway_map == null ||
                !EnvoyDefaults.gateway_map.ContainsKey(segments[1]))
            {
                return Ok(
                    new
                    {
                        virtual_hosts = new[]
                        {
                            new {
                                name = "Dummy",
                                domains = new List<string>() { "*" },
                                routes = ret
                            }
                        }
                    });
            }


            var filterConfigs = EnvoyDefaults.gateway_map[segments[1]];
            foreach (var config in filterConfigs)
            {
                if (config.Type == ListenerFilterConfig.ListenerFilterType.Http)
                {
                    var httpHosts = ((ListenerHttpFilterConfig)config).Hosts;
                    foreach (var host in httpHosts)
                    {
                        var routes = new List<EnvoyRoute>();
                        foreach (var route in host.Routes)
                        {
                            var serviceName = route.Destination.EnvoyServiceName();
                            if (!SF_Services.services_.ContainsKey(serviceName))
                            {
                                continue;
                            }
                            var service = SF_Services.services_[serviceName];
                            foreach (var partition in service.Partitions)
                            {
                                string routeConfigForPartition = partition.ToString() + "|" + service.EndpointIndex.ToString() + "|0";
                                var pId = partition;
                                var info = SF_Services.EnvoyInformationForPartition(pId);
                                foreach (var serviceInfo in info)
                                {
                                    if (serviceInfo.cluster.name != routeConfigForPartition)
                                    {
                                        continue;
                                    }
                                    foreach (var envoyRoute in serviceInfo.routes)
                                    {
                                        var tuple = RemoveStatefulHeadersAndFilter(envoyRoute.headers, route.Destination.EndpointName);
                                        if (!tuple.Item1)
                                        {
                                            continue;
                                        }

                                        // We should filter only on user specified headers
                                        envoyRoute.headers = new List<JObject>();
                                        envoyRoute.prefix = route.Match.Path.Value;
                                        envoyRoute.prefix_rewrite = route.Match.Path.Rewrite;
                                        if (route.Match.Headers != null)
                                        {
                                            foreach (var header in route.Match.Headers)
                                            {
                                                var envoyHeader = new JObject();
                                                envoyHeader.Add("name", header.Name);
                                                envoyHeader.Add("value", header.Value);
                                                envoyRoute.headers.Add(envoyHeader);
                                            }
                                        }
                                        envoyRoute.cluster = serviceName;
                                        routes.Add(envoyRoute);
                                    }
                                }
                            }

                        }
                        var domain = new List<string>() { host.Name };
                        var virtual_host = new EnvoyVirtualHost(host.Name,
                            domain,
                            routes);
                        ret.Add(virtual_host);
                    }
                }
            }

            return Ok(
                new
                {
                    virtual_hosts = ret
                });
        }