Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_calyptia_fleet: general fixes from #8371 #8392

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 29 additions & 44 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ struct flb_in_calyptia_fleet_config {
};

static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn,
const char *url,
time_t timestamp);

Expand Down Expand Up @@ -878,14 +877,20 @@ static flb_sds_t get_project_id_from_api_key(struct flb_in_calyptia_fleet_config
}

static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn,
flb_sds_t url)
{
struct flb_http_client *client;
size_t b_sent;
int ret = -1;
size_t b_sent;
struct flb_connection *u_conn;
struct flb_http_client *client;

if (ctx == NULL || url == NULL) {
return NULL;
}

if (ctx == NULL || u_conn == NULL || url == NULL) {
u_conn = flb_upstream_conn_get(ctx->u);
if (u_conn == NULL) {
flb_plg_error(ctx->ins, "unable to get upstream connection");
return NULL;
}

Expand All @@ -904,7 +909,6 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config
ctx->api_key, flb_sds_len(ctx->api_key));

ret = flb_http_do(client, &b_sent);

if (ret != 0) {
flb_plg_error(ctx->ins, "http do error");
goto http_do_error;
Expand All @@ -920,28 +924,28 @@ static struct flb_http_client *fleet_http_do(struct flb_in_calyptia_fleet_config
goto http_do_error;
}

flb_upstream_conn_release(u_conn);
return client;

http_do_error:
flb_http_client_destroy(client);
http_client_error:
flb_upstream_conn_release(u_conn);
return NULL;
}

static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn,
struct flb_config *config)
{
struct flb_http_client *client;
flb_sds_t url;
flb_sds_t project_id;

if (ctx == NULL || u_conn == NULL || config == NULL) {
if (ctx == NULL || config == NULL) {
return -1;
}

project_id = get_project_id_from_api_key(ctx);

if (project_id == NULL) {
return -1;
}
Expand All @@ -955,7 +959,7 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
flb_sds_printf(&url, "/v1/search?project_id=%s&resource=fleet&term=%s",
project_id, ctx->fleet_name);

client = fleet_http_do(ctx, u_conn, url);
client = fleet_http_do(ctx, url);
flb_sds_destroy(url);

if (!client) {
Expand All @@ -981,7 +985,6 @@ static int get_calyptia_fleet_id_by_name(struct flb_in_calyptia_fleet_config *ct
}

static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn,
flb_sds_t url,
const char *hdr,
const char *dst,
Expand All @@ -997,18 +1000,17 @@ static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx,
time_t last_modified;
flb_sds_t fname;

if (ctx == NULL || u_conn == NULL || url == NULL) {
if (ctx == NULL || url == NULL) {
return -1;
}

client = fleet_http_do(ctx, u_conn, url);

client = fleet_http_do(ctx, url);
if (client == NULL) {
return -1;
}

ret = case_header_lookup(client, "Last-modified", strlen("Last-modified"),
&fbit_last_modified, &fbit_last_modified_len);
&fbit_last_modified, &fbit_last_modified_len);

if (ret < 0) {
goto client_error;
Expand All @@ -1019,7 +1021,8 @@ static int get_calyptia_file(struct flb_in_calyptia_fleet_config *ctx,
last_modified = mktime(&tm_last_modified.tm);

fname = time_fleet_config_filename(ctx, last_modified);
} else {
}
else {
fname = flb_sds_create_len(dst, strlen(dst));
}

Expand Down Expand Up @@ -1353,7 +1356,6 @@ static int calyptia_config_delete_old_dir(const char *cfgpath)
for (idx = 0; idx < ((ssize_t)files->entry_count); idx++) {
unlink(files->entries[idx]->data.as_string);
}
cfl_array_destroy(files);
}

/* attempt to delete the main directory */
Expand Down Expand Up @@ -1740,8 +1742,7 @@ static int create_fleet_header(struct flb_in_calyptia_fleet_config *ctx)
return rc;
}

static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn)
static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t cfgname;
flb_sds_t cfgnewname;
Expand Down Expand Up @@ -1778,14 +1779,12 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx,
flb_sds_destroy(hdrname);

/* create the base file. */
ret = get_calyptia_file(ctx, u_conn, ctx->fleet_url, header,
NULL, &time_last_modified);

ret = get_calyptia_file(ctx, ctx->fleet_url, header, NULL, &time_last_modified);
flb_sds_destroy(header);

/* new file created! */
if (ret == 1) {
get_calyptia_files(ctx, u_conn, ctx->fleet_files_url, time_last_modified);
get_calyptia_files(ctx, ctx->fleet_files_url, time_last_modified);

cfgname = time_fleet_config_filename(ctx, time_last_modified);

Expand Down Expand Up @@ -1819,31 +1818,19 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins,
struct flb_config *config,
void *in_context)
{
struct flb_in_calyptia_fleet_config *ctx = in_context;
struct flb_connection *u_conn;
int ret = -1;

u_conn = flb_upstream_conn_get(ctx->u);

if (!u_conn) {
flb_plg_error(ctx->ins, "could not get an upstream connection to %s:%u",
ctx->ins->host.name, ctx->ins->host.port);
goto conn_error;
}
struct flb_in_calyptia_fleet_config *ctx = in_context;

if (ctx->fleet_id == NULL) {

if (get_calyptia_fleet_id_by_name(ctx, u_conn, config) == -1) {
if (get_calyptia_fleet_id_by_name(ctx, config) == -1) {
flb_plg_error(ctx->ins, "unable to find fleet: %s", ctx->fleet_name);
goto calyptia_error;
goto fleet_id_error;
}
}

ret = get_calyptia_fleet_config(ctx, u_conn);
ret = get_calyptia_fleet_config(ctx);

calyptia_error:
flb_upstream_conn_release(u_conn);
conn_error:
fleet_id_error:
FLB_INPUT_RETURN(ret);
}

Expand Down Expand Up @@ -2104,19 +2091,17 @@ static int create_fleet_files(struct flb_in_calyptia_fleet_config *ctx,
}

static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
struct flb_connection *u_conn,
const char *url,
time_t timestamp)
{
struct flb_http_client *client;
int ret = -1;

if (ctx == NULL || u_conn == NULL || url == NULL) {
if (ctx == NULL || url == NULL) {
return -1;
}

client = fleet_http_do(ctx, u_conn, ctx->fleet_files_url);

client = fleet_http_do(ctx, ctx->fleet_files_url);
if (client == NULL) {
return -1;
}
Expand Down
Loading