Skip to content

Commit

Permalink
process.GetPodInfo: Don't return Cilium endpoint
Browse files Browse the repository at this point in the history
The return value is always ignored, and it's only being used by unit
tests.

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Aug 28, 2023
1 parent 0de4662 commit e323824
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/grpc/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (msg *MsgExecveEventUnix) Retry(internal *process.ProcessInternal, ev notif
nspid := msg.Process.NSPID

if option.Config.EnableK8s && containerId != "" {
podInfo, _ = process.GetPodInfo(containerId, filename, args, nspid)
podInfo = process.GetPodInfo(containerId, filename, args, nspid)
if podInfo == nil {
errormetrics.ErrorTotalInc(errormetrics.EventCachePodInfoRetryFailed)
return eventcache.ErrFailedToGetPodInfo
Expand Down
9 changes: 3 additions & 6 deletions pkg/grpc/process_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ func TestProcessManager_getPodInfo(t *testing.T) {
err = process.InitCache(watcher.NewFakeK8sWatcher(pods), 10)
assert.NoError(t, err)
defer process.FreeCache()
pod, endpoint := process.GetPodInfo("container-id-not-found", "", "", 0)
pod := process.GetPodInfo("container-id-not-found", "", "", 0)
assert.Nil(t, pod)
assert.Nil(t, endpoint)
pod, endpoint = process.GetPodInfo("aaaaaaa", "", "", 1234)
pod = process.GetPodInfo("aaaaaaa", "", "", 1234)
assert.Equal(t,
&tetragon.Pod{
Namespace: podA.Namespace,
Expand All @@ -81,7 +80,6 @@ func TestProcessManager_getPodInfo(t *testing.T) {
Pid: &wrapperspb.UInt32Value{Value: 1234},
},
}, pod)
assert.Nil(t, endpoint)
}

func TestProcessManager_getPodInfoMaybeExecProbe(t *testing.T) {
Expand Down Expand Up @@ -119,7 +117,7 @@ func TestProcessManager_getPodInfoMaybeExecProbe(t *testing.T) {
err = process.InitCache(watcher.NewFakeK8sWatcher(pods), 10)
assert.NoError(t, err)
defer process.FreeCache()
pod, endpoint := process.GetPodInfo("aaaaaaa", "/bin/command", "arg-a arg-b", 1234)
pod := process.GetPodInfo("aaaaaaa", "/bin/command", "arg-a arg-b", 1234)
assert.Equal(t,
&tetragon.Pod{
Namespace: podA.Namespace,
Expand All @@ -132,7 +130,6 @@ func TestProcessManager_getPodInfoMaybeExecProbe(t *testing.T) {
MaybeExecProbe: true,
},
}, pod)
assert.Nil(t, endpoint)
}

func TestProcessManager_GetProcessExec(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions pkg/process/podinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/cilium/tetragon/pkg/filters"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/metrics/watchermetrics"
hubblev1 "github.com/cilium/tetragon/pkg/oldhubble/api/v1"
"github.com/cilium/tetragon/pkg/watcher"

"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -39,15 +38,15 @@ func getPodInfo(
binary string,
args string,
nspid uint32,
) (*tetragon.Pod, *hubblev1.Endpoint) {
) *tetragon.Pod {
if containerID == "" {
return nil, nil
return nil
}
pod, container, ok := w.FindContainer(containerID)
if !ok {
watchermetrics.GetWatcherErrors("k8s", watchermetrics.FailedToGetPodError).Inc()
logger.GetLogger().WithField("container id", containerID).Trace("failed to get pod")
return nil, nil
return nil
}
var startTime *timestamppb.Timestamp
livenessProbe, readinessProbe := getProbes(pod, container)
Expand Down Expand Up @@ -89,5 +88,5 @@ func getPodInfo(
StartTime: startTime,
MaybeExecProbe: maybeExecProbe,
},
}, endpoint
}
}
2 changes: 1 addition & 1 deletion pkg/process/podinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestK8sWatcher_GetPodInfo(t *testing.T) {
k8sClient := fake.NewSimpleClientset(&pod)
watcher := watcher.NewK8sWatcher(k8sClient, time.Hour)
pid := uint32(1)
podInfo, _ := getPodInfo(watcher, "abcd1234", "curl", "cilium.io", 1)
podInfo := getPodInfo(watcher, "abcd1234", "curl", "cilium.io", 1)
assert.True(t, proto.Equal(podInfo, &tetragon.Pod{
Namespace: pod.Namespace,
Name: pod.Name,
Expand Down
18 changes: 8 additions & 10 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"github.com/cilium/tetragon/pkg/watcher"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/wrapperspb"

hubblev1 "github.com/cilium/tetragon/pkg/oldhubble/api/v1"
)

// ProcessInternal is the internal representation of a process.
Expand Down Expand Up @@ -192,7 +190,7 @@ func initProcessInternalExec(
parent tetragonAPI.MsgExecveKey,
capabilities tetragonAPI.MsgCapabilities,
namespaces tetragonAPI.MsgNamespaces,
) (*ProcessInternal, *hubblev1.Endpoint) {
) *ProcessInternal {
args, cwd := ArgsDecoder(process.Args, process.Flags)
var parentExecID string
if parent.Pid != 0 {
Expand All @@ -201,7 +199,7 @@ func initProcessInternalExec(
parentExecID = GetProcessID(0, 1)
}
execID := GetExecID(&process)
protoPod, endpoint := GetPodInfo(containerID, process.Filename, args, process.NSPID)
protoPod := GetPodInfo(containerID, process.Filename, args, process.NSPID)
caps := caps.GetMsgCapabilities(capabilities)
ns := namespace.GetMsgNamespaces(namespaces)
return &ProcessInternal{
Expand All @@ -224,7 +222,7 @@ func initProcessInternalExec(
capabilities: caps,
namespaces: ns,
refcnt: 1,
}, endpoint
}
}

// initProcessInternalClone() initialize and returns ProcessInternal from
Expand Down Expand Up @@ -269,17 +267,17 @@ func initProcessInternalClone(event *tetragonAPI.MsgCloneEvent,
pi.process.Pod.Container.Pid = &wrapperspb.UInt32Value{Value: event.NSPID}
}
if option.Config.EnableK8s && pi.process.Docker != "" && pi.process.Pod == nil {
if podInfo, _ := GetPodInfo(pi.process.Docker, pi.process.Binary, pi.process.Arguments, event.NSPID); podInfo != nil {
if podInfo := GetPodInfo(pi.process.Docker, pi.process.Binary, pi.process.Arguments, event.NSPID); podInfo != nil {
pi.AddPodInfo(podInfo)
}
}

return pi, nil
}

// GetPodInfo() constructs and returns the Kubernetes Pod information associated with
// GetPodInfo constructs and returns the Kubernetes Pod information associated with
// the Container ID and the PID inside this container.
func GetPodInfo(cid, bin, args string, nspid uint32) (*tetragon.Pod, *hubblev1.Endpoint) {
func GetPodInfo(cid, bin, args string, nspid uint32) *tetragon.Pod {
return getPodInfo(k8s, cid, bin, args, nspid)
}

Expand Down Expand Up @@ -307,9 +305,9 @@ func AddExecEvent(event *tetragonAPI.MsgExecveEventUnix) *ProcessInternal {
if event.CleanupProcess.Ktime == 0 || event.Process.Flags&api.EventClone != 0 {
// there is a case where we cannot find this entry in execve_map
// in that case we use as parent what Linux knows
proc, _ = initProcessInternalExec(event.Process, event.Kube.Docker, event.Parent, event.Capabilities, event.Namespaces)
proc = initProcessInternalExec(event.Process, event.Kube.Docker, event.Parent, event.Capabilities, event.Namespaces)
} else {
proc, _ = initProcessInternalExec(event.Process, event.Kube.Docker, event.CleanupProcess, event.Capabilities, event.Namespaces)
proc = initProcessInternalExec(event.Process, event.Kube.Docker, event.CleanupProcess, event.Capabilities, event.Namespaces)
}

// Ensure that exported events have the TID set. For events from Kernel
Expand Down

0 comments on commit e323824

Please sign in to comment.