I’m trying to create a plugin based on the example docs provided and get stuck on the build step (Golang plugins).
docker run --rm -v `pwd`:/plugin-source \
--platform=linux/amd64 \
tykio/tyk-plugin-compiler:v5.2.5 plugin.so
PLUGIN_BUILD_PATH: /go/src/[github.com/TykTechnologies/plugin_plugin](http://github.com/TykTechnologies/plugin_plugin)
PLUGIN_SOURCE_PATH: /plugin-source
INFO: No plugin id provided, keeping go.mod as is
go: errors parsing go.mod:
/go/src/[github.com/TykTechnologies/plugin_plugin/go.mod:3](http://github.com/TykTechnologies/plugin_plugin/go.mod:3): invalid go version '1.22.1': must match format 1.23
I’ve tried changing the go version in go.mod to 1.22 to try and heed to error message, but then get a different error message saying that it “cannot compile Go 1.22 code.”
docker run --rm -v `pwd`:/plugin-source \
--platform=linux/amd64 \
tykio/tyk-plugin-compiler:v5.2.5 plugin.so
PLUGIN_BUILD_PATH: /go/src/[github.com/TykTechnologies/plugin_plugin](http://github.com/TykTechnologies/plugin_plugin)
PLUGIN_SOURCE_PATH: /plugin-source
INFO: No plugin id provided, keeping go.mod as is
tyk-plugin: cannot compile Go 1.22 code
I’m currently running this command on an M1 mac, but was able to confirm that my colleague was running into a similar issue on his linux workstation. I am able to compile if I drop our gateway and compiler version to 5.0.0, so it’s not just an issue with my local setup.
$ go version
go version go1.22.1 darwin/arm64
$ docker version
Client: Docker Engine - Community
Version: 24.0.6
API version: 1.43
Go version: go1.21.1
Git commit: ed223bc820
Built: Thu Aug 31 17:24:32 2023
OS/Arch: darwin/arm64
Context: desktop-linux
Server: Docker Desktop 4.24.2 (124339)
Engine:
Version: 24.0.6
API version: 1.43 (minimum version 1.12)
Go version: go1.20.7
Git commit: 1a79695
Built: Mon Sep 4 12:31:36 2023
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.6.22
GitCommit: 8165feabfdfe38c65b599c4993d227328c231fca
runc:
Version: 1.1.8
GitCommit: v1.1.8-0-g82f18fe
docker-init:
Version: 0.19.0
GitCommit: de40ad0
$ docker-compose version
Docker Compose version 2.24.3
afaik you cannot use another Go version than the one used with the version the Gateway was compiled with (in case of v5.2.5, this is Go 1.19).
First, the build setup within the container is 1.19, which can’t compile 1.22, and second is that the created “binary” would be incompatible with the gateway (at least in case you want to build a "native2 Go plugin, not a gRPC one).
This is absolutely right. The limitation is with golang rather than the gateway, but for a module to be loadable it must have been compiled with the same golang version and the same versions of all of the libraries used. This is why the plugin compiler image is provided.
To get around the go.mod problem I use the plugin compiler to generate it like this:
(Please backup your go.mod and go.sum before doing this if they’re important to you.)
rm go.mod go.sum
docker container run -v `pwd`:/plugin-source -t --workdir /plugin-source --entrypoint go --rm tykio/tyk-plugin-compiler:v5.2.5 mod init myCustomPlugin
Then I can compile the plugin. Since you’re using v5.2.5 it’s possible to get the compiler to run the necessary go mod tidy and go get commands itself like this:
Note the $(date +%s%N) on the end of the compilation command above. This adds a plugin id that is unique to this build of the plugin. Golang requires that plugins have a unique plugin id. Without a unique plugin id you’ll eventually run into the dreaded plugin is already loaged error during plugin development.
Once this is completed you should have a directory that looks like this no matter what OS or local golang version is installed.:
$ ls -l
total 8636
-rw-rw-r-- 1 user group 450 Nov 2 2022 myCustomPlugin.go
-rw-r--r-- 1 user group 31 Apr 2 09:24 go.mod
-rw-r--r-- 1 root root 8826904 Apr 2 09:27 myCustomPlugin_v5.2.5_linux_amd64.so
Thanks for the detailed instructions @Pete ! The go version incompatibility was definitely the issue and now I can compile the plugin with your instructions!
Hi @Pete, after performing all the steps above, I am encountering the following error. Since we are using TYK 5.4.0:
plugin.Open("/opt/tyk-gateway/middleware/CustomGoPlugin_v5.4.0_linux_amd64.so"): The plugin was built with a different version of the package golang.org/x/net/internal/socks. mwPath=/opt/tyk-gateway/middleware/CustomGoPlugin_v5.4.0_linux_amd64.so, mwSymbolName=Internal