Skip to content

Commit

Permalink
Add container image in Kubernetes metadata (elastic#13356)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrsMark authored Aug 28, 2019
1 parent 65db71c commit aa3f38d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Update Suricata module to populate ECS DNS fields and handle EVE DNS version 2. {issue}13320[13320] {pull}13329[13329]
- Update PAN-OS fileset to use the ECS NAT fields. {issue}13320[13320] {pull}13330[13330]
- Add fields to the Zeek DNS fileset for ECS DNS. {issue}13320[13320] {pull}13324[13324]
- Add container image in Kubernetes metadata {pull}13356[13356] {issue}12688[12688]

*Heartbeat*

Expand Down
2 changes: 1 addition & 1 deletion libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (p *Provider) emitEvents(pod *kubernetes.Pod, flag string, containers []kub
"image": c.Image,
"runtime": runtimes[c.Name],
}
meta := p.metagen.ContainerMetadata(pod, c.Name)
meta := p.metagen.ContainerMetadata(pod, c.Name, c.Image)

// Information that can be used in discovering a workload
kubemeta := meta.Clone()
Expand Down
9 changes: 6 additions & 3 deletions libbeat/autodiscover/providers/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ func TestEmitEvent(t *testing.T) {
"kubernetes": common.MapStr{
"namespace": "default",
"container": common.MapStr{
"name": "filebeat",
"name": "filebeat",
"image": "elastic/filebeat:6.3.0",
}, "pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
Expand Down Expand Up @@ -359,7 +360,8 @@ func TestEmitEvent(t *testing.T) {
"kubernetes": common.MapStr{
"namespace": "default",
"container": common.MapStr{
"name": "filebeat",
"name": "filebeat",
"image": "elastic/filebeat:6.3.0",
}, "pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
Expand Down Expand Up @@ -426,7 +428,8 @@ func TestEmitEvent(t *testing.T) {
"kubernetes": common.MapStr{
"namespace": "default",
"container": common.MapStr{
"name": "filebeat",
"name": "filebeat",
"image": "elastic/filebeat:6.3.0",
}, "pod": common.MapStr{
"name": "filebeat",
"uid": "005f3b90-4b9d-12f8-acf0-31020a840133",
Expand Down
7 changes: 4 additions & 3 deletions libbeat/common/kubernetes/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MetaGenerator interface {
PodMetadata(pod *Pod) common.MapStr

// Containermetadata generates metadata for the given container of a pod
ContainerMetadata(pod *Pod, container string) common.MapStr
ContainerMetadata(pod *Pod, container string, image string) common.MapStr
}

// MetaGeneratorConfig settings
Expand Down Expand Up @@ -140,12 +140,13 @@ func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr {
}

// Containermetadata generates metadata for the given container of a pod
func (g *metaGenerator) ContainerMetadata(pod *Pod, container string) common.MapStr {
func (g *metaGenerator) ContainerMetadata(pod *Pod, container string, image string) common.MapStr {
podMeta := g.PodMetadata(pod)

// Add container details
podMeta["container"] = common.MapStr{
"name": container,
"name": container,
"image": image,
}

return podMeta
Expand Down
4 changes: 2 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
}
metadata = append(metadata, MetadataIndex{
Index: cID,
Data: c.metaGen.ContainerMetadata(pod, status.Name),
Data: c.metaGen.ContainerMetadata(pod, status.Name, status.Image),
})
}

Expand Down Expand Up @@ -245,7 +245,7 @@ func (h *IPPortIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {

metadata = append(metadata, MetadataIndex{
Index: fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort),
Data: h.metaGen.ContainerMetadata(pod, container.Name),
Data: h.metaGen.ContainerMetadata(pod, container.Name, container.Image),
})
}
}
Expand Down
16 changes: 12 additions & 4 deletions libbeat/processors/add_kubernetes_metadata/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func TestContainerIndexer(t *testing.T) {
ns := "testns"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
container := "container"
containerImage := "containerimage"
initContainerImage := "initcontainerimage"
initContainer := "initcontainer"
nodeName := "testnode"

Expand Down Expand Up @@ -184,13 +186,15 @@ func TestContainerIndexer(t *testing.T) {
pod.Status.ContainerStatuses = []kubernetes.PodContainerStatus{
{
Name: container,
Image: containerImage,
ContainerID: container1,
},
}
container2 := "docker://fghij"
pod.Status.InitContainerStatuses = []kubernetes.PodContainerStatus{
{
Name: initContainer,
Image: initContainerImage,
ContainerID: container2,
},
}
Expand All @@ -206,12 +210,14 @@ func TestContainerIndexer(t *testing.T) {
assert.Equal(t, indices[1], "fghij")

expected["container"] = common.MapStr{
"name": container,
"name": container,
"image": containerImage,
}
assert.Equal(t, expected.String(), indexers[0].Data.String())

expected["container"] = common.MapStr{
"name": initContainer,
"name": initContainer,
"image": initContainerImage,
}
assert.Equal(t, expected.String(), indexers[1].Data.String())
}
Expand Down Expand Up @@ -350,6 +356,7 @@ func TestIpPortIndexer(t *testing.T) {
ns := "testns"
uid := "005f3b90-4b9d-12f8-acf0-31020a840133"
container := "container"
containerImage := "containerimage"
ip := "1.2.3.4"
port := int32(80)
pod := kubernetes.Pod{
Expand Down Expand Up @@ -398,7 +405,8 @@ func TestIpPortIndexer(t *testing.T) {

pod.Spec.Containers = []v1.Container{
{
Name: container,
Name: container,
Image: containerImage,
Ports: []v1.ContainerPort{
{
Name: container,
Expand All @@ -422,6 +430,6 @@ func TestIpPortIndexer(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s:%d", ip, port), indices[1])

assert.Equal(t, expected.String(), indexers[0].Data.String())
expected["container"] = common.MapStr{"name": container}
expected["container"] = common.MapStr{"name": container, "image": containerImage}
assert.Equal(t, expected.String(), indexers[1].Data.String())
}

0 comments on commit aa3f38d

Please sign in to comment.