From bcf9429a5cdc4454e8d69f02b1e5796f60b7df31 Mon Sep 17 00:00:00 2001 From: Kevin Sheldrake Date: Tue, 20 Aug 2024 10:59:50 +0100 Subject: [PATCH] Watcher: fix NODE_NAME if missing The pkg/reader/node package gets the value of the NODE_NAME environment variable (if the HUBBLE_NODE_NAME is empty). If it is empty, it gets the hostname and uses that. The pkg/watcher package just attempts to get the NODE_NAME environment variable and warns that it will not work as expected if it is empty or missing. This commit changes pkg/watcher so that it uses pkg/reader/node's function instead. Signed-off-by: Kevin Sheldrake --- pkg/watcher/watcher.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/watcher/watcher.go b/pkg/watcher/watcher.go index e3b8fe34b8a..ea5c5ea2eb7 100644 --- a/pkg/watcher/watcher.go +++ b/pkg/watcher/watcher.go @@ -6,13 +6,13 @@ package watcher import ( "errors" "fmt" - "os" "reflect" "strings" "time" "github.com/cilium/tetragon/pkg/logger" "github.com/cilium/tetragon/pkg/podhooks" + "github.com/cilium/tetragon/pkg/reader/node" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" @@ -121,7 +121,7 @@ func containerIndexFunc(obj interface{}) ([]string, error) { // NewK8sWatcher returns a pointer to an initialized K8sWatcher struct. func NewK8sWatcher(k8sClient kubernetes.Interface, stateSyncIntervalSec time.Duration) *K8sWatcher { - nodeName := os.Getenv("NODE_NAME") + nodeName := node.GetNodeNameForExport() if nodeName == "" { logger.GetLogger().Warn("env var NODE_NAME not specified, K8s watcher will not work as expected") } @@ -134,7 +134,7 @@ func NewK8sWatcher(k8sClient kubernetes.Interface, stateSyncIntervalSec time.Dur k8sInformerFactory := informers.NewSharedInformerFactoryWithOptions(k8sClient, stateSyncIntervalSec, informers.WithTweakListOptions(func(options *metav1.ListOptions) { // Watch local pods only. - options.FieldSelector = "spec.nodeName=" + os.Getenv("NODE_NAME") + options.FieldSelector = "spec.nodeName=" + nodeName })) podInformer := k8sInformerFactory.Core().V1().Pods().Informer() k8sWatcher.AddInformers(k8sInformerFactory, &InternalInformer{