|
| 1 | +# Configuring Custom Entities |
| 2 | + |
| 3 | +This is an **advanced-level** guide for users using custom entities in Kong. |
| 4 | +Most users do not need to use this feature. |
| 5 | + |
| 6 | +Kong has in-built extensibility with its plugin architecture. |
| 7 | +Plugins in Kong have a `config` property where users can store configuration |
| 8 | +for any custom plugin and this suffices in most use cases. |
| 9 | +In some use cases, plugins define custom entities to store additional |
| 10 | +configuration outside the plugin instance itself. |
| 11 | +This guide elaborates on how such custom entities can be used with the Kong |
| 12 | +Ingress Controller. |
| 13 | + |
| 14 | +> Note: All entities shipped with Kong are supported by Kong Ingress Controller |
| 15 | +out of the box. This guide applies only if you have a custom entity in your |
| 16 | +plugin. To check if your plugin contains a custom entity, the source code |
| 17 | +will usually contain a `daos.lua` file. |
| 18 | +Custom plugins have first-class support in Kong Ingress Controller |
| 19 | +via the `KongPlugin` CRD. |
| 20 | +Please read [the custom plugin guide](../setting-up-custom-plugins.md) instead |
| 21 | +if you are only using Custom plugins. |
| 22 | + |
| 23 | +## Caveats |
| 24 | + |
| 25 | +- The feature discussed in this guide apply for DB-less deployments of Kong. |
| 26 | + The feature is not supported for deployments where Kong is used with a |
| 27 | + database or Kong is used in hybrid mode. |
| 28 | + For these deployments, configure custom entities directly using Kong's Admin |
| 29 | + API. |
| 30 | +- Custom entities which have a foreign relation with other core entities in Kong |
| 31 | + are not supported. Only entities which can exist by themselves and then |
| 32 | + be referenced via plugin configuration are supported. |
| 33 | + |
| 34 | +## Creating a JSON representation of the custom entity |
| 35 | + |
| 36 | +In this section, we will learn how to create a JSON representation of |
| 37 | +a custom entity. |
| 38 | + |
| 39 | +Suppose you have a custom entity with the following schema in your plugin source: |
| 40 | + |
| 41 | +```lua |
| 42 | +{ |
| 43 | + name = "xkcds", |
| 44 | + primary_key = { "id" }, |
| 45 | + cache_key = { "name" }, |
| 46 | + endpoint_key = "name", |
| 47 | + fields = { |
| 48 | + { id = typedefs.uuid }, |
| 49 | + { |
| 50 | + name = { |
| 51 | + type= "string", |
| 52 | + required = true, |
| 53 | + unique = true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + url = { |
| 58 | + type = "string", |
| 59 | + required = true, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { created_at = typedefs.auto_timestamp_s }, |
| 63 | + { updated_at = typedefs.auto_timestamp_s }, |
| 64 | + }, |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +An instance of such an entity would look like: |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "id": "385def6e-3059-4929-bb12-d205e97284c5", |
| 73 | + "name": "Bobby Drop Tables", |
| 74 | + "url": "https://xkcd.com/327/" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Multiple instances of such an entity are represented as follows: |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "xkcds": [ |
| 83 | + { |
| 84 | + "id": "385def6e-3059-4929-bb12-d205e97284c5", |
| 85 | + "name": "bobby_tables", |
| 86 | + "url": "https://xkcd.com/327/" |
| 87 | + }, |
| 88 | + { |
| 89 | + "id": "d079a632-ac8d-4a9a-860c-71de82e8fc11", |
| 90 | + "name": "compiling", |
| 91 | + "url": "https://xkcd.com/303/" |
| 92 | + } |
| 93 | + ] |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +If you have more than one custom entities that you would like to configure |
| 98 | +then you can create other entities by specifying the entity name at the root |
| 99 | +level of the JSON as the key and then a JSON array containing the |
| 100 | +custom entities as the value of the key. |
| 101 | + |
| 102 | +To configure custom entities in a DB-less instance of Kong, |
| 103 | +you first need to create such a JSON representation of your entities. |
| 104 | + |
| 105 | +## Configuring the custom entity secret |
| 106 | + |
| 107 | +Once you have the JSON representation, we need to store the configuration |
| 108 | +inside a Kubernetes Secret. |
| 109 | +The following command assumes the filename to be `entities.json` but you can |
| 110 | +use any other filename as well: |
| 111 | + |
| 112 | +```bash |
| 113 | +$ kubectl create secret generic -n kong kong-custom-entities --from-file=config=entities.json |
| 114 | +secret/kong-custom-entities created |
| 115 | +``` |
| 116 | + |
| 117 | +Some things to note: |
| 118 | +- The key inside the secret must be `config`. This is not configurable at the |
| 119 | + moment. |
| 120 | +- The secret must be accessible by the Ingress Controller. The recommended |
| 121 | + practice here is to install the secret in the same namespace in which Kong |
| 122 | + is running. |
| 123 | + |
| 124 | +## Configure the Ingress Controller |
| 125 | + |
| 126 | +Once you have the secret containing the custom entities configured, |
| 127 | +you need to instruct the controller to read the secret and sync the custom |
| 128 | +entities to Kong. |
| 129 | + |
| 130 | +To do this, you need to add the following environment variable to the |
| 131 | +`ingress-ccontroller` container: |
| 132 | + |
| 133 | +```yaml |
| 134 | +env: |
| 135 | +- name: CONTROLLER_KONG_CUSTOM_ENTITIES_SECRET |
| 136 | + value: kong/kong-custom-entities |
| 137 | +``` |
| 138 | +
|
| 139 | +This value of the environment variable takes the form of `<namespace>/<name>`. |
| 140 | +You need to configure this only once. |
| 141 | + |
| 142 | +This instructs the controller to watch the above secret and configure Kong |
| 143 | +with any custom entities present inside the secret. |
| 144 | +If you change the configuration and update the secret with different entities, |
| 145 | +the controller will dynamically fetch the updated secret and configure Kong. |
| 146 | + |
| 147 | +## Verification |
| 148 | + |
| 149 | +You can verify that the custom entity was actually created in Kong's memory |
| 150 | +using the `GET /xkcds` (endpoint will differ based on the name of the entity) |
| 151 | +on Kong's Admin API. |
| 152 | +You can forward traffic from your local machine to the Kong Pod to access it: |
| 153 | + |
| 154 | +```bash |
| 155 | +$ kubectl port-forward kong/kong-pod-name 8444:8444 |
| 156 | +``` |
| 157 | + |
| 158 | +and in a separate terminal: |
| 159 | + |
| 160 | +```bash |
| 161 | + $ curl -k https://localhost:8444/<entity-name> |
| 162 | +``` |
| 163 | + |
| 164 | +## Using the custom entity |
| 165 | + |
| 166 | +You can now use reference the custom entity in any of your custom plugin's |
| 167 | +`config` object: |
| 168 | + |
| 169 | +```yaml |
| 170 | +apiVersion: configuration.konghq.com/v1 |
| 171 | +kind: KongPlugin |
| 172 | +metadata: |
| 173 | + name: random-xkcd-header |
| 174 | +config: |
| 175 | + xkcds: |
| 176 | + - d079a632-ac8d-4a9a-860c-71de82e8fc11 |
| 177 | +plugin: xkcd-header |
| 178 | +``` |
0 commit comments