Skip to content

Commit 39adecf

Browse files
swiatekmblakerouse
authored andcommitted
Emit Pod data only for running Pods in the Kubernetes provider (#6011)
1 parent b826e4f commit 39adecf

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Emit Pod data only for running Pods in the Kubernetes provider
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
#pr: https://github.com/owner/repo/1234
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

internal/pkg/composable/providers/kubernetes/pod.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"sync"
1010
"time"
1111

12+
v1 "k8s.io/api/core/v1"
13+
1214
"k8s.io/apimachinery/pkg/runtime/schema"
1315

1416
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
@@ -224,7 +226,9 @@ func (p *pod) Stop() {
224226
}
225227

226228
func (p *pod) emitRunning(pod *kubernetes.Pod) {
227-
229+
if pod.Status.Phase == v1.PodPending || pod.Status.Phase == v1.PodUnknown {
230+
return
231+
}
228232
namespaceAnnotations := kubernetes.PodNamespaceAnnotations(pod, p.namespaceWatcher)
229233

230234
data := generatePodData(pod, p.metagen, namespaceAnnotations)

internal/pkg/composable/providers/kubernetes/pod_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"fmt"
1010
"testing"
11+
"time"
1112

1213
v1 "k8s.io/api/core/v1"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -516,6 +517,63 @@ func TestPodEventer_Namespace_Node_Watcher(t *testing.T) {
516517
}
517518
}
518519

520+
func TestPodEventer_OnlyRunningPods(t *testing.T) {
521+
client := k8sfake.NewSimpleClientset()
522+
523+
log, err := logger.New("service-eventer-test", true)
524+
assert.NoError(t, err)
525+
526+
providerDataChan := make(chan providerData, 1)
527+
528+
comm := MockDynamicComm{
529+
context.TODO(),
530+
providerDataChan,
531+
}
532+
533+
var cfg Config
534+
cfg.InitDefaults()
535+
536+
eventer, err := NewPodEventer(&comm, &cfg, log, client, "cluster", false)
537+
if err != nil {
538+
t.Fatal(err)
539+
}
540+
541+
pod := &kubernetes.Pod{
542+
ObjectMeta: metav1.ObjectMeta{
543+
Name: "testpod",
544+
UID: types.UID(uid),
545+
Namespace: "testns",
546+
},
547+
TypeMeta: metav1.TypeMeta{
548+
Kind: "Pod",
549+
APIVersion: "v1",
550+
},
551+
Spec: kubernetes.PodSpec{
552+
NodeName: "testnode",
553+
},
554+
Status: kubernetes.PodStatus{
555+
PodIP: "127.0.0.5",
556+
Phase: v1.PodPending,
557+
},
558+
}
559+
560+
eventer.OnUpdate(pod)
561+
select {
562+
case <-providerDataChan:
563+
assert.Fail(t, "should not receive update for Pending Pod")
564+
default:
565+
}
566+
567+
// set status to Running, we should get an update now
568+
pod.Status.Phase = v1.PodRunning
569+
eventer.OnUpdate(pod)
570+
select {
571+
case <-providerDataChan:
572+
case <-time.After(time.Second * 5):
573+
assert.Fail(t, "should receive update for Pending Pod")
574+
}
575+
}
576+
519577
// MockDynamicComm is used in tests.
520578
type MockDynamicComm struct {
521579
context.Context

0 commit comments

Comments
 (0)