Skip to content

Reduce binary size using -s -w ldflags#9627

Open
caxu-rh wants to merge 1 commit intovelero-io:mainfrom
caxu-rh:ldflags-s-w
Open

Reduce binary size using -s -w ldflags#9627
caxu-rh wants to merge 1 commit intovelero-io:mainfrom
caxu-rh:ldflags-s-w

Conversation

@caxu-rh
Copy link
Copy Markdown

@caxu-rh caxu-rh commented Mar 18, 2026

Thank you for contributing to Velero!

Please add a summary of your change

This change adds the Go ldflags -s -w which omits the symbol table and DWARF debugging information from the resulting binaries. The flags are added in GoReleaser configuration, Dockerfile{-Windows}, and the hack/build.sh for consistency. This allows for some reductions in binary size. Note that the size of release tarballs might not change as much (symbol table and DWARF debug info compress well), but the size of the unpacked binary on-disk will see improvements.

From local testing:

os/arch CGO_ENABLED=0 go build ./cmd/velero/velero.go CGO_ENABLED=0 go build -ldflags '-s -w' ./cmd/velero/velero.go
darwin/arm64 132M 90M
darwin/amd64 139M 97M
linux/amd64 135M 94M
linux/arm64 126M 87M
linux/arm 124M 86M
linux/ppc64le 131M 92M
linux/s390x 135M 95M
windows/amd64 136M 97M

Does your change fix a particular issue?

This change does not fix a particular issue.

Please indicate you've done the following:

Signed-off-by: Caleb Xu <caxu@redhat.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.73%. Comparing base (48e66b1) to head (37d1e9f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9627      +/-   ##
==========================================
- Coverage   60.76%   60.73%   -0.03%     
==========================================
  Files         387      387              
  Lines       36618    36618              
==========================================
- Hits        22250    22241       -9     
- Misses      12773    12781       +8     
- Partials     1595     1596       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ywk253100
Copy link
Copy Markdown
Contributor

Thanks @caxu-rh for the PR!
Based on the size of release tarballs might not change as much (symbol table and DWARF debug info compress well), seems the change has not significantly reduced the size of deliverables.
And without the symbol table, we could not see the function names when the process panics or prints the error message with stack, which impacts the debugging.

@caxu-rh
Copy link
Copy Markdown
Author

caxu-rh commented Mar 23, 2026

seems the change has not significantly reduced the size of deliverables.

Yes, but this should still reduce the size of what users will actually run/install/keep on their disk. You can't execute the binary without unpacking it.

we could not see the function names when the process panics or prints the error message with stack, which impacts the debugging.

I think the function names/stack trace is not impacted by the addition of these flags. For example, I inserted a panic(nil) in pkg/client/factory.go to see how it would look:

panic: panic called with nil argument

goroutine 1 [running]:
github.com/vmware-tanzu/velero/pkg/client.NewFactory(...)
	/Users/caxu/velero/ldflags-s-w/pkg/client/factory.go:91
github.com/vmware-tanzu/velero/pkg/cmd/velero.NewCommand({0x16d1eb3b2, 0x6})
	/Users/caxu/velero/ldflags-s-w/pkg/cmd/velero/velero.go:97 +0x124
main.main()
	/Users/caxu/velero/ldflags-s-w/cmd/velero/velero.go:34 +0x50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants