Skip to content

Commit

Permalink
Merge pull request #9702 from stubenhuang/issue-gw-k8s-special-url-9681
Browse files Browse the repository at this point in the history
feat: 容器化特殊域名处理 #9681
  • Loading branch information
irwinsun authored Nov 21, 2023
2 parents 4dccd04 + 2aebcd3 commit 0bfddd6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/gateway/core/lua/util/loadbalance_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
ngx.var.devops_token = config.gw_token
end
local kubernetes_domain = nil

if gateway_project == 'codecc' then
kubernetes_domain = config.kubernetes.codecc.domain
else
kubernetes_domain = config.kubernetes.domain
end

-- 特殊处理的域名,优先级最高
local special_key = gateway_project .. ":" .. devops_tag
if config.kubernetes.special_domain[special_key] ~= nil then
kubernetes_domain = config.kubernetes.special_domain[special_key]
end
return kubernetes_domain .. "/ms/" .. service_name
end

-- 容器环境
if in_container then
if ngx.var.multi_cluster == 'true' then -- 多集群场景
local dns = resolver:new{
local dns = resolver:new {
nameservers = resolvUtil.nameservers,
retrans = 5,
timeout = 2000 -- 2 sec
Expand All @@ -66,7 +73,7 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
prefix = service_name .. '-' .. ngx.var.chart_name .. '-' .. service_name
end
local domain = prefix .. '.' .. devops_tag .. '.svc.cluster.local'
local records = dns:query(domain, {qtype = dns.TYPE_A})
local records = dns:query(domain, { qtype = dns.TYPE_A })
-- 兜底策略
if ngx.var.default_namespace ~= '' and ngx.var.default_namespace ~= nil and not records then
domain = prefix .. ngx.var.default_namespace .. '.svc.cluster.local'
Expand All @@ -75,14 +82,14 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
else -- 单一集群场景
return
ngx.var.release_name .. '-' .. ngx.var.chart_name .. '-' .. service_name .. '.' .. ngx.var.namespace ..
'.svc.cluster.local'
'.svc.cluster.local'
end
end

-- 获取consul查询域名
local query_subdomain = devops_tag .. "." .. service_name .. ns_config.suffix .. ".service." .. ns_config.domain

local ips = {} -- address
local ips = {} -- address
local port = nil -- port

local router_srv_key = query_subdomain .. cache_tail
Expand All @@ -94,22 +101,22 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
local dnsIps = {}
if type(ns_config.ip) == 'table' then
for i, v in ipairs(ns_config.ip) do
table.insert(dnsIps, {v, ns_config.port})
table.insert(dnsIps, { v, ns_config.port })
end
else
table.insert(dnsIps, {ns_config.ip, ns_config.port})
table.insert(dnsIps, { ns_config.ip, ns_config.port })
end

-- 连接consul dns
local dns, err = resolver:new{nameservers = dnsIps, retrans = 5, timeout = 2000}
local dns, err = resolver:new { nameservers = dnsIps, retrans = 5, timeout = 2000 }

if not dns then
ngx.log(ngx.ERR, "failed to instantiate the resolver: ", err)
return nil
end

-- 查询dns
local records, err = dns:query(query_subdomain, {qtype = dns.TYPE_SRV, additional_section = true})
local records, err = dns:query(query_subdomain, { qtype = dns.TYPE_SRV, additional_section = true })

if not records then
ngx.log(ngx.ERR, "failed to query the DNS server: ", err)
Expand All @@ -119,7 +126,7 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
if records.errcode then
if records.errcode == 3 then
ngx.log(ngx.ERR, "DNS error code #" .. records.errcode .. ": ", records.errstr, " , query_subdomain : ",
query_subdomain)
query_subdomain)
return nil
else
ngx.log(ngx.ERR, "DNS error #" .. records.errcode .. ": ", err, " , query_subdomain : ", query_subdomain)
Expand All @@ -146,7 +153,6 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)

-- set cache
router_srv_cache:set(router_srv_key, table.concat(ips, ",") .. ":" .. port, 1)

else
local func_itor = string.gmatch(router_srv_value, "([^:]+)")
local ips_str = func_itor()
Expand All @@ -155,7 +161,6 @@ function _M:getTarget(devops_tag, service_name, cache_tail, ns_config)
for ip in string.gmatch(ips_str, "([^,]+)") do
table.insert(ips, ip)
end

end

return ips[math.random(#ips)] .. ":" .. port
Expand Down
3 changes: 2 additions & 1 deletion support-files/templates/gateway#core#lua#init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ config = {
host = "kubernetes.demo.com",
port = 6443 ,
token = ""
}
},
special_domain={}
}
}

Expand Down

0 comments on commit 0bfddd6

Please sign in to comment.