Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions internal/shared/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
}
14 changes: 14 additions & 0 deletions openshift/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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=<sha>`. 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)/../
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions openshift/catalogd.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion openshift/operator-controller.Dockerfile
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 && \
# 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
Expand Down
3 changes: 3 additions & 0 deletions openshift/registry.Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down