oci_config_merge.c 8.5 KB
Newer Older
O
overweight 已提交
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
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
 * iSulad licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Author: lifeng
 * Create: 2018-11-08
 * Description: provide oci config merge functions
 ******************************************************************************/

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include "oci_config_merge.h"
#include <fcntl.h>              /* Obtain O_* constant definitions */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "securec.h"
#include "utils.h"
#include "log.h"
#include "liblcrd.h"
#include "specs_mount.h"
#include "specs_extend.h"

static void oci_image_merge_working_dir(const char *working_dir, oci_runtime_spec *oci_spec)
{
    if (working_dir == NULL) {
        return;
    }

    free(oci_spec->process->cwd);
    oci_spec->process->cwd = util_strdup_s(working_dir);
}

static int oci_image_merge_env(const oci_image_spec_config *config, oci_runtime_spec *oci_spec)
{
    if (config->env == NULL || config->env_len == 0) {
        return 0;
    }
    if (merge_env(oci_spec, (const char **)config->env, config->env_len) != 0) {
        ERROR("Failed to merge environment variables");
        return -1;
    }

    return 0;
}

static int do_duplicate_commands(const oci_image_spec_config *config, container_custom_config *custom_spec)
{
    size_t i;

    if (custom_spec->cmd_len != 0 || config->cmd_len == 0) {
        return 0;
    }

    if (config->cmd_len > SIZE_MAX / sizeof(char *)) {
        ERROR("too many commands!");
        return -1;
    }

    custom_spec->cmd = (char **)util_common_calloc_s(sizeof(char *) * config->cmd_len);
    if (custom_spec->cmd == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    for (i = 0; i < config->cmd_len; i++) {
        custom_spec->cmd[i] = util_strdup_s(config->cmd[i]);
        custom_spec->cmd_len++;
    }

    return 0;
}

static int do_duplicate_entrypoints(const oci_image_spec_config *config, container_custom_config *custom_spec)
{
    size_t i;

    if (config->entrypoint_len == 0) {
        return 0;
    }

    if (config->entrypoint_len > SIZE_MAX / sizeof(char *)) {
        ERROR("too many entrypoints!");
        return -1;
    }

    custom_spec->entrypoint = (char **)util_common_calloc_s(sizeof(char *) * config->entrypoint_len);
    if (custom_spec->entrypoint == NULL) {
        ERROR("Out of memory");
        return -1;
    }

    for (i = 0; i < config->entrypoint_len; i++) {
        custom_spec->entrypoint[i] = util_strdup_s(config->entrypoint[i]);
        custom_spec->entrypoint_len++;
    }

    return 0;
}

static int oci_image_merge_entrypoint(const oci_image_spec_config *config, container_custom_config *custom_spec)
{
    if (custom_spec->entrypoint_len != 0) {
        return 0;
    }

    if (do_duplicate_commands(config, custom_spec) != 0) {
        return -1;
    }

    if (do_duplicate_entrypoints(config, custom_spec) != 0) {
        return -1;
    }

    return 0;
}

static void oci_image_merge_user(const char *user, container_custom_config *custom_spec)
{
    if (custom_spec->user != NULL) {
        return;
    }

    custom_spec->user = util_strdup_s(user);
}

static int oci_image_merge_volumes(const oci_image_spec_config *config, oci_runtime_spec *oci_spec)
{
    int ret;

    if (config->volumes == NULL) {
        return 0;
    }
    ret = merge_volumes(oci_spec, config->volumes->keys, config->volumes->len, NULL, parse_volume);
    if (ret != 0) {
        ERROR("Failed to merge volumes");
        return -1;
    }

    return 0;
}

static int dup_health_check_from_image(const defs_health_check *image_health_check,
                                       container_custom_config *custom_spec)
{
    int ret = 0;
    size_t i;
    defs_health_check *health_check = (defs_health_check *)util_common_calloc_s(sizeof(defs_health_check));
    if (health_check == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    if (image_health_check->test_len > SIZE_MAX / sizeof(char *)) {
        ERROR("invalid health check commands!");
        ret = -1;
        goto out;
    }

    health_check->test = util_common_calloc_s(sizeof(char *) * image_health_check->test_len);
    if (health_check->test == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }

    for (i = 0; i < image_health_check->test_len; i++) {
        health_check->test[i] = util_strdup_s(image_health_check->test[i]);
        health_check->test_len++;
    }
    health_check->interval = image_health_check->interval;
    health_check->timeout = image_health_check->timeout;
    health_check->start_period = image_health_check->start_period;
    health_check->retries = image_health_check->retries;
    health_check->exit_on_unhealthy = image_health_check->exit_on_unhealthy;

    custom_spec->health_check = health_check;

    health_check = NULL;

out:
    free_defs_health_check(health_check);
    return ret;
}

static int update_health_check_from_image(const defs_health_check *image_health_check,
                                          container_custom_config *custom_spec)
{
    if (custom_spec->health_check->test_len == 0) {
        size_t i;

        if (image_health_check->test_len > SIZE_MAX / sizeof(char *)) {
            ERROR("invalid health check commands!");
            return -1;
        }
        custom_spec->health_check->test = util_common_calloc_s(sizeof(char *) * image_health_check->test_len);
        if (custom_spec->health_check->test == NULL) {
            ERROR("Out of memory");
            return -1;
        }
        for (i = 0; i < image_health_check->test_len; i++) {
            custom_spec->health_check->test[i] = util_strdup_s(image_health_check->test[i]);
            custom_spec->health_check->test_len++;
        }
    }
    if (custom_spec->health_check->interval == 0) {
        custom_spec->health_check->interval = image_health_check->interval;
    }
    if (custom_spec->health_check->timeout == 0) {
        custom_spec->health_check->timeout = image_health_check->timeout;
    }
    if (custom_spec->health_check->start_period == 0) {
        custom_spec->health_check->start_period = image_health_check->start_period;
    }
    if (custom_spec->health_check->retries == 0) {
        custom_spec->health_check->retries = image_health_check->retries;
    }

    return 0;
}

static int oci_image_merge_health_check(const defs_health_check *image_health_check,
                                        container_custom_config *custom_spec)
{
    int ret = 0;

    if (image_health_check == NULL) {
        return 0;
    }

    if (image_health_check->test_len == 0) {
        ERROR("health check commands required");
        return -1;
    }

    if (custom_spec->health_check == NULL) {
        if (dup_health_check_from_image(image_health_check, custom_spec) != 0) {
            ret = -1;
            goto out;
        }
    } else {
        if (update_health_check_from_image(image_health_check, custom_spec) != 0) {
            ret = -1;
            goto out;
        }
    }

out:
    return ret;
}

int oci_image_merge_config(imagetool_image *image_conf, oci_runtime_spec *oci_spec,
                           container_custom_config *custom_spec)
{
    int ret = 0;

Z
zhuchunyi 已提交
262
    if (image_conf == NULL || oci_spec == NULL || custom_spec == NULL) {
O
overweight 已提交
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
        ERROR("Invalid input arguments");
        return -1;
    }

    if (image_conf->spec != NULL && image_conf->spec->config != NULL) {
        oci_image_merge_working_dir(image_conf->spec->config->working_dir, oci_spec);

        if (oci_image_merge_env(image_conf->spec->config, oci_spec) != 0) {
            ret = -1;
            goto out;
        }

        if (oci_image_merge_entrypoint(image_conf->spec->config, custom_spec) != 0) {
            ret = -1;
            goto out;
        }

        oci_image_merge_user(image_conf->spec->config->user, custom_spec);

        if (oci_image_merge_volumes(image_conf->spec->config, oci_spec) != 0) {
            ret = -1;
            goto out;
        }
    }

    if (oci_image_merge_health_check(image_conf->healthcheck, custom_spec) != 0) {
        ret = -1;
        goto out;
    }

out:
    return ret;
}