Skip to content

Commit

Permalink
refactor(tools): update references from kong.tools.utils to `kong.t…
Browse files Browse the repository at this point in the history
…ools.ip` (#13108)

KAG-3148
  • Loading branch information
Water-Melon authored May 30, 2024
1 parent 3e05a9b commit ffb5ce1
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 99 deletions.
4 changes: 2 additions & 2 deletions kong/cache/warmup.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require "kong.tools.utils"
local hostname_type = require("kong.tools.ip").hostname_type
local constants = require "kong.constants"
local buffer = require "string.buffer"
local acl_groups
Expand Down Expand Up @@ -131,7 +131,7 @@ function cache_warmup.single_dao(dao)
end

if entity_name == "services" then
if utils.hostname_type(entity.host) == "name"
if hostname_type(entity.host) == "name"
and hosts_set[entity.host] == nil then
host_count = host_count + 1
hosts_array[host_count] = entity.host
Expand Down
6 changes: 3 additions & 3 deletions kong/conf_loader/listeners.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pl_stringx = require "pl.stringx"
local utils = require "kong.tools.utils"
local tools_ip = require "kong.tools.ip"


local type = type
Expand Down Expand Up @@ -109,14 +109,14 @@ local function parse_listeners(values, flags)
-- verify IP for remainder
local ip

if utils.hostname_type(remainder) == "name" then
if tools_ip.hostname_type(remainder) == "name" then
-- it's not an IP address, so a name/wildcard/regex
ip = {}
ip.host, ip.port = remainder:match("(.+):([%d]+)$")

else
-- It's an IPv4 or IPv6, normalize it
ip = utils.normalize_ip(remainder)
ip = tools_ip.normalize_ip(remainder)
-- nginx requires brackets in IPv6 addresses, but normalize_ip does
-- not include them (due to backwards compatibility with its other uses)
if ip and ip.type == "ipv6" then
Expand Down
12 changes: 6 additions & 6 deletions kong/db/dao/targets.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local balancer = require "kong.runloop.balancer"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local workspaces = require "kong.workspaces"
local tools_ip = require "kong.tools.ip"


local setmetatable = setmetatable
Expand Down Expand Up @@ -29,11 +29,11 @@ end


local function format_target(target)
local p = utils.normalize_ip(target)
local p = tools_ip.normalize_ip(target)
if not p then
return false, "Invalid target; not a valid hostname or ip address"
end
return utils.format_host(p, DEFAULT_PORT)
return tools_ip.format_host(p, DEFAULT_PORT)
end


Expand Down Expand Up @@ -296,13 +296,13 @@ end

function _TARGETS:post_health(upstream_pk, target, address, is_healthy)
local upstream = balancer.get_upstream_by_id(upstream_pk.id)
local host_addr = utils.normalize_ip(target.target)
local hostname = utils.format_host(host_addr.host)
local host_addr = tools_ip.normalize_ip(target.target)
local hostname = tools_ip.format_host(host_addr.host)
local ip
local port

if address ~= nil then
local addr = utils.normalize_ip(address)
local addr = tools_ip.normalize_ip(address)
ip = addr.host
if addr.port then
port = addr.port
Expand Down
3 changes: 2 additions & 1 deletion kong/db/schema/entities/targets.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
local typedefs = require "kong.db.schema.typedefs"
local utils = require "kong.tools.utils"
local normalize_ip = require("kong.tools.ip").normalize_ip


local function validate_target(target)
local p = utils.normalize_ip(target)
local p = normalize_ip(target)
if not p then
local ok = utils.validate_utf8(target)
if not ok then
Expand Down
3 changes: 2 additions & 1 deletion kong/db/schema/entities/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local Schema = require "kong.db.schema"
local typedefs = require "kong.db.schema.typedefs"
local utils = require "kong.tools.utils"
local normalize_ip = require("kong.tools.ip").normalize_ip
local null = ngx.null


Expand All @@ -15,7 +16,7 @@ end


local validate_name = function(name)
local p = utils.normalize_ip(name)
local p = normalize_ip(name)
if not p then
return nil, get_name_for_error(name) .. "; must be a valid hostname"
end
Expand Down
15 changes: 8 additions & 7 deletions kong/db/schema/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local openssl_x509 = require "resty.openssl.x509"
local Schema = require "kong.db.schema"
local socket_url = require "socket.url"
local constants = require "kong.constants"
local tools_ip = require "kong.tools.ip"


local DAO_MAX_TTL = constants.DATABASE.DAO_MAX_TTL
Expand All @@ -20,7 +21,7 @@ local type = type


local function validate_host(host)
local res, err_or_port = utils.normalize_ip(host)
local res, err_or_port = tools_ip.normalize_ip(host)
if type(err_or_port) == "string" and err_or_port ~= "invalid port number" then
return nil, "invalid value: " .. host
end
Expand All @@ -34,13 +35,13 @@ end


local function validate_host_with_optional_port(host)
local res, err_or_port = utils.normalize_ip(host)
local res, err_or_port = tools_ip.normalize_ip(host)
return (res and true or nil), err_or_port
end


local function validate_ip(ip)
if utils.is_valid_ip(ip) then
if tools_ip.is_valid_ip(ip) then
return true
end

Expand All @@ -49,7 +50,7 @@ end


local function validate_ip_or_cidr(ip_or_cidr)
if utils.is_valid_ip_or_cidr(ip_or_cidr) then
if tools_ip.is_valid_ip_or_cidr(ip_or_cidr) then
return true
end

Expand All @@ -58,7 +59,7 @@ end


local function validate_ip_or_cidr_v4(ip_or_cidr_v4)
if utils.is_valid_ip_or_cidr_v4(ip_or_cidr_v4) then
if tools_ip.is_valid_ip_or_cidr_v4(ip_or_cidr_v4) then
return true
end

Expand Down Expand Up @@ -150,7 +151,7 @@ end


local function validate_sni(host)
local res, err_or_port = utils.normalize_ip(host)
local res, err_or_port = tools_ip.normalize_ip(host)
if type(err_or_port) == "string" and err_or_port ~= "invalid port number" then
return nil, "invalid value: " .. host
end
Expand Down Expand Up @@ -183,7 +184,7 @@ local function validate_wildcard_host(host)
host = mock_host
end

local res, err_or_port = utils.normalize_ip(host)
local res, err_or_port = tools_ip.normalize_ip(host)
if type(err_or_port) == "string" and err_or_port ~= "invalid port number" then
return nil, "invalid value: " .. host
end
Expand Down
4 changes: 2 additions & 2 deletions kong/pdk/ip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-- See the [documentation on trusted IPs](https://docs.konghq.com/gateway/latest/reference/configuration/#trusted_ips).
--
-- @module kong.ip
local utils = require "kong.tools.utils"
local is_valid_ip_or_cidr = require("kong.tools.ip").is_valid_ip_or_cidr
local ipmatcher = require "resty.ipmatcher"

---
Expand Down Expand Up @@ -47,7 +47,7 @@ local function new(self)
for i = 1, n_ips do
local address = ips[i]

if utils.is_valid_ip_or_cidr(address) then
if is_valid_ip_or_cidr(address) then
trusted_ips[idx] = address
idx = idx + 1

Expand Down
2 changes: 1 addition & 1 deletion kong/router/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local constants = require("kong.constants")
local hostname_type = require("kong.tools.utils").hostname_type
local hostname_type = require("kong.tools.ip").hostname_type
local normalize = require("kong.tools.uri").normalize


Expand Down
4 changes: 2 additions & 2 deletions kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pl_tablex = require "pl.tablex"
local utils = require "kong.tools.utils"
local hostname_type = require("kong.tools.ip").hostname_type
local hooks = require "kong.hooks"
local recreate_request = require("ngx.balancer").recreate_request
local uuid = require("kong.tools.uuid").uuid
Expand Down Expand Up @@ -412,7 +412,7 @@ local function post_health(upstream, hostname, ip, port, is_healthy)
end

local ok, err
if ip and (utils.hostname_type(ip) ~= "name") then
if ip and (hostname_type(ip) ~= "name") then
ok, err = healthchecker:set_target_status(ip, port, hostname, is_healthy)
else
ok, err = healthchecker:set_all_target_statuses_for_hostname(hostname, port, is_healthy)
Expand Down
Loading

1 comment on commit ffb5ce1

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:ffb5ce1c1d089ad81cec00ddaf4372a179264269
Artifacts available https://github.com/Kong/kong/actions/runs/9298687053

Please sign in to comment.