Skip to content
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

Cannot build statically linked plugins because of CGO use in sigs.k8s.io/kustomize/v3/pkg #1482

Closed
oboukili opened this issue Aug 26, 2019 · 6 comments

Comments

@oboukili
Copy link

oboukili commented Aug 26, 2019

Hello,

I'm trying to build a statically linked kustomize v3 Go plugin, but it seems the use of CGO is required by sigs.k8s.io/kustomize/v3/pkg.

$ CGO_ENABLED=0 GOOS=linux go build -a -buildmode plugin -ldflags="-s -w" -o plugin                                     
loadinternal: cannot find runtime/cgo

Enabling the use of CGO works of course, however it results in a dynamically linked binary, not suitable for distribution (being a Go beginner, I've read an interesting article here: https://dave.cheney.net/2016/01/18/cgo-is-not-go):

$ CGO_ENABLED=1 GOOS=linux go build -a -buildmode plugin -ldflags="-s -w" -o plugin

$ file plugin
plugin: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=d7aaee9f3aaf211fbec2f71be6b9b9c3af422a63, stripped

$ ldd plugin
	linux-vdso.so.1 (0x00007fff633d2000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f50641da000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5064019000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f5065cf8000)

Here's my list of imports:

import (
	"github.com/pkg/errors"
	"go.mozilla.org/sops/decrypt"
	"k8s.io/apimachinery/pkg/util/json"
	"os"
	"sigs.k8s.io/kustomize/v3/pkg/ifc"
	"sigs.k8s.io/kustomize/v3/pkg/resmap"
	"sigs.k8s.io/kustomize/v3/pkg/types"
	"sigs.k8s.io/yaml"
)

I couldn't find which exact import among ifc,resmap or types required it, however since I never had that kind of issue with the other imports I'm pretty sure it comes from here.

Is using CGO a definitive sigs.k8s.io/kustomize/v3/pkg/ prerequisite?

Best regards,

@oboukili
Copy link
Author

I managed to read a bit of documentation and found out that go list could help pinpoint which import brought cgo (http://golang.so/pkg/cmd/go/internal/list/ <- each import) , so with a bit of tinkering I found which ones required cgo:

$ go list -f "{{if .CgoFiles}}{{.ImportPath}} {{.CgoFiles}}{{end}}" $(go list -f "{{.ImportPath}}{{range .Deps}} {{.}}{{end}}")
net [cgo_linux.go cgo_resnew.go cgo_socknew.go cgo_unix.go]
os/user [cgo_lookup_unix.go getgrouplist_unix.go listgroups_unix.go]
runtime/cgo [cgo.go]

So I guess it's not kustomize after all! Sorry about that

@oboukili
Copy link
Author

oboukili commented Aug 26, 2019

To bring a closing comment to this issue, this is actually a golang issue when using -buildmode plugin: golang/go#19569

@arthurgustin
Copy link
Contributor

@oboukili did you manage to use your plugin ? I'm getting fails to load: plugin: not implemented when trying to use mine... Created an issue for this but it seems related to yours #1641

@oboukili
Copy link
Author

@arthurgustin this error means you have not put your plugin in your plugins home folder, read https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/goPluginGuidedExample.md#make-a-home-for-plugins for more info, cheers

@oboukili
Copy link
Author

@arthurgustin
Copy link
Contributor

@oboukili I override XDG_CONFIG_HOME to point to my plugin location and it works for bash plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants