From ec00c10dfd21ff006ff12c4a49ea974c2bb7e0d2 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Thu, 24 Oct 2024 11:04:01 +0200 Subject: [PATCH 1/5] Makefile: prepare-release updates internal/version/version.go Signed-off-by: Eguzki Astiz Lezaun --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b4097c5..f2b637e 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) +PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH))) + # VERSION defines the project version for the bundle. # Update this value when you upgrade the version of your project. # To re-generate a bundle for another specific version without changing the standard setup, you can: @@ -527,7 +530,7 @@ prepare-release: ## Generates a makefile that will override environment variable VERSION=$(VERSION)\nREPLACES_VERSION=$(REPLACES_VERSION)" > $(RELEASE_FILE) $(MAKE) bundle $(MAKE) helm-build VERSION=$(VERSION) - + sed -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go # Include last to avoid changing MAKEFILE_LIST used above include ./make/*.mk From cba5bb38f21d2727b8994ae607ce4eb636fdb187 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Thu, 24 Oct 2024 11:55:30 +0200 Subject: [PATCH 2/5] ci: on release check git version matches release version in code Signed-off-by: Eguzki Astiz Lezaun --- .../build-images-for-tag-release.yaml | 12 +++++++++++ Makefile | 21 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-images-for-tag-release.yaml b/.github/workflows/build-images-for-tag-release.yaml index 954d706..837802a 100644 --- a/.github/workflows/build-images-for-tag-release.yaml +++ b/.github/workflows/build-images-for-tag-release.yaml @@ -21,6 +21,18 @@ jobs: - name: Check out code uses: actions/checkout@v4 + - name: Read release string version + id: release + run: | + version=`make read-release-version` + echo version=$version >> $GITHUB_OUTPUT + + - name: Print tags + run: echo "Git reference name = ${{ github.ref_name }}, release version = ${{ steps.release.outputs.version }}" + - name: Verify git reference name matches the release version + if: ${{ github.ref_name != steps.release.outputs.version }} + run: exit 1 + - name: Install qemu dependency run: | sudo apt-get update diff --git a/Makefile b/Makefile index f2b637e..9b2867b 100644 --- a/Makefile +++ b/Makefile @@ -341,6 +341,17 @@ YQ = $(LOCALBIN)/yq GINKGO ?= $(LOCALBIN)/ginkgo GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint HELM ?= $(LOCALBIN)/helm +ifeq ($(shell uname),Darwin) +SED=$(shell which gsed) +else +SED=$(shell which sed) +endif +.PHONY: sed +sed: ## Checks if GNU sed is installed +ifeq ($(SED),) + @echo "Cannot find GNU sed installed." + exit 1 +endif ## Tool Versions KUSTOMIZE_VERSION ?= v5.0.1 @@ -530,7 +541,15 @@ prepare-release: ## Generates a makefile that will override environment variable VERSION=$(VERSION)\nREPLACES_VERSION=$(REPLACES_VERSION)" > $(RELEASE_FILE) $(MAKE) bundle $(MAKE) helm-build VERSION=$(VERSION) - sed -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go + $(MAKE) update-release-version VERSION=$(VERSION) + +.PHONY: read-version +read-release-version: ## Reads release version + @$(SED) -n 's/^.*Version = "\([^"]*\)".*/\1/p' $(PROJECT_PATH)/internal/version/version.go + +.PHONY: update-release-version +update-release-version: ## Updates release version to $(VERSION) + $(SED) -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go # Include last to avoid changing MAKEFILE_LIST used above include ./make/*.mk From 1891c4eb0a56c7dd5a8b6253ffaf010bc69fe26b Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Thu, 24 Oct 2024 12:07:41 +0200 Subject: [PATCH 3/5] internal/version/version.go with version prefixed with 'v' like git tags Signed-off-by: Eguzki Astiz Lezaun --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 9b2867b..b9915c6 100644 --- a/Makefile +++ b/Makefile @@ -548,8 +548,8 @@ read-release-version: ## Reads release version @$(SED) -n 's/^.*Version = "\([^"]*\)".*/\1/p' $(PROJECT_PATH)/internal/version/version.go .PHONY: update-release-version -update-release-version: ## Updates release version to $(VERSION) - $(SED) -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go +update-release-version: ## Updates release version to v$(VERSION). Note 'v' prefix. + $(SED) -i -e 's/Version = ".*"/Version = "v$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go # Include last to avoid changing MAKEFILE_LIST used above include ./make/*.mk From 8a140770115bb1f975ed99c6c9b3abb05f3a1be6 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Mon, 28 Oct 2024 22:12:10 +0100 Subject: [PATCH 4/5] release version passed in by ldflag Signed-off-by: Eguzki Astiz Lezaun --- Dockerfile | 5 +++-- Makefile | 27 ++++----------------------- cmd/main.go | 4 ++-- internal/version/version.go | 21 --------------------- 4 files changed, 9 insertions(+), 48 deletions(-) delete mode 100644 internal/version/version.go diff --git a/Dockerfile b/Dockerfile index 89a9710..5700cef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,10 +5,11 @@ ARG TARGETARCH ARG GIT_SHA ARG DIRTY +ARG VERSION ENV GIT_SHA=${GIT_SHA:-unknown} ENV DIRTY=${DIRTY:-unknown} - +ENV VERSION=${VERSION:-unknown} WORKDIR /workspace # Copy the Go Modules manifests @@ -27,7 +28,7 @@ COPY internal/ internal/ # was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO # the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, # by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -a -o manager cmd/main.go +RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -a -o manager cmd/main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index b9915c6..56c7751 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,3 @@ -MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) -PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH))) - # VERSION defines the project version for the bundle. # Update this value when you upgrade the version of your project. # To re-generate a bundle for another specific version without changing the standard setup, you can: @@ -236,19 +233,19 @@ local-deploy-namespaced: docker-build kind-load-image ## Deploy the dns operator build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") build: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown") build: manifests generate fmt vet ## Build manager binary. - go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o bin/manager cmd/main.go + go build -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o bin/manager cmd/main.go .PHONY: run run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") run: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown") run: manifests generate fmt vet ## Run a controller from your host. - go run -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure + go run -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure .PHONY: run-with-probes run-with-probes: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") run-with-probes: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown") run-with-probes: manifests generate fmt vet ## Run a controller from your host. - go run -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure + go run -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure # If you wish built the manager image targeting other platforms you can use the --platform flag. # (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it. @@ -341,17 +338,6 @@ YQ = $(LOCALBIN)/yq GINKGO ?= $(LOCALBIN)/ginkgo GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint HELM ?= $(LOCALBIN)/helm -ifeq ($(shell uname),Darwin) -SED=$(shell which gsed) -else -SED=$(shell which sed) -endif -.PHONY: sed -sed: ## Checks if GNU sed is installed -ifeq ($(SED),) - @echo "Cannot find GNU sed installed." - exit 1 -endif ## Tool Versions KUSTOMIZE_VERSION ?= v5.0.1 @@ -541,15 +527,10 @@ prepare-release: ## Generates a makefile that will override environment variable VERSION=$(VERSION)\nREPLACES_VERSION=$(REPLACES_VERSION)" > $(RELEASE_FILE) $(MAKE) bundle $(MAKE) helm-build VERSION=$(VERSION) - $(MAKE) update-release-version VERSION=$(VERSION) .PHONY: read-version read-release-version: ## Reads release version - @$(SED) -n 's/^.*Version = "\([^"]*\)".*/\1/p' $(PROJECT_PATH)/internal/version/version.go - -.PHONY: update-release-version -update-release-version: ## Updates release version to v$(VERSION). Note 'v' prefix. - $(SED) -i -e 's/Version = ".*"/Version = "v$(VERSION)"/' $(PROJECT_PATH)/internal/version/version.go + @echo "v$(VERSION)" # Include last to avoid changing MAKEFILE_LIST used above include ./make/*.mk diff --git a/cmd/main.go b/cmd/main.go index f8c4498..fb90b68 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -42,7 +42,6 @@ import ( _ "github.com/kuadrant/dns-operator/internal/provider/azure" _ "github.com/kuadrant/dns-operator/internal/provider/google" _ "github.com/kuadrant/dns-operator/internal/provider/inmemory" - "github.com/kuadrant/dns-operator/internal/version" //+kubebuilder:scaffold:imports ) @@ -51,6 +50,7 @@ var ( setupLog = ctrl.Log.WithName("setup") gitSHA string // pass ldflag here to display gitSHA hash dirty string // must be string as passed in by ldflag to determine display . + version string // must be string as passed in by ldflag to determine display . ) const ( @@ -67,7 +67,7 @@ func init() { } func printControllerMetaInfo() { - setupLog.Info("build information", "version", version.Version, "commit", gitSHA, "dirty", dirty) + setupLog.Info("build information", "version", version, "commit", gitSHA, "dirty", dirty) } func main() { diff --git a/internal/version/version.go b/internal/version/version.go deleted file mode 100644 index e8a5d0d..0000000 --- a/internal/version/version.go +++ /dev/null @@ -1,21 +0,0 @@ -/* -Copyright 2021 Red Hat, Inc. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package version - -var ( - // This variable is dependent on what the current release is e.g. if it is v0.10.0 then this variable, outside of releases, will be v0.10.1-dev . - Version = "0.4.0-dev" // Change as versions are released -) From f8b971f526339a209c93ef6c4d31904dcff27f4b Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Mon, 28 Oct 2024 22:39:46 +0100 Subject: [PATCH 5/5] Makefile: fix .PHONY name Signed-off-by: Eguzki Astiz Lezaun --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 56c7751..97b1729 100644 --- a/Makefile +++ b/Makefile @@ -528,7 +528,7 @@ prepare-release: ## Generates a makefile that will override environment variable $(MAKE) bundle $(MAKE) helm-build VERSION=$(VERSION) -.PHONY: read-version +.PHONY: read-release-version read-release-version: ## Reads release version @echo "v$(VERSION)"