Skip to content

Commit 56c7ac7

Browse files
committed
Fix #46
kubernetes.container_image and docker.container_id are now attached to mounted-file originating logs.
1 parent 6719b3b commit 56c7ac7

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

config-reloader/datasource/datasource.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package datasource
55

66
import (
77
"sort"
8+
89
core "k8s.io/api/core/v1"
910
)
1011

@@ -23,6 +24,9 @@ type MiniContainer struct {
2324
PodID string
2425
PodName string
2526

27+
Image string
28+
ContainerID string
29+
2630
// pod labels
2731
Labels map[string]string
2832

@@ -71,17 +75,34 @@ func (s byLength) Less(i, j int) bool {
7175
return len(s[i].Path) > len(s[j].Path)
7276
}
7377

78+
func findContainerStatus(statuses []core.ContainerStatus, name string) *core.ContainerStatus {
79+
for _, st := range statuses {
80+
if st.Name == name {
81+
return &st
82+
}
83+
}
84+
return nil
85+
}
86+
7487
func convertPodToMinis(resp *core.PodList) []*MiniContainer {
7588
var res []*MiniContainer
7689

7790
for _, pod := range resp.Items {
7891
for _, cont := range pod.Spec.Containers {
92+
contStatus := findContainerStatus(pod.Status.ContainerStatuses, cont.Name)
93+
cid := ""
94+
if contStatus != nil {
95+
cid = contStatus.ContainerID
96+
}
97+
7998
mini := &MiniContainer{
80-
PodID: string(pod.UID),
81-
PodName: pod.Name,
82-
Labels: pod.Labels,
83-
Name: cont.Name,
84-
NodeName: pod.Spec.NodeName,
99+
PodID: string(pod.UID),
100+
PodName: pod.Name,
101+
Labels: pod.Labels,
102+
Name: cont.Name,
103+
NodeName: pod.Spec.NodeName,
104+
Image: cont.Image,
105+
ContainerID: cid,
85106
}
86107

87108
for _, vm := range cont.VolumeMounts {

config-reloader/processors/mounted_file.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,16 @@ func (state *mountedFileState) makeAttachK8sMetadataDirective(tag string, mc *da
162162
buf := &bytes.Buffer{}
163163
fmt.Fprintf(buf, "record['stream']='%s'; ", cf.Path)
164164
fmt.Fprintf(buf, "record['kubernetes']=%s; ", util.ToRubyMapLiteral(map[string]string{
165-
"container_name": mc.Name,
166-
"namespace_name": state.Context.Namepsace,
167-
"pod_name": mc.PodName,
168-
"pod_id": mc.PodID,
169-
"host": mc.NodeName,
165+
"container_name": mc.Name,
166+
"container_image": mc.Image,
167+
"namespace_name": state.Context.Namepsace,
168+
"pod_name": mc.PodName,
169+
"pod_id": mc.PodID,
170+
"host": mc.NodeName,
171+
}))
172+
173+
fmt.Fprintf(buf, "record['docker']=%s; ", util.ToRubyMapLiteral(map[string]string{
174+
"container_id": mc.ContainerID,
170175
}))
171176

172177
fmt.Fprintf(buf, "record['container_info']='%s'; ", util.Hash(mc.PodID, cf.Path))

config-reloader/processors/mounted_file_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestMountedFileCatchesMissingFile(t *testing.T) {
6666
@type mounted-file
6767
labels app=spring-mvc
6868
</source>
69-
69+
7070
<match **>
7171
@type logzio
7272
</match
@@ -80,9 +80,9 @@ func TestMountedFileCatchesEmptyLabels(t *testing.T) {
8080
missingPath := `
8181
<source>
8282
@type mounted-file
83-
labels
83+
labels
8484
</source>
85-
85+
8686
<match **>
8787
@type logzio
8888
</match
@@ -98,7 +98,7 @@ func TestMountedFileCatchesMissingLabels(t *testing.T) {
9898
@type mounted-file
9999
file /etc/hosts
100100
</source>
101-
101+
102102
<match **>
103103
@type logzio
104104
</match
@@ -235,10 +235,12 @@ func TestConvertToFragment(t *testing.T) {
235235

236236
func TestProcessMountedFile(t *testing.T) {
237237
c1 := &datasource.MiniContainer{
238-
PodID: "123-id",
239-
PodName: "123",
240-
Name: "redis-main",
241-
Labels: map[string]string{"app": "redis"},
238+
PodID: "123-id",
239+
PodName: "123",
240+
Image: "image-c1",
241+
ContainerID: "contid-c1",
242+
Name: "redis-main",
243+
Labels: map[string]string{"app": "redis"},
242244
HostMounts: []*datasource.Mount{
243245
{
244246
Path: "/var/log",
@@ -248,10 +250,12 @@ func TestProcessMountedFile(t *testing.T) {
248250
}
249251

250252
c2 := &datasource.MiniContainer{
251-
PodID: "abc-id",
252-
PodName: "abc",
253-
Name: "nginx-main",
254-
Labels: map[string]string{"app": "nginx"},
253+
PodID: "abc-id",
254+
PodName: "abc",
255+
Image: "image-c2",
256+
ContainerID: "contid-c2",
257+
Name: "nginx-main",
258+
Labels: map[string]string{"app": "nginx"},
255259
HostMounts: []*datasource.Mount{
256260
{
257261
Path: "/var/log",
@@ -306,6 +310,12 @@ func TestProcessMountedFile(t *testing.T) {
306310
assert.Equal(t, "/kubelet-root/pods/123-id/volumes/kubernetes.io~empty-dir/logs/redis.log", prep[0].Param("path"))
307311
assert.Equal(t, "/kubelet-root/pods/abc-id/volumes/kubernetes.io~empty-dir/logs/nginx.log", prep[2].Param("path"))
308312

313+
payload := prep.String()
314+
assert.True(t, strings.Contains(payload, "'container_image'=>'image-c2'"))
315+
assert.True(t, strings.Contains(payload, "'container_image'=>'image-c1'"))
316+
assert.True(t, strings.Contains(payload, "record['docker']={'container_id'=>'contid-c1'}"))
317+
assert.True(t, strings.Contains(payload, "record['docker']={'container_id'=>'contid-c2'}"))
318+
309319
main, err := Process(input, ctx, state)
310320
assert.Nil(t, err)
311321
assert.Equal(t, 1, len(main))

0 commit comments

Comments
 (0)