-
Notifications
You must be signed in to change notification settings - Fork 56
Update Readme with documentation #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Guaris
wants to merge
2
commits into
Kong:master
Choose a base branch
from
Guaris:update-readme
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,135 @@ | ||
| ```markdown | ||
| [![Build Status][gha-workflow-badge]][gha-workflow-url] [![Latest release][gha-latest-release]][gha-releases-url] | ||
|
|
||
| # Kong Plugin Development Kit - Go edition | ||
|
|
||
| Docs: https://pkg.go.dev/github.com/Kong/go-pdk. | ||
| Docs: https://pkg.go.dev/github.com/Kong/go-pdk | ||
|
|
||
| The Kong Plugin Development Kit (PDK) for Go lets you write custom Kong Gateway plugins in Go. | ||
|
|
||
| ## Getting Started | ||
|
|
||
| To write a Kong Gateway plugin in Go: | ||
|
|
||
| 1. Define a `struct` type to hold configuration. | ||
| 2. Write a `New()` function to create instances of your struct. | ||
| 3. Add methods to that struct to handle request phases. | ||
| 4. Include the `go-pdk/server` sub-library. | ||
| 5. Add a `main()` function that calls `server.StartServer(New, Version, Priority)`. | ||
| 6. Compile as an executable with `go build`. | ||
|
|
||
| ## Plugin Structure | ||
|
|
||
| ### Configuration struct | ||
|
|
||
| The plugin uses a Go `struct` to receive configuration from Kong: | ||
|
|
||
| ```go | ||
| type MyConfig struct { | ||
| Path string `json:"my_file_path"` | ||
| Reopen bool `json:"reopen"` | ||
| } | ||
| ``` | ||
|
|
||
| ### `New()` constructor | ||
|
|
||
| Define a function to return a new instance of your config struct: | ||
|
|
||
| ```go | ||
| func New() interface{} { | ||
| return &MyConfig{} | ||
| } | ||
| ``` | ||
|
|
||
| ### `main()` function | ||
|
|
||
| Include `github.com/Kong/go-pdk/server` and start the plugin: | ||
|
|
||
| ```go | ||
| func main () { | ||
| server.StartServer(New, Version, Priority) | ||
| } | ||
| ``` | ||
|
|
||
| When run, the plugin creates a socket file within the Kong prefix directory. | ||
|
|
||
| ### Phase handlers | ||
|
|
||
| You can implement logic in the following phases using the same signature: | ||
|
|
||
| ```go | ||
| func (conf *MyConfig) Access(kong *pdk.PDK) { | ||
| ... | ||
| } | ||
| ``` | ||
|
|
||
| Supported phases: | ||
|
|
||
| - `Certificate` | ||
| - `Rewrite` | ||
| - `Access` | ||
| - `Response` *(enables buffered proxy mode)* | ||
| - `Preread` | ||
| - `Log` | ||
|
|
||
| ### Version and priority | ||
|
|
||
| Set execution order with constants: | ||
|
|
||
| ```go | ||
| const Version = "1.0.0" | ||
| const Priority = 1 | ||
| ``` | ||
|
|
||
| Higher priority runs earlier. | ||
|
|
||
| ## Configuring with `kong.conf` | ||
|
|
||
| To register plugins in Kong, define plugin server settings in `kong.conf`: | ||
|
|
||
| ```conf | ||
| pluginserver_names = my-plugin | ||
|
|
||
| pluginserver_my_plugin_socket = /usr/local/kong/my-plugin.socket | ||
| pluginserver_my_plugin_start_cmd = /usr/local/bin/my-plugin | ||
| pluginserver_my_plugin_query_cmd = /usr/local/bin/my-plugin -dump | ||
| ``` | ||
|
|
||
| The socket and start command lines can be omitted if using defaults. | ||
|
|
||
|
|
||
| ## Using in Containers or Kubernetes | ||
|
|
||
| To run Go plugins with Kong Gateway in a container, copy your plugin binary into the Kong image and ensure it is executable by the container. | ||
|
|
||
| > **Note:** Kong's official Docker images run as the `nobody` user. Use `USER root` temporarily to install or copy files. | ||
|
|
||
| Example `Dockerfile`: | ||
|
|
||
| ```dockerfile | ||
| FROM kong | ||
| USER root | ||
|
|
||
| # Copy your compiled Go plugin into the container | ||
| COPY your-go-plugin /usr/local/bin/your-go-plugin | ||
| RUN chmod +x /usr/local/bin/your-go-plugin | ||
|
|
||
| USER kong | ||
| ENTRYPOINT ["/docker-entrypoint.sh"] | ||
| EXPOSE 8000 8443 8001 8444 | ||
| STOPSIGNAL SIGQUIT | ||
| HEALTHCHECK --interval=10s --timeout=10s --retries=10 CMD kong health | ||
| CMD ["kong", "docker-start"] | ||
| ``` | ||
|
|
||
| Ensure the plugin path matches the value set in `kong.conf` under `pluginserver_your_plugin_start_cmd`. | ||
|
|
||
| ## Example Plugins | ||
|
|
||
| Explore [example plugins](https://github.com/Kong/go-pdk/tree/master/examples). | ||
|
|
||
| [gha-workflow-badge]: https://github.com/Kong/go-pdk/actions/workflows/test.yml/badge.svg | ||
| [gha-workflow-url]: https://github.com/Kong/go-pdk/actions/workflows/test.yml | ||
| [gha-latest-release]: https://img.shields.io/github/v/release/Kong/go-pdk.svg | ||
| [gha-releases-url]: https://github.com/Kong/go-pdk/releases | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we also mention the default values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WE can @spacewander I don't know what the default values are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value is already
/usr/local/bin/my-pluginand/usr/local/bin/my-plugin -dumpfor pluginmy-plugin.