Skip to content

Commit e75e256

Browse files
committed
router_config: add support for rules
Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
1 parent d87d0f9 commit e75e256

1 file changed

Lines changed: 94 additions & 2 deletions

File tree

src/flb_router_config.c

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <fluent-bit/flb_mem.h>
3030
#include <fluent-bit/flb_router.h>
3131
#include <fluent-bit/flb_sds.h>
32+
#include <fluent-bit/flb_conditionals.h>
3233
#include <fluent-bit/config_format/flb_cf.h>
3334

3435
#include <cfl/cfl_array.h>
@@ -276,10 +277,24 @@ static void route_condition_destroy(struct flb_route_condition *condition)
276277
if (rule->value) {
277278
flb_sds_destroy(rule->value);
278279
}
280+
if (rule->values) {
281+
size_t idx;
282+
283+
for (idx = 0; idx < rule->values_count; idx++) {
284+
if (rule->values[idx]) {
285+
flb_sds_destroy(rule->values[idx]);
286+
}
287+
}
288+
flb_free(rule->values);
289+
}
279290

280291
flb_free(rule);
281292
}
282293

294+
if (condition->compiled) {
295+
flb_condition_destroy(condition->compiled);
296+
}
297+
283298
flb_free(condition);
284299
}
285300

@@ -530,6 +545,8 @@ static struct flb_route_condition_rule *parse_condition_rule(struct cfl_variant
530545
return NULL;
531546
}
532547
cfl_list_init(&rule->_head);
548+
rule->values = NULL;
549+
rule->values_count = 0;
533550

534551
rule->field = copy_from_cfl_sds(field_var->data.as_string);
535552
if (!rule->field) {
@@ -544,9 +561,56 @@ static struct flb_route_condition_rule *parse_condition_rule(struct cfl_variant
544561
return NULL;
545562
}
546563

547-
if (value_var) {
564+
if (!value_var) {
565+
flb_sds_destroy(rule->op);
566+
flb_sds_destroy(rule->field);
567+
flb_free(rule);
568+
return NULL;
569+
}
570+
571+
if (value_var->type == CFL_VARIANT_ARRAY) {
572+
struct cfl_array *array;
573+
struct cfl_variant *entry;
574+
size_t idx;
575+
576+
array = value_var->data.as_array;
577+
if (!array || cfl_array_size(array) == 0) {
578+
flb_sds_destroy(rule->op);
579+
flb_sds_destroy(rule->field);
580+
flb_free(rule);
581+
return NULL;
582+
}
583+
584+
rule->values = flb_calloc(cfl_array_size(array), sizeof(flb_sds_t));
585+
if (!rule->values) {
586+
flb_errno();
587+
flb_sds_destroy(rule->op);
588+
flb_sds_destroy(rule->field);
589+
flb_free(rule);
590+
return NULL;
591+
}
592+
593+
for (idx = 0; idx < cfl_array_size(array); idx++) {
594+
entry = cfl_array_fetch_by_index(array, idx);
595+
rule->values[idx] = variant_to_sds(entry);
596+
if (!rule->values[idx]) {
597+
size_t j;
598+
599+
for (j = 0; j < idx; j++) {
600+
flb_sds_destroy(rule->values[j]);
601+
}
602+
flb_free(rule->values);
603+
flb_sds_destroy(rule->op);
604+
flb_sds_destroy(rule->field);
605+
flb_free(rule);
606+
return NULL;
607+
}
608+
}
609+
rule->values_count = cfl_array_size(array);
610+
}
611+
else {
548612
rule->value = variant_to_sds(value_var);
549-
if (!rule->value && strcmp(rule->op, "exists") != 0) {
613+
if (!rule->value) {
550614
flb_sds_destroy(rule->op);
551615
flb_sds_destroy(rule->field);
552616
flb_free(rule);
@@ -563,6 +627,7 @@ static struct flb_route_condition *parse_condition(struct cfl_variant *variant,
563627
struct flb_route_condition *condition;
564628
struct cfl_variant *rules_var;
565629
struct cfl_variant *default_var;
630+
struct cfl_variant *op_var;
566631
struct cfl_array *rules_array;
567632
struct cfl_variant *entry;
568633
struct flb_route_condition_rule *rule;
@@ -579,9 +644,31 @@ static struct flb_route_condition *parse_condition(struct cfl_variant *variant,
579644
return NULL;
580645
}
581646
cfl_list_init(&condition->rules);
647+
condition->op = FLB_COND_OP_AND;
648+
condition->compiled = NULL;
649+
condition->compiled_status = 0;
582650

583651
rules_var = cfl_kvlist_fetch(variant->data.as_kvlist, "rules");
584652
default_var = cfl_kvlist_fetch(variant->data.as_kvlist, "default");
653+
op_var = cfl_kvlist_fetch(variant->data.as_kvlist, "op");
654+
655+
if (op_var) {
656+
if (op_var->type != CFL_VARIANT_STRING) {
657+
route_condition_destroy(condition);
658+
return NULL;
659+
}
660+
661+
if (strcasecmp(op_var->data.as_string, "and") == 0) {
662+
condition->op = FLB_COND_OP_AND;
663+
}
664+
else if (strcasecmp(op_var->data.as_string, "or") == 0) {
665+
condition->op = FLB_COND_OP_OR;
666+
}
667+
else {
668+
route_condition_destroy(condition);
669+
return NULL;
670+
}
671+
}
585672

586673
if (default_var) {
587674
if (variant_to_bool(default_var, &val) != 0) {
@@ -1220,6 +1307,11 @@ int flb_router_apply_config(struct flb_config *config)
12201307
}
12211308

12221309
if (flb_router_connect_direct(input_ins, output_ins) == 0) {
1310+
struct flb_router_path *path;
1311+
1312+
path = mk_list_entry_last(&input_ins->routes_direct,
1313+
struct flb_router_path, _head);
1314+
path->route = route;
12231315
created++;
12241316
flb_debug("[router] connected input '%s' route '%s' to output '%s'",
12251317
flb_input_name(input_ins),

0 commit comments

Comments
 (0)