LoginSignup
1
3

More than 5 years have passed since last update.

Kong Yaml Admin Configuration

Last updated at Posted at 2018-07-14

Kong への登録は Admin API 経由で行うことが可能だが自動化等考えると設定として記述できた方が都合が良い。

kfig.png

検証として Yaml による Admin 設定が可能な nokamoto/kfig を作成する。

kfig

Kong 1.13.x での検証を行う。

以下の Yaml により設定を行うことが可能。

consumers:
  - username: nokamoto
    custom_id: nokamoto
    present: yes
    key_auths:
      - key: my-api-key
        present: yes

services:
  - name: mock
    url: http://mockbin.org
    present: yes
    routes:
      - hosts:
          - example.com
        protocols:
          - http
    plugins:
      - name: key-auth
        config:
          hide_credentials: yes
        enabled: yes
$ curl -s localhost:8001/consumers | jq .
{
  "total": 1,
  "data": [
    {
      "custom_id": "nokamoto",
      "created_at": 1531555411038,
      "username": "nokamoto",
      "id": "399b0359-5f37-4292-a6c7-3414815460b0"
    }
  ]
}

$ curl -s localhost:8001/key-auths | jq .
{
  "total": 1,
  "data": [
    {
      "id": "2042bda2-2699-4238-9b8c-5f3ea40caec0",
      "created_at": 1531555411121,
      "key": "my-api-key",
      "consumer_id": "399b0359-5f37-4292-a6c7-3414815460b0"
    }
  ]
}

$ curl -s localhost:8001/services | jq .
{
  "next": null,
  "data": [
    {
      "host": "mockbin.org",
      "created_at": 1531555411,
      "connect_timeout": null,
      "id": "30985b4f-aed8-4c78-9f8a-795bd04970c9",
      "protocol": "http",
      "name": "mock",
      "read_timeout": null,
      "retries": null,
      "path": null,
      "updated_at": 1531555411,
      "port": 80,
      "write_timeout": null
    }
  ]
}

デザイン

Yaml は Admin API への出力に加えて present フラグを定義する。

consumers:
    - username: nokamoto
      custom_id: nokamoto
      present: yes

{
  "username": "nokamoto",
  "custom_id": "nokamoto"
}

presentyes の場合は POST (or PATCH) により登録、 no の場合は DELETE による削除で実装できる。

ただし Route はユーザー定義の ID が提供されていないため、一度全ての情報を取得して差を計算する処理を実装する必要があり、 present のような定義はできないため一貫性は維持できない。

また、プラグイン関係など Admin API には記載されていない API も多くあるため、完全な Yaml 実装にはそれなりのコストが必要となる。

実装

テスト関連以外には特に記載することはない。

Docker Compose

Kong Installation にはデータベースのマイグレーションの実行が含まれる。
これを docker-compose.yaml で定義する場合、kong の立ち上げとは別に kong-migration を実行する必要がある。kong の立ち上げは kong-migration が成功するまでは失敗し続けるため docker-compose 立ち上げの後に kong が正常な状態であるかを確認する必要がある。

CircleCI

CircleCI の limitations により、 Machine Executor に設定することでしか解決できなかった。

For instance, if you require low-level access to the network or need to mount external volumes consider using machine.

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3