Skip to content

Commit

Permalink
Merge pull request #296 from kubescape/profiles
Browse files Browse the repository at this point in the history
use all statuses, add fallback annotation for app
  • Loading branch information
matthyx authored May 27, 2024
2 parents 0aa3ecd + ae7761d commit 789d536
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
17 changes: 7 additions & 10 deletions pkg/containerwatcher/v1/ig_k8sclient.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package containerwatcher

import (
"node-agent/pkg/utils"
"slices"
"strings"

containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
)

type IGK8sClient interface {
Expand All @@ -26,13 +27,9 @@ func NewIGK8sClientMock() *IGK8sClientMock {

// GetNonRunningContainers returns the list of containers IDs that are not running.
func (k *IGK8sClientMock) GetNonRunningContainers(pod *corev1.Pod) []string {
ret := []string{}
var ret []string

containerStatuses := append([]v1.ContainerStatus{}, pod.Status.InitContainerStatuses...)
containerStatuses = append(containerStatuses, pod.Status.ContainerStatuses...)
containerStatuses = append(containerStatuses, pod.Status.EphemeralContainerStatuses...)

for _, s := range containerStatuses {
for _, s := range utils.GetContainerStatuses(pod.Status) {
if s.ContainerID != "" && s.State.Running == nil {
id := trimRuntimePrefix(s.ContainerID)
if id == "" {
Expand All @@ -48,15 +45,15 @@ func (k *IGK8sClientMock) GetNonRunningContainers(pod *corev1.Pod) []string {

// GetRunningContainers returns a list of the containers of a given Pod that are running.
func (k *IGK8sClientMock) GetRunningContainers(pod *corev1.Pod) []containercollection.Container {
containers := []containercollection.Container{}
var containers []containercollection.Container

labels := map[string]string{}
for k, v := range pod.ObjectMeta.Labels {
labels[k] = v
}

containerStatuses := append([]v1.Container{}, pod.Spec.InitContainers...)
containerStatuses = append(containerStatuses, pod.Spec.Containers...)
containerStatuses := slices.Concat(pod.Spec.InitContainers,
pod.Spec.Containers)

for _, s := range containerStatuses {

Expand Down
9 changes: 7 additions & 2 deletions pkg/nodeprofilemanager/v1/nodeprofile_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ func (n *NodeProfileManager) getProfile() (*armotypes.NodeProfile, error) {
for _, pod := range n.k8sObjectCache.GetPods() {
var app string
if pod.Labels != nil {
app = pod.Labels["app"]
for _, k := range []string{"app", "app.kubernetes.io/name"} {
if v, ok := pod.Labels[k]; ok {
app = v
break
}
}
}
state, reason, message, transitionTime := getPodState(pod.Status.Conditions)
statusesMap := mapContainerStatuses(pod.Status.ContainerStatuses)
statusesMap := mapContainerStatuses(utils.GetContainerStatuses(pod.Status))
podStatus := armotypes.PodStatus{
CustomerGUID: n.clusterData.AccountID,
Cluster: n.clusterData.ClusterName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/relevancymanager/v1/relevancy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (rm *RelevancyManager) ReportFileOpen(containerID, k8sContainerID, file str
}

func (rm *RelevancyManager) HasRelevancyCalculating(pod *v1.Pod) bool {
for _, c := range pod.Status.ContainerStatuses {
for _, c := range utils.GetContainerStatuses(pod.Status) {
if rm.watchedContainerChannels.Has(utils.TrimRuntimePrefix(c.ContainerID)) {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/rulemanager/v1/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func (rm *RuleManager) HasApplicableRuleBindings(namespace, name string) bool {
}

func (rm *RuleManager) HasFinalApplicationProfile(pod *corev1.Pod) bool {
for _, c := range pod.Status.ContainerStatuses {
for _, c := range utils.GetContainerStatuses(pod.Status) {
ap := rm.objectCache.ApplicationProfileCache().GetApplicationProfile(utils.TrimRuntimePrefix(c.ContainerID))
if ap != nil {
if status, ok := ap.Annotations[helpersv1.StatusMetadataKey]; ok {
Expand Down
5 changes: 5 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -661,3 +662,7 @@ func TrimRuntimePrefix(id string) string {

return parts[1]
}

func GetContainerStatuses(podStatus v1.PodStatus) []v1.ContainerStatus {
return slices.Concat(podStatus.ContainerStatuses, podStatus.InitContainerStatuses, podStatus.EphemeralContainerStatuses)
}

0 comments on commit 789d536

Please sign in to comment.