-
Notifications
You must be signed in to change notification settings - Fork 31
OCPBUGS-23055: bugfix for operator-controller not populating commit info #376
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
Changes from all commits
ea7eb1c
8ae090d
e29a35b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,7 +252,10 @@ E2E_REGISTRY_IMAGE=localhost/e2e-test-registry:devel | |
image-registry: export GOOS=linux | ||
image-registry: export GOARCH=amd64 | ||
image-registry: ## Build the testdata catalog used for e2e tests and push it to the image registry | ||
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 | ||
# Use double quotes (not single quotes) for build flags like -ldflags, -tags, etc. | ||
# Single quotes are passed literally and not stripped by `go build`, which can cause errors | ||
# or inject unintended characters into the binary (e.g., version metadata). | ||
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 | ||
$(CONTAINER_RUNTIME) build -f ./testdata/Dockerfile -t $(E2E_REGISTRY_IMAGE) ./testdata | ||
$(CONTAINER_RUNTIME) save $(E2E_REGISTRY_IMAGE) | $(KIND) load image-archive /dev/stdin --name $(KIND_CLUSTER_NAME) | ||
./testdata/build-test-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME) $(E2E_REGISTRY_IMAGE) | ||
|
@@ -374,13 +377,16 @@ export GO_BUILD_GCFLAGS := all=-trimpath=$(PWD) | |
export GO_BUILD_EXTRA_FLAGS := | ||
export GO_BUILD_LDFLAGS := -s -w \ | ||
-X '$(VERSION_PATH).version=$(VERSION)' \ | ||
-X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)' \ | ||
-X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)' | ||
|
||
BINARIES=operator-controller catalogd | ||
|
||
.PHONY: $(BINARIES) | ||
$(BINARIES): | ||
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/$@ | ||
# use double quotes around $(GO_BUILD_LDFLAGS) to avoid conflicts with the | ||
# single quotes that are embedded inside the variable itself. this prevents | ||
# malformed arguments such as "malformed import path \" \"" when the git commit is empty. | ||
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/$@ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reformated a bit, hopefully better now. see next commit |
||
|
||
.PHONY: build-deps | ||
build-deps: manifests generate fmt | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package version | |
import ( | ||
"fmt" | ||
"runtime/debug" | ||
"strings" | ||
) | ||
|
||
var ( | ||
|
@@ -17,8 +18,32 @@ var ( | |
} | ||
) | ||
|
||
// isUnset returns true when the provided string should be treated as an | ||
// "unset" value. Builds that inject ldflags such as "-X var=" will set the | ||
// variable to the empty string, which previously prevented the runtime build | ||
// information gathered via debug.ReadBuildInfo from populating the field. For | ||
// the purposes of version reporting we treat both the empty string and the | ||
// literal "unknown" as unset. | ||
func isUnset(s string) bool { | ||
// trim any surrounding whitespace to ensure accurate unset detection | ||
s = strings.TrimSpace(s) | ||
return s == "" || s == "unknown" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The string will be used many times, could we create a const with this value?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, Per noted too, changing in next commit |
||
} | ||
|
||
func String() string { | ||
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState) | ||
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", | ||
valueOrUnknown(version), | ||
valueOrUnknown(gitCommit), | ||
valueOrUnknown(commitDate), | ||
valueOrUnknown(repoState)) | ||
} | ||
|
||
func valueOrUnknown(v string) string { | ||
v = strings.TrimSpace(v) | ||
if v == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we strings.TrimSpace here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, |
||
return "unknown" | ||
} | ||
return v | ||
} | ||
|
||
func init() { | ||
|
@@ -29,18 +54,20 @@ func init() { | |
for _, setting := range info.Settings { | ||
switch setting.Key { | ||
case "vcs.revision": | ||
if gitCommit == "unknown" { | ||
if isUnset(gitCommit) { | ||
gitCommit = setting.Value | ||
} | ||
case "vcs.time": | ||
commitDate = setting.Value | ||
if isUnset(commitDate) { | ||
commitDate = setting.Value | ||
} | ||
case "vcs.modified": | ||
if v, ok := stateMap[setting.Value]; ok { | ||
if v, ok := stateMap[setting.Value]; ok && isUnset(repoState) { | ||
repoState = v | ||
} | ||
} | ||
} | ||
if version == "unknown" { | ||
if isUnset(version) && info.Main.Version != "" { | ||
version = info.Main.Version | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS builder | ||
|
||
ARG SOURCE_GIT_COMMIT | ||
ENV GIT_COMMIT=${SOURCE_GIT_COMMIT} | ||
WORKDIR /build | ||
COPY . . | ||
RUN make go-build-local && \ | ||
RUN make -f openshift/Makefile go-build-local && \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to make this change to sort out the issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the extra “-f openshift/Makefile” is necessary
Because the build currently works only with the downstream Makefile, the flag is not optional; removing it would silently switch us back to the upstream build path and drop the commit/version injection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I should have removed this question. I put it first in the review, and then, after navigating, I got it. |
||
# Build the OLMv1 Test Extension binary. | ||
# This is used by openshift/origin to allow us to register the OLMv1 test extension | ||
# The binary needs to be added in the component image and OCP image | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO: It seems better adding the inline comment — this is a subtle detail that can easily break things and go unnoticed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure we can have the comment here too, can't hurt. Changing in next commit