Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use all statuses, add fallback annotation for app #296

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't even check if the status is running here @dwertent

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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very fancy 😉

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)
}
Loading