Kong - 自定义插件返回:自定义插件插件已启用但未安装;

Kong - custom plugins returning: custom-plugin plugin is enabled but not installed;

我有一个 Kong 实例,我正在 运行正在使用 Dockerfile,内容为:

FROM kong:1.4.0

WORKDIR /files
COPY plugins kong/plugins
ENV KONG_LOG_LEVEL=debug
ENV KONG_PLUGINS custom-plugin
ENV KONG_LUA_PACKAGE_PATH /files/?.lua;;

但是,在 docker 运行 上,这个 returns

error loading plugin schemas: on plugin 'custom-plugin': custom-plugin plugin is enabled but not installed;

module 'kong.plugins.custom-plugin.handler' not found:No LuaRocks module found for kong.plugins.custom-plugin

我已确认文件结构正确,在 运行 时嵌套在 kong/plugins 目录中。

谁能帮忙解决这个问题?

查看来自 Kong/kong-plugin-zipkin issue 5(另一个依赖于 kong.plugins.custom-plugin.handler 的插件)的指令之一是否适用于 kong.plugins.custom-plugin.handler 本身。

The gist of it is that the plugin should be available to Kong on disk, and added to the custom_plugins configuration property.

You can achieve this via various ways:

Don't forget that you can also specify configure Kong with environment variables, so using -e KONG_CUSTOM_PLUGINS=my-custom-plugin would also work.

The plugin also needs to be in the LUA_PATH (like PATH, but for Lua modules).

So, in short, one of the possible solutions would be to use volumes and environment variables:

  • Mount the plugin's code: -v /path/to/my-custom-plugin:/etc/kong/plugins/my-custom-plugin
  • Add the directory containing custom plugins to the LUA_PATH: -e KONG_LUA_PACKAGE_PATH=/etc/?.lua (Kong requires kong.plugins.custom-plugin.handler, which translates to /etc/kong/plugins/my-custom-plugin/handler.lua)
  • Instruct Kong to load the plugin with: -e KONG_CUSTOM_PLUGINS=my-custom-plugin

接下来,首先检查这是否是 KONG_LUA_PATH 问题,如 Kong/kong issue 3394.

这是关于插件迁移,但也可能对这里产生影响。

as a short term solution, you should set your LUA_PATH environment variable to point to your custom plugin's installation path
(instead of lua_package_path in kong.conf or KONG_LUA_PACKAGE_PATH).

E.g. I just moved:

export KONG_LUA_PACKAGE_PATH="/path/to/some-custom-plugin/?.lua;;"

to:

export LUA_PATH="/path/to/some-custom-plugin/?.lua;$LUA_PATH"

And ran kong migrations up again, which successfully ran the plugin's migration.

That is because lua_package_path is only for runtime Kong (within nginx), and is not being considered by the CLI (the kong migrations command)

If I just commented the lua_package_path in kong.conf and exported LUA_PATH as you mentioned, it started giving me this error:

./kong:3: module 'luarocks.loader' not found:

The shell from which I am executing kong migrations up never had LUA_PATH set, even before. It appears luarocks internally creates LUA_PATH if it is not already set in the shell.

So the additional step I had to perform was to get the current LUA_PATH that's set internally by running "luarocks path" command and then take the result of that, and append my custom login plugin path to it and then export that LUA_PATH to get this to work.

同样,即使您当前的问题与迁移无关,其中一个变量可能会有所帮助。

查看这些资源:

来自 Kong 的错误报告: https://github.com/Kong/kong/issues/4696

I don't think it's the luarocks' problem
Indeed, I installed kong in docker whose image is built by a dockerfile.
In my docker file, I went into the folder which store the custom plugins,and then traverse and luarocks make them by shell.It looks like:

#install private plugins
cd APIGPlugins

for dir in `ls`; do
    if [ -d $dir ]; then
      cd $dir;
      luarocks make;
      cd ../;
    fi
done
and then, I run the docker images for a container by the directive:

sudo docker run -d --name kong \
    -e "KONG_DATABASE=off" \
    -e "KONG_DECLARATIVE_CONFIG=/etc/kong/kong.yml" \
    -e "KONG_PLUGINS=apig-response-transform" \
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
    -p 8000:8000 \
    -p 8443:8443 \
    -p 8001:8001 \
    -p 8444:8444 \
    kong-plugin:v4
As u see, I set the kong plugin by the docker run env variable parameter for enable the plugin instead of setting in the kong.conf.
The logs were generated by directive of docker logs "container ID"
It works when I tried to install another custom plugin in this way,but not work when install the custom plugin I described before

更新

您需要安装 lua-包管理器 luarocks