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/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 } } diff --git a/openshift/Makefile b/openshift/Makefile index 250a969e5..8e179162e 100644 --- a/openshift/Makefile +++ b/openshift/Makefile @@ -7,6 +7,16 @@ include $(addprefix $(DIR)/vendor/github.com/openshift/build-machinery-go/make/, include $(DIR)/.bingo/Variables.mk +# 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)' # + .PHONY: verify verify: ## Run downstream-specific verify $(MAKE) tidy fmt generate -C $(DIR)/../ @@ -45,3 +55,7 @@ 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/...; + +PHONY: go-build-local +go-build-local: + $(MAKE) -f Makefile go-build-local diff --git a/openshift/catalogd.Dockerfile b/openshift/catalogd.Dockerfile index 0774772ee..541b0dd18 100644 --- a/openshift/catalogd.Dockerfile +++ b/openshift/catalogd.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 . . RUN make go-build-local diff --git a/openshift/operator-controller.Dockerfile b/openshift/operator-controller.Dockerfile index 843861b7d..971784403 100644 --- a/openshift/operator-controller.Dockerfile +++ b/openshift/operator-controller.Dockerfile @@ -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 && \ # 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 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