Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions gateway/src/apicast/policy_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ function _M:add_policy(name, version, ...)
end
end

local default_policy_order_check = PolicyOrderChecker.new(policy_manifests_loader.get_all())

-- Checks if there are any policies placed in the wrong place in the chain.
-- It doesn't return anything, it prints error messages when there's a problem.
function _M:check_order(manifests)
PolicyOrderChecker.new(
manifests or policy_manifests_loader.get_all()
):check(self)
if manifests then
PolicyOrderChecker.new(manifests):check(self)
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A nit, it looks like the method is only called in gateway/src/apicast/configuration.lua without parameters. So perhaps makes sense to simplify by removing the argument from it as well the if clause.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Probably the idea was to use the manifest cache somewhere?

In any case - why is this change implemented? 🤔

It seems to me that:

  1. It always loads all policies (on line :196) regardless of whether manifests is passed or not.
  2. If manifests is passed, and is not null (as @akostadinov pointed out - this does not happen at this point), then the policy order check will be performed 2 times. Is that intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Still why do we need this condition if we never call the method with parameters? Anyway, not super important as I said.

default_policy_order_check:check(self)
end

local function call_chain(phase_name)
Expand Down
40 changes: 33 additions & 7 deletions gateway/src/apicast/policy_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ local concat = table.concat
local setmetatable = setmetatable
local pcall = pcall

local tab_new = require('table.new')
local isempty = require('table.isempty')

local manifests_cache = tab_new(32, 0)
Comment thread
tkan145 marked this conversation as resolved.
Outdated

local _M = {}

local resty_env = require('resty.env')
Expand Down Expand Up @@ -70,8 +75,22 @@ local function lua_load_path(load_path)
return format('%s/?.lua', load_path)
end

local function get_manifest(name, version)
Comment thread
tkan145 marked this conversation as resolved.
Outdated
local manifests = manifests_cache[name]
if manifests then
for _, manifest in ipairs(manifests) do
if version == manifest.version then
return manifest
end
end
end
end

local function load_manifest(name, version, path)
local manifest = read_manifest(path)
local manifest = get_manifest(name, version)
if not manifest then
manifest = read_manifest(path)
end

if manifest then
if manifest.version ~= version then
Expand Down Expand Up @@ -110,8 +129,8 @@ end
function _M:load_path(name, version, paths)
local failures = {}

for _, path in ipairs(paths or self.policy_load_paths()) do
local manifest, load_path = load_manifest(name, version, format('%s/%s/%s', path, name, version) )
if version == 'builtin' then
local manifest, load_path = load_manifest(name, version, format('%s/%s', self.builtin_policy_load_path(), name) )

if manifest then
return load_path, manifest.configuration
Expand All @@ -120,8 +139,8 @@ function _M:load_path(name, version, paths)
end
end

if version == 'builtin' then
local manifest, load_path = load_manifest(name, version, format('%s/%s', self.builtin_policy_load_path(), name) )
for _, path in ipairs(paths or self.policy_load_paths()) do
local manifest, load_path = load_manifest(name, version, format('%s/%s/%s', path, name, version) )

if manifest then
return load_path, manifest.configuration
Expand All @@ -130,6 +149,7 @@ function _M:load_path(name, version, paths)
end
end


return nil, nil, failures
end

Expand Down Expand Up @@ -173,9 +193,15 @@ end
-- Returns all the policy modules
function _M:get_all()
local policy_modules = {}
local manifests

local policy_manifests_loader = require('apicast.policy_manifests_loader')
local manifests = policy_manifests_loader.get_all()
if isempty(manifests_cache) then
local policy_manifests_loader = require('apicast.policy_manifests_loader')
manifests = policy_manifests_loader.get_all()
manifests_cache = manifests
else
manifests = manifests_cache
end

for policy_name, policy_manifests in pairs(manifests) do
for _, manifest in ipairs(policy_manifests) do
Expand Down