spring-cloud / spring-cloud-kubernetes

Kubernetes integration with Spring Cloud Discovery Client, Configuration, etc...
Apache License 2.0
3.46k stars 1.03k forks source link

Cross-Namespace Service Discovery issue #1670

Closed tarun-bb closed 2 months ago

tarun-bb commented 3 months ago

Describe the bug I am trying to invoke from serviceA in namespace1 to my serviceB in namespace2 in my k8 env. It works fine when I use SPRING_CLOUD_KUBERNETES_DISCOVERY_ALL_NAMESPACES: true(with clusterRole, clusterRoleBinding to the ServiceAccount they use)

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cross-namespace-service-discovery
rules:
- apiGroups: [""]
  resources: ["pods", "services", "endpoints"]
  verbs: ["get", "list", "watch"]

  -----

  apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cross-namespace-service-discovery-binding
subjects:
- kind: ServiceAccount
  name: default
  namespace: namespace1
roleRef:
  kind: ClusterRole
  name: cross-namespace-service-discovery
  apiGroup: rbac.authorization.k8s.io

  -------

  apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cross-namespace-service-discovery-binding
subjects:
- kind: ServiceAccount
  name: default
  namespace: namespace2
roleRef:
  kind: ClusterRole
  name: cross-namespace-service-discovery
  apiGroup: rbac.authorization.k8s.io

but when i turn it off and use specific namespaces like below then it doesn't work and i see an error Service Instance cannot be null, serviceId: serviceB

- name: SPRING_CLOUD_KUBERNETES_DISCOVERY_ALL_NAMESPACES
   value: "true"
- name: SPRING_CLOUD_KUBERNETES_DISCOVERY_NAMESPACES_0
   value: namespace1
- name: SPRING_CLOUD_KUBERNETES_DISCOVERY_NAMESPACES_1
   value: namespace2

spring-cloud-starter-kubernetes-fabric8 version is : 3.0.5

However with specific namespace, I do see logs says but somehow the restTemplate returns this error.

2024-07-08T08:51:59.290Z DEBUG [retail-onboarding,,] 1 --- [nio-8080-exec-3] .c.k.f.d.Fabric8DiscoveryServicesAdapter : searching in namespaces : [namespace1, namespace2] with filter : null 

2024-07-08T08:51:59.336Z DEBUG [retail-onboarding,,] 1 --- [io-8080-exec-10] o.s.c.k.f.d.KubernetesDiscoveryClient    : will return services : [serviceA, serviceB, ingress-nginx-controller, ingress-nginx-controller-admission, kubernetes,]

2024-07-08T08:52:22.099Z ERROR [retail-onboarding,,] 1 --- [nio-8080-exec-9] c.b.f.i.a.RunWithoutAuthorizationAspect  : Exception thrown when invoking join point

com.backbase.audit.client.exceptions.InternalServerErrorException: java.lang.IllegalArgumentException: Service Instance cannot be null, serviceId: audit-service                                                                                                                         Caused by: java.lang.IllegalArgumentException: Service Instance cannot be null, serviceId: serviceB
wind57 commented 2 months ago

The error comes from your codebase btw.

If you can provide a minimal reproducible example we can run locally where the problem can be seen with clear instructions, I will take a look what is going on.

Thank you

wind57 commented 2 months ago

@ryanjbaxter can you please add the "feedback" label? thank you

tarun-bb commented 2 months ago

@ryanjbaxter I actually tried it locally with minikube and very small application setup(not the one above and also latest version) and it did worked there. However I noticed when it worked on my local i see these log lines from o.s.c.k.f.l.Fabric8ServicesListSupplier but no such logs on my client server when it fail actually to find that server id

image

wind57 commented 2 months ago

Fabric8ServicesListSupplier is used when we have to deal with load-balancing (and you included that dependency or enabled load-balancing). From your initial description of what you are trying to do, you are most probably indeed using load-balancing; but without a sample and clear instructions on what you are trying to, I don't think we will be able to help you.

spring-cloud-issues commented 2 months ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues commented 2 months ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.