Skip to content

Commit

Permalink
calyptia: add support for configurable format
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Stephens <[email protected]>
  • Loading branch information
patrick-stephens committed Dec 11, 2024
1 parent 93c4a64 commit a6a9c9c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
1 change: 1 addition & 0 deletions include/fluent-bit/calyptia/calyptia_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define CALYPTIA_ENDPOINT_TRACE "/v1/traces/%s"

#define CALYPTIA_ENDPOINT_FLEETS "/v1/fleets"
#define CALYPTIA_ENDPOINT_FLEET_CONFIG_INI "/v1/fleets/%s/config?format=ini&config_format=ini"
#define CALYPTIA_ENDPOINT_FLEET_CONFIG_YAML "/v1/fleets/%s/config?format=yaml&config_format=yaml"
#define CALYPTIA_ENDPOINT_FLEET_FILES "/v1/fleets/%s/files"
#define CALYPTIA_ENDPOINT_FLEET_BY_NAME "/v1/search?project_id=%s&resource=fleet&term=%s&exact=true"
Expand Down
6 changes: 6 additions & 0 deletions plugins/custom_calyptia/calyptia.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ int set_fleet_input_properties(struct calyptia *ctx, struct flb_input_instance *
flb_input_set_property(fleet, "host", ctx->cloud_host);
flb_input_set_property(fleet, "port", ctx->cloud_port);
flb_input_set_property(fleet, "config_dir", ctx->fleet_config_dir);
flb_input_set_property(fleet, "fleet_config_legacy_format", ctx->fleet_config_legacy_format == 1 ? "on" : "off");

/* Set TLS properties */
flb_input_set_property(fleet, "tls", ctx->cloud_tls == 1 ? "on" : "off");
Expand Down Expand Up @@ -774,6 +775,11 @@ static struct flb_config_map config_map[] = {
0, FLB_TRUE, offsetof(struct calyptia, register_retry_on_flush),
"Retry agent registration on flush if failed on init."
},
{
FLB_CONFIG_MAP_BOOL, "fleet_config_legacy_format", "true",
0, FLB_TRUE, offsetof(struct calyptia, fleet_config_legacy_format),
"If set, use legacy (TOML) format for configuration files."
},
/* EOF */
{0}
};
Expand Down
3 changes: 2 additions & 1 deletion plugins/custom_calyptia/calyptia.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ struct calyptia {
flb_sds_t fleet_max_http_buffer_size;
flb_sds_t fleet_interval_sec;
flb_sds_t fleet_interval_nsec;
int register_retry_on_flush; /* retry registration on flush if failed */
int register_retry_on_flush; /* retry registration on flush if failed */
int fleet_config_legacy_format; /* Fleet config format to use: INI (true) or YAML (false) */
};

int set_fleet_input_properties(struct calyptia *ctx, struct flb_input_instance *fleet);
Expand Down
83 changes: 66 additions & 17 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx,
return NULL;
}

ret = flb_sds_printf(&cfgname, PATH_SEPARATOR "%s.yaml", fname);
if (ctx->fleet_config_legacy_format) {
ret = flb_sds_printf(&cfgname, PATH_SEPARATOR "%s.conf", fname);
} else {
ret = flb_sds_printf(&cfgname, PATH_SEPARATOR "%s.yaml", fname);
}

if (ret == NULL) {
flb_sds_destroy(cfgname);
return NULL;
Expand Down Expand Up @@ -309,7 +314,7 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config
char *end;
long val;

if (path == NULL) {
if (path == NULL || ctx == NULL) {
return FLB_FALSE;
}

Expand All @@ -327,7 +332,12 @@ static int is_timestamped_fleet_config_path(struct flb_in_calyptia_fleet_config
return FLB_FALSE;
}

if (strcmp(end, ".yaml") == 0) {
if (ctx->fleet_config_legacy_format) {
if (strcmp(end, ".conf") == 0) {
return FLB_TRUE;
}
}
else if (strcmp(end, ".yaml") == 0) {
return FLB_TRUE;
}

Expand Down Expand Up @@ -471,15 +481,15 @@ static int test_config_is_valid(struct flb_in_calyptia_fleet_config *ctx,

conf = flb_cf_create();
if (conf == NULL) {
flb_plg_debug(ctx->ins, "unable to create conf during validation test: %s",
flb_plg_debug(ctx->ins, "unable to create config during validation test: %s",
cfgpath);
goto config_init_error;
}

conf = flb_cf_create_from_file(conf, cfgpath);
if (conf == NULL) {
flb_plg_debug(ctx->ins,
"unable to create conf from file during validation test: %s",
"unable to create config from file during validation test: %s",
cfgpath);
goto cf_create_from_file_error;
}
Expand Down Expand Up @@ -1292,7 +1302,12 @@ static int calyptia_config_delete_old(struct flb_in_calyptia_fleet_config *ctx)
return -1;
}

if (flb_sds_cat_safe(&glob_files, PATH_SEPARATOR "*.yaml", strlen(PATH_SEPARATOR "*.yaml")) != 0) {
if (ctx->fleet_config_legacy_format) {
if (flb_sds_cat_safe(&glob_files, PATH_SEPARATOR "*.conf", strlen(PATH_SEPARATOR "*.conf")) != 0) {
flb_sds_destroy(glob_files);
return -1;
}
} else if (flb_sds_cat_safe(&glob_files, PATH_SEPARATOR "*.yaml", strlen(PATH_SEPARATOR "*.yaml")) != 0) {
flb_sds_destroy(glob_files);
return -1;
}
Expand Down Expand Up @@ -1356,15 +1371,21 @@ static flb_sds_t calyptia_config_get_newest(struct flb_in_calyptia_fleet_config
return NULL;
}

if (flb_sds_cat_safe(&glob_conf_files, PATH_SEPARATOR "*.yaml", strlen(PATH_SEPARATOR "*.yaml")) != 0) {
if (ctx->fleet_config_legacy_format) {
if (flb_sds_cat_safe(&glob_conf_files, PATH_SEPARATOR "*.conf", strlen(PATH_SEPARATOR "*.conf")) != 0) {
flb_plg_error(ctx->ins, "unable to concatenate fleet glob");
flb_sds_destroy(glob_conf_files);
return NULL;
}
} else if (flb_sds_cat_safe(&glob_conf_files, PATH_SEPARATOR "*.yaml", strlen(PATH_SEPARATOR "*.yaml")) != 0) {
flb_plg_error(ctx->ins, "unable to concatenate fleet glob");
flb_sds_destroy(glob_conf_files);
return NULL;
}

inis = read_glob(glob_conf_files);
if (inis == NULL) {
flb_plg_error(ctx->ins, "unable to read fleet directory for conf files: %s",
flb_plg_error(ctx->ins, "unable to read fleet directory for config files: %s",
glob_conf_files);
flb_sds_destroy(glob_conf_files);
return NULL;
Expand Down Expand Up @@ -1551,7 +1572,7 @@ static int calyptia_config_rollback(struct flb_in_calyptia_fleet_config *ctx,
}
#endif

static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props)
static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props, int fleet_config_legacy_format)
{
struct mk_list *head;
struct flb_kv *kv;
Expand All @@ -1560,7 +1581,11 @@ static void fleet_config_get_properties(flb_sds_t *buf, struct mk_list *props)
kv = mk_list_entry(head, struct flb_kv, _head);

if (kv->key != NULL && kv->val != NULL) {
flb_sds_printf(buf, " %s: ", kv->key);
if (fleet_config_legacy_format) {
flb_sds_printf(buf, " %s ", kv->key);
} else {
flb_sds_printf(buf, " %s: ", kv->key);
}
flb_sds_cat_safe(buf, kv->val, strlen(kv->val));
flb_sds_cat_safe(buf, "\n", 1);
}
Expand Down Expand Up @@ -1630,6 +1655,9 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
flb_ctx_t *flb = flb_context_get();
flb_sds_t fleet_id = NULL;

if( !ctx ) {
return NULL;
}

buf = flb_sds_create_size(2048);

Expand All @@ -1642,14 +1670,23 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
if (strcasecmp(c_ins->p->name, "calyptia")) {
continue;
}
flb_sds_printf(&buf, "customs:\n");
flb_sds_printf(&buf, " - name: %s\n", c_ins->p->name);
if (ctx->fleet_config_legacy_format) {
flb_sds_printf(&buf, "[CUSTOM]\n");
flb_sds_printf(&buf, " name %s\n", c_ins->p->name);
} else {
flb_sds_printf(&buf, "customs:\n");
flb_sds_printf(&buf, " - name: %s\n", c_ins->p->name);
}

fleet_config_get_properties(&buf, &c_ins->properties);
fleet_config_get_properties(&buf, &c_ins->properties, ctx->fleet_config_legacy_format);

if (flb_config_prop_get("fleet_id", &c_ins->properties) == NULL) {
if (ctx->fleet_id != NULL) {
flb_sds_printf(&buf, " fleet_id: %s\n", ctx->fleet_id);
if (ctx->fleet_config_legacy_format) {
flb_sds_printf(&buf, " fleet_id %s\n", ctx->fleet_id);
} else {
flb_sds_printf(&buf, " fleet_id: %s\n", ctx->fleet_id);
}
}
else {
fleet_id = get_fleet_id_from_header(ctx);
Expand All @@ -1659,7 +1696,11 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
return NULL;
}

flb_sds_printf(&buf, " fleet_id: %s\n", ctx->fleet_id);
if (ctx->fleet_config_legacy_format) {
flb_sds_printf(&buf, " fleet_id %s\n", fleet_id);
} else {
flb_sds_printf(&buf, " fleet_id: %s\n", fleet_id);
}
flb_sds_destroy(fleet_id);
}
}
Expand Down Expand Up @@ -1724,7 +1765,11 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
return -1;
}

flb_sds_printf(&ctx->fleet_url, CALYPTIA_ENDPOINT_FLEET_CONFIG_YAML, ctx->fleet_id);
if (ctx->fleet_config_legacy_format) {
flb_sds_printf(&ctx->fleet_url, CALYPTIA_ENDPOINT_FLEET_CONFIG_INI, ctx->fleet_id);
} else {
flb_sds_printf(&ctx->fleet_url, CALYPTIA_ENDPOINT_FLEET_CONFIG_YAML, ctx->fleet_id);
}
}

if (ctx->fleet_files_url == NULL) {
Expand All @@ -1741,7 +1786,11 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)

hdrname = fleet_config_filename(ctx, "header");
header = flb_sds_create_size(CALYPTIA_MAX_DIR_SIZE);
flb_sds_printf(&header, "includes: \n - %s\n", hdrname);
if (ctx->fleet_config_legacy_format) {
flb_sds_printf(&header, "@include %s\n\n", hdrname);
} else {
flb_sds_printf(&header, "includes: \n - %s\n", hdrname);
}
flb_sds_destroy(hdrname);

/* create the base file. */
Expand Down
3 changes: 3 additions & 0 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct flb_in_calyptia_fleet_config {
flb_sds_t fleet_url;
flb_sds_t fleet_files_url;

/* whether to use legacy INI/TOML or YAML format */
int fleet_config_legacy_format;

struct flb_input_instance *ins; /* plugin instance */

/* Networking */
Expand Down

0 comments on commit a6a9c9c

Please sign in to comment.