Skip to content

Commit

Permalink
Add support for ephemeral containers in add_kubernetes_metadata (elas…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Nov 9, 2020
1 parent 3dd6931 commit 92b4e3d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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 Expand Up @@ -492,6 +491,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add cloud.account.id for GCP into add_cloud_metadata processor. {pull}21776[21776]
- Add proxy metricset for istio module. {pull}21751[21751]
- Added Kafka version 2.2 to the list of supported versions. {pull}22328[22328]
- Add support for ephemeral containers in kubernetes autodiscover and `add_kubernetes_metadata`. {pull}22389[22389] {pull}22439[22439]

*Auditbeat*

Expand Down
17 changes: 14 additions & 3 deletions libbeat/processors/add_kubernetes_metadata/indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func NewContainerIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, er
// GetMetadata returns the composed metadata list from all registered indexers
func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
var m []MetadataIndex
for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
for _, status := range getContainerStatusesInPod(pod) {
cID, runtime := kubernetes.ContainerIDWithRuntime(status)
if cID == "" {
continue
Expand All @@ -205,7 +205,7 @@ func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
// GetIndexes returns the indexes for the given Pod
func (c *ContainerIndexer) GetIndexes(pod *kubernetes.Pod) []string {
var containers []string
for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
for _, status := range getContainerStatusesInPod(pod) {
cID := kubernetes.ContainerID(status)
if cID == "" {
continue
Expand Down Expand Up @@ -241,7 +241,7 @@ func (h *IPPortIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {

cIDs := make(map[string]string)
runtimes := make(map[string]string)
for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
for _, status := range getContainerStatusesInPod(pod) {
cID, runtime := kubernetes.ContainerIDWithRuntime(status)
if cID == "" {
continue
Expand Down Expand Up @@ -294,3 +294,14 @@ func (h *IPPortIndexer) GetIndexes(pod *kubernetes.Pod) []string {

return hostPorts
}

func getContainerStatusesInPod(pod *kubernetes.Pod) []kubernetes.PodContainerStatus {
if pod == nil {
return nil
}
var statuses []kubernetes.PodContainerStatus
statuses = append(statuses, pod.Status.ContainerStatuses...)
statuses = append(statuses, pod.Status.InitContainerStatuses...)
statuses = append(statuses, pod.Status.EphemeralContainerStatuses...)
return statuses
}
31 changes: 24 additions & 7 deletions libbeat/processors/add_kubernetes_metadata/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func TestContainerIndexer(t *testing.T) {
containerImage := "containerimage"
initContainerImage := "initcontainerimage"
initContainer := "initcontainer"
ephemeralContainerImage := "ephemeralcontainerimage"
ephemeralContainer := "ephemeralcontainer"
nodeName := "testnode"

pod := kubernetes.Pod{
Expand All @@ -158,11 +160,8 @@ func TestContainerIndexer(t *testing.T) {
"labelkey": "labelvalue",
},
},
Status: v1.PodStatus{
ContainerStatuses: make([]kubernetes.PodContainerStatus, 0),
InitContainerStatuses: make([]kubernetes.PodContainerStatus, 0),
},
Spec: v1.PodSpec{},
Status: v1.PodStatus{},
Spec: v1.PodSpec{},
}

indexers := conIndexer.GetMetadata(&pod)
Expand Down Expand Up @@ -199,16 +198,26 @@ func TestContainerIndexer(t *testing.T) {
ContainerID: container2,
},
}
container3 := "docker://klmno"
pod.Status.EphemeralContainerStatuses = []kubernetes.PodContainerStatus{
{
Name: ephemeralContainer,
Image: ephemeralContainerImage,
ContainerID: container3,
},
}

indexers = conIndexer.GetMetadata(&pod)
assert.Equal(t, len(indexers), 2)
assert.Equal(t, len(indexers), 3)
assert.Equal(t, indexers[0].Index, "abcde")
assert.Equal(t, indexers[1].Index, "fghij")
assert.Equal(t, indexers[2].Index, "klmno")

indices = conIndexer.GetIndexes(&pod)
assert.Equal(t, len(indices), 2)
assert.Equal(t, len(indices), 3)
assert.Equal(t, indices[0], "abcde")
assert.Equal(t, indices[1], "fghij")
assert.Equal(t, indices[2], "klmno")

expected["container"] = common.MapStr{
"name": container,
Expand All @@ -225,6 +234,14 @@ func TestContainerIndexer(t *testing.T) {
"runtime": "docker",
}
assert.Equal(t, expected.String(), indexers[1].Data.String())

expected["container"] = common.MapStr{
"name": ephemeralContainer,
"image": ephemeralContainerImage,
"id": "klmno",
"runtime": "docker",
}
assert.Equal(t, expected.String(), indexers[2].Data.String())
}

func TestFilteredGenMeta(t *testing.T) {
Expand Down

0 comments on commit 92b4e3d

Please sign in to comment.