diff --git a/pkg/observer/observertesthelper/observer_test_helper.go b/pkg/observer/observertesthelper/observer_test_helper.go index 3cb8bea9425..e580d184a95 100644 --- a/pkg/observer/observertesthelper/observer_test_helper.go +++ b/pkg/observer/observertesthelper/observer_test_helper.go @@ -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" @@ -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" ) @@ -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 { diff --git a/pkg/watcher/fake.go b/pkg/watcher/fake.go index 4f10f7c55e5..b5d6e40eb36 100644 --- a/pkg/watcher/fake.go +++ b/pkg/watcher/fake.go @@ -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. @@ -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 +} diff --git a/pkg/watcher/pod.go b/pkg/watcher/pod.go index d73e97f200b..09c7b14d21b 100644 --- a/pkg/watcher/pod.go +++ b/pkg/watcher/pod.go @@ -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") diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index 2ce0363a0c4..c0ba8302258 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -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 } @@ -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), } } @@ -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{ @@ -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 +}