-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ecs.c
1704 lines (1524 loc) · 59.4 KB
/
ecs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_aws_util.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_filter.h>
#include <fluent-bit/flb_filter_plugin.h>
#include <fluent-bit/flb_http_client.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_io.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_env.h>
#include <monkey/mk_core/mk_list.h>
#include <msgpack.h>
#include <stdlib.h>
#include <errno.h>
#include "ecs.h"
static int get_ecs_cluster_metadata(struct flb_filter_ecs *ctx);
static void flb_filter_ecs_destroy(struct flb_filter_ecs *ctx);
/* cluster meta is static so we can expose it on global ctx for other plugins to use */
static void expose_ecs_cluster_meta(struct flb_filter_ecs *ctx)
{
struct flb_env *env;
struct flb_config *config = ctx->ins->config;
env = config->env;
flb_env_set(env, "ecs", "enabled");
if (ctx->cluster_metadata.cluster_name) {
flb_env_set(env,
"aws.ecs.cluster_name",
ctx->cluster_metadata.cluster_name);
}
if (ctx->cluster_metadata.container_instance_arn) {
flb_env_set(env,
"aws.ecs.container_instance_arn",
ctx->cluster_metadata.container_instance_arn);
}
if (ctx->cluster_metadata.container_instance_id) {
flb_env_set(env,
"aws.ecs.container_instance_id",
ctx->cluster_metadata.container_instance_id);
}
if (ctx->cluster_metadata.ecs_agent_version) {
flb_env_set(env,
"aws.ecs.ecs_agent_version",
ctx->cluster_metadata.container_instance_id);
}
}
static int cb_ecs_init(struct flb_filter_instance *f_ins,
struct flb_config *config,
void *data)
{
int ret;
struct flb_filter_ecs *ctx = NULL;
struct mk_list *head;
struct mk_list *split;
struct flb_kv *kv;
struct flb_split_entry *sentry;
int list_size;
struct flb_ecs_metadata_key *ecs_meta = NULL;
(void) data;
/* Create context */
ctx = flb_calloc(1, sizeof(struct flb_filter_ecs));
if (!ctx) {
flb_errno();
return -1;
}
ctx->ins = f_ins;
/* Populate context with config map defaults and incoming properties */
ret = flb_filter_config_map_set(f_ins, (void *) ctx);
if (ret == -1) {
flb_plg_error(f_ins, "configuration error");
flb_free(ctx);
return -1;
}
mk_list_init(&ctx->metadata_keys);
ctx->metadata_keys_len = 0;
mk_list_init(&ctx->metadata_buffers);
mk_list_foreach(head, &f_ins->properties) {
kv = mk_list_entry(head, struct flb_kv, _head);
if (strcasecmp(kv->key, "add") == 0) {
split = flb_utils_split(kv->val, ' ', 2);
list_size = mk_list_size(split);
if (list_size == 0 || list_size > 2) {
flb_plg_error(ctx->ins, "Invalid config for %s", kv->key);
flb_utils_split_free(split);
goto error;
}
sentry = mk_list_entry_first(split, struct flb_split_entry, _head);
ecs_meta = flb_calloc(1, sizeof(struct flb_ecs_metadata_key));
if (!ecs_meta) {
flb_errno();
flb_utils_split_free(split);
goto error;
}
ecs_meta->key = flb_sds_create_len(sentry->value, sentry->len);
if (!ecs_meta->key) {
flb_errno();
flb_utils_split_free(split);
goto error;
}
sentry = mk_list_entry_last(split, struct flb_split_entry, _head);
ecs_meta->template = flb_sds_create_len(sentry->value, sentry->len);
if (!ecs_meta->template) {
flb_errno();
flb_utils_split_free(split);
goto error;
}
ecs_meta->ra = flb_ra_create(ecs_meta->template, FLB_FALSE);
if (ecs_meta->ra == NULL) {
flb_plg_error(ctx->ins, "Could not parse template for `%s`", ecs_meta->key);
flb_utils_split_free(split);
goto error;
}
mk_list_add(&ecs_meta->_head, &ctx->metadata_keys);
ctx->metadata_keys_len++;
flb_utils_split_free(split);
}
}
ctx->ecs_upstream = flb_upstream_create(config,
ctx->ecs_host,
ctx->ecs_port,
FLB_IO_TCP,
NULL);
if (!ctx->ecs_upstream) {
flb_errno();
flb_plg_error(ctx->ins, "Could not create upstream connection to ECS Agent");
goto error;
}
flb_stream_disable_async_mode(&ctx->ecs_upstream->base);
ctx->has_cluster_metadata = FLB_FALSE;
/* entries are only evicted when TTL is reached and a get is issued */
ctx->container_hash_table = flb_hash_table_create_with_ttl(ctx->ecs_meta_cache_ttl,
FLB_HASH_TABLE_EVICT_OLDER,
FLB_ECS_FILTER_HASH_TABLE_SIZE,
FLB_ECS_FILTER_HASH_TABLE_SIZE);
if (!ctx->container_hash_table) {
flb_plg_error(f_ins, "failed to create container_hash_table");
goto error;
}
ctx->failed_metadata_request_tags = flb_hash_table_create_with_ttl(ctx->ecs_meta_cache_ttl,
FLB_HASH_TABLE_EVICT_OLDER,
FLB_ECS_FILTER_HASH_TABLE_SIZE,
FLB_ECS_FILTER_HASH_TABLE_SIZE);
if (!ctx->failed_metadata_request_tags) {
flb_plg_error(f_ins, "failed to create failed_metadata_request_tags table");
goto error;
}
ctx->ecs_tag_prefix_len = strlen(ctx->ecs_tag_prefix);
/* attempt to get metadata in init, can retry in cb_filter */
ret = get_ecs_cluster_metadata(ctx);
flb_filter_set_context(f_ins, ctx);
return 0;
error:
flb_plg_error(ctx->ins, "Initialization failed.");
flb_filter_ecs_destroy(ctx);
return -1;
}
static int plugin_under_test()
{
if (getenv("FLB_ECS_PLUGIN_UNDER_TEST") != NULL) {
return FLB_TRUE;
}
return FLB_FALSE;
}
static char *mock_error_response(char *error_env_var)
{
char *err_val = NULL;
char *error = NULL;
int len = 0;
err_val = getenv(error_env_var);
if (err_val != NULL && strlen(err_val) > 0) {
error = flb_malloc(strlen(err_val) + sizeof(char));
if (error == NULL) {
flb_errno();
return NULL;
}
len = strlen(err_val);
memcpy(error, err_val, len);
error[len] = '\0';
return error;
}
return NULL;
}
static struct flb_http_client *mock_http_call(char *error_env_var, char *api)
{
/* create an http client so that we can set the response */
struct flb_http_client *c = NULL;
char *error = mock_error_response(error_env_var);
c = flb_calloc(1, sizeof(struct flb_http_client));
if (!c) {
flb_errno();
flb_free(error);
return NULL;
}
mk_list_init(&c->headers);
if (error != NULL) {
c->resp.status = 400;
/* resp.data is freed on destroy, payload is supposed to reference it */
c->resp.data = error;
c->resp.payload = c->resp.data;
c->resp.payload_size = strlen(error);
}
else {
c->resp.status = 200;
if (strcmp(api, "Cluster") == 0) {
/* mocked success response */
c->resp.payload = "{\"Cluster\": \"cluster_name\",\"ContainerInstanceArn\": \"arn:aws:ecs:region:aws_account_id:container-instance/cluster_name/container_instance_id\",\"Version\": \"Amazon ECS Agent - v1.30.0 (02ff320c)\"}";
c->resp.payload_size = strlen(c->resp.payload);
}
else {
c->resp.payload = "{\"Arn\": \"arn:aws:ecs:us-west-2:012345678910:task/default/e01d58a8-151b-40e8-bc01-22647b9ecfec\",\"Containers\": [{\"DockerId\": \"79c796ed2a7f864f485c76f83f3165488097279d296a7c05bd5201a1c69b2920\",\"DockerName\": \"ecs-nginx-efs-2-nginx-9ac0808dd0afa495f001\",\"Name\": \"nginx\"}],\"DesiredStatus\": \"RUNNING\",\"Family\": \"nginx-efs\",\"KnownStatus\": \"RUNNING\",\"Version\": \"2\"}";
c->resp.payload_size = strlen(c->resp.payload);
}
}
return c;
}
/*
* Both container instance and task ARNs have the ID at the end after last '/'
*/
static flb_sds_t parse_id_from_arn(const char *arn, int len)
{
int i;
flb_sds_t ID = NULL;
int last_slash = 0;
int id_start = 0;
for (i = 0; i < len; i++) {
if (arn[i] == '/') {
last_slash = i;
}
}
if (last_slash == 0 || last_slash >= len - 2) {
return NULL;
}
id_start = last_slash + 1;
ID = flb_sds_create_len(arn + id_start, len - id_start);
if (ID == NULL) {
flb_errno();
return NULL;
}
return ID;
}
/*
* This deserializes the msgpack metadata buf to msgpack_object
* which can be used with flb_ra_translate in the main filter callback
*/
static int flb_ecs_metadata_buffer_init(struct flb_filter_ecs *ctx,
struct flb_ecs_metadata_buffer *meta)
{
msgpack_unpacked result;
msgpack_object root;
size_t off = 0;
int ret;
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, meta->buf, meta->size, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
flb_plg_error(ctx->ins, "Cannot unpack flb_ecs_metadata_buffer");
msgpack_unpacked_destroy(&result);
return -1;
}
root = result.data;
if (root.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "Cannot unpack flb_ecs_metadata_buffer, msgpack_type=%i",
root.type);
msgpack_unpacked_destroy(&result);
return -1;
}
meta->unpacked = result;
meta->obj = root;
meta->last_used_time = time(NULL);
meta->free_packer = FLB_TRUE;
return 0;
}
static void flb_ecs_metadata_buffer_destroy(struct flb_ecs_metadata_buffer *meta)
{
if (meta) {
flb_free(meta->buf);
if (meta->free_packer == FLB_TRUE) {
msgpack_unpacked_destroy(&meta->unpacked);
}
if (meta->id) {
flb_sds_destroy(meta->id);
}
flb_free(meta);
}
}
/*
* Get cluster and container instance info, which are static and never change
*/
static int get_ecs_cluster_metadata(struct flb_filter_ecs *ctx)
{
struct flb_http_client *c;
struct flb_connection *u_conn;
int ret;
int root_type;
int found_cluster = FLB_FALSE;
int found_version = FLB_FALSE;
int found_instance = FLB_FALSE;
int free_conn = FLB_FALSE;
int i;
int len;
char *buffer;
size_t size;
size_t b_sent;
size_t off = 0;
msgpack_unpacked result;
msgpack_object root;
msgpack_object key;
msgpack_object val;
msgpack_sbuffer tmp_sbuf;
msgpack_packer tmp_pck;
flb_sds_t container_instance_id = NULL;
flb_sds_t tmp = NULL;
/* Compose HTTP Client request*/
if (plugin_under_test() == FLB_TRUE) {
c = mock_http_call("TEST_CLUSTER_ERROR", "Cluster");
ret = 0;
}
else {
u_conn = flb_upstream_conn_get(ctx->ecs_upstream);
if (!u_conn) {
flb_plg_error(ctx->ins, "ECS agent introspection endpoint connection error");
return -1;
}
free_conn = FLB_TRUE;
c = flb_http_client(u_conn, FLB_HTTP_GET,
FLB_ECS_FILTER_CLUSTER_PATH,
NULL, 0,
ctx->ecs_host, ctx->ecs_port,
NULL, 0);
flb_http_buffer_size(c, 0); /* 0 means unlimited */
flb_http_add_header(c, "User-Agent", 10, "Fluent-Bit", 10);
ret = flb_http_do(c, &b_sent);
flb_plg_debug(ctx->ins, "http_do=%i, "
"HTTP Status: %i",
ret, c->resp.status);
}
if (ret != 0 || c->resp.status != 200) {
if (c->resp.payload_size > 0) {
flb_plg_warn(ctx->ins, "Failed to get metadata from %s, will retry",
FLB_ECS_FILTER_CLUSTER_PATH);
flb_plg_debug(ctx->ins, "HTTP response\n%s",
c->resp.payload);
} else {
flb_plg_warn(ctx->ins, "%s response status was %d with no payload, will retry",
FLB_ECS_FILTER_CLUSTER_PATH,
c->resp.status);
}
flb_http_client_destroy(c);
if (free_conn == FLB_TRUE) {
flb_upstream_conn_release(u_conn);
}
return -1;
}
if (free_conn == FLB_TRUE) {
flb_upstream_conn_release(u_conn);
}
ret = flb_pack_json(c->resp.payload, c->resp.payload_size,
&buffer, &size, &root_type);
if (ret < 0) {
flb_plg_warn(ctx->ins, "Could not parse response from %s; response=\n%s",
FLB_ECS_FILTER_CLUSTER_PATH, c->resp.payload);
flb_http_client_destroy(c);
return -1;
}
/* parse metadata response */
msgpack_unpacked_init(&result);
ret = msgpack_unpack_next(&result, buffer, size, &off);
if (ret != MSGPACK_UNPACK_SUCCESS) {
flb_plg_error(ctx->ins, "Cannot unpack %s response to find metadata\n%s",
FLB_ECS_FILTER_CLUSTER_PATH, c->resp.payload);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
flb_http_client_destroy(c);
return -1;
}
flb_http_client_destroy(c);
root = result.data;
if (root.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "%s response parsing failed, msgpack_type=%i",
FLB_ECS_FILTER_CLUSTER_PATH,
root.type);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
/*
Metadata Response:
{
"Cluster": "cluster_name",
"ContainerInstanceArn": "arn:aws:ecs:region:aws_account_id:container-instance/cluster_name/container_instance_id",
"Version": "Amazon ECS Agent - v1.30.0 (02ff320c)"
}
But our metadata keys names are:
{
"ClusterName": "cluster_name",
"ContainerInstanceArn": "arn:aws:ecs:region:aws_account_id:container-instance/cluster_name/container_instance_id",
"ContainerInstanceID": "container_instance_id"
"ECSAgentVersion": "Amazon ECS Agent - v1.30.0 (02ff320c)"
}
*/
for (i = 0; i < root.via.map.size; i++) {
key = root.via.map.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "%s response parsing failed, msgpack key type=%i",
FLB_ECS_FILTER_CLUSTER_PATH,
key.type);
continue;
}
if (key.via.str.size == 7 && strncmp(key.via.str.ptr, "Cluster", 7) == 0) {
val = root.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'Cluster' value type=%i",
val.type);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
found_cluster = FLB_TRUE;
if (ctx->cluster_metadata.cluster_name == NULL) {
tmp = flb_sds_create_len(val.via.str.ptr, (int) val.via.str.size);
if (!tmp) {
flb_errno();
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
ctx->cluster_metadata.cluster_name = tmp;
}
}
else if (key.via.str.size == 20 && strncmp(key.via.str.ptr, "ContainerInstanceArn", 20) == 0) {
val = root.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'ContainerInstanceArn' value type=%i",
val.type);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
/* first the ARN */
found_instance = FLB_TRUE;
if (ctx->cluster_metadata.container_instance_arn == NULL) {
tmp = flb_sds_create_len(val.via.str.ptr, (int) val.via.str.size);
if (!tmp) {
flb_errno();
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
ctx->cluster_metadata.container_instance_arn = tmp;
}
/* then the ID */
if (ctx->cluster_metadata.container_instance_id == NULL) {
container_instance_id = parse_id_from_arn(val.via.str.ptr, (int) val.via.str.size);
if (container_instance_id == NULL) {
flb_plg_error(ctx->ins, "metadata parsing: failed to get ID from %.*s",
(int) val.via.str.size, val.via.str.ptr);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
ctx->cluster_metadata.container_instance_id = container_instance_id;
}
} else if (key.via.str.size == 7 && strncmp(key.via.str.ptr, "Version", 7) == 0) {
val = root.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'Version' value type=%i",
val.type);
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
found_version = FLB_TRUE;
if (ctx->cluster_metadata.ecs_agent_version == NULL) {
tmp = flb_sds_create_len(val.via.str.ptr, (int) val.via.str.size);
if (!tmp) {
flb_errno();
flb_free(buffer);
msgpack_unpacked_destroy(&result);
return -1;
}
ctx->cluster_metadata.ecs_agent_version = tmp;
}
}
}
flb_free(buffer);
msgpack_unpacked_destroy(&result);
if (found_cluster == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse 'Cluster' from %s response",
FLB_ECS_FILTER_CLUSTER_PATH);
return -1;
}
if (found_instance == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse 'ContainerInstanceArn' from %s response",
FLB_ECS_FILTER_CLUSTER_PATH);
return -1;
}
if (found_version == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse 'Version' from %s response",
FLB_ECS_FILTER_CLUSTER_PATH);
return -1;
}
/*
* We also create a standalone cluster metadata msgpack object
* This is used as a fallback for logs when we can't find the
* task metadata for a log. It is valid to attach cluster meta
* to eg. Docker daemon logs which are not an AWS ECS Task via
* the `cluster_metadata_only` setting.
*/
msgpack_sbuffer_init(&tmp_sbuf);
msgpack_packer_init(&tmp_pck, &tmp_sbuf, msgpack_sbuffer_write);
msgpack_pack_map(&tmp_pck, 4);
msgpack_pack_str(&tmp_pck, 11);
msgpack_pack_str_body(&tmp_pck,
"ClusterName",
11);
len = flb_sds_len(ctx->cluster_metadata.cluster_name);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.cluster_name,
len);
msgpack_pack_str(&tmp_pck, 20);
msgpack_pack_str_body(&tmp_pck,
"ContainerInstanceArn",
20);
len = flb_sds_len(ctx->cluster_metadata.container_instance_arn);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.container_instance_arn,
len);
msgpack_pack_str(&tmp_pck, 19);
msgpack_pack_str_body(&tmp_pck,
"ContainerInstanceID",
19);
len = flb_sds_len(ctx->cluster_metadata.container_instance_id);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.container_instance_id,
len);
msgpack_pack_str(&tmp_pck, 15);
msgpack_pack_str_body(&tmp_pck,
"ECSAgentVersion",
15);
len = flb_sds_len(ctx->cluster_metadata.ecs_agent_version);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.ecs_agent_version,
len);
ctx->cluster_meta_buf.buf = tmp_sbuf.data;
ctx->cluster_meta_buf.size = tmp_sbuf.size;
ret = flb_ecs_metadata_buffer_init(ctx, &ctx->cluster_meta_buf);
if (ret < 0) {
flb_plg_error(ctx->ins, "Could not init metadata buffer from %s response",
FLB_ECS_FILTER_CLUSTER_PATH);
msgpack_sbuffer_destroy(&tmp_sbuf);
ctx->cluster_meta_buf.buf = NULL;
ctx->cluster_meta_buf.size = 0;
return -1;
}
ctx->has_cluster_metadata = FLB_TRUE;
expose_ecs_cluster_meta(ctx);
return 0;
}
/*
* This is the helper function used by get_task_metadata()
* that actually creates the final metadata msgpack buffer
* with our final key names.
* It collects cluster, task, and container metadata into one
The new metadata msgpack is flat and looks like:
{
"ContainerID": "79c796ed2a7f864f485c76f83f3165488097279d296a7c05bd5201a1c69b2920",
"DockerContainerName": "ecs-nginx-efs-2-nginx-9ac0808dd0afa495f001",
"ECSContainerName": "nginx",
"ClusterName": "cluster_name",
"ContainerInstanceArn": "arn:aws:ecs:region:aws_account_id:container-instance/cluster_name/container_instance_id",
"ContainerInstanceID": "container_instance_id"
"ECSAgentVersion": "Amazon ECS Agent - v1.30.0 (02ff320c)"
"TaskARN": "arn:aws:ecs:us-west-2:012345678910:task/default/example5-58ff-46c9-ae05-543f8example",
"TaskID: "example5-58ff-46c9-ae05-543f8example",
"TaskDefinitionFamily": "hello_world",
"TaskDefinitionVersion": "8",
}
*/
static int process_container_response(struct flb_filter_ecs *ctx,
msgpack_object container,
struct flb_ecs_task_metadata task_meta)
{
int ret;
int found_id = FLB_FALSE;
int found_ecs_name = FLB_FALSE;
int found_docker_name = FLB_FALSE;
int i;
int len;
struct flb_ecs_metadata_buffer *cont_meta_buf;
msgpack_object key;
msgpack_object val;
msgpack_sbuffer tmp_sbuf;
msgpack_packer tmp_pck;
flb_sds_t short_id = NULL;
/*
* We copy the metadata response to a new buffer
* So we can define the metadata key names
*/
msgpack_sbuffer_init(&tmp_sbuf);
msgpack_packer_init(&tmp_pck, &tmp_sbuf, msgpack_sbuffer_write);
/* 3 container metadata keys, 4 for instance/cluster, 4 for the task */
msgpack_pack_map(&tmp_pck, 11);
/* 1st- process/pack the raw container metadata response */
for (i = 0; i < container.via.map.size; i++) {
key = container.via.map.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "Container metadata parsing failed, msgpack key type=%i",
key.type);
continue;
}
if (key.via.str.size == 8 && strncmp(key.via.str.ptr, "DockerId", 8) == 0) {
val = container.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'DockerId' value type=%i",
val.type);
msgpack_sbuffer_destroy(&tmp_sbuf);
if (short_id != NULL) {
flb_sds_destroy(short_id);
}
return -1;
}
/* save the short ID for hash table key */
short_id = flb_sds_create_len(val.via.str.ptr, 12);
if (!short_id) {
flb_errno();
msgpack_sbuffer_destroy(&tmp_sbuf);
return -1;
}
found_id = FLB_TRUE;
msgpack_pack_str(&tmp_pck, 11);
msgpack_pack_str_body(&tmp_pck,
"ContainerID",
11);
msgpack_pack_str(&tmp_pck, (int) val.via.str.size);
msgpack_pack_str_body(&tmp_pck,
val.via.str.ptr,
(int) val.via.str.size);
}
else if (key.via.str.size == 10 && strncmp(key.via.str.ptr, "DockerName", 10) == 0) {
val = container.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'DockerName' value type=%i",
val.type);
msgpack_sbuffer_destroy(&tmp_sbuf);
if (short_id != NULL) {
flb_sds_destroy(short_id);
}
return -1;
}
/* first pack the ARN */
found_docker_name = FLB_TRUE;
msgpack_pack_str(&tmp_pck, 19);
msgpack_pack_str_body(&tmp_pck,
"DockerContainerName",
19);
msgpack_pack_str(&tmp_pck, (int) val.via.str.size);
msgpack_pack_str_body(&tmp_pck,
val.via.str.ptr,
(int) val.via.str.size);
} else if (key.via.str.size == 4 && strncmp(key.via.str.ptr, "Name", 4) == 0) {
val = container.via.map.ptr[i].val;
if (val.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "metadata parsing: unexpected 'Name' value type=%i",
val.type);
msgpack_sbuffer_destroy(&tmp_sbuf);
if (short_id != NULL) {
flb_sds_destroy(short_id);
}
return -1;
}
found_ecs_name = FLB_TRUE;
msgpack_pack_str(&tmp_pck, 16);
msgpack_pack_str_body(&tmp_pck,
"ECSContainerName",
16);
msgpack_pack_str(&tmp_pck, (int) val.via.str.size);
msgpack_pack_str_body(&tmp_pck,
val.via.str.ptr,
(int) val.via.str.size);
}
}
if (found_id == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse Task 'DockerId' from container response");
msgpack_sbuffer_destroy(&tmp_sbuf);
return -1;
}
if (found_docker_name == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse 'DockerName' from container response");
msgpack_sbuffer_destroy(&tmp_sbuf);
if (short_id != NULL) {
flb_sds_destroy(short_id);
}
return -1;
}
if (found_ecs_name == FLB_FALSE) {
flb_plg_error(ctx->ins, "Could not parse 'Name' from container response");
msgpack_sbuffer_destroy(&tmp_sbuf);
if (short_id != NULL) {
flb_sds_destroy(short_id);
}
return -1;
}
/* 2nd - Add the task fields from the task_meta temp buf we were given */
msgpack_pack_str(&tmp_pck, 20);
msgpack_pack_str_body(&tmp_pck,
"TaskDefinitionFamily",
20);
msgpack_pack_str(&tmp_pck, task_meta.task_def_family_len);
msgpack_pack_str_body(&tmp_pck,
task_meta.task_def_family,
task_meta.task_def_family_len);
msgpack_pack_str(&tmp_pck, 7);
msgpack_pack_str_body(&tmp_pck,
"TaskARN",
7);
msgpack_pack_str(&tmp_pck, task_meta.task_arn_len);
msgpack_pack_str_body(&tmp_pck,
task_meta.task_arn,
task_meta.task_arn_len);
msgpack_pack_str(&tmp_pck, 6);
msgpack_pack_str_body(&tmp_pck,
"TaskID",
6);
msgpack_pack_str(&tmp_pck, task_meta.task_id_len);
msgpack_pack_str_body(&tmp_pck,
task_meta.task_id,
task_meta.task_id_len);
msgpack_pack_str(&tmp_pck, 21);
msgpack_pack_str_body(&tmp_pck,
"TaskDefinitionVersion",
21);
msgpack_pack_str(&tmp_pck, task_meta.task_def_version_len);
msgpack_pack_str_body(&tmp_pck,
task_meta.task_def_version,
task_meta.task_def_version_len);
/* 3rd - Add the static cluster fields from the plugin context */
msgpack_pack_str(&tmp_pck, 11);
msgpack_pack_str_body(&tmp_pck,
"ClusterName",
11);
len = flb_sds_len(ctx->cluster_metadata.cluster_name);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.cluster_name,
len);
msgpack_pack_str(&tmp_pck, 20);
msgpack_pack_str_body(&tmp_pck,
"ContainerInstanceArn",
20);
len = flb_sds_len(ctx->cluster_metadata.container_instance_arn);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.container_instance_arn,
len);
msgpack_pack_str(&tmp_pck, 19);
msgpack_pack_str_body(&tmp_pck,
"ContainerInstanceID",
19);
len = flb_sds_len(ctx->cluster_metadata.container_instance_id);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.container_instance_id,
len);
msgpack_pack_str(&tmp_pck, 15);
msgpack_pack_str_body(&tmp_pck,
"ECSAgentVersion",
15);
len = flb_sds_len(ctx->cluster_metadata.ecs_agent_version);
msgpack_pack_str(&tmp_pck, len);
msgpack_pack_str_body(&tmp_pck,
ctx->cluster_metadata.ecs_agent_version,
len);
cont_meta_buf = flb_calloc(1, sizeof(struct flb_ecs_metadata_buffer));
if (!cont_meta_buf) {
flb_errno();
msgpack_sbuffer_destroy(&tmp_sbuf);
flb_sds_destroy(short_id);
return -1;
}
cont_meta_buf->buf = tmp_sbuf.data;
cont_meta_buf->size = tmp_sbuf.size;
ret = flb_ecs_metadata_buffer_init(ctx, cont_meta_buf);
if (ret < 0) {
flb_plg_error(ctx->ins, "Could not init metadata buffer from container response");
msgpack_sbuffer_destroy(&tmp_sbuf);
flb_free(cont_meta_buf);
flb_sds_destroy(short_id);
return -1;
}
cont_meta_buf->id = short_id;
mk_list_add(&cont_meta_buf->_head, &ctx->metadata_buffers);
/*
* Size is set to 0 so the table just stores our pointer
* Otherwise it will try to copy the memory to a new buffer
*/
ret = flb_hash_table_add(ctx->container_hash_table,
short_id, strlen(short_id),
cont_meta_buf, 0);
if (ret == -1) {
flb_plg_error(ctx->ins, "Could not add container ID %s to metadata hash table",
short_id);
flb_ecs_metadata_buffer_destroy(cont_meta_buf);
} else {
ret = 0;
flb_plg_debug(ctx->ins, "Added `%s` to container metadata hash table",
short_id);
}
return ret;
}
/*
* Gets the container and task metadata for a task via a container's
* 12 char short ID. This can be used with the ECS Agent
* Introspection API: http://localhost:51678/v1/tasks?dockerid={short_id}
* Entries in the hash table will be added for all containers in the task
*/
static int get_task_metadata(struct flb_filter_ecs *ctx, char* short_id)
{
struct flb_http_client *c;
struct flb_connection *u_conn;
int ret;
int root_type;
int found_task = FLB_FALSE;
int found_version = FLB_FALSE;
int found_family = FLB_FALSE;
int found_containers = FLB_FALSE;
int free_conn = FLB_FALSE;
int i;
int k;
char *buffer;
size_t size;
size_t b_sent;
size_t off = 0;
msgpack_unpacked result;
msgpack_object root;
msgpack_object key;
msgpack_object val;
msgpack_object container;
flb_sds_t tmp;
flb_sds_t http_path;
flb_sds_t task_id = NULL;
struct flb_ecs_task_metadata task_meta;
tmp = flb_sds_create_size(64);
if (!tmp) {
return -1;
}
http_path = flb_sds_printf(&tmp, FLB_ECS_FILTER_TASK_PATH_FORMAT, short_id);
if (!http_path) {
flb_sds_destroy(tmp);
return -1;
}
/* Compose HTTP Client request*/
if (plugin_under_test() == FLB_TRUE) {
c = mock_http_call("TEST_TASK_ERROR", "Task");
ret = 0;
}
else {
u_conn = flb_upstream_conn_get(ctx->ecs_upstream);
if (!u_conn) {
flb_plg_error(ctx->ins, "ECS agent introspection endpoint connection error");
flb_sds_destroy(http_path);
return -1;