Skip to content
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

feat(core) make service on routes optional (transparent proxying) #4286

Merged
merged 3 commits into from
Mar 11, 2019
Merged
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
2 changes: 1 addition & 1 deletion kong/db/schema/entities/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ return {
},
}, },
{ tags = typedefs.tags },
{ service = { type = "foreign", reference = "services", required = true }, },
{ service = { type = "foreign", reference = "services" }, },
},

entity_checks = {
Expand Down
88 changes: 54 additions & 34 deletions kong/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ local bit = require "bit"
local hostname_type = utils.hostname_type
local re_match = ngx.re.match
local re_find = ngx.re.find
local null = ngx.null
local insert = table.insert
local sort = table.sort
local upper = string.upper
local lower = string.lower
local find = string.find
local sub = string.sub
local tonumber = tonumber
local ipairs = ipairs
local pairs = pairs
local error = error
Expand Down Expand Up @@ -68,6 +68,9 @@ local MATCH_RULES = {
}


local EMPTY_T = {}


local match_route
local reduce

Expand All @@ -89,18 +92,22 @@ local protocol_subsystem = {
}

local function marshall_route(r)
local route = r.route or null
local service = r.service or null
local headers = r.headers or null
local paths = route.paths or null
local methods = route.methods or null
local protocol = service.protocol or null
local sources = route.sources or null
local destinations = route.destinations or null
local snis = route.snis or null

if not (headers ~= null or methods ~= null or paths ~= null or
sources ~= null or destinations ~= null or snis ~= null) then
local route = r.route
local service = r.service
local headers = r.headers
local paths = route.paths
local methods = route.methods
local snis = route.snis
local sources = route.sources
local destinations = route.destinations


local protocol
if service then
protocol = service.protocol
end

if not (headers or methods or paths or snis or sources or destinations) then
return nil, "could not categorize route"
end

Expand All @@ -125,7 +132,7 @@ local function marshall_route(r)
-- headers


if headers ~= null then
if headers then
if type(headers) ~= "table" then
return nil, "headers field must be a table"
end
Expand Down Expand Up @@ -174,7 +181,7 @@ local function marshall_route(r)
-- paths


if paths ~= null then
if paths then
if type(paths) ~= "table" then
return nil, "paths field must be a table"
end
Expand Down Expand Up @@ -219,7 +226,7 @@ local function marshall_route(r)
-- methods


if methods ~= null then
if methods then
if type(methods) ~= "table" then
return nil, "methods field must be a table"
end
Expand All @@ -238,7 +245,7 @@ local function marshall_route(r)
-- sources


if sources ~= null then
if sources then
if type(sources) ~= "table" then
return nil, "sources field must be a table"
end
Expand Down Expand Up @@ -271,7 +278,7 @@ local function marshall_route(r)
-- destinations


if destinations ~= null then
if destinations then
if type(destinations) ~= "table" then
return nil, "destinations field must be a table"
end
Expand Down Expand Up @@ -304,7 +311,7 @@ local function marshall_route(r)
-- snis


if snis ~= null then
if snis then
if type(snis) ~= "table" then
return nil, "snis field must be a table"
end
Expand All @@ -323,25 +330,26 @@ local function marshall_route(r)
end


if protocol ~= null then
route_t.upstream_url_t.scheme = protocol
end
-- upstream_url parsing


-- upstream_url parsing
if protocol then
route_t.upstream_url_t.scheme = protocol
end

local s = service or EMPTY_T

local host = service.host or null
if host ~= null then
local host = s.host
if host then
route_t.upstream_url_t.host = host
route_t.upstream_url_t.type = hostname_type(host)

else
route_t.upstream_url_t.type = hostname_type("")
end

local port = service.port or null
if port ~= null then
local port = s.port
if port then
route_t.upstream_url_t.port = port

else
Expand All @@ -354,7 +362,7 @@ local function marshall_route(r)
end

if route_t.type == "http" then
route_t.upstream_url_t.path = service.path or "/"
route_t.upstream_url_t.path = s.path or "/"
end

return route_t
Expand Down Expand Up @@ -1127,14 +1135,13 @@ function _M.new(routes)
local upstream_url_t = matched_route.upstream_url_t
local matches = ctx.matches


-- Path construction

if matched_route.type == "http" then
-- if we do not have a path-match, then the postfix is simply the
-- incoming path, without the initial slash
local request_postfix = matches.uri_postfix or sub(req_uri, 2, -1)
local upstream_base = upstream_url_t.path
local upstream_base = upstream_url_t.path or "/"

if matched_route.strip_uri then
-- we drop the matched part, replacing it with the upstream path
Expand Down Expand Up @@ -1227,11 +1234,24 @@ function _M.new(routes)
-- debug HTTP request header logic

if ngx.var.http_kong_debug then
ngx.header["Kong-Route-Id"] = match_t.route.id
ngx.header["Kong-Service-Id"] = match_t.service.id
if match_t.route then
if match_t.route.id then
ngx.header["Kong-Route-Id"] = match_t.route.id
end

if match_t.service.name then
ngx.header["Kong-Service-Name"] = match_t.service.name
if match_t.route.name then
ngx.header["Kong-Route-Name"] = match_t.route.name
end
end

if match_t.service then
if match_t.service.id then
ngx.header["Kong-Service-Id"] = match_t.service.id
end

if match_t.service.name then
bungle marked this conversation as resolved.
Show resolved Hide resolved
ngx.header["Kong-Service-Name"] = match_t.service.name
end
end
end

Expand Down
Loading