Skip to content

help request: how to mannually routing to another service base on some condition #12103

@darkSheep404

Description

@darkSheep404

Description

Dear apisix team, i want to base on some condition to routing to different service
when i use ctx.upstream_id = conf.upstream_id it will routing to new upstream
but when i use ctx.service_id = conf.service_id , it still routing to old version
Cause we have plugins on service level , routing to new service will be better than routing to a new upstream
May I know how can we mannually change service to annother service in plugin code ?

local core       = require("apisix.core")
local upstream   = require("apisix.upstream")
local schema_def = require("apisix.schema_def")
local expr       = require("resty.expr.v1")

local plugin_name = "custom-traffic-split"
local schema = {
    type = "object",
    properties = {
        upstream_id = schema_def.id_schema,
        service_id = schema_def.id_schema
    }
}

local _M = {
    version = 0.1,
    priority = 966,
    name = plugin_name,
    schema = schema
}

function _M.check_schema(conf)
    return core.schema.check(schema, conf)
end

function _M.access(conf, ctx)
    if not conf then
        return
    end

    --  
    local gary_release = core.request.header(ctx, "gary_version")
    if not gary_release then
        return
    end

    -- 
    if conf.upstream_id then
        ctx.upstream_id = conf.upstream_id
        return
    end


    if conf.service_id then
        ctx.service_id = conf.service_id 
        return
    end


   
end

return _M

Environment

  • APISIX version (run apisix version):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

Metadata

Metadata

Assignees

No one assigned

    Labels

    pluginquestionlabel for questions asked by users

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions