Skip to content

Commit ad73e97

Browse files
bentitoci-robot
authored andcommitted
UPSTREAM: <carry>: add source commit into binaries when linking
- Removes extra GIT_COMMIT set - fixup Dockerfiles after rebase - consider "" unset so build-info can fill commit/date - double quote go flags & honor GIT_COMMIT if set - improve robustness of build-info parsing - Trim whitespace on all version fields - isUnset and valueOrUnknown now call strings.TrimSpace - Avoid clobbering values injected via ldflags - set repoState from build-info only when repoState is still unset - set version from build-info only when unset and build-info value is non-empty
1 parent 275daa9 commit ad73e97

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel
259259
image-registry: export GOOS=linux
260260
image-registry: export GOARCH=amd64
261261
image-registry: ## Build the testdata catalog used for e2e tests and push it to the image registry
262-
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o ./testdata/push/bin/push ./testdata/push/push.go
262+
# Use double quotes (not single quotes) for build flags like -ldflags, -tags, etc.
263+
# Single quotes are passed literally and not stripped by `go build`, which can cause errors
264+
# or inject unintended characters into the binary (e.g., version metadata).
265+
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags "$(GO_BUILD_LDFLAGS)" -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o ./testdata/push/bin/push ./testdata/push/push.go
263266
$(CONTAINER_RUNTIME) build -f ./testdata/Dockerfile -t $(E2E_REGISTRY_IMAGE) ./testdata
264267
$(CONTAINER_RUNTIME) save $(E2E_REGISTRY_IMAGE) | $(KIND) load image-archive /dev/stdin --name $(KIND_CLUSTER_NAME)
265268
./testdata/build-test-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME) $(E2E_REGISTRY_IMAGE)
@@ -372,13 +375,16 @@ export GO_BUILD_GCFLAGS := all=-trimpath=$(PWD)
372375
export GO_BUILD_EXTRA_FLAGS :=
373376
export GO_BUILD_LDFLAGS := -s -w \
374377
-X '$(VERSION_PATH).version=$(VERSION)' \
375-
-X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)' \
378+
-X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)'
376379

377380
BINARIES=operator-controller catalogd
378381

379382
.PHONY: $(BINARIES)
380383
$(BINARIES):
381-
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/$@ ./cmd/$@
384+
# use double quotes around $(GO_BUILD_LDFLAGS) to avoid conflicts with the
385+
# single quotes that are embedded inside the variable itself. this prevents
386+
# malformed arguments such as "malformed import path \" \"" when the git commit is empty.
387+
go build $(GO_BUILD_FLAGS) $(GO_BUILD_EXTRA_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags "$(GO_BUILD_LDFLAGS)" -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/$@ ./cmd/$@
382388

383389
.PHONY: build-deps
384390
build-deps: manifests generate fmt

internal/shared/version/version.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package version
33
import (
44
"fmt"
55
"runtime/debug"
6+
"strings"
67
)
78

89
var (
@@ -17,8 +18,32 @@ var (
1718
}
1819
)
1920

21+
// isUnset returns true when the provided string should be treated as an
22+
// "unset" value. Builds that inject ldflags such as "-X var=" will set the
23+
// variable to the empty string, which previously prevented the runtime build
24+
// information gathered via debug.ReadBuildInfo from populating the field. For
25+
// the purposes of version reporting we treat both the empty string and the
26+
// literal "unknown" as unset.
27+
func isUnset(s string) bool {
28+
// trim any surrounding whitespace to ensure accurate unset detection
29+
s = strings.TrimSpace(s)
30+
return s == "" || s == "unknown"
31+
}
32+
2033
func String() string {
21-
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState)
34+
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q",
35+
valueOrUnknown(version),
36+
valueOrUnknown(gitCommit),
37+
valueOrUnknown(commitDate),
38+
valueOrUnknown(repoState))
39+
}
40+
41+
func valueOrUnknown(v string) string {
42+
v = strings.TrimSpace(v)
43+
if v == "" {
44+
return "unknown"
45+
}
46+
return v
2247
}
2348

2449
func init() {
@@ -29,18 +54,20 @@ func init() {
2954
for _, setting := range info.Settings {
3055
switch setting.Key {
3156
case "vcs.revision":
32-
if gitCommit == "unknown" {
57+
if isUnset(gitCommit) {
3358
gitCommit = setting.Value
3459
}
3560
case "vcs.time":
36-
commitDate = setting.Value
61+
if isUnset(commitDate) {
62+
commitDate = setting.Value
63+
}
3764
case "vcs.modified":
38-
if v, ok := stateMap[setting.Value]; ok {
65+
if v, ok := stateMap[setting.Value]; ok && isUnset(repoState) {
3966
repoState = v
4067
}
4168
}
4269
}
43-
if version == "unknown" {
70+
if isUnset(version) && info.Main.Version != "" {
4471
version = info.Main.Version
4572
}
4673
}

openshift/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ include $(addprefix $(DIR)/vendor/github.com/openshift/build-machinery-go/make/,
77

88
include $(DIR)/.bingo/Variables.mk
99

10+
# Prefer the upstream source commit that the Dockerfile passes in via
11+
# `ENV GIT_COMMIT=<sha>`. If that variable is not already defined fall back to
12+
# the commit recorded by the OpenShift image build pipeline.
13+
ifeq ($(origin GIT_COMMIT), undefined)
14+
GIT_COMMIT := $(OPENSHIFT_BUILD_COMMIT) # populated by OpenShift build machinery
15+
endif
16+
export GIT_COMMIT
17+
VERSION_PATH := github.com/operator-framework/operator-controller/internal/shared/version
18+
export GO_BUILD_LDFLAGS := -s -w -X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)'
19+
1020
.PHONY: verify
1121
verify: ## Run downstream-specific verify
1222
$(MAKE) tidy fmt generate -C $(DIR)/../
@@ -46,7 +56,6 @@ test-experimental-e2e: ## Run the experimental e2e tests.
4656
cd $(DIR)/../; \
4757
go test $(DOWNSTREAM_EXPERIMENTAL_E2E_FLAGS) ./test/experimental-e2e/...;
4858

49-
export GIT_COMMIT := ${SOURCE_GIT_COMMIT}
5059
PHONY: go-build-local
5160
go-build-local:
52-
$(MAKE) -n -f Makefile go-build-local
61+
$(MAKE) -f Makefile go-build-local

openshift/catalogd.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS builder
22

3+
ARG SOURCE_GIT_COMMIT
34
ENV GIT_COMMIT=${SOURCE_GIT_COMMIT}
45
WORKDIR /build
56
COPY . .
6-
RUN make -f openshift/Makefile go-build-local
7+
RUN make go-build-local
78

89
FROM registry.ci.openshift.org/ocp/4.20:base-rhel9
910
USER 1001

openshift/operator-controller.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS builder
22

3+
ARG SOURCE_GIT_COMMIT
34
ENV GIT_COMMIT=${SOURCE_GIT_COMMIT}
45
WORKDIR /build
56
COPY . .

0 commit comments

Comments
 (0)