Skip to content

Commit

Permalink
Watcher: fix NODE_NAME if missing
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
kevsecurity authored and kkourt committed Aug 20, 2024
1 parent c2f3dbd commit bcf9429
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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{
Expand Down

0 comments on commit bcf9429

Please sign in to comment.