watch deletes
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m58s

This commit is contained in:
matst80
2024-11-11 07:17:37 +01:00
parent 745eca60f1
commit 46d9f1dd96
2 changed files with 34 additions and 17 deletions

View File

@@ -13,7 +13,7 @@ import (
type Discovery interface {
Discover() ([]string, error)
Watch() (<-chan string, error)
Watch() (<-chan HostChange, error)
}
type K8sDiscovery struct {
@@ -38,7 +38,12 @@ func (k *K8sDiscovery) DiscoverInNamespace(namespace string) ([]string, error) {
return hosts, nil
}
func (k *K8sDiscovery) Watch() (<-chan string, error) {
type HostChange struct {
Host string
Type watch.EventType
}
func (k *K8sDiscovery) Watch() (<-chan HostChange, error) {
timeout := int64(30)
watcherFn := func(options metav1.ListOptions) (watch.Interface, error) {
return k.client.CoreV1().Pods("").Watch(k.ctx, metav1.ListOptions{
@@ -50,15 +55,15 @@ func (k *K8sDiscovery) Watch() (<-chan string, error) {
if err != nil {
return nil, err
}
ch := make(chan string)
ch := make(chan HostChange)
go func() {
for event := range watcher.ResultChan() {
if event.Type != watch.Added {
continue
}
pod := event.Object.(*v1.Pod)
ch <- pod.Status.PodIP
pod := event.Object.(*v1.Pod)
ch <- HostChange{
Host: pod.Status.PodIP,
Type: event.Type,
}
}
}()
return ch, nil