watch deletes
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m58s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m58s
This commit is contained in:
21
discovery.go
21
discovery.go
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user