From ea7eb1cdb5fc853bd7edde9b3482ead1fb0393c7 Mon Sep 17 00:00:00 2001 From: Rashmi Gottipati Date: Fri, 6 Jun 2025 16:25:43 -0400 Subject: [PATCH 1/6] UPSTREAM: : set GIT_COMMIT env from SOURCE_GIT_COMMIT in Dockerfiles for operator-controller and catalogd Signed-off-by: Rashmi Gottipati --- openshift/catalogd.Dockerfile | 2 ++ openshift/operator-controller.Dockerfile | 2 ++ openshift/registry.Dockerfile | 3 +++ 3 files changed, 7 insertions(+) diff --git a/openshift/catalogd.Dockerfile b/openshift/catalogd.Dockerfile index 0774772ee..a4b586e8b 100644 --- a/openshift/catalogd.Dockerfile +++ b/openshift/catalogd.Dockerfile @@ -1,4 +1,6 @@ FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS builder + +ENV GIT_COMMIT=${SOURCE_GIT_COMMIT} WORKDIR /build COPY . . RUN make go-build-local diff --git a/openshift/operator-controller.Dockerfile b/openshift/operator-controller.Dockerfile index 843861b7d..c0ae7ec58 100644 --- a/openshift/operator-controller.Dockerfile +++ b/openshift/operator-controller.Dockerfile @@ -1,4 +1,6 @@ FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS builder + +ENV GIT_COMMIT=${SOURCE_GIT_COMMIT} WORKDIR /build COPY . . RUN make go-build-local && \ diff --git a/openshift/registry.Dockerfile b/openshift/registry.Dockerfile index 0bfbb265f..4fa09b480 100644 --- a/openshift/registry.Dockerfile +++ b/openshift/registry.Dockerfile @@ -1,4 +1,7 @@ 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 . . # TODO Modify upstream Makefile to separate the 'go build' commands From 8ae090dce7ca1aec3e993e7a5bf86a593b04f462 Mon Sep 17 00:00:00 2001 From: Ankita Thomas Date: Wed, 11 Jun 2025 16:53:47 -0400 Subject: [PATCH 2/6] UPSTREAM: : add openshift specific build target to pass commit info downstream Signed-off-by: Ankita Thomas --- openshift/Makefile | 5 +++++ openshift/catalogd.Dockerfile | 2 +- openshift/operator-controller.Dockerfile | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/openshift/Makefile b/openshift/Makefile index 250a969e5..0448e3497 100644 --- a/openshift/Makefile +++ b/openshift/Makefile @@ -45,3 +45,8 @@ test-experimental-e2e: ## Run the experimental e2e tests. $(DIR)/operator-controller/build-test-registry.sh $(E2E_REGISTRY_NAMESPACE) $(E2E_REGISTRY_NAME) $(E2E_REGISTRY_IMAGE) cd $(DIR)/../; \ go test $(DOWNSTREAM_EXPERIMENTAL_E2E_FLAGS) ./test/experimental-e2e/...; + +export GIT_COMMIT := ${SOURCE_GIT_COMMIT} +PHONY: go-build-local +go-build-local: + $(MAKE) -n -f Makefile go-build-local \ No newline at end of file diff --git a/openshift/catalogd.Dockerfile b/openshift/catalogd.Dockerfile index a4b586e8b..0abbbc1d6 100644 --- a/openshift/catalogd.Dockerfile +++ b/openshift/catalogd.Dockerfile @@ -3,7 +3,7 @@ FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS ENV GIT_COMMIT=${SOURCE_GIT_COMMIT} WORKDIR /build COPY . . -RUN make go-build-local +RUN make -f openshift/Makefile go-build-local FROM registry.ci.openshift.org/ocp/4.20:base-rhel9 USER 1001 diff --git a/openshift/operator-controller.Dockerfile b/openshift/operator-controller.Dockerfile index c0ae7ec58..a935d9f84 100644 --- a/openshift/operator-controller.Dockerfile +++ b/openshift/operator-controller.Dockerfile @@ -3,7 +3,7 @@ FROM registry.ci.openshift.org/ocp/builder:rhel-9-golang-1.24-openshift-4.20 AS ENV GIT_COMMIT=${SOURCE_GIT_COMMIT} WORKDIR /build COPY . . -RUN make go-build-local && \ +RUN make -f openshift/Makefile go-build-local && \ # 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 From ea1215eb85328726318b7f33c9b118854a51726c Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Wed, 9 Jul 2025 08:44:04 -0400 Subject: [PATCH 3/6] UPSTREAM: : add source commit into binaries when linking Removes extra GIT_COMMIT set --- openshift/Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/openshift/Makefile b/openshift/Makefile index 0448e3497..5ee18efd9 100644 --- a/openshift/Makefile +++ b/openshift/Makefile @@ -7,6 +7,10 @@ include $(addprefix $(DIR)/vendor/github.com/openshift/build-machinery-go/make/, include $(DIR)/.bingo/Variables.mk +export GIT_COMMIT := $(OPENSHIFT_BUILD_COMMIT) # from OpenShift image build (https://docs.openshift.com/container-platform/3.11/dev_guide/builds/build_output.html) +VERSION_PATH := github.com/operator-framework/operator-controller/internal/shared/version +export GO_BUILD_LDFLAGS := -s -w -X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)' # + .PHONY: verify verify: ## Run downstream-specific verify $(MAKE) tidy fmt generate -C $(DIR)/../ @@ -46,7 +50,6 @@ test-experimental-e2e: ## Run the experimental e2e tests. cd $(DIR)/../; \ go test $(DOWNSTREAM_EXPERIMENTAL_E2E_FLAGS) ./test/experimental-e2e/...; -export GIT_COMMIT := ${SOURCE_GIT_COMMIT} PHONY: go-build-local go-build-local: - $(MAKE) -n -f Makefile go-build-local \ No newline at end of file + $(MAKE) -f Makefile go-build-local From 05a5f821acb039a60b6c067bbbe243d40cc1a972 Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Fri, 11 Jul 2025 14:35:04 -0400 Subject: [PATCH 4/6] UPSTREAM: : fixup Dockerfiles after rebase --- openshift/catalogd.Dockerfile | 3 ++- openshift/operator-controller.Dockerfile | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/openshift/catalogd.Dockerfile b/openshift/catalogd.Dockerfile index 0abbbc1d6..541b0dd18 100644 --- a/openshift/catalogd.Dockerfile +++ b/openshift/catalogd.Dockerfile @@ -1,9 +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 -f openshift/Makefile go-build-local +RUN make go-build-local FROM registry.ci.openshift.org/ocp/4.20:base-rhel9 USER 1001 diff --git a/openshift/operator-controller.Dockerfile b/openshift/operator-controller.Dockerfile index a935d9f84..971784403 100644 --- a/openshift/operator-controller.Dockerfile +++ b/openshift/operator-controller.Dockerfile @@ -1,5 +1,6 @@ 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 . . From 4c70baae78084d68a6760760a3ae8fd1e0ce38ac Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Tue, 15 Jul 2025 16:06:43 -0400 Subject: [PATCH 5/6] UPSTREAM: : consider "" unset so build-info can fill commit/date --- internal/shared/version/version.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/shared/version/version.go b/internal/shared/version/version.go index e61952e91..f4f7c0a0b 100644 --- a/internal/shared/version/version.go +++ b/internal/shared/version/version.go @@ -17,8 +17,26 @@ 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 { + return s == "" || s == "unknown" +} + 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 { + if v == "" { + return "unknown" + } + return v } func init() { @@ -29,18 +47,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 { repoState = v } } } - if version == "unknown" { + if isUnset(version) { version = info.Main.Version } } From fa53ba1388093ebac5cc6d3a17909beb947d043c Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Wed, 16 Jul 2025 10:36:08 -0400 Subject: [PATCH 6/6] UPSTREAM: : double quote go flags & honor GIT_COMMIT if set Should build the image again post rebase --- Makefile | 10 +++++++--- openshift/Makefile | 8 +++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 410205a89..ec6c1bce4 100644 --- a/Makefile +++ b/Makefile @@ -252,7 +252,7 @@ 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 + 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 +374,17 @@ 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/$@ .PHONY: build-deps build-deps: manifests generate fmt diff --git a/openshift/Makefile b/openshift/Makefile index 5ee18efd9..8e179162e 100644 --- a/openshift/Makefile +++ b/openshift/Makefile @@ -7,7 +7,13 @@ include $(addprefix $(DIR)/vendor/github.com/openshift/build-machinery-go/make/, include $(DIR)/.bingo/Variables.mk -export GIT_COMMIT := $(OPENSHIFT_BUILD_COMMIT) # from OpenShift image build (https://docs.openshift.com/container-platform/3.11/dev_guide/builds/build_output.html) +# Prefer the upstream source commit that the Dockerfile passes in via +# `ENV GIT_COMMIT=`. If that variable is not already defined fall back to +# the commit recorded by the OpenShift image build pipeline. +ifeq ($(origin GIT_COMMIT), undefined) +GIT_COMMIT := $(OPENSHIFT_BUILD_COMMIT) # populated by OpenShift build machinery +endif +export GIT_COMMIT VERSION_PATH := github.com/operator-framework/operator-controller/internal/shared/version export GO_BUILD_LDFLAGS := -s -w -X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)' #