apisix icon indicating copy to clipboard operation
apisix copied to clipboard

feat: support environment variables in config-default.yaml

Open flearc opened this issue 1 year ago • 3 comments

Description

Comment in config.yaml shows that APISIX support use environment variables in yaml

# To configure via environment variables, you can use `${{VAR}}` syntax. For instance:
#
# deployment:
#   role: traditional
#   role_traditional:
#     config_provider: etcd
#   etcd:
#     host:
#       - http://${{ETCD_HOST}}:2379

But in config-defautl.yaml, it does not work.

-- apisix/cli/file.lua

    local default_conf = yaml.load(default_conf_yaml)
    if not default_conf then
        return nil, "invalid config-default.yaml file"
    end

    local_conf_path = profile:customized_yaml_path()
    if not local_conf_path then
        local_conf_path = profile:yaml_path("config")
    end
    local user_conf_yaml, err = util.read_file(local_conf_path)
    if not user_conf_yaml then
        return nil, err
    end

    local is_empty_file = true
    for line in str_gmatch(user_conf_yaml .. '\n', '(.-)\r?\n') do
        if not is_empty_yaml_line(line) then
            is_empty_file = false
            break
        end
    end

    if not is_empty_file then
        local user_conf = yaml.load(user_conf_yaml)
        if not user_conf then
            return nil, "invalid config.yaml file"
        end

        local ok, err = resolve_conf_var(user_conf)
        if not ok then
            return nil, err
        end

        ok, err = merge_conf(default_conf, user_conf)
        if not ok then
            return nil, err
        end
    end

The ${{ VAR }} syntax is processed by resolve_conf_var, but it is only called for user_conf. To support it in config-default.yaml, run resolve_conf_var for default_conf as well.

flearc avatar Jun 07 '24 09:06 flearc

ping @bzp2010

nfrankel avatar Jun 09 '24 20:06 nfrankel

@flea1lt The issue is not completed. It hasn't been merged due to the implementation change of default-config.yaml and its implementation should change accordingly.

Please reopen.

nfrankel avatar Jun 13 '24 03:06 nfrankel

Thank you @flea1lt.

Once #11343 is merged, I'll rework this PR

nfrankel avatar Jun 13 '24 04:06 nfrankel

config-default.yaml is not meant to be modified by users. This is part of the reason why it was moved to a hardcoded lua file. Wondering why it needs to support env var? Can this issue be closed?

kayx23 avatar Aug 02 '24 05:08 kayx23

@kayx23 Since the default configuration has been converted to a hardcoded Lua file, this feature is no logger necessary.

flearc avatar Aug 02 '24 06:08 flearc