Skip to content

Commit f6e1bdc

Browse files
nokute78edsiper
authored andcommitted
in_docker_events: re-initialize docker socket when dockerd is restarted(#3439)
Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent 7194459 commit f6e1bdc

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

plugins/in_docker_events/docker_events.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ static int in_de_collect(struct flb_input_instance *ins,
102102
size_t out_size = 0;
103103
struct flb_time out_time;
104104

105-
if ((ret = read(ctx->fd, ctx->buf, ctx->buf_size - 1)) > 0) {
105+
ret = read(ctx->fd, ctx->buf, ctx->buf_size - 1);
106+
107+
if (ret > 0) {
106108
str_len = ret;
107109
ctx->buf[str_len] = '\0';
108110

@@ -153,6 +155,39 @@ static int in_de_collect(struct flb_input_instance *ins,
153155
}
154156
}
155157
}
158+
else if (ret == 0) {
159+
/* EOF */
160+
/* docker service may be restarted */
161+
flb_plg_info(ctx->ins, "EOF detected. Re-initialize");
162+
/* remove old socket collector */
163+
ret = flb_input_collector_delete(ctx->coll_id, ins);
164+
close(ctx->fd);
165+
if (ret < 0) {
166+
flb_plg_error(ctx->ins, "failed to pause event");
167+
return -1;
168+
}
169+
ctx->fd = -1;
170+
/* create socket again */
171+
if (de_unix_create(ctx) < 0) {
172+
flb_plg_error(ctx->ins, "failed to re-initialize socket");
173+
return -1;
174+
}
175+
/* set event */
176+
ctx->coll_id = flb_input_set_collector_event(ins,
177+
in_de_collect,
178+
ctx->fd, config);
179+
if (ctx->coll_id < 0) {
180+
flb_plg_error(ctx->ins,
181+
"could not set collector for IN_DOCKER_EVENTS plugin");
182+
return -1;
183+
}
184+
ret = flb_input_collector_start(ctx->coll_id, ins);
185+
if (ret < 0) {
186+
flb_plg_error(ctx->ins,
187+
"could not start collector for IN_DOCKER_EVENTS plugin");
188+
return -1;
189+
}
190+
}
156191
else {
157192
error = errno;
158193
flb_plg_error(ctx->ins, "read returned error: %d, %s", error,
@@ -194,8 +229,9 @@ static int in_de_init(struct flb_input_instance *ins,
194229
return -1;
195230
}
196231

197-
if (flb_input_set_collector_event(ins, in_de_collect,
198-
ctx->fd, config) == -1) {
232+
ctx->coll_id = flb_input_set_collector_event(ins, in_de_collect,
233+
ctx->fd, config);
234+
if(ctx->coll_id < 0){
199235
flb_plg_error(ctx->ins,
200236
"could not set collector for IN_DOCKER_EVENTS plugin");
201237
de_config_destroy(ctx);

plugins/in_docker_events/docker_events.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
struct flb_in_de_config
3434
{
3535
int fd; /* File descriptor */
36+
int coll_id; /* collector id */
3637
flb_sds_t unix_path; /* Unix path for socket */
3738
char *buf;
3839
size_t buf_size;

0 commit comments

Comments
 (0)