mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-10-13 11:37:07 +00:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"regexp"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
)
|
|
|
|
func FilteredWatch(ctx context.Context, watcher watch.Interface, podFilter *regexp.Regexp) (chan *corev1.Pod, chan *corev1.Pod, chan *corev1.Pod, chan error) {
|
|
addedChan := make(chan *corev1.Pod)
|
|
modifiedChan := make(chan *corev1.Pod)
|
|
removedChan := make(chan *corev1.Pod)
|
|
errorChan := make(chan error)
|
|
go func() {
|
|
for {
|
|
select {
|
|
case e := <-watcher.ResultChan():
|
|
|
|
if e.Object == nil {
|
|
errorChan <- errors.New("kubernetes pod watch failed")
|
|
return
|
|
}
|
|
|
|
pod := e.Object.(*corev1.Pod)
|
|
|
|
if !podFilter.MatchString(pod.Name) {
|
|
continue
|
|
}
|
|
|
|
switch e.Type {
|
|
case watch.Added:
|
|
addedChan <- pod
|
|
case watch.Modified:
|
|
modifiedChan <- pod
|
|
case watch.Deleted:
|
|
removedChan <- pod
|
|
}
|
|
case <-ctx.Done():
|
|
watcher.Stop()
|
|
close(addedChan)
|
|
close(modifiedChan)
|
|
close(removedChan)
|
|
close(errorChan)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return addedChan, modifiedChan, removedChan, errorChan
|
|
}
|