Custom plugin installation kubernetes

Hi!

While adding a custom plugin to Kong in Kubernetes it fails to start with the next error:

2023/12/18 11:41:51 [error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/tools/utils.lua:786: error loading module 'kong.plugins.custom-rate-limit.handler':
93
/opt/kong/plugins/custom-rate-limit/handler.lua:2: module 'lunajson' not found:No LuaRocks module found for lunajson
92
	no field package.preload['lunajson']

As I understand, the default Kong image doesn’t have lunajson module and that’s why it complains about it.
How can I add a missed module - updating the default Kong image?

Thank you.

@cactus You need to create a custom image with the dependencies added. This will increase the size of the image - if you find any other alternatives to bring back the size, please do respond here (I am looking for a solution)

FROM kong/kong-gateway:3.4
USER root
RUN apt-get update
RUN apt-get install -y lua5.1 luarocks
#RUN apk add gcc expat expat-dev expat-static musl-dev
RUN luarocks install lua-cjson 2.1.0.6-1 # add your dependency here
RUN luarocks install xml2lua 1.5-2
USER kong
WORKDIR /usr/local/share/lua/5.1/kong/plugins
COPY kong/plugins/ ./

Hey!

Thanks for the reply. We have fixed this problem by installing the plugin in the docker image:

FROM ubuntu:jammy

COPY kong.deb /tmp/kong.deb

RUN set -ex; \
   apt-get update \
   && apt-get install --yes /tmp/kong.deb wget unzip \
   && rm -rf /var/lib/apt/lists/* \
   && rm -rf /tmp/kong.deb \
   && chown kong:0 /usr/local/bin/kong \
   && chown -R kong:0 /usr/local/kong \
   && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/luajit \
   && ln -s /usr/local/openresty/luajit/bin/luajit /usr/local/bin/lua \
   && ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/bin/nginx \
   && kong version \
   && wget https://url/custom-rate-limit-1.all.rock \
   && luarocks install custom-rate-limit-1.all.rock

In kubernetes kong-gateway config added next:

  env:
    plugins: "bundled,custom-rate-limit"

Also, a plugin was added to Kubernetes using Kind: KongPlugin, according to this manual.
Plugin is available, but when we tried to use it(add annotation to service or route), we got the error:

proxy 2023/12/20 17:20:52 [debug] 1262#0: *311 [lua] init.lua:23: poll(): worker-events: emulate poll method                                                                                                                │
│ proxy 2023/12/20 17:20:52 [error] 1262#0: *311 [lua] api_helpers.lua:540: handle_error(): /usr/local/share/lua/5.1/lapis/application.lua:439: /usr/local/share/lua/5.1/kong/db/schema/init.lua:1204: attempt to index a nil │
│  value                                                                                                                                                                                                                      │
│ proxy stack traceback:                                                                                                                                                                                                      │
│ proxy     /usr/local/share/lua/5.1/kong/db/schema/init.lua: in function 'run_entity_check'                                                                                                                                  │
│ proxy     /usr/local/share/lua/5.1/kong/db/schema/init.lua:1321: in function 'run_checks'                                                                                                                                   │
│ proxy     /usr/local/share/lua/5.1/kong/db/schema/init.lua:1355: in function 'run_entity_checks'                                                                                                                            │
│ proxy     /usr/local/share/lua/5.1/kong/db/schema/init.lua:1986: in function 'validate_upsert'                                                                                                                              │
│ proxy     /usr/local/share/lua/5.1/kong/db/dao/init.lua:596: in function 'check_upsert'                                                                                                                                     │
│ proxy     /usr/local/share/lua/5.1/kong/db/dao/init.lua:1211: in function 'upsert_entity'                                                                                                                                   │
│ proxy     /usr/local/share/lua/5.1/kong/api/endpoints.lua:499: in function 'fn'                                                                                                                                             │
│ proxy     /usr/local/share/lua/5.1/kong/api/api_helpers.lua:311: in function </usr/local/share/lua/5.1/kong/api/api_helpers.lua:289>                                                                                        │
│ proxy                                                                                                                                                                                                                       │
│ proxy stack traceback:                                                                                                                                                                                                      │
│ proxy     [C]: in function 'error'                                                                                                                                                                                          │
│ proxy     /usr/local/share/lua/5.1/lapis/application.lua:439: in function 'handler'                                                                                                                                         │
│ proxy     /usr/local/share/lua/5.1/lapis/application.lua:185: in function 'resolve'                                                                                                                                         │
│ proxy     /usr/local/share/lua/5.1/lapis/application.lua:216: in function </usr/local/share/lua/5.1/lapis/application.lua:214>                                                                                              │
│ proxy     [C]: in function 'xpcall'                                                                                                                                                                                         │
│ proxy     /usr/local/share/lua/5.1/lapis/application.lua:214: in function 'dispatch'                                                                                                                                        │
│ proxy     /usr/local/share/lua/5.1/lapis/nginx.lua:231: in function 'serve'                                                                                                                                                 │
│ proxy     /usr/local/share/lua/5.1/kong/init.lua:1626: in function 'admin_content'                                                                                                                                          │
│ proxy     content_by_lua(nginx-kong.conf:330):2: in main chunk, client: 10.15.64.85, server: kong_admin, request: "PUT /plugins/470bfd5f-6718-497b-a638-67da79a0ee2d HTTP/2.0", host: "10.15.67.7:8444"                     │
│ proxy 2023/12/20 17:20:52 [debug] 1262#0: *311 [lua] init.lua:23: poll(): worker-events: emulate poll method

This custom plugin was written for kong-2.1(also postgres was used as db for kong), we are trying to use it on kong-3.4 in kubernetes in db-less mode.

What is the best way to import plugins from old kong version into new one?

Thanks.

Hey, @JohnWilliams
We tried to add a plugin in this way:

During Kong startup, we see that the plugin is loaded:

│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: custom-rate-limit                                                                                                               │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:242: loader_fn(): Loading custom plugin entity: 'custom-rate-limit.ratelimiting_metrics'                                                                            │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: jwt                                                                                                                            │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:242: loader_fn(): Loading custom plugin entity: 'jwt.jwt_secrets'                                                                                                  │
│ proxy 2023/12/21 08:09:58 [debug] 1#0: [lua] plugins.lua:284: load_plugin(): Loading plugin: acl   
..........                        

But after trying to annotate a route or service with this plugin got the same error above.

All the source code in the directory with plugin name

tree custom-rate-limit/
custom-rate-limit/
├── README.md
├── daos.lua
├── expiration.lua
├── handler.lua
├── policies
│   └── init.lua
├── schema.lua
└── custom-rate-limit-1.rockspec

1 directory, 7 files

I am not familiar with the error.

Try posting this in Kong Slack Channel - it might get you a quick answer.