spring-cloud-kubernetes icon indicating copy to clipboard operation
spring-cloud-kubernetes copied to clipboard

KubernetesDiscoveryClient.getInstances(String serviceId) always return null when spring.cloud.kubernetes.client.namespace=

Open weixsun opened this issue 7 years ago • 8 comments

spring frame version is: spring cloud kubernetes 1.0.1.RELEASE

I have a token that is a kubernetes-admin RBAC,I want to get all instances in every service when i confiure "spring.cloud.kubernetes.client.namespace=${a specific namespace}", that is very OK.

If i want to get instances in all kubernetes namespaces, the config is "spring.cloud.kubernetes.client.namespace=",this is a empty string. i only can get List service. this.discoveryClient.getInstances(service) is null,the detail code in following:

@Autowired
private DiscoveryClient discoveryClient;

@RequestMapping("/skynet")
    public List<SkynetService> skynetService() {
    	List<SkynetService> skynetServices = new ArrayList<>();
    	
    	List<String> services = this.discoveryClient.getServices();
    	
    	for (String service : services) {
			List<ServiceInstance> instances = this.discoveryClient.getInstances(service);
			
			List<SkynetServiceInstance> skynetServiceInstances = new ArrayList<>();
			
			for (ServiceInstance instance : instances) {
				skynetServiceInstances.add(new SkynetServiceInstance(instance.getInstanceId(), 
						instance.getServiceId(), 
						instance.getScheme(), 
						instance.getHost(), 
						instance.getPort(), 
						instance.isSecure(),
						instance.getUri(), 
						instance.getMetadata()));
			}
			
			skynetServices.add(new SkynetService(service, 
					skynetServiceInstances));
		}
    	return skynetServices;
    }
    @lombok.Data
    @lombok.AllArgsConstructor
    public class SkynetServiceInstance {
    	
    	private String instanceId;
    	
    	private String serviceId;
    	
    	private String scheme;
    	
    	private String host;
    	
    	private int port;
    	
    	private boolean isSecure;
    	
    	private URI uri;
    	
    	private Map<String, String> metadata;
    	
    }
    
    @lombok.Data
    @lombok.AllArgsConstructor
    public class SkynetService {
        private String name;
        
//        @javax.annotation.Nullable
//        private String buildVersion;
        
//        private Instant statusTimestamp = Instant.now();
        
        private List<SkynetServiceInstance> instances = new ArrayList<>();
    }

weixsun avatar May 17 '19 11:05 weixsun

Preliminary findings: KubernetesDiscoveryClient.java 104 rows code Endpoints endpoints = this.client.endpoints().withName(serviceId).get(); return null And must allocate a special namesapce eg: Endpoints endpoints = this.client.endpoints().inNamespace("kube-public").withName(services.get(0)).get();

i think should traversal namespace when "spring.cloud.kubernetes.client.namespace=" in KubernetesDiscoveryClient.java public List getInstances(String serviceId)

weixsun avatar May 17 '19 12:05 weixsun

Just to make sure I undestand correctly, you are saying that when you have set an empty namespace, this.discoveryClient.getServices() correctly all services, but that this.discoveryClient.getInstances(service) doesn't return the instances for some of the returned services?

geoand avatar May 17 '19 13:05 geoand

@geoand your understand very correctly, through my test result , when i have set an empty namesapce ,and i have the kubernetes cluster admin premission, and correctly set "spring.cloud.kubernetes.client*" all config, this.discoveryClient.getInstances(service) always return null

weixsun avatar May 18 '19 13:05 weixsun

@iocanel Shouldn't this.client.endpoints().withName(serviceId).get() return services from all namespaces (assuming no namespace is set for the client)?

geoand avatar May 20 '19 06:05 geoand

@geoand AFAIR, the client will always default to the current namespace, unless explicitly specified. So to answer your question, I'd say no, it should return only stuff found in the current namespace. To get services found in all namespaces:

client.services().inAnyNamespace().list()

iocanel avatar May 20 '19 06:05 iocanel

Thanks @iocanel!

geoand avatar May 20 '19 06:05 geoand

I am wondering if we should perhaps disallow the use of an empty namespace in order to avoid problems like this...

geoand avatar May 20 '19 07:05 geoand

Actually, I think I am going to close this because it looks like a duplicate of #163. Feel free to reopen if you feel this issue has some differences

geoand avatar May 20 '19 08:05 geoand