From f7253ced0092a8b1aa3d1cf0b715aec3f02fd31a Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Tue, 13 Jun 2023 10:21:51 -0400 Subject: [PATCH] in_kubernetes_events: allow listening to a single namespace with the 'namespace' parameter. Signed-off-by: Phillip Whelan --- .../in_kubernetes_events/kubernetes_events.c | 19 ++++++++++++++++--- .../in_kubernetes_events/kubernetes_events.h | 1 + .../kubernetes_events_conf.h | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/plugins/in_kubernetes_events/kubernetes_events.c b/plugins/in_kubernetes_events/kubernetes_events.c index f407d434558..1d7695aa76b 100644 --- a/plugins/in_kubernetes_events/kubernetes_events.c +++ b/plugins/in_kubernetes_events/kubernetes_events.c @@ -555,12 +555,19 @@ static struct flb_http_client *make_event_api_request(struct k8s_events *ctx, struct flb_http_client *c; - if (continue_token == NULL && ctx->limit_request == 0) { + if (continue_token == NULL && ctx->limit_request == 0 && ctx->namespace == NULL) { return flb_http_client(u_conn, FLB_HTTP_GET, K8S_EVENTS_KUBE_API_URI, - NULL, 0, ctx->api_host, ctx->api_port, NULL, 0); + NULL, 0, ctx->api_host, ctx->api_port, NULL, 0); + } + + if (ctx->namespace == NULL) { + url = flb_sds_create(K8S_EVENTS_KUBE_API_URI); + } else { + url = flb_sds_create_size(strlen(K8S_EVENTS_KUBE_NAMESPACE_API_URI) + + strlen(ctx->namespace)); + flb_sds_printf(&url, K8S_EVENTS_KUBE_NAMESPACE_API_URI, ctx->namespace); } - url = flb_sds_create(K8S_EVENTS_KUBE_API_URI); flb_sds_cat_safe(&url, "?", 1); if (ctx->limit_request) { if (continue_token != NULL) { @@ -874,6 +881,12 @@ static struct flb_config_map config_map[] = { "kubernetes retention time for events. Default: 1h" }, + { + FLB_CONFIG_MAP_STR, "kube_namespace", NULL, + 0, FLB_TRUE, offsetof(struct k8s_events, namespace), + "kubernetes namespace to get events from, gets event from all namespaces by default." + }, + #ifdef FLB_HAVE_SQLDB { FLB_CONFIG_MAP_STR, "db", NULL, diff --git a/plugins/in_kubernetes_events/kubernetes_events.h b/plugins/in_kubernetes_events/kubernetes_events.h index 7986a5014c9..1f59e480175 100644 --- a/plugins/in_kubernetes_events/kubernetes_events.h +++ b/plugins/in_kubernetes_events/kubernetes_events.h @@ -44,6 +44,7 @@ struct k8s_events { int tls_debug; int tls_verify; int kube_token_ttl; + flb_sds_t namespace; /* API Server end point */ char kube_url[1024]; diff --git a/plugins/in_kubernetes_events/kubernetes_events_conf.h b/plugins/in_kubernetes_events/kubernetes_events_conf.h index fb960b9b183..9d6b54197f1 100644 --- a/plugins/in_kubernetes_events/kubernetes_events_conf.h +++ b/plugins/in_kubernetes_events/kubernetes_events_conf.h @@ -29,7 +29,10 @@ /* Kubernetes API server info */ #define K8S_EVENTS_KUBE_API_HOST "kubernetes.default.svc" #define K8S_EVENTS_KUBE_API_PORT 443 -#define K8S_EVENTS_KUBE_API_URI "/api/v1/events/" +// /apis/events.k8s.io/v1/events +// /apis/events.k8s.io/v1/namespaces/{namespace}/events +#define K8S_EVENTS_KUBE_API_URI "/api/v1/events" +#define K8S_EVENTS_KUBE_NAMESPACE_API_URI "/api/v1/namespaces/%s/events" /* secrets */ #define K8S_EVENTS_KUBE_TOKEN "/var/run/secrets/kubernetes.io/serviceaccount/token"