Skip to content

Commit 69b1ead

Browse files
shuaichedsiper
authored andcommitted
filter_kubernetes: add owner references information into record.
Signed-off-by: shuaichen <[email protected]>
1 parent d6c4b3d commit 69b1ead

8 files changed

+168
-5
lines changed

plugins/filter_kubernetes/kube_conf.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct flb_kube *flb_kube_conf_create(struct flb_filter_instance *ins,
136136
FLB_HASH_TABLE_SIZE,
137137
FLB_HASH_TABLE_SIZE);
138138
}
139-
139+
140140
if (ctx->kube_meta_namespace_cache_ttl > 0) {
141141
ctx->namespace_hash_table = flb_hash_table_create_with_ttl(
142142
ctx->kube_meta_namespace_cache_ttl,
@@ -150,7 +150,7 @@ struct flb_kube *flb_kube_conf_create(struct flb_filter_instance *ins,
150150
FLB_HASH_TABLE_SIZE,
151151
FLB_HASH_TABLE_SIZE);
152152
}
153-
153+
154154

155155
if (!ctx->hash_table || !ctx->namespace_hash_table) {
156156
flb_kube_conf_destroy(ctx);

plugins/filter_kubernetes/kube_conf.h

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct flb_kube {
7474
int cache_use_docker_id;
7575
int labels;
7676
int annotations;
77+
int owner_references;
7778
int namespace_labels;
7879
int namespace_annotations;
7980
int namespace_metadata_only;

plugins/filter_kubernetes/kube_meta.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ static int merge_pod_meta(struct flb_kube_meta *meta, struct flb_kube *ctx,
11221122
int have_uid = -1;
11231123
int have_labels = -1;
11241124
int have_annotations = -1;
1125+
int have_owner_references = -1;
11251126
int have_nodename = -1;
11261127
int have_podip = -1;
11271128
size_t off = 0;
@@ -1228,15 +1229,20 @@ static int merge_pod_meta(struct flb_kube_meta *meta, struct flb_kube *ctx,
12281229
map_size++;
12291230
}
12301231
}
1231-
12321232
else if (size == 11 && strncmp(ptr, "annotations", 11) == 0) {
12331233
have_annotations = i;
12341234
if (ctx->annotations == FLB_TRUE) {
12351235
map_size++;
12361236
}
12371237
}
1238+
else if (size == 15 && strncmp(ptr, "ownerReferences", 15) == 0) {
1239+
have_owner_references = i;
1240+
if (ctx->owner_references == FLB_TRUE) {
1241+
map_size++;
1242+
}
1243+
}
12381244

1239-
if (have_uid >= 0 && have_labels >= 0 && have_annotations >= 0) {
1245+
if (have_uid >= 0 && have_labels >= 0 && have_annotations >= 0 && have_owner_references >= 0) {
12401246
break;
12411247
}
12421248
}
@@ -1317,6 +1323,14 @@ static int merge_pod_meta(struct flb_kube_meta *meta, struct flb_kube *ctx,
13171323
msgpack_pack_object(&mp_pck, v);
13181324
}
13191325

1326+
if (have_owner_references >= 0 && ctx->owner_references == FLB_TRUE) {
1327+
k = meta_val.via.map.ptr[have_owner_references].key;
1328+
v = meta_val.via.map.ptr[have_owner_references].val;
1329+
1330+
msgpack_pack_object(&mp_pck, k);
1331+
msgpack_pack_object(&mp_pck, v);
1332+
}
1333+
13201334
if (have_nodename >= 0) {
13211335
v = spec_val.via.map.ptr[have_nodename].val;
13221336

plugins/filter_kubernetes/kubernetes.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ static int pack_map_content(struct flb_log_event_encoder *log_encoder,
518518
ret = flb_log_event_encoder_append_body_cstring(
519519
log_encoder,
520520
"kubernetes_namespace");
521-
521+
522522
off = 0;
523523
msgpack_unpacked_init(&result);
524524
msgpack_unpack_next(&result, namespace_kube_buf,
@@ -909,6 +909,13 @@ static struct flb_config_map config_map[] = {
909909
"include Kubernetes annotations on every record"
910910
},
911911

912+
/* Include Kubernetes OwnerReferences in the final record ? */
913+
{
914+
FLB_CONFIG_MAP_BOOL, "owner_references", "false",
915+
0, FLB_TRUE, offsetof(struct flb_kube, owner_references),
916+
"include Kubernetes owner references on every record"
917+
},
918+
912919
/* Include Kubernetes Namespace Labels in the final record ? */
913920
{
914921
FLB_CONFIG_MAP_BOOL, "namespace_labels", "false",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"log":"Fluent Bit is logging\n","stream":"stdout","time":"2019-04-01T17:58:33.598656444Z"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"apiVersion": "v1",
3+
"kind": "Pod",
4+
"metadata": {
5+
"uid": "e9f2963f-55f2-11e9-84c5-02e422b8a84a",
6+
"annotations": {
7+
"prometheus.io/path": "/api/v1/metrics/prometheus",
8+
"prometheus.io/port": "2020",
9+
"prometheus.io/scrape": "true"
10+
},
11+
"creationTimestamp": "2019-04-03T09:29:00Z",
12+
"labels": {
13+
"app.kubernetes.io/name": "fluent-bit"
14+
},
15+
"ownerReferences": [
16+
{
17+
"apiVersion": "apps/v1",
18+
"kind": "DaemonSet",
19+
"name": "fluentbit-max",
20+
"uid": "a0a2ee0a-08da-5a7f-ac1a-c0a4255f82f2",
21+
"controller": true,
22+
"blockOwnerDeletion": true
23+
},
24+
],
25+
"name": "base",
26+
"namespace": "core",
27+
"resourceVersion": "74466568",
28+
"selfLink": "/api/v1/namespaces/core/pods/base"
29+
},
30+
"spec": {
31+
"containers": [
32+
{
33+
"image": "fluent/fluent-bit",
34+
"imagePullPolicy": "Always",
35+
"name": "fluent-bit",
36+
"resources": {},
37+
"stdin": true,
38+
"stdinOnce": true,
39+
"terminationMessagePath": "/dev/termination-log",
40+
"terminationMessagePolicy": "File",
41+
"tty": true,
42+
"volumeMounts": [
43+
{
44+
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
45+
"name": "default-token-9ffht",
46+
"readOnly": true
47+
}
48+
]
49+
}
50+
],
51+
"dnsPolicy": "ClusterFirst",
52+
"nodeName": "ip-10-49-18-80.eu-west-1.compute.internal",
53+
"restartPolicy": "Never",
54+
"schedulerName": "default-scheduler",
55+
"securityContext": {},
56+
"serviceAccount": "default",
57+
"serviceAccountName": "default",
58+
"terminationGracePeriodSeconds": 30,
59+
"tolerations": [
60+
{
61+
"effect": "NoExecute",
62+
"key": "node.kubernetes.io/not-ready",
63+
"operator": "Exists",
64+
"tolerationSeconds": 300
65+
},
66+
{
67+
"effect": "NoExecute",
68+
"key": "node.kubernetes.io/unreachable",
69+
"operator": "Exists",
70+
"tolerationSeconds": 300
71+
}
72+
],
73+
"volumes": [
74+
{
75+
"name": "default-token-9ffht",
76+
"secret": {
77+
"defaultMode": 420,
78+
"secretName": "default-token-9ffht"
79+
}
80+
}
81+
]
82+
},
83+
"status": {
84+
"conditions": [
85+
{
86+
"lastProbeTime": null,
87+
"lastTransitionTime": "2019-04-03T09:29:00Z",
88+
"status": "True",
89+
"type": "Initialized"
90+
},
91+
{
92+
"lastProbeTime": null,
93+
"lastTransitionTime": "2019-04-03T09:29:06Z",
94+
"status": "True",
95+
"type": "Ready"
96+
},
97+
{
98+
"lastProbeTime": null,
99+
"lastTransitionTime": "2019-04-03T09:29:00Z",
100+
"status": "True",
101+
"type": "PodScheduled"
102+
}
103+
],
104+
"containerStatuses": [
105+
{
106+
"containerID": "docker://c9898099f6d235126d564ed38a020007ea7a6fac6e25e718de683c9dd0076c16",
107+
"image": "fluent/fluent-bit:latest",
108+
"imageID": "docker-pullable://fluent/fluent-bit@sha256:7ac0fd3569af866e9a6a22eb592744200d2dbe098cf066162453f8d0b06c531f",
109+
"lastState": {},
110+
"name": "fluent-bit",
111+
"ready": true,
112+
"restartCount": 0,
113+
"state": {
114+
"running": {
115+
"startedAt": "2019-04-03T09:29:05Z"
116+
}
117+
}
118+
}
119+
],
120+
"hostIP": "10.49.18.80",
121+
"phase": "Running",
122+
"podIP": "100.116.192.42",
123+
"qosClass": "BestEffort",
124+
"startTime": "2019-04-03T09:29:00Z"
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1554141513.598656,{"log":"Fluent Bit is logging\n","stream":"stdout","kubernetes":{"pod_name":"base-with-owner-references","namespace_name":"core","pod_id":"e9f2963f-55f2-11e9-84c5-02e422b8a84a","ownerReferences":[{"apiVersion":"apps/v1","kind":"DaemonSet","name":"fluentbit-max","uid":"a0a2ee0a-08da-5a7f-ac1a-c0a4255f82f2","controller":true,"blockOwnerDeletion":true}],"host":"ip-10-49-18-80.eu-west-1.compute.internal","pod_ip":"100.116.192.42","container_name":"fluent-bit","docker_id":"c9898099f6d235126d564ed38a020007ea7a6fac6e25e718de683c9dd0076c16","container_hash":"fluent/fluent-bit@sha256:7ac0fd3569af866e9a6a22eb592744200d2dbe098cf066162453f8d0b06c531f","container_image":"fluent/fluent-bit:latest"}}]

tests/runtime/filter_kubernetes.c

+13
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ static void flb_test_core_base_with_namespace_labels_and_annotations()
411411
flb_test_namespace_labels_and_annotations("core_base-with-namespace-labels-and-annotations_fluent-bit", NULL, 1);
412412
}
413413

414+
#define flb_test_owner_references(target, suffix, nExpected) \
415+
kube_test("core/" target, KUBE_TAIL, suffix, nExpected, \
416+
"Labels", "Off", \
417+
"Annotations", "Off", \
418+
"Owner_References", "On", \
419+
NULL); \
420+
421+
static void flb_test_core_base_with_owner_references()
422+
{
423+
flb_test_owner_references("core_base-with-owner-references_fluent-bit", NULL, 1);
424+
}
425+
414426
#define flb_test_options_use_kubelet_enabled(target, suffix, nExpected) \
415427
kube_test("options/" target, KUBE_TAIL, suffix, nExpected, \
416428
"use_kubelet", "true", \
@@ -1005,6 +1017,7 @@ TEST_LIST = {
10051017
{"kube_core_unescaping_text", flb_test_core_unescaping_text},
10061018
{"kube_core_unescaping_json", flb_test_core_unescaping_json},
10071019
{"kube_core_base_with_namespace_labels_and_annotations", flb_test_core_base_with_namespace_labels_and_annotations},
1020+
{"kube_core_base_with_owner_references", flb_test_core_base_with_owner_references},
10081021
{"kube_options_use-kubelet_enabled_json", flb_test_options_use_kubelet_enabled_json},
10091022
{"kube_options_use-kubelet_disabled_json", flb_test_options_use_kubelet_disabled_json},
10101023
{"kube_options_merge_log_enabled_text", flb_test_options_merge_log_enabled_text},

0 commit comments

Comments
 (0)