Skip to content

The problem occurs when you have two or more namespaces, where the service name is the same #473

@claitonp

Description

@claitonp

The problem occurs when you have two or more namespaces, where the service name is the same.
Example: service 'price' is in namespaces ns1 and ns2 in the same cluster kubernetes.

xxx-service.yaml

metadata:
   namespace: ns1
   name: price
   labels:
       app: price
       group.department: dept1

yyy-service.yaml

metadata:
  namespace: ns2
  name: price
  labels:
      app: price
      group.department: dept2
	  

app-discovery.yaml

spring:
  cloud:
    kubernetes:
	discovery:
	  service-labels:
	  - group.department: dept1

In this case, the current code returns the endpoints of both namespaces, which is not expected.
To resolve this issue you have to apply the service-labels filter, .withLabels(serviceLabels).

spring-cloud-kubernetes, version:1.0.4.BUILD-SNAPSHOT

package org.springframework.cloud.kubernetes.discovery;
public class KubernetesDiscoveryClient implements DiscoveryClient {
  ...
	@Override
	public List<ServiceInstance> getInstances(String serviceId) {
		...
		// bug
		// List<Endpoints> endpointsList = this.properties.isAllNamespaces()
		//	? this.client.endpoints().inAnyNamespace().withField("metadata.name", serviceId).list().getItems()
		//	: Collections.singletonList(this.client.endpoints().withName(serviceId).get());

		//adjusted
		Map<String, String> serviceLabels = properties.getServiceLabels();
		List<Endpoints> endpointsList = properties.isAllNamespaces() ?
				client.endpoints().inAnyNamespace().withField("metadata.name", serviceId).withLabels(serviceLabels).list().getItems()
				: Collections.singletonList(client.endpoints().withName(serviceId).get());
	}			
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions