Skip to content

Commit

Permalink
Set node_name to hostname if NODE_NAME envvar is not found
Browse files Browse the repository at this point in the history
upstream commit: b2540c9

This enables node_name field in the standalone (non-k8s) mode.

Signed-off-by: Anna Kapuscinska <[email protected]>
  • Loading branch information
lambdanis committed Feb 21, 2024
1 parent f9bac6a commit cc40a14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/grpc/process_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestProcessManager_GetProcessExec(t *testing.T) {
}

func Test_getNodeNameForExport(t *testing.T) {
assert.Equal(t, "", node.GetNodeNameForExport())
assert.NotEqual(t, "", node.GetNodeNameForExport()) // we should get the hostname here
assert.NoError(t, os.Setenv("NODE_NAME", "from-node-name"))
assert.Equal(t, "from-node-name", node.GetNodeNameForExport())
assert.NoError(t, os.Setenv("HUBBLE_NODE_NAME", "from-hubble-node-name"))
Expand Down
19 changes: 15 additions & 4 deletions pkg/reader/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// Copyright Authors of Tetragon
package node

import "os"
import (
"os"

"github.com/cilium/tetragon/pkg/logger"
)

// getNodeNameForExport returns node name string for JSON export. It uses NODE_NAME
// env variable by default, which is also used by k8s watcher to watch for local pods:
Expand All @@ -11,9 +15,16 @@ import "os"
//
// Set HUBBLE_NODE_NAME to override the node_name field for JSON export.
func GetNodeNameForExport() string {
var err error
nodeName := os.Getenv("HUBBLE_NODE_NAME")
if nodeName != "" {
return nodeName
if nodeName == "" {
nodeName = os.Getenv("NODE_NAME")
}
if nodeName == "" {
nodeName, err = os.Hostname()
if err != nil {
logger.GetLogger().WithError(err).Warn("failed to retrieve hostname")
}
}
return os.Getenv("NODE_NAME")
return nodeName
}

0 comments on commit cc40a14

Please sign in to comment.