public static Cluster ToEnvoyIngressCluster(this EndpointInstance e)
        {
            var cluster = new Cluster()
            {
                Name           = e.Type.IngressId,
                LbPolicy       = Cluster.Types.LbPolicy.RoundRobin,
                Type           = Cluster.Types.DiscoveryType.Static,
                ConnectTimeout = Duration.FromTimeSpan(TimeSpan.FromMilliseconds(250)),
            };

            cluster.Hosts.Add(new Address()
            {
                SocketAddress = new SocketAddress
                {
                    Address   = e.NetworkAddress.Host,
                    PortValue = (uint)e.NetworkAddress.Port
                }
            });

            return(cluster);
        }
        private static List <EndpointInstance> ExtractEndpoints(ServiceReplicaList replicas, Partition partition,
                                                                Uri service)
        {
            List <EndpointInstance> endpointInstances = new List <EndpointInstance>();

            foreach (var replica in replicas)
            {
                if (replica.ReplicaAddress.Length == 0)
                {
                    continue;
                }

                JObject addresses;
                try
                {
                    addresses = JObject.Parse(replica.ReplicaAddress);
                }
                catch
                {
                    continue;
                }

                var endpoints = addresses["Endpoints"].Value <JObject>();
                foreach (var endpoint in endpoints)
                {
                    var endpointName    = endpoint.Key;
                    var endpointAddress = endpoint.Value.ToString();

                    if (!endpointAddress.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    EndpointType endpointType = null;

                    if (partition.ServiceKind == ServiceKind.Stateful)
                    {
                        var statefulRole = ((StatefulServiceReplica)replica).ReplicaRole;
                        ServiceEndpointRole role;
                        switch (statefulRole)
                        {
                        case ReplicaRole.Primary:
                            role = ServiceEndpointRole.StatefulPrimary;
                            break;

                        case ReplicaRole.ActiveSecondary:
                            role = ServiceEndpointRole.StatefulSecondary;
                            break;

                        default:
                            role = ServiceEndpointRole.Invalid;
                            break;
                        }

                        switch (partition.PartitionInformation.Kind)
                        {
                        case ServicePartitionKind.Singleton:
                            endpointType = EndpointType.CreateSingleton(service.AbsolutePath,
                                                                        partition.PartitionInformation.Id, endpointName, endpoints.Count == 1, role);
                            break;

                        case ServicePartitionKind.Int64Range:
                            var rangePartitionInformation =
                                (Int64RangePartitionInformation)partition.PartitionInformation;
                            endpointType = EndpointType.CreateInt64Partitioned(service.AbsolutePath,
                                                                               partition.PartitionInformation.Id, endpointName, endpoints.Count == 1, role,
                                                                               rangePartitionInformation.LowKey, rangePartitionInformation.HighKey);
                            break;

                        case ServicePartitionKind.Named:
                            var namedPartitionInformation =
                                (NamedPartitionInformation)partition.PartitionInformation;
                            endpointType = EndpointType.CreateNamedPartitioned(service.AbsolutePath,
                                                                               partition.PartitionInformation.Id, endpointName, endpoints.Count == 1, role,
                                                                               namedPartitionInformation.Name);
                            break;

                        case ServicePartitionKind.Invalid:
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                    else
                    {
                        endpointType = EndpointType.CreateStateless(service.AbsolutePath,
                                                                    partition.PartitionInformation.Id, endpointName, endpoints.Count == 1);
                    }

                    if (endpointType != null)
                    {
                        var endpointInstanceObject =
                            new EndpointInstance(endpointType, new Uri(endpointAddress), replica.NodeName);
                        endpointInstances.Add(endpointInstanceObject);
                    }
                }
            }

            return(endpointInstances);
        }