Customize the kong plugin golang version

To develop a go version of the kong plugin, usually the following three steps are required:
write and compile the kong plugin based on the go language version PDK provided by kong --> configure the plugin location in the kong.conf file of kong --> through kong's admin api to enable plugins

1. Use the go language to write the kong plug-in

1. Define the Config object

type Config struct {
    
    
	Message string
}

2. Define New() to instantiate the Config object

func New() interface{
    
    } {
    
    
	return &Config{
    
    }
}

3. Define the access phase to process the request

func (conf Config) Access(kong *pdk.PDK) {
    
    
	host, err := kong.Request.GetHeader("host")
	if err != nil {
    
    
		log.Printf("Error reading 'host' header: %s", err.Error())
	}

	message := conf.Message
	if message == "" {
    
    
		message = "hello"
	}
	kong.Response.SetHeader("x-hello-from-go", fmt.Sprintf("Go says %s to %s", message, host))
}

4. Import the go-pdk/server package into the project

import (
	"fmt"
	"log"

	"github.com/Kong/go-pdk"
	"github.com/Kong/go-pdk/server"
)

5. Create a main function and use server.startServer to start

func main() {
    
    
	server.StartServer(New, Version, Priority)
}

Add a main() function that calls server.StartServer(New, Version, Priority).

6. Compile the code written above into an executable file

The above codes are all derived from the example code in kong’s go-plugins, the address is https://github.com/Kong/go-plugins/tree/master
Here we can download the code directly on the linux server through git

6.1. Download the source code corresponding to plugins

[root@min ~]# git clone https://github.com/Kong/go-plugins.git
Cloning into 'go-plugins'...
remote: Enumerating objects: 76, done.
remote: Counting objects: 100% (51/51), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 76 (delta 27), reused 22 (delta 17), pack-reused 25
Unpacking objects: 100% (76/76), done.

6.2. Go to the downloaded code package and compile it

[root@min ~]# cd go-plugins/
[root@min go-plugins]# ls
go-hello.go  go-hello-lm.go  go-log.go  go-log-lm.go  go.mod  go.sum  LICENSE  Makefile  README.md
[root@min go-plugins]# go build go-hello.go 
[root@min go-plugins]# ls
go-hello  go-hello.go  go-hello-lm.go  go-log.go  go-log-lm.go  go.mod  go.sum  LICENSE  Makefile  README.md

6.3. Create a file to store the compiled go-hello

[root@min go-plugins]# mkdir -p /etc/kong/plugins

6.4. Move go-hello into the /etc/kong/plugins directory

[root@min go-plugins]# mv go-hello /etc/kong/plugins/
[root@min go-plugins]# ls
go-hello.go  go-hello-lm.go  go-log.go  go-log-lm.go  go.mod  go.sum  LICENSE  Makefile  README.md
[root@min go-plugins]# ls /etc/kong/plugins/
go-hello

2. Add a reference to the plugin in the kong configuration file kong.conf

2.1, kong.conf configuration kong plug-in location

Write the following content in the kong configuration file, and then hang the configuration file of the host in the docker through -v /etc/kong/:/etc/kong/

[root@min kong]# vi kong.conf 
[root@min kong]# cat kong.conf 
plugins = bundled,go-hello
pluginserver_names = go-hello
pluginserver_go_hello_socket = /usr/local/kong/go-hello.socket
pluginserver_go_hello_start_cmd = /etc/kong/plugins/go-hello
pluginserver_go_hello_query_cmd = /etc/kong/plugins/go-hello -dump

[root@min kong]# 

# 2.2, use docker to start the kong container

If you don’t know how to install kong, please refer to the kong installation guide I wrote before, address: https://blog.csdn.net/zhangshenglu1/article/details/130934300

[root@min kong]# docker run -d --name kong-gateway \
>  --network=kong-net \
>  -e "KONG_DATABASE=postgres" \
>  -e "KONG_PG_HOST=kong-database" \
>  -e "KONG_PG_USER=kong" \
>  -e "KONG_PG_PASSWORD=kongpass" \
>  -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
>  -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
>  -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
>  -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
>  -e "KONG_ADMIN_LISTEN=0.0.0.0:8001" \
>  -e "KONG_ADMIN_GUI_URL=http://localhost:8002" \
>  -e KONG_LICENSE_DATA \
>  -p 8000:8000 \
>  -p 8443:8443 \
>  -p 8001:8001 \
>  -p 8444:8444 \
>  -p 8002:8002 \
>  -p 8445:8445 \
>  -p 8003:8003 \
>  -p 8004:8004 \
>  -v /etc/kong:/etc/kong \
>  kong/kong-gateway:3.3.0.0
1a1768bddb94fee9a23249d14ba2e6f45c54950c0d35d1bb76fce72cce964e15

After the installation is complete, check whether the corresponding kong-gateway container is up. The
insert image description here
left and right of this plug-in is to get the sent request host, and then add a x-hello-from-go response header.

3. Enable the go-hello plugin

[root@min kong]# curl -X POST http://localhost:8001/plugins/ \
      --data "name=go-hello" \
      --data "config.message=go-hello" 
{
    
    "protocols":["grpc","grpcs","http","https"],"id":"d5336e66-1ff3-47ea-9b2a-31f8b52c9b05","consumer":null,"instance_name":null,"config":{
    
    "message":"go-hello"},"tags":null,"updated_at":1685563005,"name":"go-hello","ordering":null,"service":null,"enabled":true,"route":null,"created_at":1685563005}

The name in the request is a required parameter, which represents which plug-in to open. The plug-in name we configure here is go-hello, so the value of name needs to be given to go-hello.
The value of config.message can be specified arbitrarily. Since there is no servicename, serviceid
/route and other information specified here, it is globally effective.
If you want to take effect for the service, you can use the following command :
curl -X POST
http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins
–data “name=go-hello”
–data “config.message=go-hello”
If you only want to take effect for a certain route, you can use the following command
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins
–data “name=go-hello”
–data “config.message=go - hello"

Check whether the plug-in has run successfully, here we can watch it on konga, or call it through the admin api interface.

insert image description here
As you can see from here, our go-hello has been installed

4. Test whether the function of the go-hello plugin is normal

We initiate a request call from postman, and then observe whether there is an x-hello-from-go response header in the response header. From the test results, we can see that it meets our expectations. So far, we have completed a simple kong plug-in development and configuration!

insert image description here

Guess you like

Origin blog.csdn.net/zhangshenglu1/article/details/130966505