Writing Kong plugins with Go
The Kong, quoting its own documentation, is an open-source API gateway, cloud-native, platform-agnostic, scalable API Gateway distinguished for its high performance and extensibility via plugins. Kong has a lot of official plugins that allow us to customize what we need, and when there aren’t any available, you can build your own.
The default language for building plugins its Lua, but other languages are supported, including Go. I don’t have much experience writing code with Lua, so using Go allows me a higher development speed and quality on my plugins.
Development
Building a Go plugin is easy, as you need to follow the signature proposed by Kong’s plugin development kit or PDK, but it is easy to understand. To demonstrate that, we will build a plugin that will read a request header and set a new response header.
The first thing to do is define both constants: version and priority. As the name says, we will configure the current version of the plugin and its execution priority within the other plugins. Its worth mentioning that the higher the priority, the earlier it will be executed. If you wanna understand more about it, check this post.
| |
After, we will define which params the plugin accepts by building a struct called Config. Here, we will receive the field message as a string.
| |
All kong plugins work based on which phase access they need to run. The possible phase access are:
- Certificate
- Rewrite
- Access
- Response
- Preread
- Log
In this example, we will be using Access because we need to update the response before forwarding the request to the responsible microservice. You don’t need to worry about it because the signatures for all phases are the same. To know more about all phase access, check this documentation.
| |
This function uses the PDK to read the available headers by using kong.Request.GetHeader, and later we add a response header using kong.Response.SetHeader. For more information about all available functions, you can check the Go PDK.
At last, let’s check the main function:
| |
As we can notice, the main function initializes the Kong server, sending the Config, Version, and Priority defined before. The full code is:
| |
Tests
Testing our Go plugins is also easy once the PDK gives us tools that make it straightforward. Let’s test our plugin:
| |
Deploy
To deploy our plugin, we build an executable and must add it to the Kong image.
| |
Kong needs some information to be able to find and load the customized plugin, so we need to add some environment variables:
| |
Considerations about performance
The Kong company already did a study about the performance, and you can find it here.
Conclusion
Go is an excellent tool for writing plugins for Kong. We can have all the performance and tools it provides to us. In my case, I have also been able to speed up my deliveries and have a higher code coverage. It is also possible to isolate the plugin’s logic with the PDK use, making it easy to write plugins that work for different technologies, decoupling Kong.
If you wanna check all the code shown in this post, you can access the repo.
You also can find me on Twitter, Github, or LinkedIn.
