apisix icon indicating copy to clipboard operation
apisix copied to clipboard

request help: adminapi supports yaml as the configuration center

Open backnero opened this issue 6 years ago • 12 comments

Issue description

When the configuration center is yaml, can adminapi support the GET method to get the list and detailed information of routing, upstream, service, ssl, etc.

Only need to support get request

Environment

  • apisix version (cmd: apisix version):
  • OS:

backnero avatar Nov 26 '20 05:11 backnero

Admin API server only connects the etcd cluster, it could not know the dispatched yaml files.

spacewander avatar Nov 26 '20 05:11 spacewander

I now use yaml as the configuration center, and there is a requirement that I need to obtain routing, service, upstream lists, and routing id information through interfaces. Can it be expanded through plugin?

backnero avatar Nov 26 '20 05:11 backnero

I doubt if it can be expanded through plugin. As far as I know, people who use yaml store the original configuration in their internal system, and dispatch the generated yaml file via agents. Maybe you can write the API in your internal system.

spacewander avatar Nov 26 '20 06:11 spacewander

Hi, @backnero. I think your request help like get a YAML file content through adminapi? If this, I think you can write a plugin to support.

Besides, if you want to obtain routing, service, upstream lists, and routing id information through interfaces. You can also write a plugin to support.

starsz avatar Nov 26 '20 15:11 starsz

Hi, @backnero. I think your request help like get a YAML file content through adminapi? If this, I think you can write a plugin to support.

No. We can't do it because plugin can't add API to the admin API.

spacewander avatar Nov 27 '20 01:11 spacewander

Hi, @backnero. I think your request help like get a YAML file content through adminapi? If this, I think you can write a plugin to support.

No. We can't do it because plugin can't add API to the admin API.

Yeah. Can't add admin API, but we can add a route to return that info.

starsz avatar Nov 27 '20 02:11 starsz

Hi, @backnero. I think your request help like get a YAML file content through adminapi? If this, I think you can write a plugin to support.

No. We can't do it because plugin can't add API to the admin API.

Yeah. Can't add admin API, but we can add a route to return that info.

The route is added in the data panel, not in the admin API. So there is impossible to expose the info in admin API if you are not using etcd.

spacewander avatar Nov 27 '20 02:11 spacewander

As long as routing, service, upstream and other information can be obtained, because yaml is used as the configuration center, configuration addition, deletion and modification will all be performed in yaml.

-- function _M.api()
--     return {
--         {
--             methods = {"GET"},
--             uri = "/apisix/routers/",
--             handler = getrouters,
--         }
--     }
-- end

Maybe it can be similar to this, I don't know what method should be used to obtain routing information.

    local co_yaml = read_apisix_yaml()
    -- local config_data = yaml.parse(co_yaml)
    core.log.error("yaml:::::: ",core.json.delay_encode(co_yaml))

I try to use the method in core/conf_yaml.lua to get yaml information,

Can print out part of the yaml content, but don’t know how to continue

backnero avatar Nov 27 '20 02:11 backnero

It may be the problem I stated. For me, it doesn’t have to be adminapi. The interface released by the plug-in is also ok, just get the information

backnero avatar Nov 27 '20 02:11 backnero

@backnero You can take a look at how we do in https://github.com/apache/apisix/blob/f4161d39a495879edffbc75e2099f6e487bae81d/apisix/http/router/radixtree_uri.lua#L84

Don't forget to protect your route via https://github.com/apache/apisix/blob/master/doc/plugin-interceptors.md

I will close this issue as it is off topic now. Feel free to open a new one if needed.

spacewander avatar Nov 27 '20 03:11 spacewander

    local user_routes, err = core.config.new("/routes", {
        automatic = true,
        item_schema = core.schema.route,
        filter = filter,
    })

What is the reason user_routes.value does not exist

backnero avatar Nov 27 '20 06:11 backnero


local core     = require("apisix.core")
local ipairs   = ipairs
local ngx      = ngx
local ngx_time = ngx.time
local sub_str  = string.sub
local plugin_name = "dev"
local http     = require("resty.http")
local json     = require("apisix.core.json")
local type     = type
local string   = string
-- local router = require("apisix.router")
-- local get_routes = router.http_routes
-- local profile      = require("apisix.core.profile")
-- local lfs          = require("lfs")
-- local re_find      = ngx.re.find
local yaml         = require("tinyyaml")
-- local apisix_yaml_path = profile:yaml_path("apisix")
local loadstring = loadstring
local user_routes
local router = require("resty.radixtree")

local schema = {
    type = "object",
    properties = {
        },
}


local _M = {
    version = 0.1,
    priority = 70000,
    type = 'auth',
    name = plugin_name,
    schema = schema,
}

function _M.check_schema(conf)
    core.log.info("input conf: ", core.json.delay_encode(conf))

    local ok, err = core.schema.check(schema, conf)
    if not ok then
        return false, err
    end


    return true
end
local function fail_response(message, init_values)
    local response = init_values or {}
    response.message = message
    return response
end

local function success_response(message, init_values)
    local response = init_values or {}
    response.message = message
    return response
end

local function create_radixtree_router(routes)
    local uri_routes = {}
    local uri_router
    routes = routes or {}

    core.table.clear(uri_routes)

    for _, route in ipairs(routes) do
        core.log.error("route for: ", core.json.delay_encode(route, true))

        if type(route) == "table" then
            local filter_fun, err
            if route.value.filter_func then
                filter_fun, err = loadstring(
                                        "return " .. route.value.filter_func,
                                        "router#" .. route.value.id)
                if not filter_fun then
                    core.log.error("failed to load filter function: ", err,
                                   " route id: ", route.value.id)
                    goto CONTINUE
                end

                filter_fun = filter_fun()
            end

            core.log.error("insert uri route: ",
                          core.json.delay_encode(route.value))
            core.table.insert(uri_routes, {
                paths = route.value.uris or route.value.uri,
                methods = route.value.methods,
                priority = route.value.priority,
                hosts = route.value.hosts or route.value.host,
                remote_addrs = route.value.remote_addrs
                               or route.value.remote_addr,
                vars = route.value.vars,
                filter_fun = filter_fun,
                handler = function (api_ctx, match_opts)
                    api_ctx.matched_params = nil
                    api_ctx.matched_route = route
                    api_ctx.curr_req_matched = match_opts.matched
                end
            })

            ::CONTINUE::
        end
    end

    -- core.log.error("route items:2 ",core.json.uri_routes)
    uri_router = router.new(uri_routes)
    core.response.exit(200, success_response(nil, {routes = uri_routes}))


end
function get_routes()
    local user_routes, err = core.config.new("/routes", {
        automatic = true,
        item_schema = core.schema.route,
        filter = filter,
    })
    create_radixtree_router(user_routes.values)

    -- core.log.error("route items:1 ", core.json.uri_routes)


end


function _M.api()
    return {
        {
            methods = {"GET"},
            uri = "/apisix/plugin/routes",
            handler = get_routes,
        }
    }
end


return _M

response

{
    "routes": {}
}

backnero avatar Nov 27 '20 08:11 backnero