Skip to content

Commit

Permalink
fix: use retry watchers is UI (#2404)
Browse files Browse the repository at this point in the history
Co-authored-by: Ben Elferink <[email protected]>
  • Loading branch information
RonFed and BenElferink authored Feb 7, 2025
1 parent 3c5a428 commit 5c4afe1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
10 changes: 8 additions & 2 deletions frontend/kube/watchers/destination_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package watchers
import (
"context"
"fmt"
"log"
"time"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/odigos-io/odigos/frontend/services/sse"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
)

var destinationAddedEventBatcher *EventBatcher
Expand Down Expand Up @@ -63,9 +66,11 @@ func StartDestinationWatcher(ctx context.Context, namespace string) error {
},
)

watcher, err := kube.DefaultClient.OdigosClient.Destinations(namespace).Watch(context.Background(), metav1.ListOptions{})
watcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return kube.DefaultClient.OdigosClient.Destinations(namespace).Watch(ctx, metav1.ListOptions{})
}})
if err != nil {
return fmt.Errorf("error creating watcher: %v", err)
return fmt.Errorf("error creating destinations watcher: %v", err)
}

go handleDestinationWatchEvents(ctx, watcher)
Expand All @@ -84,6 +89,7 @@ func handleDestinationWatchEvents(ctx context.Context, watcher watch.Interface)
return
case event, ok := <-ch:
if !ok {
log.Println("Destination watcher closed")
return
}
switch event.Type {
Expand Down
10 changes: 8 additions & 2 deletions frontend/kube/watchers/instrumentation_config_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package watchers
import (
"context"
"fmt"
"log"
"time"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
Expand All @@ -12,6 +13,8 @@ import (
commonutils "github.com/odigos-io/odigos/k8sutils/pkg/workload"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
)

var instrumentationConfigAddedEventBatcher *EventBatcher
Expand Down Expand Up @@ -64,9 +67,11 @@ func StartInstrumentationConfigWatcher(ctx context.Context, namespace string) er
},
)

watcher, err := kube.DefaultClient.OdigosClient.InstrumentationConfigs(namespace).Watch(context.Background(), metav1.ListOptions{})
watcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return kube.DefaultClient.OdigosClient.InstrumentationConfigs(namespace).Watch(ctx, metav1.ListOptions{})
}})
if err != nil {
return fmt.Errorf("error creating watcher: %w", err)
return fmt.Errorf("failed to create instrumentation config watcher: %w", err)
}

go handleInstrumentationConfigWatchEvents(ctx, watcher)
Expand All @@ -85,6 +90,7 @@ func handleInstrumentationConfigWatchEvents(ctx context.Context, watcher watch.I
return
case event, ok := <-ch:
if !ok {
log.Println("InstrumentationConfig watcher closed")
return
}
switch event.Type {
Expand Down
10 changes: 8 additions & 2 deletions frontend/kube/watchers/instrumentation_instance_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package watchers
import (
"context"
"fmt"
"log"
"time"

"github.com/odigos-io/odigos/api/odigos/v1alpha1"
Expand All @@ -12,6 +13,8 @@ import (
commonutils "github.com/odigos-io/odigos/k8sutils/pkg/workload"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
)

var instrumentationInstanceModifiedEventBatcher *EventBatcher
Expand All @@ -30,9 +33,11 @@ func StartInstrumentationInstanceWatcher(ctx context.Context, namespace string)
},
)

watcher, err := kube.DefaultClient.OdigosClient.InstrumentationInstances(namespace).Watch(context.Background(), metav1.ListOptions{})
watcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return kube.DefaultClient.OdigosClient.InstrumentationInstances(namespace).Watch(ctx, metav1.ListOptions{})
}})
if err != nil {
return fmt.Errorf("error creating watcher: %v", err)
return fmt.Errorf("failed to create instrumentation instance watcher: %w", err)
}

go handleInstrumentationInstanceWatchEvents(ctx, watcher)
Expand All @@ -49,6 +54,7 @@ func handleInstrumentationInstanceWatchEvents(ctx context.Context, watcher watch
return
case event, ok := <-ch:
if !ok {
log.Println("InstrumentationInstance watcher closed")
return
}
switch event.Type {
Expand Down
27 changes: 20 additions & 7 deletions frontend/services/collector_metrics/watchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"errors"
"fmt"

"github.com/odigos-io/odigos/api/k8sconsts"
"github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/frontend/kube"
"github.com/odigos-io/odigos/frontend/services/common"
"github.com/odigos-io/odigos/api/k8sconsts"
commonutils "github.com/odigos-io/odigos/k8sutils/pkg/workload"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
toolsWatch "k8s.io/client-go/tools/watch"
)

type notification struct {
Expand Down Expand Up @@ -43,27 +45,38 @@ type watchers struct {
}

func runWatcher(ctx context.Context, cw *deleteWatcher) error {
nodeWatcher, err := newCollectorWatcher(ctx, cw.odigosNS, k8sconsts.CollectorsRoleNodeCollector)
nodeCollectorWatcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return newCollectorWatcher(ctx, cw.odigosNS, k8sconsts.CollectorsRoleNodeCollector)
}})
if err != nil {
return err
}
clusterWatcher, err := newCollectorWatcher(ctx, cw.odigosNS, k8sconsts.CollectorsRoleClusterGateway)

clusterCollectorWatcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return newCollectorWatcher(ctx, cw.odigosNS, k8sconsts.CollectorsRoleClusterGateway)
}})
if err != nil {
return err
}
destsWatcher, err := kube.DefaultClient.OdigosClient.Destinations(cw.odigosNS).Watch(ctx, metav1.ListOptions{})

sourcesWatcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return kube.DefaultClient.OdigosClient.InstrumentationConfigs("").Watch(ctx, metav1.ListOptions{})
}})
if err != nil {
return err
}
sourcesWatcher, err := kube.DefaultClient.OdigosClient.InstrumentationConfigs("").Watch(ctx, metav1.ListOptions{})

destsWatcher, err := toolsWatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: func(_ metav1.ListOptions) (watch.Interface, error) {
return kube.DefaultClient.OdigosClient.Destinations(cw.odigosNS).Watch(ctx, metav1.ListOptions{})
}})
if err != nil {
return err
}

return runWatcherLoop(ctx,
watchers{
nodeCollectors: nodeWatcher,
clusterCollectors: clusterWatcher,
nodeCollectors: nodeCollectorWatcher,
clusterCollectors: clusterCollectorWatcher,
destinations: destsWatcher,
sources: sourcesWatcher,
}, cw.deleteNotifications)
Expand Down

0 comments on commit 5c4afe1

Please sign in to comment.