🔨 Move cli folder contents into project root (#1253)

* Remove `logger` module

* Remove `shared` module

* Move `cli` folder contents into project root

* Fix linter

* Change the module name from `github.com/kubeshark/kubeshark/cli` to `github.com/kubeshark/kubeshark`

* Set the default `Makefile` rule to `build`

* Add `lint` rule

* Fix the linter errors
This commit is contained in:
M. Mert Yildiran
2022-11-25 14:17:50 -08:00
committed by GitHub
parent 9aeb1fadea
commit cb60a4cc4c
92 changed files with 512 additions and 2219 deletions

110
kubernetes/watch.go Normal file
View File

@@ -0,0 +1,110 @@
package kubernetes
import (
"context"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/kubeshark/kubeshark/debounce"
"k8s.io/apimachinery/pkg/watch"
)
type EventFilterer interface {
Filter(*WatchEvent) (bool, error)
}
type WatchCreator interface {
NewWatcher(ctx context.Context, namespace string) (watch.Interface, error)
}
func FilteredWatch(ctx context.Context, watcherCreator WatchCreator, targetNamespaces []string, filterer EventFilterer) (<-chan *WatchEvent, <-chan error) {
eventChan := make(chan *WatchEvent)
errorChan := make(chan error)
var wg sync.WaitGroup
for _, targetNamespace := range targetNamespaces {
wg.Add(1)
go func(targetNamespace string) {
defer wg.Done()
watchRestartDebouncer := debounce.NewDebouncer(1*time.Minute, func() {})
for {
watcher, err := watcherCreator.NewWatcher(ctx, targetNamespace)
if err != nil {
errorChan <- fmt.Errorf("error in k8s watch: %v", err)
break
}
err = startWatchLoop(ctx, watcher, filterer, eventChan) // blocking
watcher.Stop()
select {
case <-ctx.Done():
return
default:
break
}
if err != nil {
errorChan <- fmt.Errorf("error in k8s watch: %v", err)
break
} else {
if !watchRestartDebouncer.IsOn() {
if err := watchRestartDebouncer.SetOn(); err != nil {
log.Print(err)
}
log.Print("k8s watch channel closed, restarting watcher")
time.Sleep(time.Second * 5)
continue
} else {
errorChan <- errors.New("k8s watch unstable, closes frequently")
break
}
}
}
}(targetNamespace)
}
go func() {
<-ctx.Done()
wg.Wait()
close(eventChan)
close(errorChan)
}()
return eventChan, errorChan
}
func startWatchLoop(ctx context.Context, watcher watch.Interface, filterer EventFilterer, eventChan chan<- *WatchEvent) error {
resultChan := watcher.ResultChan()
for {
select {
case e, isChannelOpen := <-resultChan:
if !isChannelOpen {
return nil
}
wEvent := WatchEvent(e)
if wEvent.Type == watch.Error {
return wEvent.ToError()
}
if pass, err := filterer.Filter(&wEvent); err != nil {
return err
} else if !pass {
continue
}
eventChan <- &wEvent
case <-ctx.Done():
return nil
}
}
}