watch pods instead of polling
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m57s

This commit is contained in:
matst80
2024-11-10 10:26:21 +01:00
parent 1628c8ca31
commit 351280347b
6 changed files with 90 additions and 13 deletions

View File

@@ -3,12 +3,17 @@ package main
import (
"context"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
)
type Discovery interface {
Discover() ([]string, error)
Watch() (<-chan string, error)
}
type K8sDiscovery struct {
@@ -33,6 +38,32 @@ func (k *K8sDiscovery) DiscoverInNamespace(namespace string) ([]string, error) {
return hosts, nil
}
func (k *K8sDiscovery) Watch() (<-chan string, error) {
timeout := int64(30)
watcherFn := func(options metav1.ListOptions) (watch.Interface, error) {
return k.client.CoreV1().Pods("").Watch(k.ctx, metav1.ListOptions{
LabelSelector: "actor-pool=cart",
TimeoutSeconds: &timeout,
})
}
watcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: watcherFn})
if err != nil {
return nil, err
}
ch := make(chan string)
go func() {
for event := range watcher.ResultChan() {
if event.Type != watch.Added {
continue
}
pod := event.Object.(*v1.Pod)
ch <- pod.Status.PodIP
}
}()
return ch, nil
}
func NewK8sDiscovery(client *kubernetes.Clientset) *K8sDiscovery {
return &K8sDiscovery{
ctx: context.Background(),