summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/filter_kubernetes/kube_property.c
blob: 4399d692cd1b0e0b7d13d1f7a5d55323447cc3e1 (plain)
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
/* -*- 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_info.h>
#include <fluent-bit/flb_filter_plugin.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_parser.h>

#include <msgpack.h>

#include "kube_conf.h"
#include "kube_meta.h"
#include "kube_property.h"

#define FLB_KUBE_PROP_PARSER "parser"
#define FLB_KUBE_PROP_PARSER_LEN (sizeof(FLB_KUBE_PROP_PARSER) - 1)
#define FLB_KUBE_PROP_EXCLUDE "exclude"
#define FLB_KUBE_PROP_EXCLUDE_LEN (sizeof(FLB_KUBE_PROP_EXCLUDE) - 1)

static inline int prop_cmp(const char *key, size_t keylen,
                           const char *property, size_t proplen)
{
    return proplen >= keylen && strncmp(key, property, keylen) == 0;
}

static inline void prop_not_allowed(const char *prop, struct flb_kube_meta *meta,
                                    struct flb_kube *ctx)
{
    flb_plg_warn(ctx->ins, "annotation '%s' not allowed "
                 "(ns='%s' pod_name='%s')",
                 prop, meta->namespace, meta->podname);
}

/* Property: parser */
static int prop_set_parser(struct flb_kube *ctx, struct flb_kube_meta *meta,
                           int is_container_specific, int stream,
                           const char *val_buf, size_t val_len,
                           struct flb_kube_props *props)
{
    char *tmp;
    struct flb_parser *parser;

    /* Parser property must be allowed by k8s-logging.parser */
    if (ctx->k8s_logging_parser == FLB_FALSE) {
        prop_not_allowed("fluentbit.io/parser", meta, ctx);
        return -1;
    }

    /* Check the parser exists */
    tmp = flb_strndup(val_buf, val_len);
    if (!tmp) {
        flb_errno();
        return -1;
    }

    /* Get parser context */
    parser = flb_parser_get(tmp, ctx->config);
    if (!parser) {
        flb_plg_warn(ctx->ins, "annotation parser '%s' not found "
                     "(ns='%s' pod_name='%s', container_name='%s')",
                     tmp, meta->namespace, meta->podname, meta->container_name);
        flb_free(tmp);
        return -1;
    }

    /* Save the parser in the properties context */
    if ((stream == FLB_KUBE_PROP_NO_STREAM ||
         stream == FLB_KUBE_PROP_STREAM_STDOUT) &&
        (is_container_specific == FLB_TRUE ||
         props->stdout_parser == FLB_KUBE_PROP_UNDEF)) {
        props->stdout_parser = flb_sds_create(tmp);
    }
    if ((stream == FLB_KUBE_PROP_NO_STREAM ||
         stream == FLB_KUBE_PROP_STREAM_STDERR) &&
        (is_container_specific == FLB_TRUE ||
         props->stderr_parser == FLB_KUBE_PROP_UNDEF)) {
        props->stderr_parser = flb_sds_create(tmp);
    }

    flb_free(tmp);

    return 0;
}

static int prop_set_exclude(struct flb_kube *ctx, struct flb_kube_meta *meta,
                            int is_container_specific, int stream,
                            const char *val_buf, size_t val_len,
                            struct flb_kube_props *props)
{
    char *tmp;
    int exclude;

    /* Exclude property must be allowed by k8s-logging.exclude */
    if (ctx->k8s_logging_exclude == FLB_FALSE) {
        prop_not_allowed("fluentbit.io/exclude", meta, ctx);
        return -1;
    }

    /* Get the bool value */
    tmp = flb_strndup(val_buf, val_len);
    if (!tmp) {
        flb_errno();
        return -1;
    }

    exclude = flb_utils_bool(tmp) == FLB_TRUE ?
              FLB_KUBE_PROP_TRUE : FLB_KUBE_PROP_FALSE;

    /* Save the exclude property in the context */
    if ((stream == FLB_KUBE_PROP_NO_STREAM ||
         stream == FLB_KUBE_PROP_STREAM_STDOUT) &&
        (is_container_specific == FLB_TRUE ||
         props->stdout_exclude == FLB_KUBE_PROP_UNDEF)) {
        props->stdout_exclude = exclude;
    }
    if ((stream == FLB_KUBE_PROP_NO_STREAM ||
         stream == FLB_KUBE_PROP_STREAM_STDERR) &&
        (is_container_specific == FLB_TRUE ||
         props->stderr_exclude == FLB_KUBE_PROP_UNDEF)) {
        props->stderr_exclude = exclude;
    }

    flb_free(tmp);

    return 0;
}

int flb_kube_prop_set(struct flb_kube *ctx, struct flb_kube_meta *meta,
                      const char *prop, int prop_len,
                      const char *val_buf, size_t val_len,
                      struct flb_kube_props *props)
{
    /*
     * Property can take the following forms:
     *  <property> applies to streams stdout and stderr of every pod's containers
     *  <property>-<container> applies to streams stdout and stderr of a specific pod's container
     *  <property>_stdout applies to stream stdout of every pod's containers
     *  <property>_stderr applies to stream stderr of every pod's containers
     *  <property>_stdout-<container> applies to stream stdout of a specific pod's container
     *  <property>_stderr-<container> applies to stream stderr of a specific pod's container
     */
    const char *cur = prop;
    size_t len = prop_len;
    const char *container = NULL;
    size_t container_len = 0;
    int stream = FLB_KUBE_PROP_NO_STREAM;
    int (*function)(struct flb_kube *ctx, struct flb_kube_meta *meta,
                    int is_container_specific, int stream,
                    const char *val_buf, size_t val_len,
                    struct flb_kube_props *props);

    if (prop_cmp(FLB_KUBE_PROP_PARSER, FLB_KUBE_PROP_PARSER_LEN, prop, prop_len)) {
        function = prop_set_parser;
        cur += FLB_KUBE_PROP_PARSER_LEN;
    }
    else if (prop_cmp(FLB_KUBE_PROP_EXCLUDE, FLB_KUBE_PROP_EXCLUDE_LEN, prop, prop_len)) {
        function = prop_set_exclude;
        cur += FLB_KUBE_PROP_EXCLUDE_LEN;
    }
    else {
        flb_plg_warn(ctx->ins, "unknown annotation 'fluentbit.io/%.*s' "
                     "(ns='%s' pod_name='%s')",
                     prop_len, prop, meta->namespace, meta->podname);
        return -1;
    }

    len = prop_len - (cur - prop);

    if (prop_cmp("_", 1, cur, len)) {
        cur++;
        len--;

        if (prop_cmp("stdout", sizeof("stdout") - 1, cur, len)) {
            stream = FLB_KUBE_PROP_STREAM_STDOUT;
            cur += sizeof("stdout") - 1;
        }
        else if (prop_cmp("stderr", sizeof("stderr") - 1, cur, len)) {
            stream = FLB_KUBE_PROP_STREAM_STDERR;
            cur += sizeof("stderr") - 1;
        }
        else {
            flb_plg_warn(ctx->ins, "invalid stream in annotation "
                         "'fluentbit.io/%.*s' (ns='%s' pod_name='%s')",
                      prop_len, prop, meta->namespace, meta->podname);
            return -1;
        }

        len = prop_len - (cur - prop);
    }

    if (prop_cmp("-", 1, cur, len)) {
        cur++;
        len--;

        if (len == 0) {
            flb_plg_warn(ctx->ins, "invalid container in annotation "
                         "'fluentbit.io/%.*s' (ns='%s' pod_name='%s')",
                      prop_len, prop, meta->namespace, meta->podname);
            return -1;
        }

        container = cur;
        container_len = len;
        len = 0;
    }

    if (len > 0) {
        flb_plg_warn(ctx->ins, "invalid annotation 'fluentbit.io/%.*s' "
                     "(ns='%s' pod_name='%s')",
                     prop_len, prop, meta->namespace, meta->podname);
        return -1;
    }

    /* If the property is for a specific container, and this is not
     * that container, bail out
     */
    if (container && strncmp(container, meta->container_name, container_len)) {
        return 0;
    }

    return function(ctx, meta,
                    (container ? FLB_TRUE : FLB_FALSE), stream,
                    val_buf, val_len, props);
}

int flb_kube_prop_pack(struct flb_kube_props *props,
                       void **out_buf, size_t *out_size)
{
    int size;
    msgpack_packer pck;
    msgpack_sbuffer sbuf;

    /* Number of fields in props structure */
    size = FLB_KUBE_NUMBER_OF_PROPS;

    /* Create msgpack buffer */
    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);

    /* Main array */
    msgpack_pack_array(&pck, size);

    /* Index 0: FLB_KUBE_PROPS_STDOUT_PARSER */
    if (props->stdout_parser) {
        msgpack_pack_str(&pck, flb_sds_len(props->stdout_parser));
        msgpack_pack_str_body(&pck, props->stdout_parser, flb_sds_len(props->stdout_parser));
    }
    else {
        msgpack_pack_nil(&pck);
    }

    /* Index 1: FLB_KUBE_PROPS_STDERR_PARSER */
    if (props->stderr_parser) {
        msgpack_pack_str(&pck, flb_sds_len(props->stderr_parser));
        msgpack_pack_str_body(&pck, props->stderr_parser, flb_sds_len(props->stderr_parser));
    }
    else {
        msgpack_pack_nil(&pck);
    }

    /* Index 2: FLB_KUBE_PROPS_STDOUT_EXCLUDE */
    if (props->stdout_exclude == FLB_KUBE_PROP_TRUE) {
        msgpack_pack_true(&pck);
    }
    else {
        msgpack_pack_false(&pck);
    }

    /* Index 3: FLB_KUBE_PROPS_STDERR_EXCLUDE */
    if (props->stderr_exclude == FLB_KUBE_PROP_TRUE) {
        msgpack_pack_true(&pck);
    }
    else {
        msgpack_pack_false(&pck);
    }

    /* Set outgoing msgpack buffer */
    *out_buf = sbuf.data;
    *out_size = sbuf.size;

    return 0;
}

int flb_kube_prop_unpack(struct flb_kube_props *props,
                         const char *buf, size_t size)
{
    int ret;
    size_t off = 0;
    msgpack_object o;
    msgpack_object root;
    msgpack_unpacked result;

    memset(props, '\0', sizeof(struct flb_kube_props));

    msgpack_unpacked_init(&result);
    ret = msgpack_unpack_next(&result, buf, size, &off);
    if (ret == MSGPACK_UNPACK_PARSE_ERROR) {
        msgpack_unpacked_destroy(&result);
        return -1;
    }
    root = result.data;

    /* Index 0: stdout_parser */
    o = root.via.array.ptr[FLB_KUBE_PROPS_STDOUT_PARSER];
    if (o.type == MSGPACK_OBJECT_NIL) {
        props->stdout_parser = NULL;
    }
    else {
        props->stdout_parser = flb_sds_create_len(o.via.str.ptr, o.via.str.size);
    }

    /* Index 1: stderr_parser */
    o = root.via.array.ptr[FLB_KUBE_PROPS_STDERR_PARSER];
    if (o.type == MSGPACK_OBJECT_NIL) {
        props->stderr_parser = NULL;
    }
    else {
        props->stderr_parser = flb_sds_create_len(o.via.str.ptr, o.via.str.size);
    }

    /* Index 2: stdout_exclude */
    o = root.via.array.ptr[FLB_KUBE_PROPS_STDOUT_EXCLUDE];
    props->stdout_exclude = o.via.boolean;

    /* Index 3: stderr_exclude */
    o = root.via.array.ptr[FLB_KUBE_PROPS_STDERR_EXCLUDE];
    props->stderr_exclude = o.via.boolean;

    msgpack_unpacked_destroy(&result);
    return 0;
}

/* Destroy any resource held by a props element */
void flb_kube_prop_destroy(struct flb_kube_props *props)
{
    if (props->stdout_parser) {
        flb_sds_destroy(props->stdout_parser);
        props->stdout_parser = NULL;
    }
    if (props->stderr_parser) {
        flb_sds_destroy(props->stderr_parser);
        props->stderr_parser = NULL;
    }
}