From d86aab7dcac6734b3cd63abc4e108b20f2c64550 Mon Sep 17 00:00:00 2001 From: ryanohnemus Date: Wed, 13 Dec 2023 08:51:48 -0600 Subject: [PATCH] filter_kubernetes: Add Tests for namespace labels & annotations - 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 --- plugins/filter_kubernetes/kube_meta.c | 15 ++- ...pace-labels-and-annotations_fluent-bit.log | 1 + .../data/kubernetes/meta/core.namespace_meta | 19 +++ ...with-namespace-labels-and-annotations.meta | 116 ++++++++++++++++++ ...pace-labels-and-annotations_fluent-bit.out | 1 + tests/runtime/filter_kubernetes.c | 11 ++ 6 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 tests/runtime/data/kubernetes/log/core/core_base-with-namespace-labels-and-annotations_fluent-bit.log create mode 100644 tests/runtime/data/kubernetes/meta/core.namespace_meta create mode 100644 tests/runtime/data/kubernetes/meta/core_base-with-namespace-labels-and-annotations.meta create mode 100644 tests/runtime/data/kubernetes/out/core/core_base-with-namespace-labels-and-annotations_fluent-bit.out diff --git a/plugins/filter_kubernetes/kube_meta.c b/plugins/filter_kubernetes/kube_meta.c index a7b70365f8d..f296d07c60d 100644 --- a/plugins/filter_kubernetes/kube_meta.c +++ b/plugins/filter_kubernetes/kube_meta.c @@ -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) { @@ -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; } @@ -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; @@ -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); diff --git a/tests/runtime/data/kubernetes/log/core/core_base-with-namespace-labels-and-annotations_fluent-bit.log b/tests/runtime/data/kubernetes/log/core/core_base-with-namespace-labels-and-annotations_fluent-bit.log new file mode 100644 index 00000000000..259723131be --- /dev/null +++ b/tests/runtime/data/kubernetes/log/core/core_base-with-namespace-labels-and-annotations_fluent-bit.log @@ -0,0 +1 @@ +{"log":"Fluent Bit is logging\n","stream":"stdout","time":"2019-04-01T17:58:33.598656444Z"} diff --git a/tests/runtime/data/kubernetes/meta/core.namespace_meta b/tests/runtime/data/kubernetes/meta/core.namespace_meta new file mode 100644 index 00000000000..12e10df0307 --- /dev/null +++ b/tests/runtime/data/kubernetes/meta/core.namespace_meta @@ -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": {} +} diff --git a/tests/runtime/data/kubernetes/meta/core_base-with-namespace-labels-and-annotations.meta b/tests/runtime/data/kubernetes/meta/core_base-with-namespace-labels-and-annotations.meta new file mode 100644 index 00000000000..3ccca44357e --- /dev/null +++ b/tests/runtime/data/kubernetes/meta/core_base-with-namespace-labels-and-annotations.meta @@ -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" + } +} diff --git a/tests/runtime/data/kubernetes/out/core/core_base-with-namespace-labels-and-annotations_fluent-bit.out b/tests/runtime/data/kubernetes/out/core/core_base-with-namespace-labels-and-annotations_fluent-bit.out new file mode 100644 index 00000000000..c5063d17300 --- /dev/null +++ b/tests/runtime/data/kubernetes/out/core/core_base-with-namespace-labels-and-annotations_fluent-bit.out @@ -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"}}}] diff --git a/tests/runtime/filter_kubernetes.c b/tests/runtime/filter_kubernetes.c index 46c9a966dff..0693f601ba4 100644 --- a/tests/runtime/filter_kubernetes.c +++ b/tests/runtime/filter_kubernetes.c @@ -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, \ @@ -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},