Skip to content

Commit

Permalink
Autodiscover ephemeral containers in kubernetes (elastic#22389)
Browse files Browse the repository at this point in the history
  • Loading branch information
marqc authored Nov 4, 2020
1 parent a5df38a commit 5a4d407
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Update to Golang 1.12.1. {pull}11330[11330]
- Disable Alibaba Cloud and Tencent Cloud metadata providers by default. {pull}13812[12812]
- API address is a required setting in `add_cloudfoundry_metadata`. {pull}21759[21759]
- Autodiscover kubernetes provider will find ephemeral containers. {pull}22389[22389]

*Auditbeat*

Expand Down
9 changes: 9 additions & 0 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ func (p *pod) emit(pod *kubernetes.Pod, flag string) {

// Emit events for all initContainers
p.emitEvents(pod, flag, pod.Spec.InitContainers, pod.Status.InitContainerStatuses)

// Emit events for all ephemeralContainers
// Ephemeral containers are alpha feature in k8s and this code may require some changes, if their
// api change in the future.
var mappedEphemeralsAsContainers []kubernetes.Container
for _, c := range pod.Spec.EphemeralContainers {
mappedEphemeralsAsContainers = append(mappedEphemeralsAsContainers, kubernetes.Container(c.EphemeralContainerCommon))
}
p.emitEvents(pod, flag, mappedEphemeralsAsContainers, pod.Status.EphemeralContainerStatuses)
}

func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernetes.Container,
Expand Down
114 changes: 114 additions & 0 deletions libbeat/autodiscover/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,120 @@ func TestEmitEvent(t *testing.T) {
},
},
},
{
Message: "Test ephemeral container in common pod",
Flag: "start",
Pod: &kubernetes.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
UID: types.UID(uid),
Namespace: namespace,
Labels: map[string]string{},
Annotations: map[string]string{},
},
TypeMeta: typeMeta,
Status: v1.PodStatus{
PodIP: podIP,
EphemeralContainerStatuses: []kubernetes.PodContainerStatus{
{
Name: name,
ContainerID: containerID,
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
},
Spec: v1.PodSpec{
NodeName: node,
EphemeralContainers: []v1.EphemeralContainer{
v1.EphemeralContainer{
EphemeralContainerCommon: v1.EphemeralContainerCommon{
Image: containerImage,
Name: name,
},
},
},
},
},
Expected: []bus.Event{
{
"start": true,
"host": "127.0.0.1",
"id": uid,
"provider": UUID,
"ports": common.MapStr{},
"kubernetes": common.MapStr{
"pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{
"name": "node",
},
"namespace": "default",
"annotations": common.MapStr{},
},
"meta": common.MapStr{
"kubernetes": common.MapStr{
"namespace": "default",
"pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
}, "node": common.MapStr{
"name": "node",
},
},
},
"config": []*common.Config{},
},
{
"start": true,
"host": "127.0.0.1",
"port": 0,
"id": cid,
"provider": UUID,
"kubernetes": common.MapStr{
"container": common.MapStr{
"id": "foobar",
"name": "filebeat",
"image": "elastic/filebeat:6.3.0",
"runtime": "docker",
},
"pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
},
"node": common.MapStr{
"name": "node",
},
"namespace": "default",
"annotations": common.MapStr{},
},
"meta": common.MapStr{
"kubernetes": common.MapStr{
"namespace": "default",
"pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
}, "node": common.MapStr{
"name": "node",
},
"container": common.MapStr{
"name": "filebeat",
"image": "elastic/filebeat:6.3.0",
},
},
"container": common.MapStr{
"image": common.MapStr{"name": "elastic/filebeat:6.3.0"},
"id": "foobar",
"runtime": "docker",
},
},
"config": []*common.Config{},
},
},
},
}

for _, test := range tests {
Expand Down

0 comments on commit 5a4d407

Please sign in to comment.