Skip to content

Commit

Permalink
🌱 Refactor version cmd (#409)
Browse files Browse the repository at this point in the history
* Refactor version cmd

Signed-off-by: Jian Qiu <jqiu@redhat.com>

* Fix comments

Signed-off-by: Jian Qiu <jqiu@redhat.com>

---------

Signed-off-by: Jian Qiu <jqiu@redhat.com>
  • Loading branch information
qiujian16 committed Apr 7, 2024
1 parent 41a5ed4 commit 4cd7991
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 44 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 24 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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 && \
Expand Down
1 change: 0 additions & 1 deletion VERSION.txt

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/install/hubaddon/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/install/hubaddon/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/install/hubaddon/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/upgrade/clustermanager/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/upgrade/klusterlet/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/version/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pkg/helpers/version/version.go → pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/clusteradm/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 0 additions & 14 deletions version.go

This file was deleted.

0 comments on commit 4cd7991

Please sign in to comment.