private IList <IServiceInstance> GetNamespacedServiceInstances(EndpointSubsetNs es, string serviceId)
        {
            var k8SNamespace = es.Namespace;
            var subsets      = es.EndpointSubsets;
            var instances    = new List <IServiceInstance>();

            if (subsets.Any())
            {
                var service = KubernetesClient.ListNamespacedService(
                    namespaceParameter: k8SNamespace,
                    fieldSelector: $"metadata.name={serviceId}").Items.FirstOrDefault();
                var serviceMetadata = GetServiceMetadata(service);
                var metadataProps   = _discoveryOptions.CurrentValue.Metadata;

                foreach (var subset in subsets)
                {
                    // Extend the service metadata map with per-endpoint port information (if requested)
                    var endpointMetadata = new Dictionary <string, string>(serviceMetadata);
                    if (metadataProps.AddPorts)
                    {
                        var ports = subset.Ports
                                    .Where(p => !string.IsNullOrWhiteSpace(p.Name))
                                    .ToDictionary(p => p.Name, p => p.Port.ToString());

                        var portMetadata = GetDictionaryWithPrefixedKeys(ports, metadataProps.PortsPrefix);

                        foreach (var portMetadataRecord in portMetadata)
                        {
                            endpointMetadata.Add(portMetadataRecord.Key, portMetadataRecord.Value);
                        }
                    }

                    var addresses = subset.Addresses;
                    foreach (var endpointAddress in addresses)
                    {
                        string instanceId = null;
                        if (endpointAddress.TargetRef != null)
                        {
                            instanceId = endpointAddress.TargetRef.Uid;
                        }

                        var endpointPort = FindEndpointPort(subset);
                        instances.Add(
                            new KubernetesServiceInstance(
                                instanceId,
                                serviceId,
                                endpointAddress,
                                endpointPort,
                                endpointMetadata,
                                _isServicePortSecureResolver.Resolve(new Input(service?.Metadata.Name, endpointPort.Port, service?.Metadata.Labels, service?.Metadata.Annotations))));
                    }
                }
            }

            return(instances);
        }
        private EndpointSubsetNs GetSubsetsFromEndpoints(V1Endpoints endpoints)
        {
            // Start with config or default
            var es = new EndpointSubsetNs {
                Namespace = _discoveryOptions.CurrentValue.Namespace ?? DefaultNamespace
            };

            if (endpoints?.Subsets == null)
            {
                return(es);
            }

            es.Namespace       = endpoints.Metadata.NamespaceProperty;
            es.EndpointSubsets = endpoints.Subsets;

            return(es);
        }