Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refresh watchers after timeout #27

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions adapters/incluster/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package incluster

import (
"context"
"errors"
"fmt"
"slices"
"time"

jsonpatch "github.com/evanphx/json-patch"
"github.com/kubescape/go-logger"
Expand All @@ -19,6 +21,11 @@ import (
"k8s.io/client-go/dynamic"
)

// resourceVersionGetter is an interface used to get resource version from events.
type resourceVersionGetter interface {
GetResourceVersion() string
}

type Client struct {
client dynamic.Interface
account string
Expand Down Expand Up @@ -86,28 +93,38 @@ func (c *Client) Start(ctx context.Context) error {
watchOpts.ResourceVersion = list.GetResourceVersion()
}
// begin watch
watcher, err := c.client.Resource(c.res).Namespace("").Watch(context.Background(), watchOpts)
if err != nil {
logger.L().Ctx(ctx).Fatal("unable to watch for resources", helpers.String("resource", c.res.Resource), helpers.Error(err))
}
eventQueue := utils.NewCooldownQueue(utils.DefaultQueueSize, utils.DefaultTTL)
go func() {
for {
event, chanActive := <-watcher.ResultChan()
if !chanActive {
watcher.Stop()
eventQueue.Stop()
break
watcher, err := c.client.Resource(c.res).Namespace("").Watch(context.Background(), watchOpts)
if err != nil {
logger.L().Ctx(ctx).Fatal("unable to watch for resources", helpers.String("resource", c.res.Resource), helpers.Error(err))
}
for {
event, chanActive := <-watcher.ResultChan()
if eventQueue.Closed() {
watcher.Stop()
return
}
if !chanActive {
logger.L().Debug("watcher channel closed, restarting in 10s", helpers.String("resource", c.res.Resource))
time.Sleep(10 * time.Second)
break
}
// set resource version to resume watch from
metaObject, ok := event.Object.(resourceVersionGetter)
if ok {
watchOpts.ResourceVersion = metaObject.GetResourceVersion()
}
eventQueue.Enqueue(event)
}
eventQueue.Enqueue(event)
}
}()
for event := range eventQueue.ResultChan {
ctx := utils.ContextFromGeneric(ctx, domain.Generic{})
if event.Type == watch.Error {
logger.L().Ctx(ctx).Error("watch event failed", helpers.String("resource", c.res.Resource), helpers.Interface("event", event))
watcher.Stop()
break
eventQueue.Stop()
return errors.New("watch event failed")
}
d, ok := event.Object.(*unstructured.Unstructured)
if !ok {
Expand Down
4 changes: 4 additions & 0 deletions utils/cooldownqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func makeEventKey(e watch.Event) string {
return eventKey
}

func (q *CooldownQueue) Closed() bool {
return q.closed
}

// Enqueue enqueues an event in the Cooldown Queue
func (q *CooldownQueue) Enqueue(e watch.Event) {
if q.closed {
Expand Down
Loading