Skip to content

Commit 76e2f69

Browse files
authored
Lua: Fix ExternalName services without endpoints. (#13154)
1 parent 66c248a commit 76e2f69

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

rootfs/etc/nginx/lua/balancer.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ local function get_implementation(backend)
7373
end
7474

7575
local function resolve_external_names(original_backend)
76+
if not original_backend.endpoints or
77+
#original_backend.endpoints == 0 then
78+
return original_backend
79+
end
7680
local backend = util.deepcopy(original_backend)
7781
local endpoints = {}
7882
for _, endpoint in ipairs(backend.endpoints) do

rootfs/etc/nginx/lua/test/balancer_test.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,40 @@ describe("Balancer", function()
439439
assert.stub(mock_instance.sync).was_called_with(mock_instance, expected_backend)
440440
end)
441441

442+
it("sets balancer to nil when service is of type External name and DNS could not resolve", function()
443+
backend = {
444+
name = "example2-com", service = { spec = { ["type"] = "ExternalName" } },
445+
endpoints = {
446+
{ address = "example2.com", port = "80", maxFails = 0, failTimeout = 0 }
447+
}
448+
}
449+
450+
helpers.mock_resty_dns_query(nil, { errcode = 3, errstr = "NXDNS: no such host (mock)" })
451+
local mock_instance = { sync = function(backend) end }
452+
setmetatable(mock_instance, implementation)
453+
implementation.new = function(self, backend) return mock_instance end
454+
local s = spy.on(implementation, "new")
455+
assert.has_no.errors(function() balancer.sync_backend(backend) end)
456+
assert.spy(s).was_not_called()
457+
assert.is_nil(balancer.get_balancer_by_upstream_name(backend.name))
458+
end)
459+
460+
it("sets balancer to nil when service is of type External name and endpoints in nil (omitted by go calling POST /configuration/backends)", function()
461+
backend = {
462+
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
463+
endpoints = nil
464+
}
465+
466+
helpers.mock_resty_dns_query(nil, { errcode = 3, errstr = "NXDNS: no such host (mock)" })
467+
local mock_instance = { sync = function(backend) end }
468+
setmetatable(mock_instance, implementation)
469+
implementation.new = function(self, backend) return mock_instance end
470+
local s = spy.on(implementation, "new")
471+
assert.has_no.errors(function() balancer.sync_backend(backend) end)
472+
assert.spy(s).was_not_called()
473+
assert.is_nil(balancer.get_balancer_by_upstream_name(backend.name))
474+
end)
475+
442476
it("wraps IPv6 addresses into square brackets", function()
443477
local backend = {
444478
name = "example-com",

0 commit comments

Comments
 (0)