Skip to content

Commit

Permalink
filter_kubernetes: Add Tests for namespace labels & annotations
Browse files Browse the repository at this point in the history
- also updates error text for kubelet vs kube api upstream errors
- get_namespace_api_server_info passes in meta->namespace
  instead of ctx->namespace which may not be set at time of call

amended: Added missing test log file.

Signed-off-by: ryanohnemus <[email protected]>
  • Loading branch information
ryanohnemus committed Dec 13, 2023
1 parent 9a23246 commit d86aab7
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 5 deletions.
15 changes: 10 additions & 5 deletions plugins/filter_kubernetes/kube_meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ static int get_meta_file_info(struct flb_kube *ctx, const char *namespace,

if (ctx->meta_preload_cache_dir && namespace) {

if (podname) {
if (podname && strlen(podname) > 0) {
ret = snprintf(uri, sizeof(uri) - 1, "%s/%s_%s.meta",
ctx->meta_preload_cache_dir, namespace, podname);
}
else {
ret = snprintf(uri, sizeof(uri) - 1, "%s/%s.meta",
ret = snprintf(uri, sizeof(uri) - 1, "%s/%s.namespace_meta",
ctx->meta_preload_cache_dir, namespace);
}
if (ret > 0) {
Expand Down Expand Up @@ -382,7 +382,12 @@ static int get_meta_info_from_request(struct flb_kube *ctx,
}

if (!u_conn) {
flb_plg_error(ctx->ins, "kubelet upstream connection error");
if(use_kubelet_connection == FLB_TRUE) {
flb_plg_error(ctx->ins, "kubelet upstream connection error");
}
else {
flb_plg_error(ctx->ins, "kube api upstream connection error");
}
return -1;
}

Expand Down Expand Up @@ -1594,7 +1599,7 @@ static int get_and_merge_namespace_meta(struct flb_kube *ctx, struct flb_kube_me
char *api_buf;
size_t api_size;

ret = get_namespace_api_server_info(ctx, ctx->namespace,
ret = get_namespace_api_server_info(ctx, meta->namespace,
&api_buf, &api_size);
if (ret == -1) {
return -1;
Expand Down Expand Up @@ -2042,7 +2047,7 @@ int flb_kube_meta_get(struct flb_kube *ctx,
{
int ret_namespace_meta = -1;
int ret_pod_meta;

if(ctx->namespace_labels == FLB_TRUE || ctx->namespace_annotations == FLB_TRUE) {
ret_namespace_meta = flb_kube_namespace_meta_get(ctx, tag, tag_len, data,
data_size, namespace_out_buf, namespace_out_size, namespace_meta);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"log":"Fluent Bit is logging\n","stream":"stdout","time":"2019-04-01T17:58:33.598656444Z"}
19 changes: 19 additions & 0 deletions tests/runtime/data/kubernetes/meta/core.namespace_meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"apiVersion": "v1",
"kind": "Namespace",
"metadata": {
"annotations": {
"fake-annotation1": "test1",
"fake-annotation2": "test2"
},
"creationTimestamp": "2019-04-03T09:29:00Z",
"labels": {
"fake-namespace-label1": "label1",
"fake-namespace-label2": "label2"
},
"name": "core",
"resourceVersion": "74466568",
"uid": "e9f2963f-55f2-11e9-84c5-02e422b8a84b"
},
"spec": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"annotations": {
"prometheus.io/path": "/api/v1/metrics/prometheus",
"prometheus.io/port": "2020",
"prometheus.io/scrape": "true"
},
"creationTimestamp": "2019-04-03T09:29:00Z",
"labels": {
"app.kubernetes.io/name": "fluent-bit"
},
"name": "base",
"namespace": "core",
"resourceVersion": "74466568",
"selfLink": "/api/v1/namespaces/core/pods/base",
"uid": "e9f2963f-55f2-11e9-84c5-02e422b8a84a"
},
"spec": {
"containers": [
{
"image": "fluent/fluent-bit",
"imagePullPolicy": "Always",
"name": "fluent-bit",
"resources": {},
"stdin": true,
"stdinOnce": true,
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"tty": true,
"volumeMounts": [
{
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
"name": "default-token-9ffht",
"readOnly": true
}
]
}
],
"dnsPolicy": "ClusterFirst",
"nodeName": "ip-10-49-18-80.eu-west-1.compute.internal",
"restartPolicy": "Never",
"schedulerName": "default-scheduler",
"securityContext": {},
"serviceAccount": "default",
"serviceAccountName": "default",
"terminationGracePeriodSeconds": 30,
"tolerations": [
{
"effect": "NoExecute",
"key": "node.kubernetes.io/not-ready",
"operator": "Exists",
"tolerationSeconds": 300
},
{
"effect": "NoExecute",
"key": "node.kubernetes.io/unreachable",
"operator": "Exists",
"tolerationSeconds": 300
}
],
"volumes": [
{
"name": "default-token-9ffht",
"secret": {
"defaultMode": 420,
"secretName": "default-token-9ffht"
}
}
]
},
"status": {
"conditions": [
{
"lastProbeTime": null,
"lastTransitionTime": "2019-04-03T09:29:00Z",
"status": "True",
"type": "Initialized"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2019-04-03T09:29:06Z",
"status": "True",
"type": "Ready"
},
{
"lastProbeTime": null,
"lastTransitionTime": "2019-04-03T09:29:00Z",
"status": "True",
"type": "PodScheduled"
}
],
"containerStatuses": [
{
"containerID": "docker://c9898099f6d235126d564ed38a020007ea7a6fac6e25e718de683c9dd0076c16",
"image": "fluent/fluent-bit:latest",
"imageID": "docker-pullable://fluent/fluent-bit@sha256:7ac0fd3569af866e9a6a22eb592744200d2dbe098cf066162453f8d0b06c531f",
"lastState": {},
"name": "fluent-bit",
"ready": true,
"restartCount": 0,
"state": {
"running": {
"startedAt": "2019-04-03T09:29:05Z"
}
}
}
],
"hostIP": "10.49.18.80",
"phase": "Running",
"podIP": "100.116.192.42",
"qosClass": "BestEffort",
"startTime": "2019-04-03T09:29:00Z"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1554141513.598656,{"log":"Fluent Bit is logging\n","stream":"stdout","kubernetes":{"pod_name":"base-with-namespace-labels-and-annotations","namespace_name":"core","pod_id":"e9f2963f-55f2-11e9-84c5-02e422b8a84a","labels":{"app.kubernetes.io/name":"fluent-bit"},"annotations":{"prometheus.io/path":"/api/v1/metrics/prometheus","prometheus.io/port":"2020","prometheus.io/scrape":"true"},"host":"ip-10-49-18-80.eu-west-1.compute.internal","container_name":"fluent-bit","docker_id":"c9898099f6d235126d564ed38a020007ea7a6fac6e25e718de683c9dd0076c16","container_hash":"fluent/fluent-bit@sha256:7ac0fd3569af866e9a6a22eb592744200d2dbe098cf066162453f8d0b06c531f","container_image":"fluent/fluent-bit:latest"},"kubernetes_namespace":{"name":"core","labels":{"fake-namespace-label1":"label1","fake-namespace-label2":"label2"},"annotations":{"fake-annotation1":"test1","fake-annotation2":"test2"}}}]
11 changes: 11 additions & 0 deletions tests/runtime/filter_kubernetes.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ static void flb_test_core_unescaping_json()
flb_test_core("core_unescaping_json", NULL, 1);
}

#define flb_test_namespace_labels_and_annotations(target, suffix, nExpected) \
kube_test("core/" target, KUBE_TAIL, suffix, nExpected, \
"Namespace_labels", "On", \
"Namespace_annotations", "On", \
NULL); \

static void flb_test_core_base_with_namespace_labels_and_annotations()
{
flb_test_namespace_labels_and_annotations("core_base-with-namespace-labels-and-annotations_fluent-bit", NULL, 1);
}

#define flb_test_options_use_kubelet_enabled(target, suffix, nExpected) \
kube_test("options/" target, KUBE_TAIL, suffix, nExpected, \
Expand Down Expand Up @@ -994,6 +1004,7 @@ TEST_LIST = {
{"kube_core_no_meta", flb_test_core_no_meta},
{"kube_core_unescaping_text", flb_test_core_unescaping_text},
{"kube_core_unescaping_json", flb_test_core_unescaping_json},
{"kube_core_base_with_namespace_labels_and_annotations", flb_test_core_base_with_namespace_labels_and_annotations},
{"kube_options_use-kubelet_enabled_json", flb_test_options_use_kubelet_enabled_json},
{"kube_options_use-kubelet_disabled_json", flb_test_options_use_kubelet_disabled_json},
{"kube_options_merge_log_enabled_text", flb_test_options_merge_log_enabled_text},
Expand Down

0 comments on commit d86aab7

Please sign in to comment.