From 4cd7991048f98d6b572f12b985163c0d8f4d2e7d Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Sun, 7 Apr 2024 11:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20Refactor=20version=20cmd=20(#409?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor version cmd Signed-off-by: Jian Qiu * Fix comments Signed-off-by: Jian Qiu --------- Signed-off-by: Jian Qiu --- .github/workflows/release.yaml | 3 -- Makefile | 39 ++++++++++++++++---------- VERSION.txt | 1 - pkg/cmd/init/exec.go | 2 +- pkg/cmd/install/hubaddon/exec.go | 2 +- pkg/cmd/install/hubaddon/exec_test.go | 2 +- pkg/cmd/install/hubaddon/options.go | 2 +- pkg/cmd/join/exec.go | 2 +- pkg/cmd/upgrade/clustermanager/exec.go | 2 +- pkg/cmd/upgrade/klusterlet/exec.go | 2 +- pkg/cmd/version/exec.go | 5 ++-- pkg/{helpers => }/version/version.go | 29 +++++++++++++++++++ test/e2e/clusteradm/upgrade_test.go | 2 +- version.go | 14 --------- 14 files changed, 63 insertions(+), 44 deletions(-) delete mode 100644 VERSION.txt rename pkg/{helpers => }/version/version.go (77%) delete mode 100644 version.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index f50b1769d..a7ff93cdd 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -22,9 +22,6 @@ jobs: uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} - - name: Check version - run: | - cat VERSION.txt | grep ${{ github.ref_name }} - name: Build project run: | make build-bin diff --git a/Makefile b/Makefile index 7e3832284..b158275b4 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,21 @@ GOPATH := ${shell go env GOPATH} GOOS := ${shell go env GOOS} GOARCH := ${shell go env GOARCH} +SOURCE_GIT_LATEST_TAG ?= $(shell git describe --tags `git rev-list --tags --max-count=1`) +SOURCE_GIT_TAG ?=$(shell git describe --long --tags --abbrev=7 --match 'v[0-9]*' || echo 'v0.0.0-unknown-$(SOURCE_GIT_COMMIT)') +SOURCE_GIT_COMMIT ?=$(shell git rev-parse --short "HEAD^{commit}" 2>/dev/null) +SOURCE_GIT_TREE_STATE ?=$(shell ( ( [ ! -d ".git/" ] || git diff --quiet ) && echo 'clean' ) || echo 'dirty') + +GO_LD_EXTRAFLAGS ?= + +define version-ldflags +-X open-cluster-management.io/clusteradm/pkg/version.versionFromGit="$(SOURCE_GIT_TAG)" \ +-X open-cluster-management.io/clusteradm/pkg/version.commitFromGit="$(SOURCE_GIT_COMMIT)" \ +-X open-cluster-management.io/clusteradm/pkg/version.gitTreeState="$(SOURCE_GIT_TREE_STATE)" \ +-X open-cluster-management.io/clusteradm/pkg/version.buildDate="$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" +endef +GO_LD_FLAGS ?=-ldflags "$(call version-ldflags,$(GO_PACKAGE)/pkg/version) $(GO_LD_EXTRAFLAGS)" + export PROJECT_DIR = $(shell 'pwd') export PROJECT_NAME = $(shell basename ${PROJECT_DIR}) @@ -32,30 +47,24 @@ deps: .PHONY: build build: rm -f ${GOPATH}/bin/clusteradm - go install ./cmd/clusteradm/clusteradm.go + go install $(GO_LD_FLAGS) ./cmd/clusteradm/clusteradm.go .PHONY: build-bin: @rm -rf bin @mkdir -p bin - GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_darwin_amd64.tar.gz LICENSE -C bin/ clusteradm - GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_darwin_arm64.tar.gz LICENSE -C bin/ clusteradm - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_amd64.tar.gz LICENSE -C bin/ clusteradm - GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_arm64.tar.gz LICENSE -C bin/ clusteradm - GOOS=linux GOARCH=ppc64le go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_ppc64le.tar.gz LICENSE -C bin/ clusteradm - GOOS=linux GOARCH=s390x go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_s390x.tar.gz LICENSE -C bin/ clusteradm - GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=x/y -o bin/clusteradm.exe ./cmd/clusteradm/clusteradm.go && zip -q bin/clusteradm_windows_amd64.zip LICENSE -j bin/clusteradm.exe - -.PHONY: release -release: - @if [[ -z "${VERSION}" ]]; then VERSION=`cat VERSION.txt`; echo $$VERSION; fi; \ - git tag $$VERSION && git push upstream --tags + GOOS=darwin GOARCH=amd64 go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_darwin_amd64.tar.gz LICENSE -C bin/ clusteradm + GOOS=darwin GOARCH=arm64 go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_darwin_arm64.tar.gz LICENSE -C bin/ clusteradm + GOOS=linux GOARCH=amd64 go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_amd64.tar.gz LICENSE -C bin/ clusteradm + GOOS=linux GOARCH=arm64 go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_arm64.tar.gz LICENSE -C bin/ clusteradm + GOOS=linux GOARCH=ppc64le go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_ppc64le.tar.gz LICENSE -C bin/ clusteradm + GOOS=linux GOARCH=s390x go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm ./cmd/clusteradm/clusteradm.go && tar -czf bin/clusteradm_linux_s390x.tar.gz LICENSE -C bin/ clusteradm + GOOS=windows GOARCH=amd64 go build $(GO_LD_FLAGS) -gcflags=-trimpath=x/y -o bin/clusteradm.exe ./cmd/clusteradm/clusteradm.go && zip -q bin/clusteradm_windows_amd64.zip LICENSE -j bin/clusteradm.exe .PHONY: build-krew build-krew: krew-tools - @if [[ -z "${VERSION}" ]]; then VERSION=`cat VERSION.txt`; echo $$VERSION; fi; \ docker run -v ${PROJECT_DIR}/.krew.yaml:/tmp/template-file.yaml rajatjindal/krew-release-bot:v0.0.40 \ - krew-release-bot template --tag v$$VERSION --template-file /tmp/template-file.yaml > krew-manifest.yaml; + krew-release-bot template --tag ${SOURCE_GIT_LATEST_TAG} --template-file /tmp/template-file.yaml > krew-manifest.yaml; KREW=/tmp/krew-${GOOS}\_$(GOARCH) && \ KREW_ROOT=`mktemp -d` KREW_OS=darwin KREW_ARCH=amd64 $$KREW install --manifest=krew-manifest.yaml && \ KREW_ROOT=`mktemp -d` KREW_OS=linux KREW_ARCH=amd64 $$KREW install --manifest=krew-manifest.yaml && \ diff --git a/VERSION.txt b/VERSION.txt deleted file mode 100644 index b19b52118..000000000 --- a/VERSION.txt +++ /dev/null @@ -1 +0,0 @@ -v0.8.0 diff --git a/pkg/cmd/init/exec.go b/pkg/cmd/init/exec.go index 813f34017..33a26e773 100644 --- a/pkg/cmd/init/exec.go +++ b/pkg/cmd/init/exec.go @@ -25,8 +25,8 @@ import ( preflightinterface "open-cluster-management.io/clusteradm/pkg/helpers/preflight" "open-cluster-management.io/clusteradm/pkg/helpers/reader" "open-cluster-management.io/clusteradm/pkg/helpers/resourcerequirement" - "open-cluster-management.io/clusteradm/pkg/helpers/version" helperwait "open-cluster-management.io/clusteradm/pkg/helpers/wait" + "open-cluster-management.io/clusteradm/pkg/version" ) var ( diff --git a/pkg/cmd/install/hubaddon/exec.go b/pkg/cmd/install/hubaddon/exec.go index 895662105..8970649e4 100644 --- a/pkg/cmd/install/hubaddon/exec.go +++ b/pkg/cmd/install/hubaddon/exec.go @@ -12,7 +12,7 @@ import ( "k8s.io/klog/v2" "open-cluster-management.io/clusteradm/pkg/cmd/install/hubaddon/scenario" - "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/version" ) const ( diff --git a/pkg/cmd/install/hubaddon/exec_test.go b/pkg/cmd/install/hubaddon/exec_test.go index a551eb19f..96b5168c1 100644 --- a/pkg/cmd/install/hubaddon/exec_test.go +++ b/pkg/cmd/install/hubaddon/exec_test.go @@ -10,7 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericclioptions" - "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/version" ) var _ = ginkgo.Describe("install hub-addon", func() { diff --git a/pkg/cmd/install/hubaddon/options.go b/pkg/cmd/install/hubaddon/options.go index 693a3d47e..bbc2deec1 100644 --- a/pkg/cmd/install/hubaddon/options.go +++ b/pkg/cmd/install/hubaddon/options.go @@ -4,7 +4,7 @@ package hubaddon import ( "k8s.io/cli-runtime/pkg/genericclioptions" genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions" - "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/version" ) type Options struct { diff --git a/pkg/cmd/join/exec.go b/pkg/cmd/join/exec.go index 48a5fe0f9..9c8e57a54 100644 --- a/pkg/cmd/join/exec.go +++ b/pkg/cmd/join/exec.go @@ -40,8 +40,8 @@ import ( "open-cluster-management.io/clusteradm/pkg/helpers/printer" "open-cluster-management.io/clusteradm/pkg/helpers/reader" "open-cluster-management.io/clusteradm/pkg/helpers/resourcerequirement" - "open-cluster-management.io/clusteradm/pkg/helpers/version" "open-cluster-management.io/clusteradm/pkg/helpers/wait" + "open-cluster-management.io/clusteradm/pkg/version" sdkhelpers "open-cluster-management.io/sdk-go/pkg/helpers" ) diff --git a/pkg/cmd/upgrade/clustermanager/exec.go b/pkg/cmd/upgrade/clustermanager/exec.go index 46a90d3a1..9fdda7c0a 100644 --- a/pkg/cmd/upgrade/clustermanager/exec.go +++ b/pkg/cmd/upgrade/clustermanager/exec.go @@ -14,8 +14,8 @@ import ( "k8s.io/klog/v2" init_scenario "open-cluster-management.io/clusteradm/pkg/cmd/init/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" - "open-cluster-management.io/clusteradm/pkg/helpers/version" "open-cluster-management.io/clusteradm/pkg/helpers/wait" + "open-cluster-management.io/clusteradm/pkg/version" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { diff --git a/pkg/cmd/upgrade/klusterlet/exec.go b/pkg/cmd/upgrade/klusterlet/exec.go index cbc9025ec..d9cb03e6d 100644 --- a/pkg/cmd/upgrade/klusterlet/exec.go +++ b/pkg/cmd/upgrade/klusterlet/exec.go @@ -13,8 +13,8 @@ import ( join_scenario "open-cluster-management.io/clusteradm/pkg/cmd/join/scenario" "open-cluster-management.io/clusteradm/pkg/helpers" "open-cluster-management.io/clusteradm/pkg/helpers/reader" - "open-cluster-management.io/clusteradm/pkg/helpers/version" "open-cluster-management.io/clusteradm/pkg/helpers/wait" + "open-cluster-management.io/clusteradm/pkg/version" ) //nolint:deadcode,varcheck diff --git a/pkg/cmd/version/exec.go b/pkg/cmd/version/exec.go index 810a532b1..925d3a7c3 100644 --- a/pkg/cmd/version/exec.go +++ b/pkg/cmd/version/exec.go @@ -6,8 +6,7 @@ import ( "strings" "github.com/spf13/cobra" - clusteradm "open-cluster-management.io/clusteradm" - version "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/version" ) func (o *Options) complete(cmd *cobra.Command, args []string) (err error) { @@ -19,7 +18,7 @@ func (o *Options) validate() error { } func (o *Options) run() (err error) { - fmt.Printf("client\t\tversion\t:%s\n", strings.Trim(clusteradm.GetVersion(), "\n")) + fmt.Printf("client\t\tversion\t:%s\n", strings.Trim(version.Get().GitVersion, "\n")) discoveryClient, err := o.ClusteradmFlags.KubectlFactory.ToDiscoveryClient() if err != nil { return err diff --git a/pkg/helpers/version/version.go b/pkg/version/version.go similarity index 77% rename from pkg/helpers/version/version.go rename to pkg/version/version.go index bd2e0ff9e..38d7509c4 100644 --- a/pkg/helpers/version/version.go +++ b/pkg/version/version.go @@ -4,8 +4,37 @@ package version import ( "fmt" "strings" + + "k8s.io/apimachinery/pkg/version" +) + +var ( + // commitFromGit is a constant representing the source version that + // generated this build. It should be set during build via -ldflags. + commitFromGit string + // versionFromGit is a constant representing the version tag that + // generated this build. It should be set during build via -ldflags. + versionFromGit string + // major version + majorFromGit string + // minor version + minorFromGit string + // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') + buildDate string ) +// Get returns the overall codebase version. It's for detecting +// what code a binary was built from. +func Get() version.Info { + return version.Info{ + Major: majorFromGit, + Minor: minorFromGit, + GitCommit: commitFromGit, + GitVersion: versionFromGit, + BuildDate: buildDate, + } +} + type VersionBundle struct { Registration string Placement string diff --git a/test/e2e/clusteradm/upgrade_test.go b/test/e2e/clusteradm/upgrade_test.go index 9bc077fea..45691d92f 100644 --- a/test/e2e/clusteradm/upgrade_test.go +++ b/test/e2e/clusteradm/upgrade_test.go @@ -10,7 +10,7 @@ import ( "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - "open-cluster-management.io/clusteradm/pkg/helpers/version" + "open-cluster-management.io/clusteradm/pkg/version" ) var _ = ginkgo.Describe("test clusteradm upgrade clustermanager and Klusterlets", ginkgo.Ordered, func() { diff --git a/version.go b/version.go deleted file mode 100644 index 37f1a45c8..000000000 --- a/version.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright Contributors to the Open Cluster Management project - -package clusteradm - -import ( - _ "embed" -) - -//go:embed VERSION.txt -var version []byte - -func GetVersion() string { - return string(version) -}