提交 423aeff2 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!80 add --label && --label-file interface

Merge pull request !80 from JingWoo/label
......@@ -43,6 +43,12 @@ struct custom_configs {
/* environment variables file */
char **env_file;
/* label */
char **label;
/* label file */
char **label_file;
/* hugepage limits */
char **hugepage_limits;
......
......@@ -13,6 +13,7 @@
* Description: provide container create functions
******************************************************************************/
#include <unistd.h>
#include <stdio_ext.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/stat.h>
......@@ -414,6 +415,149 @@ out:
return ret;
}
static bool validate_label(const char *label)
{
bool ret = true;
char **arr = util_string_split_n(label, '=', 2);
if (arr == NULL) {
ERROR("Failed to split label string");
ret = false;
goto out;
}
if (strlen(arr[0]) == 0) {
ERROR("Invalid label: %s, empty name", label);
ret = false;
goto out;
}
out:
util_free_array(arr);
return ret;
}
static int request_pack_custom_label(struct client_arguments *args, isula_container_config_t *conf)
{
int ret = 0;
size_t i;
if (args->custom_conf.label == NULL) {
return 0;
}
for (i = 0; i < util_array_len((const char **)(args->custom_conf.label)); i++) {
if (!validate_label(args->custom_conf.label[i])) {
COMMAND_ERROR("Invalid label '%s': empty name", args->custom_conf.label[i]);
ret = -1;
goto out;
}
if (util_array_append(&conf->label, args->custom_conf.label[i]) != 0) {
COMMAND_ERROR("Failed to append custom config label list");
ret = -1;
goto out;
}
}
util_free_array(args->custom_conf.label);
args->custom_conf.label = conf->label; /* make sure args->custom_conf.label point to valid memory. */
conf->label_len = util_array_len((const char **)(conf->label));
out:
return ret;
}
static int read_label_from_file(const char *path, size_t file_size, isula_container_config_t *conf)
{
int ret = 0;
FILE *fp = NULL;
char *buf = NULL;
size_t len;
ssize_t num;
if (file_size == 0) {
return 0;
}
fp = fopen(path, "re");
if (fp == NULL) {
ERROR("Failed to open '%s'", path);
return -1;
}
__fsetlocking(fp, FSETLOCKING_BYCALLER);
num = getline(&buf, &len, fp);
while (num != -1) {
size_t len = strlen(buf);
if (len == 1) {
num = getline(&buf, &len, fp);
continue;
}
buf[len - 1] = '\0';
if (!validate_label(buf)) {
COMMAND_ERROR("Invalid label '%s': empty name", buf);
ret = -1;
goto out;
}
if (util_array_append(&conf->label, buf) != 0) {
ERROR("Failed to append label");
ret = -1;
goto out;
}
num = getline(&buf, &len, fp);
}
out:
free(buf);
fclose(fp);
return ret;
}
static int append_labels_to_conf(const char *label_file, isula_container_config_t *conf)
{
int ret = 0;
size_t file_size;
if (!util_file_exists(label_file)) {
COMMAND_ERROR("label file not exists: %s", label_file);
ret = -1;
goto out;
}
file_size = util_file_size(label_file);
if (file_size > REGULAR_FILE_SIZE) {
COMMAND_ERROR("label file '%s', size exceed limit: %lld", label_file, REGULAR_FILE_SIZE);
ret = -1;
goto out;
}
if (read_label_from_file(label_file, file_size, conf) != 0) {
COMMAND_ERROR("failed to read label from file: %s", label_file);
ret = -1;
goto out;
}
out:
return ret;
}
static int request_pack_custom_label_file(const struct client_arguments *args, isula_container_config_t *conf)
{
int ret = 0;
size_t i;
char **label_files = args->custom_conf.label_file;
size_t label_files_size = util_array_len((const char **)label_files);
if (label_files_size == 0) {
return 0;
}
for (i = 0; i < label_files_size; i++) {
if (append_labels_to_conf(label_files[i], conf) != 0) {
ret = -1;
goto out;
}
}
conf->label_len = util_array_len((const char **)(conf->label));
out:
return ret;
}
static void request_pack_custom_user(const struct client_arguments *args, isula_container_config_t *conf)
{
if (args->custom_conf.user != NULL) {
......@@ -577,17 +721,27 @@ static int request_pack_custom_conf(struct client_arguments *args, isula_contain
return -1;
}
/* Make sure --env has higher priority than --env-file */
/* make sure --env has higher priority than --env-file */
if (request_pack_custom_env(args, conf) != 0) {
return -1;
}
/* append labels from label file */
if (request_pack_custom_label_file(args, conf) != 0) {
return -1;
}
/* make sure --label has higher priority than --label-file */
if (request_pack_custom_label(args, conf) != 0) {
return -1;
}
/* user and group */
request_pack_custom_user(args, conf);
request_pack_custom_hostname(args, conf);
/* alldevices */
/* all devices */
request_pack_custom_all_devices(args, conf);
/* system container */
......@@ -1225,6 +1379,7 @@ out:
return ret;
}
int callback_annotation(command_option_t *option, const char *value)
{
struct client_arguments *args = (struct client_arguments *)option->data;
......
......@@ -53,6 +53,10 @@ extern "C" {
"Set environment variables", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "env-file", 0, &(cmdargs).custom_conf.env_file, \
"Read in a file of environment variables", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "label", 'l', &(cmdargs).custom_conf.label, \
"Set metadata on container (default [])", command_append_array }, \
{ CMD_OPT_TYPE_CALLBACK, false, "label-file", 0, &(cmdargs).custom_conf.label_file, \
"Read in a line delimited file of labels (default [])", command_append_array }, \
{ CMD_OPT_TYPE_STRING_DUP, false, "entrypoint", 0, &(cmdargs).custom_conf.entrypoint, \
"Entrypoint to run when starting the container", NULL }, \
{ CMD_OPT_TYPE_STRING, false, "external-rootfs", 0, &(cmdargs).external_rootfs, \
......
......@@ -37,6 +37,9 @@ typedef struct isula_container_config {
char **env;
size_t env_len;
char **label;
size_t label_len;
char *hostname;
char *user;
......
......@@ -1928,6 +1928,72 @@ out:
return ret;
}
static int get_label_key_value(const char *label, char **key, char **value)
{
int ret = 0;
char **arr = util_string_split_n(label, '=', 2);
if (arr == NULL) {
ERROR("Failed to split input label");
ret = -1;
goto out;
}
*key = util_strdup_s(arr[0]);
if (util_array_len((const char **)arr) == 1) {
*value = util_strdup_s("");
} else {
*value = util_strdup_s(arr[1]);
}
out:
util_free_array(arr);
return ret;
}
static int pack_container_custom_config_labels(container_config *container_spec,
const isula_container_config_t *custom_conf)
{
int ret = 0;
int i;
char *key = NULL;
char *value = NULL;
if (custom_conf->label_len == 0 || custom_conf->label == NULL) {
return 0;
}
/* labels */
container_spec->labels = util_common_calloc_s(sizeof(json_map_string_string));
if (container_spec->labels == NULL) {
ERROR("Out of memory");
ret = -1;
goto out;
}
for (i = 0; i < custom_conf->label_len; i++) {
if (get_label_key_value(custom_conf->label[i], &key, &value) != 0) {
ERROR("Failed to get key and value of label");
ret = -1;
goto out;
}
if (append_json_map_string_string(container_spec->labels, key, value)) {
ERROR("Append map failed");
ret = -1;
goto out;
}
free(key);
key = NULL;
free(value);
value = NULL;
}
out:
free(key);
free(value);
return ret;
}
static bool have_health_check(const isula_container_config_t *custom_conf)
{
bool have_health_settings = false;
......@@ -2087,6 +2153,11 @@ static int pack_container_custom_config_pre(container_config *container_spec,
goto out;
}
ret = pack_container_custom_config_labels(container_spec, custom_conf);
if (ret != 0) {
goto out;
}
ret = pack_container_custom_config_health(container_spec, custom_conf);
if (ret != 0) {
goto out;
......
......@@ -52,5 +52,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz)
......@@ -51,4 +51,4 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz)
......@@ -41,7 +41,7 @@ public:
TEST_F(IsuladShimUnitTest, test_new_process)
{
string id="aaaabbbbccccdddd";
string id = "aaaabbbbccccdddd";
string bundle = "/home/isulad/bundle";
string runtime = "kata-runtime";
......@@ -77,7 +77,7 @@ TEST_F(IsuladShimUnitTest, test_read_write_nointr)
EXPECT_EQ(read_nointr(0, NULL, 32), -1);
EXPECT_EQ(read_nointr(1, NULL, 32), -1);
fd_wr = open_no_inherit(test_file.c_str(), O_CREAT | O_RDWR | O_APPEND | O_SYNC , 0640);
fd_wr = open_no_inherit(test_file.c_str(), O_CREAT | O_RDWR | O_APPEND | O_SYNC, 0640);
EXPECT_GT(fd_wr, 0);
nwrite = write_nointr(fd_wr, test_string.c_str(), 5);
EXPECT_EQ(nwrite, 5);
......
......@@ -79,4 +79,4 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks
${CMAKE_BINARY_DIR}/json
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册