tjtjtjのメモ

自分のためのメモです

kong プラグイン配布 インストール アンインストール

前回で kong は開発環境も含んでいるのだと実感した。今回は開発したプラグインの配布について。

docs.konghq.com

Packaging sources

.rockspec のディレクトリへ移動し、パッケージング。luarocks pack に失敗する。原因は zip がない、だと?

$ cd /kong-plugin
$ luarocks make

kong-plugin-myplugin 0.1.0-1 is now installed in /usr/local (license: Apache 2.0)

$ ll
total 26
drwxrwxrwx  1 vagrant vagrant  4096 Oct  2 10:58 ./
drwxr-xr-x 26 root    root     4096 Oct  8 10:25 ../
drwxrwxrwx  1 vagrant vagrant     0 Oct  2 10:58 .git/
-rwxrwxrwx  1 vagrant vagrant     9 Oct  2 10:58 .gitignore*
drwxrwxrwx  1 vagrant vagrant     0 Oct  2 10:58 kong/
-rwxrwxrwx  1 vagrant vagrant  1402 Oct  2 10:58 kong-plugin-myplugin-0.1.0-1.rockspec*
-rwxrwxrwx  1 vagrant vagrant 11357 Oct  2 10:58 LICENSE*
-rwxrwxrwx  1 vagrant vagrant   574 Oct  2 10:58 .luacheckrc*
-rwxrwxrwx  1 vagrant vagrant   360 Oct  2 10:58 README.md*
drwxrwxrwx  1 vagrant vagrant     0 Oct  2 10:58 spec/

$ luarocks pack kong-plugin-myplugin 0.1.0-1
Error: Failed packing /kong-plugin/kong-plugin-myplugin-0.1.0-1.all.rock

$ luarocks pack kong-plugin-myplugin-0.1.0-1.rockspec
Error: Failed packing /kong-plugin/kong-plugin-myplugin-0.1.0-1.src.rock - 'zip' program not found. Make sure zip is installed and is available in your PATH (or you may want to edit the 'variables.ZIP' value in file '/usr/local/etc/luarocks/config-5.1.lua')

$ zip

Command 'zip' not found, but can be installed with:

apt install zip
Please ask your administrator.

apt install zip して luarocks pack で成功。作成された .rock を確認

$ sudo apt install zip
$ luarocks pack kong-plugin-myplugin 0.1.0-1
Packed: /kong-plugin/kong-plugin-myplugin-0.1.0-1.all.rock

$ ll *.rock
-rwxrwxrwx 1 vagrant vagrant 7456 Oct  8 10:54 kong-plugin-myplugin-0.1.0-1.all.rock*

$ zipinfo kong-plugin-myplugin-0.1.0-1.all.rock
Archive:  kong-plugin-myplugin-0.1.0-1.all.rock
Zip file size: 7456 bytes, number of entries: 11
drwxr-xr-x  3.0 unx        0 bx stor 19-Oct-08 10:54 doc/
-rw-r--r--  3.0 unx      360 tx defN 19-Oct-08 10:54 doc/README.md
-rw-r--r--  3.0 unx    11357 tx defN 19-Oct-08 10:54 doc/LICENSE
-rw-r--r--  3.0 unx     1402 tx defN 19-Oct-08 10:54 kong-plugin-myplugin-0.1.0-1.rockspec
-rw-r--r--  3.0 unx      475 tx defN 19-Oct-08 10:54 rock_manifest
drwxr-xr-x  3.0 unx        0 bx stor 19-Oct-08 10:54 lua/
drwxr-xr-x  3.0 unx        0 bx stor 19-Oct-08 10:54 lua/kong/
drwxr-xr-x  3.0 unx        0 bx stor 19-Oct-08 10:54 lua/kong/plugins/
drwxr-xr-x  3.0 unx        0 bx stor 19-Oct-08 10:54 lua/kong/plugins/myplugin/
-rw-r--r--  3.0 unx      298 tx defN 19-Oct-08 10:54 lua/kong/plugins/myplugin/schema.lua
-rw-r--r--  3.0 unx      575 tx defN 19-Oct-08 10:54 lua/kong/plugins/myplugin/handler.lua
11 files, 14467 bytes uncompressed, 5612 bytes compressed:  61.2%

Installing the plugin

vm 作る

$ git clone https://github.com/Kong/kong-vagrant.git kong-vagrant2
$ cd kong-vagrant2
$ git clone https://github.com/Kong/kong
$ cd kong
$ git checkout 1.3.0
$ cd ..
$ vi Vagrantfile
-- vb.name = "vagrant_kong" -> vagrant_kong2 
$ vagrant up
$ vagrant ssh-config --host mykong2 >> ~/.ssh/config
$ ssh mykong2

プラグインをインストール

$ luarocks install kong-plugin-myplugin-0.1.0-1.all.rock

kong-plugin-myplugin 0.1.0-1 is now installed in /usr/local (license: Apache 2.0)

kong を起動して確認するが myplugin いない?

$ cd /kong
$ bin/kong migrations bootstrap
$ bin/kong start
$ curl http://localhost:8001 | jq . | grep myplugin

luarock list にはいる。

$ luarock list 
:
kong-plugin-myplugin
   0.1.0-1 (installed) - /home/vagrant/.luarocks/lib/luarocks/rocks-5.1
   0.1.0-1 (installed) - /usr/local/lib/luarocks/rocks-5.1
:

あー 'KONG_PLUGINS' してなかった。やり直してインストール成功

$ export KONG_PLUGINS=bundled,myplugin
$ kong restart
Kong stopped
Kong started
$ curl -sS http://localhost:8001 | jq . | grep myplugin
      "myplugin": true,
      "myplugin"
      "myplugin": true,

プラグイン使ってみる

サービスとルート作成

$ curl -i -X POST \
   --url http://localhost:8001/services/ \
   --data 'name=mockbin' \
   --data 'url=http://mockbin.org/request'
$ curl -i -X POST \
  --url http://localhost:8001/services/mockbin/routes \
  --data 'paths=/'

プラグイン適用前

$ curl -i http://localhost:8000
HTTP/1.1 200 OK

プラグイン適用

$ curl -i -X POST \
  --url http://localhost:8001/services/mockbin/plugins \
  --data 'name=myplugin'
HTTP/1.1 201 Created

{"created_at":1570616021,
 "config":{},
 "id":"508d4234-217c-4e0c-8f43-8e51145fda66",
 "service":{"id":"a861ee21-52b0-4638-8633-5312e4ae2379"},
 "name":"myplugin",
 "protocols":["grpc","grpcs","http","https"],
 "enabled":true,
 "run_on":"first",
 "consumer":null,
 "route":null,
 "tags":null
}

プラグイン適用後

$ curl -i http://localhost:8000
HTTP/1.1 400 Bad Request
:
{"message":"asdf is empty"}

curl -i -H "asdf: qwer" http://localhost:8000
HTTP/1.1 401 Unauthorized
:
{"message":"asdf is not asdf"}

curl -i -H "asdf: asdf" http://localhost:8000
HTTP/1.1 200 OK

Removing a plugin

手順は3つ。

  1. サービス・ルートからプラグインを削除
  2. plugins ディレクティブからプラグインを削除し kong 再起動 or リロード
  3. luarocks remove <plugin-name>

プラグインの確認を削除

$ curl http://localhost:8001/services/mockbin/plugins
{
  "next": null,
  "data": [
    {
      "created_at": 1570616021,
      "config": {},
      "id": "508d4234-217c-4e0c-8f43-8e51145fda66",
      "service": {
        "id": "a861ee21-52b0-4638-8633-5312e4ae2379"
      },
      "name": "myplugin",
      "protocols": [
        "grpc",
        "grpcs",
        "http",
        "https"
      ],
      "enabled": true,
      "run_on": "first",
      "consumer": null,
      "route": null,
      "tags": null
    }
  ]
}

$ curl -i -X DELETE http://localhost:8001/plugins/508d4234-217c-4e0c-8f43-8e51145fda66
HTTP/1.1 204 No Content

$ curl http://localhost:8001/services/mockbin/plugins
{"next":null,"data":[]}

plugins ディレクティブからプラグインを削除し kong 再起動し確認

$ export KONG_PLUGINS=bundled
$ kong restart
Kong stopped
Kong started
$ curl -sS http://localhost:8001 | jq . | grep myplugin

プラグインを完全に削除

$ luarocks list | grep myplugin
kong-plugin-myplugin

$ luarocks remove kong-plugin-myplugin
Checking stability of dependencies in the absence of
kong-plugin-myplugin 0.1.0-1...

Removing kong-plugin-myplugin 0.1.0-1...
Removal successful.

$ luarocks list | grep myplugin

おわり

とかなんとかやっているうちに 1.4.0rc1 が来ていた。

https://github.com/Kong/kong/releases/tag/1.4.0rc1
https://github.com/Kong/kong/blob/1.4.0rc1/CHANGELOG.md#140rc1