diff --git a/plugins/filter_kubernetes/kube_conf.c b/plugins/filter_kubernetes/kube_conf.c index eaf760fed4b..1a87f630d70 100644 --- a/plugins/filter_kubernetes/kube_conf.c +++ b/plugins/filter_kubernetes/kube_conf.c @@ -40,6 +40,7 @@ struct flb_kube *flb_kube_conf_create(struct flb_filter_instance *ins, { int off; int ret; + int cache_size = FLB_HASH_TABLE_SIZE; const char *url; const char *tmp; const char *p; @@ -125,30 +126,35 @@ struct flb_kube *flb_kube_conf_create(struct flb_filter_instance *ins, } } + /* Check if a custom cache size has been defined */ + if (ctx->kube_meta_cache_size > 0) { + cache_size = ctx->kube_meta_cache_size; + } + if (ctx->kube_meta_cache_ttl > 0) { ctx->hash_table = flb_hash_table_create_with_ttl(ctx->kube_meta_cache_ttl, FLB_HASH_TABLE_EVICT_OLDER, - FLB_HASH_TABLE_SIZE, - FLB_HASH_TABLE_SIZE); + cache_size, + cache_size); } else { ctx->hash_table = flb_hash_table_create(FLB_HASH_TABLE_EVICT_RANDOM, - FLB_HASH_TABLE_SIZE, - FLB_HASH_TABLE_SIZE); + cache_size, + cache_size); } if (ctx->kube_meta_namespace_cache_ttl > 0) { ctx->namespace_hash_table = flb_hash_table_create_with_ttl( ctx->kube_meta_namespace_cache_ttl, FLB_HASH_TABLE_EVICT_OLDER, - FLB_HASH_TABLE_SIZE, - FLB_HASH_TABLE_SIZE); + cache_size, + cache_size); } else { ctx->namespace_hash_table = flb_hash_table_create( FLB_HASH_TABLE_EVICT_RANDOM, - FLB_HASH_TABLE_SIZE, - FLB_HASH_TABLE_SIZE); + cache_size, + cache_size); } @@ -192,8 +198,8 @@ struct flb_kube *flb_kube_conf_create(struct flb_filter_instance *ins, ctx->aws_pod_service_hash_table = flb_hash_table_create_with_ttl(ctx->aws_pod_service_map_ttl, FLB_HASH_TABLE_EVICT_OLDER, - FLB_HASH_TABLE_SIZE, - FLB_HASH_TABLE_SIZE); + cache_size, + cache_size); if (!ctx->aws_pod_service_hash_table) { flb_kube_conf_destroy(ctx); return NULL; diff --git a/plugins/filter_kubernetes/kube_conf.h b/plugins/filter_kubernetes/kube_conf.h index b59ed009f28..332d45dfa5f 100644 --- a/plugins/filter_kubernetes/kube_conf.h +++ b/plugins/filter_kubernetes/kube_conf.h @@ -195,6 +195,7 @@ struct flb_kube { int kubelet_port; int kube_meta_cache_ttl; + int kube_meta_cache_size; int kube_meta_namespace_cache_ttl; /* Configuration used for enabling pod to service name mapping*/ diff --git a/plugins/filter_kubernetes/kube_meta.c b/plugins/filter_kubernetes/kube_meta.c index 5c5e7508e4d..00f635d19a4 100644 --- a/plugins/filter_kubernetes/kube_meta.c +++ b/plugins/filter_kubernetes/kube_meta.c @@ -2296,7 +2296,7 @@ static inline int flb_kube_pod_meta_get(struct flb_kube *ctx, { int id; int ret; - const char *hash_meta_buf; + const char *hash_meta_buf = NULL; char *tmp_hash_meta_buf; size_t off = 0; size_t hash_meta_size; @@ -2335,6 +2335,16 @@ static inline int flb_kube_pod_meta_get(struct flb_kube *ctx, flb_hash_table_get_by_id(ctx->hash_table, id, meta->cache_key, &hash_meta_buf, &hash_meta_size); } + else { + flb_plg_error(ctx->ins, "could not add metadata to the cache: %s", + meta->cache_key); + flb_free(tmp_hash_meta_buf); + return -1; + } + } + + if (!hash_meta_buf) { + return -1; } /* @@ -2375,7 +2385,7 @@ static inline int flb_kube_namespace_meta_get(struct flb_kube *ctx, { int id; int ret; - const char *hash_meta_buf; + const char *hash_meta_buf = NULL; char *tmp_hash_meta_buf; size_t off = 0; size_t hash_meta_size; @@ -2414,6 +2424,16 @@ static inline int flb_kube_namespace_meta_get(struct flb_kube *ctx, flb_hash_table_get_by_id(ctx->namespace_hash_table, id, meta->cache_key, &hash_meta_buf, &hash_meta_size); } + else { + flb_plg_error(ctx->ins, "could not add namespace metadata to the cache: %s", + meta->cache_key); + flb_free(tmp_hash_meta_buf); + return -1; + } + } + + if (!hash_meta_buf) { + return -1; } /* diff --git a/plugins/filter_kubernetes/kubernetes.c b/plugins/filter_kubernetes/kubernetes.c index 83190a6b9f5..395524ef5ca 100644 --- a/plugins/filter_kubernetes/kubernetes.c +++ b/plugins/filter_kubernetes/kubernetes.c @@ -1114,13 +1114,19 @@ static struct flb_config_map config_map[] = { { FLB_CONFIG_MAP_TIME, "kube_meta_cache_ttl", "0", 0, FLB_TRUE, offsetof(struct flb_kube, kube_meta_cache_ttl), - "configurable TTL for K8s cached metadata. " - "By default, it is set to 0 which means TTL for cache entries is disabled and " - "cache entries are evicted at random when capacity is reached. " - "In order to enable this option, you should set the number to a time interval. " - "For example, set this value to 60 or 60s and cache entries " + "configurable TTL for K8s cached metadata. " + "By default, it is set to 0 which means TTL for cache entries is disabled and " + "cache entries are evicted at random when capacity is reached. " + "In order to enable this option, you should set the number to a time interval. " + "For example, set this value to 60 or 60s and cache entries " "which have been created more than 60s will be evicted" }, + { + FLB_CONFIG_MAP_INT, "kube_meta_cache_size", "0", + 0, FLB_TRUE, offsetof(struct flb_kube, kube_meta_cache_size), + "configurable size for K8s cached metadata hash table. " + "By default, it is set to 0 which means the default size (256) is used." + }, { FLB_CONFIG_MAP_TIME, "kube_meta_namespace_cache_ttl", "15m", 0, FLB_TRUE, offsetof(struct flb_kube, kube_meta_namespace_cache_ttl),