wrap IPv6 addresses into square brackets
This commit is contained in:
parent
4b07e73e5d
commit
589069d566
2 changed files with 40 additions and 1 deletions
|
@ -55,6 +55,18 @@ local function resolve_external_names(original_backend)
|
|||
return backend
|
||||
end
|
||||
|
||||
local function format_ipv6_endpoints(endpoints)
|
||||
local formatted_endpoints = {}
|
||||
for _, endpoint in ipairs(endpoints) do
|
||||
local formatted_endpoint = endpoint
|
||||
if not endpoint.address:match("^%d+.%d+.%d+.%d+$") then
|
||||
formatted_endpoint.address = string.format("[%s]", endpoint.address)
|
||||
end
|
||||
table.insert(formatted_endpoints, formatted_endpoint)
|
||||
end
|
||||
return formatted_endpoints
|
||||
end
|
||||
|
||||
local function sync_backend(backend)
|
||||
local implementation = get_implementation(backend)
|
||||
local balancer = balancers[backend.name]
|
||||
|
@ -79,6 +91,8 @@ local function sync_backend(backend)
|
|||
backend = resolve_external_names(backend)
|
||||
end
|
||||
|
||||
backend.endpoints = format_ipv6_endpoints(backend.endpoints)
|
||||
|
||||
balancer:sync(backend)
|
||||
end
|
||||
|
||||
|
@ -145,7 +159,7 @@ function _M.balance()
|
|||
|
||||
local ok, err = ngx_balancer.set_current_peer(peer)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "error while setting current upstream peer: " .. tostring(err))
|
||||
ngx.log(ngx.ERR, string.format("error while setting current upstream peer %s: %s", peer, err))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -115,6 +115,31 @@ describe("Balancer", function()
|
|||
assert.stub(mock_instance.sync).was_called_with(mock_instance, expected_backend)
|
||||
end)
|
||||
|
||||
it("wraps IPv6 addresses into square brackets", function()
|
||||
local backend = {
|
||||
name = "exmaple-com",
|
||||
endpoints = {
|
||||
{ address = "::1", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
{ address = "192.168.1.1", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
}
|
||||
}
|
||||
local expected_backend = {
|
||||
name = "exmaple-com",
|
||||
endpoints = {
|
||||
{ address = "[::1]", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
{ address = "192.168.1.1", port = "8080", maxFails = 0, failTimeout = 0 },
|
||||
}
|
||||
}
|
||||
|
||||
local mock_instance = { sync = function(backend) end }
|
||||
setmetatable(mock_instance, implementation)
|
||||
implementation.new = function(self, backend) return mock_instance end
|
||||
assert.has_no.errors(function() balancer.sync_backend(backend) end)
|
||||
stub(mock_instance, "sync")
|
||||
assert.has_no.errors(function() balancer.sync_backend(backend) end)
|
||||
assert.stub(mock_instance.sync).was_called_with(mock_instance, expected_backend)
|
||||
end)
|
||||
|
||||
it("replaces the existing balancer when load balancing config changes for backend", function()
|
||||
assert.has_no.errors(function() balancer.sync_backend(backend) end)
|
||||
|
||||
|
|
Loading…
Reference in a new issue