Kong plugin to maintain config to be shared across plugins
This plugin provides config with following features:
KONG_ENV
.Let’s say we are connecting to Redis in 3 different plugins. If redis configuration changes for some reason, then we need to change it in all instances of these plugins. In such cases, it is handy when the configuration is kept at one place.
luarocks install config-by-env
Clone this repo and run:
luarocks make
You will also need to enable this plugin by adding it to the list of enabled plugins using KONG_PLUGINS
environment variable or the plugins
key in kong.conf
export KONG_PLUGINS=config-by-env
OR
plugins=config-by-env
This plugin requires KONG_ENV
environment variable to be set in nginx. To do that check this article. A command like below should work and help you set environment variable in Nginx.
export KONG_ENV=production && export KONG_NGINX_MAIN_ENV=KONG_ENV && kong start
KONG_ENV
environment variable.
{
"default": {
"redis": {
"url": "localhost",
"port": 6379,
"connect_timeout": 1000
}
},
"docker": {
"redis": {
"url": "http://redis%TEAM_NAME%.dream11-staging.local",
"port": 8888
}
},
"production": {
"redis": {
"url": "http://redis.dream11.local",
"port": 9999
}
}
}
default
config. When KONG_ENV=docker
, config after merging with default
config will be:
{
"redis": {
"url": "http://redis%TEAM_NAME%.dream11-staging.local",
"port": 8888,
"connect_timeout": 1000
}
}
TEAM_NAME=user-profile
, then config after interpolating environment variables will be:
{
"redis": {
"url": "http://redis-user-profile.dream11-staging.local",
"port": 8888,
"connect_timeout": 1000
}
}
local config_by_env = kong.ctx.shared.config_by_env
local redis_host = config_by_env["redis"]["host]
Key | Type | Default | Required | Description |
---|---|---|---|---|
config | string | true | Config as JSON | |
set_service_url | boolean | false | false | Overrides service host URL |
set_service_url
in config when you want to override the host url of an upstream service. In a case where the upstream host url changes w.r.t environment, then to manage the service url we have to either keep separate kong.yaml file per environment or we can use config-by-env plugin to override the url as per environment.