Skip to content

Lua: Fix ExternalName services without endpoints. #13154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions rootfs/etc/nginx/lua/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,19 @@ local function is_backend_with_external_name(backend)
end

local function sync_backend(backend)
-- We resolve external names before checking if the endpoints are empty
-- because the behavior for resolve_external_names when the name was not
-- resolved is to return an empty table so we set the balancer to nil below
-- see https://github.com/kubernetes/ingress-nginx/pull/10989
if not backend.endpoints or #backend.endpoints == 0 then
balancers[backend.name] = nil
return
end

if is_backend_with_external_name(backend) then
backend = resolve_external_names(backend)
end

-- We check if the endpoints are empty after resolving the external names
-- because the behavior for resolve_external_names when the name was not
-- resolved is to return an empty table so we set the balancer to nil below
-- see https://github.com/kubernetes/ingress-nginx/pull/10989
if not backend.endpoints or #backend.endpoints == 0 then
balancers[backend.name] = nil
return
Expand Down
34 changes: 34 additions & 0 deletions rootfs/etc/nginx/lua/test/balancer_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,40 @@ describe("Balancer", function()
assert.stub(mock_instance.sync).was_called_with(mock_instance, expected_backend)
end)

it("sets balancer to nil when service is of type External name and DNS could not resolve", function()
backend = {
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
endpoints = {
{ address = "example.com", port = "80", maxFails = 0, failTimeout = 0 }
}
}

helpers.mock_resty_dns_query("example.com", {}, "Mock was instructed to return error")
local mock_instance = { sync = function(backend) end }
setmetatable(mock_instance, implementation)
implementation.new = function(self, backend) return mock_instance end
local s = spy.on(implementation, "new")
assert.has_no.errors(function() balancer.sync_backend(backend) end)
assert.spy(s).was_not_called()
assert.is_nil(balancer.get_balancer_by_upstream_name(backend.name))
end)

it("sets balancer to nil when service is of type External name and endpoints in nil (omitted by go calling POST /configuration/backends)", function()
backend = {
name = "example-com", service = { spec = { ["type"] = "ExternalName" } },
endpoints = nil
}

helpers.mock_resty_dns_query("example.com", {}, "Mock was instructed to return error")
local mock_instance = { sync = function(backend) end }
setmetatable(mock_instance, implementation)
implementation.new = function(self, backend) return mock_instance end
local s = spy.on(implementation, "new")
assert.has_no.errors(function() balancer.sync_backend(backend) end)
assert.spy(s).was_not_called()
assert.is_nil(balancer.get_balancer_by_upstream_name(backend.name))
end)

it("wraps IPv6 addresses into square brackets", function()
local backend = {
name = "example-com",
Expand Down
Loading