Tyk Go Plugin latest version (v4.0.3)

Hi there,

We’re trying to write a custom plugin in Golang, but we’re facing some issues during compilation. For starters, the documentation mentions v3.2.2 and v3.2.1, but we’re using a newer version of Tyk gateway (e.g. v4.0.3).

Can someone provide an example go.mod for newer versions of Tyk? Is the documentation out of date ? It seems that even for v3.2.2. the tag resolves to the go-module version ‘v1.9.2-0.20210930081546-bda54b0f790c’, what’s the version we should use for v4.0.3 ?

Kind regards, Barry

1 Like

Hi Barry,

These are the commands I use for 4.0.3 golang plugins. Be aware of the initial rm -rf command. It may not be what you want but it suits the way I work

rm -rf go.mod go.sum vendor/
go mod edit -replace github.com/jensneuse/graphql-go-tools=github.com/TykTechnologies/graphql-go-tools@0cc35471c1c
go get github.com/TykTechnologies/tyk@6c76e802a29838d058588ff924358706a078d0c5
go mod tidy
go mod vendor
docker container run -v $(pwd):/plugin-source --rm tykio/tyk-plugin-compiler:v4.0.3 plugin.so

I’ve had success with them for simple golang plugins.

Cheers,
Pete

1 Like

Hi @Pete,

Thanks for your response. If we remove the go.mod file, then the second and third command will fail.

Perhaps you can share your go.mod file? I also have a very straightforward plugin, without any dependencies.

Thanks, Barry

Actually I was able to get this to work, using the following version for tyk v4.0.3:
github.com/TykTechnologies/tyk v1.9.2-0.20220614105651-6c76e802a298

For reference, this is the go.mod file:

module my_plugin

go 1.15

replace github.com/jensneuse/graphql-go-tools => github.com/TykTechnologies/graphql-go-tools v1.6.2-0.20220426094453-0cc35471c1ca
require github.com/TykTechnologies/tyk v1.9.2-0.20220614105651-6c76e802a298 // indirect

Kind regards, Barry

Hi Barry,

I’m so sorry, there was a copy and paste error there. I missed the go mod init step

rm -rf go.mod go.sum vendor/
go mod init tyk-plugin
go mod edit -replace github.com/jensneuse/graphql-go-tools=github.com/TykTechnologies/graphql-go-tools@0cc35471c1c
go get github.com/TykTechnologies/tyk@6c76e802a29838d058588ff924358706a078d0c5
go mod tidy
go mod vendor
docker container run -v $(pwd):/plugin-source --rm tykio/tyk-plugin-compiler:v4.0.3 plugin.so

Cheers,
Pete

1 Like

Exactly, thanks for confirming and sharing your script. How did you identify the git commit hash ? (so that in the future we can also support the upcoming versions of Tyk gateway)

Kind regards, Barry

From within the tyk repo just use

git show-ref tags/v4.0.3

It’s a bit harder to find the graphql-go-tools version, but if you checkout the tyk repo at the version you’re building for version it’s in go.sum.

Cheers,
Pete

1 Like

Compilation steps for v4.0.4:

rm -rf go.mod go.sum vendor/
go mod init tyk-plugin
go mod edit -replace github.com/jensneuse/graphql-go-tools=github.com/TykTechnologies/[email protected]
go get github.com/TykTechnologies/tyk@af3430ae6e1ca689365bf67416c590d3201b7929
go mod tidy
go mod vendor
docker container run -v $(pwd):/plugin-source --rm tykio/tyk-plugin-compiler:v4.0.4 plugin.so

I also stumbled across this repo: tyk-plugins/build.sh at master · ps258/tyk-plugins · GitHub
It contains instructions for all versions of Tyk, although it has not been updated with 4.0.4 and 4.1.0

Barry