Custom plugin for Kong v1.0.2 is enabled but not installed
Asked Answered
R

3

7

I have a custom plugin for Kong which worked fine for Kong v0.14.1 but after I upgraded to v.1.0.2 it's throwing an error.

OS used: macOS Mojave

In kong.conf file I have this code:

log_level = debug
plugins=my-custom-plugin

I try to start Kong with this command:

kong start -c kong.conf

and I get this error:

Error: /usr/local/share/lua/5.1/kong/cmd/start.lua:50: nginx: [error] init_by_lua
error: /usr/local/share/lua/5.1/kong/init.lua:344: my-custom-plugin plugin is enabled but not installed;
module 'kong.plugins.my-custom-plugin.handler' not found:No LuaRocks module found for kong.plugins.my-custom-plugin.handler
no field package.preload['kong.plugins.my-custom-plugin.handler']
no file './kong/plugins/kong-my-custom-plugin/handler.lua'...

I installed the plugin using this command:

luarocks make

which gave the following output:

my-custom-plugin 1.0-1 is now installed in /usr/local/opt/kong (license: MIT)

Somehow, it appears that Kong is unable to find my installed custom plugin. Any idea why this happens?

Rowan answered 22/1, 2019 at 12:40 Comment(0)
C
5

@user5377037's answer has most of the relevant details, I just wanted to mention that as of Kong 0.14.x, "custom_plugins" is now just "plugins".

One of the reasons for this change is that you can now use this new variable name to choose to load or not to load plugins that are bundled with Kong -- a useful feature for some. However, if you want to load your custom plugin AND the bundled plugins, you now have to specify the bundled keyword to indicate that you want to keep the bundled plugins loaded.

Pre 0.14.x

The practical effect is that in Kong < 0.14.x:

custom_plugins = plugin1,plugin2

Or

KONG_CUSTOM_PLUGINS=<plugin-name>

Post 0.14.x

In Kong >= 0.14.x, you now write:

plugins = bundled,plugin1,plugin2

Or

KONG_PLUGINS=bundled,<plugin-name>

If You Don't Use bundled

If you don't add the bundled keyword, you'll likely face something like this error:

nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:292: key-auth plugin is in use but not enabled
stack traceback:
    [C]: in function 'assert'
    /usr/local/share/lua/5.1/kong/init.lua:292: in function 'init'
    init_by_lua:3: in main chunk

This means that you've set your proxy to use some plugin, but now you aren't loading that plugin on startup so Kong doesn't know what to do and quits. Essentially, you will only be loading your one single custom plugin which probably isn't what you want.

lua_package_path

The notes about lua_package_path and KONG_LUA_PACKAGE_PATH remain the same as in user5377037's post.

References

Cosmonaut answered 28/1, 2019 at 21:57 Comment(0)
S
3

Load the plugin

You must now add the custom plugin’s name to the custom_plugins list in your Kong configuration (on each Kong node):

custom_plugins = <plugin-name>

If you are using two or more custom plugins, insert commas in between, like so:

custom_plugins = plugin1,plugin2

Note: You can also set this property via its environment variable equivalent: KONG_CUSTOM_PLUGINS or define custom plugin in configuration property like:

KONG_CUSTOM_PLUGINS=<plugin-name> kong start

Reminder: don’t forget to update the custom_plugins directive for each node in your Kong cluster.

Verify loading the plugin

You should now be able to start Kong without any issue. Consult your custom plugin’s instructions on how to enable/configure your plugin on an API or Consumer object.

To make sure your plugin is being loaded by Kong, you can start Kong with a debug log level:

log_level = debug

OR:

KONG_LOG_LEVEL=debug

Then, you should see the following log for each plugin being loaded:

[debug] Loading plugin <plugin-name>

And here is workaround steps for adding things in custom_plugins and lua_package_path.

  1. Add custom plugin name in : custom_plugins = <plugin-name>
  2. Install hello-world plugin by using following steps :

    • If you have source code of your plugin then move into it and execute the command : luarocks make it will install your plugin.

    • Now you have to execute a command : make install-dev make sure your plugin have makefile like as: enter image description here

    • Once you execute this command make install-dev. It will create lua file at a location something like that :

      /your-plugin-path/**lua_modules/share/lua/5.1/kong/plugins/your-plugin-name/**?.lua

    • Just copy this path and add it into the kong configuration file in lua_package_path

      lua_package_path=/your-plugin-path/lua_modules/share/lua/5.1/kong/plugins/your-plugin-name/?.lua

  3. Just start kong : kong start --vv

Selfoperating answered 25/1, 2019 at 10:50 Comment(1)
Best answer so far! Based on this answer I identified where the plugin is installed, in my case was: "/usr/local/Cellar/kong/1.0.2/share/lua/5.1/kong/plugins/" and I set lua_package_path=/usr/local/Cellar/kong/1.0.2/share/lua/5.1/?.lua;; in my kong.conf file and now the plugin is found although I don't know why this doesn't happen automatically like in the previous versions of kong.Rowan
J
0

It looks like it can't find the handler.lua file, which is required. Can you run $ tree . at the root of your plugin project?

Here's the result of that same command for a test plugin I did a while back (https://github.com/jerneyio/kong-plugin-header-echo)

$ tree .
.
├── README.md
├── kong
│   └── plugins
│       └── kong-plugin-header-echo
│           ├── handler.lua
│           └── schema.lua
├── kong-plugin-header-echo-0.1.0-1.all.rock
└── kong-plugin-header-echo-0.1.0-1.rockspec

Also, are you sure your handler.lua is exposed in your rockspec? Again, successful example here:

$ cat kong-plugin-header-echo-0.1.0-1.rockspec 
package = "kong-plugin-header-echo"
version = "0.1.0-1"
source = {
   url = "git//github.com/jerneyio/kong-plugin-header-echo.git"
}
description = {
   homepage = "https://github.com/jerneyio/kong-plugin-header-echo",
   license = "MIT"
}
dependencies = {
  "lua >= 5.3",
  "kong >= 0.14"
}
build = {
   type = "builtin",
   modules = {
      ["kong.plugins.kong-plugin-header-echo.handler"] = "kong/plugins/kong-plugin-header-echo/handler.lua",
      ["kong.plugins.kong-plugin-header-echo.schema"] = "kong/plugins/kong-plugin-header-echo/schema.lua"
   }
}
Jeanettajeanette answered 24/1, 2019 at 16:1 Comment(1)
Yes handler.lua is exposed in rockspec and it worked on previous version of kong. The error is thrown when I try to start kong not when I install plugin.Rowan

© 2022 - 2024 — McMap. All rights reserved.