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

[Proposal] Enable CGO as much as possible #557

Open
mzz2017 opened this issue Jun 20, 2024 · 1 comment · May be fixed by #564
Open

[Proposal] Enable CGO as much as possible #557

mzz2017 opened this issue Jun 20, 2024 · 1 comment · May be fixed by #564

Comments

@mzz2017
Copy link
Contributor

mzz2017 commented Jun 20, 2024

Proposal

在编译参数 CGO_ENABLED=0 的情况下,lima 虚拟机内的 dae 始终 hang 在:

$ sudo ./dae run -c /etc/dae/config.dae
[Jun 21 02:00:36]  INFO Include config files: [/etc/dae/config.dae]
[Jun 21 02:00:36]  INFO Waiting for network...

编译参数 CGO_ENABLED=1 时一切正常。这可能是 go resolver 不支持 /etc/nsswitch.conf 导致的。

Use Cases

一些特殊场景(例如 DNS 特异环境),提高兼容性。

Potential Benefits

避免一些异常的 go resolver 行为。

Scope

No response

Reference

No response

Implementation

最简单地,可开启 CGO_ENABLED=1,使得 DNS 工作正常:

make CGO_ENABLED=1

这也是打包时的建议方案,因此建议修改 Makefile 默认 CGO_ENABLED=1。

为了提高基础库版本兼容性,可使用 musl-gcc 来尽可能开启 CGO 的情况下保持静态编译:

sudo apt install musl-tools
export CGO_ENABLED=1
export CC=musl-gcc
export BUILD_ARGS='-ldflags "-s -w -linkmode external -extldflags=-static"'
make
$ sudo ./dae run -c /etc/dae/config.dae
[Jun 21 02:25:30]  INFO Include config files: [/etc/dae/config.dae]
[Jun 21 02:25:30]  INFO Waiting for network...
[Jun 21 02:25:31]  INFO Network online.
[Jun 21 02:25:31]  INFO Fetching subscriptions...
[Jun 21 02:25:32]  INFO Loading eBPF programs and maps into the kernel...
[Jun 21 02:25:32]  INFO The loading process takes about 120MB free memory, which will be released after loading. Insufficient memory will cause loading failure.
[Jun 21 02:25:33]  INFO Loaded eBPF programs and maps

CI 里可以利用 zig 编译器来支持交叉编译:https://hrishikeshpathak.com/tips/build-static-binary-cross-compile-cgo-project-zig-compiler/

@dae-prow
Copy link
Contributor

dae-prow bot commented Jun 20, 2024

Thanks for opening this issue!

@mzz2017 mzz2017 linked a pull request Jun 24, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant