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

Implement A CatalogWatch for Kubernetes Java Client

Open wind57 opened this issue 3 years ago • 5 comments

I am trying to understand what is the purpose of KubernetesCatalogWatch in the fabric8 implementation of discovery. It seems to me that KubernetesDiscoveryClient is an implementation of DiscoveryClient and KubernetesCatalogWatch was an initial implementation before that one existed.

As such, I am very inclined to delete it from the repo... unless I am missing something. what do you think? thank you

wind57 avatar Jul 07 '22 11:07 wind57

seems like spring-boot-admin relies on this for getting proper heartbeat events, so no, we can't delete it.

wind57 avatar Jul 07 '22 11:07 wind57

what I find interesting is that such an implementation does not exist in k8s-native. May be someone can explain...

wind57 avatar Jul 07 '22 11:07 wind57

I dont know the exact history here, but this was modeled about ConsulCatalogWatch and it appears to publish a HeartBeat event when the services change. What that is used for I am not sure. @spencergibb can you describe how ConsulCatalogWatch is used?

It is not a DiscoveryClient implementation as far as I can tell.

ryanjbaxter avatar Aug 17 '22 21:08 ryanjbaxter

it appears to publish a HeartBeat event when the services change.

That's exactly right. Then something can use that event to do something like spring cloud gateway can refresh routes and if there is a new one it can add it.

spencergibb avatar Aug 17 '22 21:08 spencergibb

May be someone can explain...

I think the reason why it doesn't exist in the k8s java client is simply we never implemented it

ryanjbaxter avatar Aug 17 '22 23:08 ryanjbaxter

I started working on this, and started by refactoring a bit the existing fabric8 KubernetesCatalogWatch implementation. While doing that, I have observed a minor thing. We return inside the heartbeat the list of endpoints as a List<String>. We also support the search against "any namespace", which means that in theory we can return a List that would contain duplicates (same endpoint name across different namespace).

Do you think we should refactor this one to publish a Tuple like endpoint name and namespace where it came from? This would mean a breaking change for clients, but, imho, we would be fixing a bug here. @ryanjbaxter wdyt? thank you.

wind57 avatar Nov 13 '22 17:11 wind57

Should we be returning endpoints across all namespaces or should that be enabled via a property, kind of like service discovery?

ryanjbaxter avatar Nov 14 '22 00:11 ryanjbaxter

We already have that implemented in KubernetesCatalogWatch, we do check for "all-namespaces" property:

List<Endpoints> endpoints;
if (properties.allNamespaces()) {
	endpoints = kubernetesClient.endpoints().inAnyNamespace().withLabels(properties.serviceLabels()).list()
						.getItems();
}
else {
	endpoints = kubernetesClient.endpoints().withLabels(properties.serviceLabels()).list().getItems();
}

wind57 avatar Nov 14 '22 01:11 wind57

In theory, we can leave the code as-is, as no one complained (yet), but overall this is still a tiny issue.

wind57 avatar Nov 14 '22 16:11 wind57

So its only when all-namespaces is true that we run into this issue then?

ryanjbaxter avatar Nov 14 '22 21:11 ryanjbaxter

If so I think we should make the breaking change. I would just make it in #1138

ryanjbaxter avatar Nov 14 '22 22:11 ryanjbaxter

So its only when all-namespaces is true that we run into this issue then?

Currently. There is a PR (that I am yet to take a closer look at) here that adds the possibility to specify specific namespaces. And once we have that (and implement the same in CatalogWatcher), we might run into the same problem.

wind57 avatar Nov 15 '22 11:11 wind57

Ok makes sense. Let's make the change to a tuple in your PR

ryanjbaxter avatar Nov 15 '22 11:11 ryanjbaxter