Skip to content

Commit

Permalink
watcher: Implement getters for informer factories
Browse files Browse the repository at this point in the history
Unexport informer factories in the K8sWatcher struct and implement exported
Get* methods instead. This will make it easier to reuse them and test.

Signed-off-by: Anna Kapuscinska <[email protected]>
  • Loading branch information
lambdanis committed Feb 21, 2025
1 parent 03fb3ae commit 68bb075
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
14 changes: 14 additions & 0 deletions pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cilium/tetragon/pkg/cgrouprate"
"github.com/cilium/tetragon/pkg/defaults"
"github.com/cilium/tetragon/pkg/encoder"
"github.com/cilium/tetragon/pkg/k8s/client/informers/externalversions"
"github.com/cilium/tetragon/pkg/metricsconfig"
"github.com/cilium/tetragon/pkg/observer"
"github.com/cilium/tetragon/pkg/policyfilter"
Expand Down Expand Up @@ -49,6 +50,7 @@ import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -586,6 +588,18 @@ func (f *fakeK8sWatcher) GetInformer(_ string) cache.SharedIndexInformer {

func (f *fakeK8sWatcher) Start() {}

func (f *fakeK8sWatcher) GetK8sInformerFactory() informers.SharedInformerFactory {
return nil
}

func (f *fakeK8sWatcher) GetLocalK8sInformerFactory() informers.SharedInformerFactory {
return nil
}

func (f *fakeK8sWatcher) GetCRDInformerFactory() externalversions.SharedInformerFactory {
return nil
}

// Used to wait for a process to start, we do a lookup on PROCFS because this
// may be called before obs is created.
func WaitForProcess(process string) error {
Expand Down
15 changes: 15 additions & 0 deletions pkg/watcher/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/cache"

"github.com/cilium/tetragon/pkg/k8s/client/informers/externalversions"
)

// FakeK8sWatcher is used as an "empty" PodAccessor when --enable-k8s-api flag is not set.
Expand Down Expand Up @@ -73,3 +76,15 @@ func (watcher *FakeK8sWatcher) GetInformer(_ string) cache.SharedIndexInformer {
}

func (watcher *FakeK8sWatcher) Start() {}

func (watcher *FakeK8sWatcher) GetK8sInformerFactory() informers.SharedInformerFactory {
return nil
}

func (watcher *FakeK8sWatcher) GetLocalK8sInformerFactory() informers.SharedInformerFactory {
return nil
}

func (watcher *FakeK8sWatcher) GetCRDInformerFactory() externalversions.SharedInformerFactory {
return nil
}
4 changes: 2 additions & 2 deletions pkg/watcher/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func AddPodInformer(w *K8sWatcher, local bool) error {
if w == nil {
return fmt.Errorf("k8s watcher not initialized")
}
factory := w.K8sInformerFactory
factory := w.GetK8sInformerFactory()
if local {
factory = w.LocalK8sInformerFactory
factory = w.GetLocalK8sInformerFactory()
}
if factory == nil {
return fmt.Errorf("k8s informer factory not initialized")
Expand Down
45 changes: 30 additions & 15 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ type Watcher interface {
AddInformer(name string, informer cache.SharedIndexInformer, indexers cache.Indexers) error
GetInformer(name string) cache.SharedIndexInformer
Start()
GetK8sInformerFactory() informers.SharedInformerFactory
GetLocalK8sInformerFactory() informers.SharedInformerFactory
GetCRDInformerFactory() externalversions.SharedInformerFactory
}

type K8sWatcher struct {
K8sInformerFactory informers.SharedInformerFactory // for k8s built-in resources
LocalK8sInformerFactory informers.SharedInformerFactory // for k8s built-in resources local to the node
CRDInformerFactory externalversions.SharedInformerFactory // for Tetragon CRDs
k8sInformerFactory informers.SharedInformerFactory // for k8s built-in resources
localK8sInformerFactory informers.SharedInformerFactory // for k8s built-in resources local to the node
crdInformerFactory externalversions.SharedInformerFactory // for Tetragon CRDs
informers map[string]cache.SharedIndexInformer
deletedPodCache *deletedPodCache
}
Expand All @@ -55,9 +58,9 @@ func NewK8sWatcher(
}

return &K8sWatcher{
K8sInformerFactory: k8sInformerFactory,
LocalK8sInformerFactory: localK8sInformerFactory,
CRDInformerFactory: crdInformerFactory,
k8sInformerFactory: k8sInformerFactory,
localK8sInformerFactory: localK8sInformerFactory,
crdInformerFactory: crdInformerFactory,
informers: make(map[string]cache.SharedIndexInformer),
}
}
Expand All @@ -78,17 +81,17 @@ func (w *K8sWatcher) GetInformer(name string) cache.SharedIndexInformer {
}

func (w *K8sWatcher) Start() {
if w.K8sInformerFactory != nil {
w.K8sInformerFactory.Start(wait.NeverStop)
w.K8sInformerFactory.WaitForCacheSync(wait.NeverStop)
if w.k8sInformerFactory != nil {
w.k8sInformerFactory.Start(wait.NeverStop)
w.k8sInformerFactory.WaitForCacheSync(wait.NeverStop)
}
if w.LocalK8sInformerFactory != nil {
w.LocalK8sInformerFactory.Start(wait.NeverStop)
w.LocalK8sInformerFactory.WaitForCacheSync(wait.NeverStop)
if w.localK8sInformerFactory != nil {
w.localK8sInformerFactory.Start(wait.NeverStop)
w.localK8sInformerFactory.WaitForCacheSync(wait.NeverStop)
}
if w.CRDInformerFactory != nil {
w.CRDInformerFactory.Start(wait.NeverStop)
w.CRDInformerFactory.WaitForCacheSync(wait.NeverStop)
if w.crdInformerFactory != nil {
w.crdInformerFactory.Start(wait.NeverStop)
w.crdInformerFactory.WaitForCacheSync(wait.NeverStop)
}
for name, informer := range w.informers {
logger.GetLogger().WithFields(logrus.Fields{
Expand All @@ -97,3 +100,15 @@ func (w *K8sWatcher) Start() {
}).Info("Initialized informer cache")
}
}

func (w *K8sWatcher) GetK8sInformerFactory() informers.SharedInformerFactory {
return w.k8sInformerFactory
}

func (w *K8sWatcher) GetLocalK8sInformerFactory() informers.SharedInformerFactory {
return w.localK8sInformerFactory
}

func (w *K8sWatcher) GetCRDInformerFactory() externalversions.SharedInformerFactory {
return w.crdInformerFactory
}

0 comments on commit 68bb075

Please sign in to comment.