提交 b6ed03d4 编写于 作者: L LiuHao 提交者: haozi007

Support syslog for console log

Signed-off-by: NLiuHao <liuhao27@huawei.com>
上级 9c02d230
......@@ -9,7 +9,7 @@ include(cmake/set_build_flags.cmake)
#set(CMAKE_C_COMPILER "gcc" CACHE PATH "c compiler")
set(GIT_COMMIT_HASH "8ff7dac0993cd75494a281b509c683280417ba5c")
set(GIT_COMMIT_HASH "5bb8c3f59aa1d1c9a7ec543eeca97e54059942c1")
message("-- commit id: " ${GIT_COMMIT_HASH})
add_definitions(-DISULAD_GIT_COMMIT="${GIT_COMMIT_HASH}")
......
%global _version 2.0.0
%global _release 20200327.195112.git8ff7dac0
%global _release 20200402.163225.git5bb8c3f5
%global is_systemd 1
%global debug_package %{nil}
......
......@@ -115,10 +115,6 @@ int client_arguments_init(struct client_arguments *args)
}
args->name = NULL;
args->create_rootfs = NULL;
args->log_file = NULL;
// maximum number of rotate files : 7
args->log_file_rotate = 7;
args->log_file_size = "30KB";
args->argc = 0;
args->argv = NULL;
host = getenv("ISULAD_HOST");
......@@ -127,6 +123,7 @@ int client_arguments_init(struct client_arguments *args)
} else {
args->socket = util_strdup_s(DEFAULT_UNIX_SOCKET);
}
args->log_driver = util_strdup_s("json-file");
(void)memset(&args->custom_conf, 0, sizeof(struct custom_configs));
(void)memset(&args->cr, 0, sizeof(struct args_cgroup_resources));
......@@ -221,6 +218,9 @@ void client_arguments_free(struct client_arguments *args)
free(args->create_rootfs);
args->create_rootfs = NULL;
free(args->log_driver);
args->log_driver = NULL;
free_json_map_string_string(args->annotations);
args->annotations = NULL;
......
......@@ -235,9 +235,8 @@ struct client_arguments {
char *external_rootfs;
char *create_rootfs;
char *image_name;
char *log_file;
char *log_file_size;
unsigned int log_file_rotate;
char *log_driver;
/* notes: we should free the mem in custom_conf by hand */
struct custom_configs custom_conf;
......
......@@ -40,8 +40,6 @@ const char g_cmd_create_usage[] = "create [OPTIONS] --external-rootfs=PATH|IMAGE
struct client_arguments g_cmd_create_args = {
.runtime = "",
.restart = "no",
.log_file_size = "1MB",
.log_file_rotate = 7,
.cr.oom_score_adj = 0,
.custom_conf.health_interval = 0,
.custom_conf.health_timeout = 0,
......@@ -633,15 +631,7 @@ static void request_pack_custom_args(const struct client_arguments *args, isula_
static void request_pack_custom_log_options(const struct client_arguments *args, isula_container_config_t *conf)
{
if (args->log_file != NULL) {
conf->log_file = args->log_file;
}
if (args->log_file_size != NULL) {
conf->log_file_size = args->log_file_size;
}
conf->log_file_rotate = args->log_file_rotate;
return;
conf->log_driver = util_strdup_s(args->log_driver);
}
static int request_pack_custom_log_accel(struct client_arguments *args, isula_container_config_t *conf)
......@@ -1241,34 +1231,119 @@ out:
return ret;
}
static int log_opt_parse_options(struct client_arguments *args, const char *optkey, const char *value)
static int add_new_annotation(const char *key, const char *value, struct client_arguments *args)
{
int ret = -1;
if (key == NULL || value == NULL) {
return -1;
}
if (strcmp(optkey, "max-size") == 0) {
args->log_file_size = util_strdup_s(value);
ret = 0;
} else if (strcmp(optkey, "max-file") == 0) {
if (util_safe_uint(value, &args->log_file_rotate)) {
goto out;
if (args->annotations == NULL) {
args->annotations = util_common_calloc_s(sizeof(json_map_string_string));
if (args->annotations == NULL) {
COMMAND_ERROR("Out Memory");
return -1;
}
if (args->log_file_rotate == 0) {
COMMAND_ERROR("Invalid option 'max-file' key:%s, value:%s", optkey, value);
goto out;
}
if (append_json_map_string_string(args->annotations, key, value)) {
COMMAND_ERROR("Failed to append annotation key:%s, value:%s", key, value);
return -1;
}
return 0;
}
typedef int (*log_opt_callback_t)(const char *key, const char *value, struct client_arguments *args);
typedef struct log_opt_parse {
const char *key;
const char *anno_key;
log_opt_callback_t cb;
} log_opt_parse_t;
static int log_opt_common_cb(const char *key, const char *value, struct client_arguments *args)
{
return add_new_annotation(key, value, args);
}
static int log_opt_max_file_cb(const char *key, const char *value, struct client_arguments *args)
{
unsigned int ptr = 0;
int ret = -1;
if (util_safe_uint(value, &ptr)) {
return ret;
}
if (ptr == 0) {
COMMAND_ERROR("Invalid option 'max-file', value:%s", value);
return ret;
}
return add_new_annotation(key, value, args);
}
static int log_opt_syslog_facility(const char *key, const char *value, struct client_arguments *args)
{
#define FACILITIES_LEN 20
const char *facility_keys[FACILITIES_LEN] = {
"kern", "user", "mail", "daemon", "auth",
"syslog", "lpr", "news", "uucp", "cron", "authpriv", "ftp",
"local0", "local1", "local2", "local3", "local4", "local5", "local6", "local7"
};
int i;
for (i = 0; i < FACILITIES_LEN; i++) {
if (strcmp(facility_keys[i], value) == 0) {
break;
}
}
if (i == FACILITIES_LEN) {
return -1;
}
return add_new_annotation(key, value, args);
}
static int log_opt_disable_log_cb(const char *key, const char *value, struct client_arguments *args)
{
int ret = -1;
if (strcmp(value, "true") == 0) {
ret = add_new_annotation(key, "none", args);
} else if (strcmp(value, "false") == 0) {
ret = 0;
} else if (strcmp(optkey, "disable-log") == 0) {
if (strcmp(value, "true") == 0) {
args->log_file = util_strdup_s("none");
ret = 0;
} else if (strcmp(value, "false") == 0) {
args->log_file = NULL;
ret = 0;
} else {
COMMAND_ERROR("Invalid option 'disable-log' key:%s, value:%s", optkey, value);
} else {
COMMAND_ERROR("Invalid option 'disable-log', value:%s", value);
}
return ret;
}
static int log_opt_parse_options(struct client_arguments *args, const char *optkey, const char *value)
{
#define OPTIONS_MAX 5
log_opt_parse_t log_opts[OPTIONS_MAX] = {
{ .key = "max-size", .anno_key = CONTAINER_LOG_CONFIG_KEY_SIZE, .cb = &log_opt_common_cb, },
{ .key = "max-file", .anno_key = CONTAINER_LOG_CONFIG_KEY_ROTATE, .cb = &log_opt_max_file_cb, },
{ .key = "disable-log", .anno_key = CONTAINER_LOG_CONFIG_KEY_FILE, .cb = &log_opt_disable_log_cb, },
{ .key = "syslog-tag", .anno_key = CONTAINER_LOG_CONFIG_KEY_SYSLOG_TAG, .cb = &log_opt_common_cb, },
{ .key = "syslog-facility", .anno_key = CONTAINER_LOG_CONFIG_KEY_SYSLOG_FACILITY, .cb = &log_opt_syslog_facility, },
};
int ret = -1;
int i ;
for (i = 0; i < OPTIONS_MAX; i++) {
if (strcmp(optkey, log_opts[i].key) == 0) {
ret = log_opts[i].cb(log_opts[i].anno_key, value, args);
break;
}
}
out:
if (i == OPTIONS_MAX) {
COMMAND_ERROR("Unsupported log opt: %s", optkey);
}
return ret;
}
......@@ -1324,12 +1399,39 @@ out:
return ret;
}
int callback_log_opt(command_option_t *option, const char *value)
{
struct client_arguments *args = (struct client_arguments *)option->data;
return log_opt_parser(args, value);
}
int callback_log_driver(command_option_t *option, const char *value)
{
#define DRIVER_MAX 2
const char *drivers[] = {CONTAINER_LOG_CONFIG_JSON_FILE_DRIVER, CONTAINER_LOG_CONFIG_SYSLOG_DRIVER};
int i = 0;
struct client_arguments *args = (struct client_arguments *)option->data;
if (value == NULL) {
return -1;
}
for (; i < DRIVER_MAX; i++) {
if (strcmp(value, drivers[i]) == 0) {
break;
}
}
if (i == DRIVER_MAX) {
return -1;
}
free(args->log_driver);
args->log_driver = util_strdup_s(value);
return 0;
}
static int annotation_parser(struct client_arguments *args, const char *option)
{
int ret = -1;
......@@ -1358,20 +1460,7 @@ static int annotation_parser(struct client_arguments *args, const char *option)
goto out;
}
if (args->annotations == NULL) {
args->annotations = util_common_calloc_s(sizeof(json_map_string_string));
if (args->annotations == NULL) {
COMMAND_ERROR("Out Memory");
goto out;
}
}
if (append_json_map_string_string(args->annotations, optkey, value)) {
COMMAND_ERROR("Failed to append annotation key:%s, value:%s", optkey, value);
goto out;
}
ret = 0;
ret = add_new_annotation(optkey, value, args);
out:
if (ret < 0) {
......
......@@ -85,6 +85,8 @@ extern "C" {
"Kernel memory limit", command_convert_membytes }, \
{ CMD_OPT_TYPE_CALLBACK, false, "hugetlb-limit", 0, &(cmdargs).custom_conf.hugepage_limits, \
"Huge page limit (format: [size:]<limit>, e.g. --hugetlb-limit 2MB:32MB)", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "log-driver", 'n', &(cmdargs), \
"Container log driver, support syslog and json-file", callback_log_driver }, \
{ CMD_OPT_TYPE_CALLBACK, false, "log-opt", 0, &(cmdargs), \
"Container log options, value formate: key=value", callback_log_opt }, \
{ CMD_OPT_TYPE_CALLBACK, false, "memory", 'm', &(cmdargs).cr.memory_limit, \
......@@ -173,6 +175,8 @@ int create_checker(struct client_arguments *args);
int client_create(struct client_arguments *args);
int callback_log_driver(command_option_t *option, const char *value);
int callback_log_opt(command_option_t *option, const char *value);
int callback_annotation(command_option_t *option, const char *value);
......
......@@ -31,9 +31,6 @@ static int run_checker(struct client_arguments *args);
struct client_arguments g_cmd_run_args = {
.runtime = "",
.restart = "no",
.log_file = NULL,
.log_file_size = "1MB",
.log_file_rotate = 7,
};
static int local_cmd_start(struct client_arguments *args, uint32_t *exit_code)
......
......@@ -64,5 +64,14 @@
#define DEFAULT_WEBSOCKET_SERVER_LISTENING_PORT 10350
#define CONTAINER_LOG_CONFIG_JSON_FILE_DRIVER "json-file"
#define CONTAINER_LOG_CONFIG_SYSLOG_DRIVER "syslog"
#define CONTAINER_LOG_CONFIG_KEY_DRIVER "log.console.driver"
#define CONTAINER_LOG_CONFIG_KEY_FILE "log.console.file"
#define CONTAINER_LOG_CONFIG_KEY_ROTATE "log.console.filerotate"
#define CONTAINER_LOG_CONFIG_KEY_SIZE "log.console.filesize"
#define CONTAINER_LOG_CONFIG_KEY_SYSLOG_TAG "log.console.tag"
#define CONTAINER_LOG_CONFIG_KEY_SYSLOG_FACILITY "log.console.facility"
#endif
......@@ -80,6 +80,9 @@
"Labels": {
"$ref": "../defs.json#/definitions/mapStringString"
},
"LogDriver": {
"type": "string"
},
"Annotations": {
"$ref": "../defs.json#/definitions/mapStringString"
},
......@@ -100,20 +103,6 @@
"items": {
"type": "string"
}
},
"LogConfig": {
"type": "object",
"properties": {
"log_file": {
"type": "string"
},
"log_file_size": {
"type": "string"
},
"log_file_rotate": {
"type": "uint64"
}
}
}
},
"required": [
......
......@@ -360,11 +360,8 @@ void isula_container_config_free(isula_container_config_t *config)
free(config->entrypoint);
config->entrypoint = NULL;
free(config->log_file);
config->log_file = NULL;
free(config->log_file_size);
config->log_file_size = NULL;
free(config->log_driver);
config->log_driver = NULL;
free_json_map_string_string(config->annotations);
config->annotations = NULL;
......
......@@ -69,11 +69,7 @@ typedef struct isula_container_config {
char **cmd;
size_t cmd_len;
char *log_file;
char *log_file_size;
unsigned int log_file_rotate;
char *log_driver;
json_map_string_string *annotations;
......
......@@ -228,6 +228,8 @@ void container_log_config_free(struct container_log_config *conf)
}
free(conf->path);
conf->path = NULL;
free(conf->driver);
conf->driver = NULL;
conf->rotate = 0;
conf->size = 0;
free(conf);
......
......@@ -27,10 +27,6 @@ extern "C" {
/* record the isulad errmsg */
extern __thread char *g_isulad_errmsg;
#define CONTAINER_LOG_CONFIG_KEY_FILE "log.console.file"
#define CONTAINER_LOG_CONFIG_KEY_ROTATE "log.console.filerotate"
#define CONTAINER_LOG_CONFIG_KEY_SIZE "log.console.filesize"
#define BLOBS_PATH "blobs/sha256"
#define DIFF_LAYERS_PATH "snapshots/diff"
#define DEFAULT_TCP_HOST "tcp://localhost:2375"
......@@ -150,11 +146,6 @@ struct create_custom_config {
int command_len;
char * const *commands;
/* console log options */
char *log_file;
char *log_file_size;
unsigned int log_file_rotate;
char *share_ns[NAMESPACE_MAX];
};
......@@ -292,6 +283,7 @@ struct isulad_create_image_response {
};
struct container_log_config {
char *driver;
char *path;
int rotate;
int64_t size;
......
......@@ -1817,32 +1817,6 @@ out:
return ret;
}
static int pack_container_custom_config_log(container_config *container_spec,
const isula_container_config_t *custom_conf)
{
int ret = 0;
/* log config */
container_spec->log_config = util_common_calloc_s(sizeof(container_config_log_config));
if (container_spec->log_config == NULL) {
ret = -1;
goto out;
}
if (custom_conf->log_file != NULL) {
container_spec->log_config->log_file = util_strdup_s(custom_conf->log_file);
}
if (custom_conf->log_file_size != NULL) {
container_spec->log_config->log_file_size = util_strdup_s(custom_conf->log_file_size);
}
if (custom_conf->log_file_rotate) {
container_spec->log_config->log_file_rotate = custom_conf->log_file_rotate;
}
out:
return ret;
}
static int pack_container_custom_config_args(container_config *container_spec,
const isula_container_config_t *custom_conf)
{
......@@ -2141,11 +2115,6 @@ static int pack_container_custom_config_pre(container_config *container_spec,
{
int ret = 0;
ret = pack_container_custom_config_log(container_spec, custom_conf);
if (ret != 0) {
goto out;
}
ret = pack_container_custom_config_args(container_spec, custom_conf);
if (ret != 0) {
goto out;
......@@ -2192,6 +2161,7 @@ static int pack_container_custom_config(container_config *container_spec,
if (custom_conf->hostname != NULL) {
container_spec->hostname = util_strdup_s(custom_conf->hostname);
}
container_spec->log_driver = util_strdup_s(custom_conf->log_driver);
/* console config */
container_spec->tty = custom_conf->tty;
......
......@@ -204,18 +204,37 @@ static int add_default_log_config_to_container_spec(const char *id, const char *
container_config *container_spec)
{
int ret = 0;
int i = 0;
bool file_found = false;
bool rotate_found = false;
bool size_found = false;
/* generate default log path */
if (container_spec->log_config == NULL) {
container_spec->log_config = util_common_calloc_s(sizeof(container_config_log_config));
if (container_spec->log_config == NULL) {
ERROR("Out of memory");
ret = -1;
goto out;
}
if (container_spec->log_driver != NULL &&
strcmp(CONTAINER_LOG_CONFIG_SYSLOG_DRIVER, container_spec->log_driver) == 0) {
return 0;
}
if (container_spec->annotations == NULL) {
container_spec->annotations = util_common_calloc_s(sizeof(json_map_string_string));
}
if (container_spec->annotations == NULL) {
ERROR("Out of memory");
ret = -1;
goto out;
}
if (container_spec->log_config->log_file == NULL) {
for (; i < container_spec->annotations->len; i++) {
const char *tmp_key = container_spec->annotations->keys[i];
if (strcmp(CONTAINER_LOG_CONFIG_KEY_FILE, tmp_key) == 0) {
file_found = true;
} else if (strcmp(CONTAINER_LOG_CONFIG_KEY_ROTATE, tmp_key) == 0) {
rotate_found = true;
} else if (strcmp(CONTAINER_LOG_CONFIG_KEY_SIZE, tmp_key) == 0) {
size_found = true;
}
}
if (!file_found) {
char default_path[PATH_MAX] = { 0 };
int nret = snprintf(default_path, PATH_MAX, "%s/%s/console.log", runtime_root, id);
if (nret < 0 || nret >= PATH_MAX) {
......@@ -223,7 +242,22 @@ static int add_default_log_config_to_container_spec(const char *id, const char *
ret = -1;
goto out;
}
container_spec->log_config->log_file = util_strdup_s(default_path);
ret = append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_FILE, default_path);
if (ret != 0) {
goto out;
}
}
if (!rotate_found) {
ret = append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_ROTATE, "7");
if (ret != 0) {
goto out;
}
}
if (!size_found) {
ret = append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_SIZE, "30KB");
if (ret != 0) {
goto out;
}
}
out:
......
......@@ -2238,6 +2238,7 @@ static int container_get_container_log_config(const container_t *cont, struct co
return -1;
}
(*log_config)->path = util_strdup_s(cont->log_path);
(*log_config)->driver = util_strdup_s(cont->log_driver);
(*log_config)->rotate = cont->log_rotate;
(*log_config)->size = cont->log_maxsize;
......@@ -2256,6 +2257,20 @@ static void pack_logs_response(struct isulad_logs_response *response, uint32_t c
}
}
static bool support_logs(struct container_log_config *log_config)
{
if (log_config->driver == NULL) {
return true;
}
// syslog do not support logs
if (strcmp(CONTAINER_LOG_CONFIG_SYSLOG_DRIVER, log_config->driver) == 0) {
return false;
}
return true;
}
static int container_logs_cb(const struct isulad_logs_request *request, stream_func_wrapper *stream,
struct isulad_logs_response **response)
{
......@@ -2305,6 +2320,11 @@ static int container_logs_cb(const struct isulad_logs_request *request, stream_f
cc = ISULAD_ERR_EXEC;
goto out;
}
if (!support_logs(log_config)) {
isulad_set_error_message("Do not support logs for log driver: %s", log_config->driver);
cc = ISULAD_ERR_EXEC;
goto out;
}
EVENT("Event: {Object: %s, Content: path: %s, rotate: %d, size: %ld }", id, log_config->path, log_config->rotate,
log_config->size);
......
......@@ -427,6 +427,7 @@ int v2_spec_make_basic_info(const char *id, const char *name, const char *image_
int v2_spec_merge_contaner_spec(container_config_v2_common_config *v2_spec)
{
int ret = 0;
int i = 0;
container_config *container_spec = NULL;
if (v2_spec == NULL) {
......@@ -435,8 +436,13 @@ int v2_spec_merge_contaner_spec(container_config_v2_common_config *v2_spec)
container_spec = v2_spec->config;
if (container_spec->log_config != NULL && container_spec->log_config->log_file != NULL) {
v2_spec->log_path = util_strdup_s(container_spec->log_config->log_file);
if (container_spec->annotations != NULL) {
for (; i < container_spec->annotations->len; i++) {
if (strcmp(container_spec->annotations->keys[i], CONTAINER_LOG_CONFIG_KEY_FILE) == 0) {
v2_spec->log_path = util_strdup_s(container_spec->annotations->values[i]);
break;
}
}
}
if (pack_path_and_args_from_container_spec(container_spec, v2_spec) != 0) {
......@@ -678,6 +684,8 @@ static int do_parse_container_log_config(const char *key, const char *value, con
return util_safe_int(value, &(cont->log_rotate));
} else if (strcmp(key, CONTAINER_LOG_CONFIG_KEY_SIZE) == 0) {
return util_parse_byte_size_string(value, &(cont->log_maxsize));
} else if (strcmp(key, CONTAINER_LOG_CONFIG_KEY_DRIVER) == 0) {
cont->log_driver = util_strdup_s(value);
}
return 0;
}
......
......@@ -51,6 +51,7 @@ typedef struct _container_t_ {
health_check_manager_t *health_check;
/* log configs of container */
char *log_driver;
char *log_path;
int log_rotate;
int64_t log_maxsize;
......
......@@ -119,46 +119,17 @@ out:
static int make_annotations_log_console(const container_config *container_spec)
{
int ret = 0;
int nret = 0;
char tmp_str[ISULAD_NUMSTRLEN64] = {0};
if (container_spec->log_config != NULL) {
if (container_spec->log_config->log_file != NULL) {
if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_FILE,
container_spec->log_config->log_file)) {
ERROR("append log console file failed");
ret = -1;
goto out;
}
}
nret = snprintf(tmp_str, sizeof(tmp_str), "%llu",
(unsigned long long)(container_spec->log_config->log_file_rotate));
if (nret < 0 || (size_t)nret >= sizeof(tmp_str)) {
ERROR("create rotate string failed");
ret = -1;
goto out;
}
if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_ROTATE, tmp_str)) {
ERROR("append log console file rotate failed");
ret = -1;
goto out;
}
if (container_spec->log_driver == NULL) {
return 0;
}
if (container_spec->log_config->log_file_size != NULL) {
if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_SIZE,
container_spec->log_config->log_file_size)) {
ERROR("append log console file size failed");
ret = -1;
goto out;
}
}
if (append_json_map_string_string(container_spec->annotations, CONTAINER_LOG_CONFIG_KEY_DRIVER,
container_spec->log_driver) != 0) {
ERROR("append log console driver failed");
return -1;
}
out:
return ret;
return 0;
}
static int make_annotations_network_mode(const container_config *container_spec, const host_config *host_spec)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册