Skip to content

Commit e77b072

Browse files
committed
fix(cli/version): Insert build info in go tool installation
The go install and go build installation methods now set all the available fields in the version subcommand output. The version resolution logic has been changed to extract build info from the binary itself, rather than inject the info using ldflags at build time. Assisted by: Google/Gemini 3 Pro
1 parent 1c6f06e commit e77b072

File tree

9 files changed

+255
-442
lines changed

9 files changed

+255
-442
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ docs/book/src/docs
1616
# skip bin dirs
1717
**/bin
1818
**/testbin
19+
**/dist
1920

2021
# skip .out files (coverage tests)
2122
*.out

Makefile

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,9 @@ help: ## Display this help
5050

5151
##@ Build
5252

53-
K8S_VERSION ?= $(shell go list -m -modfile=./testdata/project-v4/go.mod -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d.%d", $$3, $$4}')
54-
55-
LD_FLAGS=-ldflags " \
56-
-X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion=$(shell git describe --tags --dirty --broken) \
57-
-X sigs.k8s.io/kubebuilder/v4/cmd.kubernetesVendorVersion=$(K8S_VERSION) \
58-
-X sigs.k8s.io/kubebuilder/v4/cmd.goos=$(shell go env GOOS) \
59-
-X sigs.k8s.io/kubebuilder/v4/cmd.goarch=$(shell go env GOARCH) \
60-
-X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit=$(shell git rev-parse HEAD) \
61-
-X sigs.k8s.io/kubebuilder/v4/cmd.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') \
62-
"
6353
.PHONY: build
6454
build: ## Build the project locally
65-
go build $(LD_FLAGS) -o bin/kubebuilder
55+
go build --trimpath -o bin/kubebuilder
6656

6757
.PHONY: install
6858
install: build ## Build and install the binary with the current source code. Use it to test your changes locally.
@@ -72,7 +62,7 @@ install: build ## Build and install the binary with the current source code. Use
7262
##@ Development
7363

7464
.PHONY: generate
75-
generate: generate-testdata generate-docs update-k8s-version ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
65+
generate: generate-testdata generate-docs ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
7666
go mod tidy
7767
make remove-spaces
7868

@@ -216,17 +206,6 @@ install-helm: ## Install the latest version of Helm locally
216206
helm-lint: install-helm ## Lint the Helm chart in testdata
217207
helm lint testdata/project-v4-with-plugins/dist/chart
218208

219-
.PHONY: update-k8s-version
220-
update-k8s-version: ## Update Kubernetes API version in version.go and .goreleaser.yml
221-
@if [ -z "$(K8S_VERSION)" ]; then echo "Error: K8S_VERSION is empty"; exit 1; fi
222-
@echo "Updating Kubernetes version to $(K8S_VERSION)"
223-
@# Update version.go
224-
@sed -i.bak 's/kubernetesVendorVersion = .*/kubernetesVendorVersion = "$(K8S_VERSION)"/' cmd/version.go
225-
@# Update .goreleaser.yml
226-
@sed -i.bak 's/KUBERNETES_VERSION=.*/KUBERNETES_VERSION=$(K8S_VERSION)/' build/.goreleaser.yml
227-
@# Clean up backup files
228-
@find . -name "*.bak" -type f -delete
229-
230209
## Tool Binaries
231210
GO_APIDIFF ?= $(LOCALBIN)/go-apidiff
232211
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

build/.goreleaser.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ builds:
3131
- id: kubebuilder
3232
binary: kubebuilder
3333
mod_timestamp: "{{ .CommitTimestamp }}"
34-
ldflags:
35-
- -X sigs.k8s.io/kubebuilder/v4/cmd.kubeBuilderVersion={{ .Version }}
36-
- -X sigs.k8s.io/kubebuilder/v4/cmd.goos={{ .Os }}
37-
- -X sigs.k8s.io/kubebuilder/v4/cmd.goarch={{ .Arch }}
38-
- -X sigs.k8s.io/kubebuilder/v4/cmd.gitCommit={{ .Commit }}
39-
- -X sigs.k8s.io/kubebuilder/v4/cmd.buildDate={{ .Date }}
40-
- -X sigs.k8s.io/kubebuilder/v4/cmd.kubernetesVendorVersion={{ .Env.KUBERNETES_VERSION }}
4134
targets:
4235
- linux_amd64
4336
- linux_arm64
@@ -46,12 +39,11 @@ builds:
4639
- darwin_amd64
4740
- darwin_arm64
4841
env:
49-
- KUBERNETES_VERSION=1.34.1
5042
- CGO_ENABLED=0
5143

5244
# Only binaries of the form "kubebuilder_${goos}_${goarch}" will be released.
5345
archives:
54-
- formats: ['binary']
46+
- formats: ["binary"]
5547
# Setting name_template correctly maps checksums to binary names.
5648
name_template: "{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}{{ if .Mips }}_{{ .Mips }}{{ end }}"
5749

cmd/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/spf13/afero"
2424

25+
"sigs.k8s.io/kubebuilder/v4/internal/version"
2526
"sigs.k8s.io/kubebuilder/v4/pkg/cli"
2627
cfgv3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
2728
"sigs.k8s.io/kubebuilder/v4/pkg/logging"
@@ -62,10 +63,11 @@ func Run() {
6263
slog.Error("error discovering external plugins", "error", err)
6364
}
6465

66+
v := version.New()
6567
c, err := cli.New(
6668
cli.WithCommandName("kubebuilder"),
67-
cli.WithVersion(versionString()),
68-
cli.WithCliVersion(getKubebuilderVersion()),
69+
cli.WithVersion(v.PrintVersion()),
70+
cli.WithCliVersion(v.GetKubeBuilderVersion()),
6971
cli.WithPlugins(
7072
golangv4.Plugin{},
7173
gov4Bundle,

cmd/version.go

Lines changed: 0 additions & 260 deletions
This file was deleted.

0 commit comments

Comments
 (0)