Skip to content

Add namespace information to service instance metadata #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

m-kay
Copy link

@m-kay m-kay commented Dec 18, 2019

As mentioned in issue #473 the namespace should be reflected in the serviceId when using discovery with all-namespaces set to true.

@Haybu
Copy link
Contributor

Haybu commented Jun 10, 2020

@m-kay - I wonder why to consider a namespace as long as it's still one service doing the same thing, but happens to be across different namespaces? why not to discover and balance across regardless of the namespace as long as endpoints are reachable using registered IP/port?

Copy link
Member

@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to rely on @Haybu and @ryanjbaxter for implementation checks

@ryanjbaxter
Copy link
Contributor

I agree with @Haybu, I don't think the service id should be different. Maybe we should add namespace information to the metadata?

@m-kay
Copy link
Author

m-kay commented Jun 11, 2020

Correct me if I'm wrong, but I think it's not possible to access the service by only its service id from outside its own namespace. See: https://kubernetes.io/docs/concepts/services-networking/service/#dns

@Haybu
Copy link
Contributor

Haybu commented Jun 12, 2020

@m-kay - that is true when a service name/id is used directly and you depend on k8s platform to resolve. But the idea here is that the discovery client uses a service id to interrogate the platform server for the service endpoints, hence providing a list of pods ip:port to pick from and hit.

@m-kay
Copy link
Author

m-kay commented Jun 15, 2020

But how can you be sure that two services are really the same? It's totally valid to have two different services with the same name in different namespaces. In my case I have different applications which are structured the same way in different namespaces. Each application has the same service for example "gateway". However each gateway does something different because it's specific for the application in that namespace.

@spencergibb
Copy link
Member

We'll need to have a discussion. What you've described and what Haytham and Ryan have said are both valid, but at odds with each other. We'll need to see if we can reconcile the two. I don't know if it's in discovery client or in loadbalancer or both.

@GianniGiglio
Copy link

Hi am looking for a similar solution where services can be deployed on multiple namespaces (1 namespace eq customer instance).

I still have a multitenant application using the kuberentesDiscoveryClient and I would need to route the request to multiple namespaces.

I was wondering why not add the namespace to kubernetesServiceInstance instead of in the serviceId?

@ryanjbaxter
Copy link
Contributor

After discussing this with the team today we think the best solution at this time is to only take the portion of this PR which puts the namespace in the service metadata
https://github.com/spring-cloud/spring-cloud-kubernetes/pull/503/files#diff-bf6bb75a365ae3173c5d845358dfdc85R116

From there clients should be able to determine which instance of the service they want to target.

@m-kay if you are interested in modifying the PR to just include those changes please let me know

@GianniGiglio
Copy link

GianniGiglio commented Jun 29, 2020

After discussing this with the team today we think the best solution at this time is to only take the portion of this PR which puts the namespace in the service metadata
https://github.com/spring-cloud/spring-cloud-kubernetes/pull/503/files#diff-bf6bb75a365ae3173c5d845358dfdc85R116

From there clients should be able to determine which instance of the service they want to target.

@m-kay if you are interested in modifying the PR to just include those changes please let me know

If the service instance would contain the namespace as a field, Spring cloud gateway could for instance forward to the correct namespace when using kubernetes services dns.

example config:

spring: cloud: gateway: discovery: locator: url-expression: "'http://'+serviceId+' + 'namespace'+''.svc.cluster-domain.example'

@m-kay
Copy link
Author

m-kay commented Jul 6, 2020

@ryanjbaxter yes I would like to change the pull requests. However the code you mentioned in the diff does not include the namespace in the service metadata but was to filter service instances in the k8s client. As I understood the idea is to include the namespace information in the ServiceInstance, right?

@GianniGiglio I could include a getter method in the KubernetesServiceInstance which returns the information from the metadata, that should also do the trick. However you will still have the problem that the service which is used can be from any namespace because the discovery client will return an unfiltered list of all instances of the service from all namespaces.

@m-kay m-kay force-pushed the feature/reflect-namespace-in-service-id branch from c036db5 to a422611 Compare July 6, 2020 09:49
m-kay pushed a commit to m-kay/spring-cloud-kubernetes that referenced this pull request Jul 6, 2020
@m-kay m-kay requested a review from spencergibb July 6, 2020 09:52
@GianniGiglio
Copy link

@ryanjbaxter yes I would like to change the pull requests. However the code you mentioned in the diff does not include the namespace in the service metadata but was to filter service instances in the k8s client. As I understood the idea is to include the namespace information in the ServiceInstance, right?

@GianniGiglio I could include a getter method in the KubernetesServiceInstance which returns the information from the metadata, that should also do the trick. However, you will still have the problem that the service which is used can be from any namespace because the discovery client will return an unfiltered list of all instances of the service from all namespaces.

That depends on how the Kubernetes client is configured I think. At least your proposed solution is how I worked around it by over-writing the kubernetesServiceInstance and populate the namespace based on the metadata.

this.kubernetesClient.endpoints().inNamespace("default") --> will only return the endpoints for the default namespace
this.kubernetesClient.endpoints().inAnyNamespace() --> will return all enpoints in all namespaces.

endpoints.getMetadata().getNamespace() should give you the namespace.

@ryanjbaxter ryanjbaxter changed the title reflect namespace in serviceId Add namespace information to service instance metadata Jul 6, 2020
Copy link
Contributor

@ryanjbaxter ryanjbaxter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just one small tweak

@m-kay
Copy link
Author

m-kay commented Jul 7, 2020

Can we get this in the next 1.1.x release by any chance or will it only make it in the next major release?

Copy link
Member

@spencergibb spencergibb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you retarget to 1.1.x branch?

/**
* Key of the namespace metadata.
*/
public static final String NAMESPACE_METADAT_KEY = "namespace";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAMESPACE_METADAT_KEY -> NAMESPACE_METADATA_KEY (metadata is missspelled).

m-kay pushed a commit to m-kay/spring-cloud-kubernetes that referenced this pull request Aug 14, 2020
@m-kay m-kay force-pushed the feature/reflect-namespace-in-service-id branch from 2e955ab to 6f7c1be Compare August 14, 2020 15:08
@m-kay m-kay changed the base branch from master to 1.1.x August 14, 2020 15:08
@m-kay m-kay force-pushed the feature/reflect-namespace-in-service-id branch from 6f7c1be to 3d42f97 Compare August 14, 2020 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants