KubernetesDiscoveryClient.getInstances(String serviceId) always return null when spring.cloud.kubernetes.client.namespace=
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
@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<>();
}
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
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 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
@iocanel Shouldn't this.client.endpoints().withName(serviceId).get() return services from all namespaces (assuming no namespace is set for the client)?
@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()
Thanks @iocanel!
I am wondering if we should perhaps disallow the use of an empty namespace in order to avoid problems like this...
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