From 3ed6b22cadb46693e8a990f5a3849d2016839ca9 Mon Sep 17 00:00:00 2001 From: Patryk Matuszak <305846+pmtk@users.noreply.github.com> Date: Thu, 22 Jun 2023 11:10:04 +0200 Subject: [PATCH] persist patch version along major and minor --- Makefile | 7 ++++-- assets/version/microshift-version.yaml | 1 + etcd/cmd/microshift-etcd/version.go | 30 +++++++++++++++-------- pkg/admin/prerun/version.go | 33 +++++++++++++++----------- pkg/controllers/version.go | 1 + pkg/version/version.go | 32 ++++++++++++++++--------- test/suites/version.robot | 9 +++++++ 7 files changed, 76 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index cc3889ccf7..e3be19fdf2 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,9 @@ SOURCE_GIT_TAG := ${MICROSHIFT_VERSION} EMBEDDED_GIT_TAG ?= ${SOURCE_GIT_TAG} EMBEDDED_GIT_COMMIT ?= ${SOURCE_GIT_COMMIT} EMBEDDED_GIT_TREE_STATE ?= ${SOURCE_GIT_TREE_STATE} -MAJOR := $(shell echo $(SOURCE_GIT_TAG) | cut -f1 -d.) -MINOR := $(shell echo $(SOURCE_GIT_TAG) | cut -f2 -d.) +MAJOR := $(shell echo $(SOURCE_GIT_TAG) | awk -F'[._-]' '{print $$1}') +MINOR := $(shell echo $(SOURCE_GIT_TAG) | awk -F'[._-]' '{print $$2}') +PATCH := $(shell echo $(SOURCE_GIT_TAG) | awk -F'[._-]' '{print $$3}') SRC_ROOT :=$(shell pwd) @@ -76,6 +77,7 @@ GO_LD_FLAGS := $(GC_FLAGS) -ldflags " \ -X k8s.io/client-go/pkg/version.buildDate=$(BIN_TIMESTAMP) \ -X github.com/openshift/microshift/pkg/version.majorFromGit=$(MAJOR) \ -X github.com/openshift/microshift/pkg/version.minorFromGit=$(MINOR) \ + -X github.com/openshift/microshift/pkg/version.patchFromGit=$(PATCH) \ -X github.com/openshift/microshift/pkg/version.versionFromGit=$(EMBEDDED_GIT_TAG) \ -X github.com/openshift/microshift/pkg/version.commitFromGit=$(EMBEDDED_GIT_COMMIT) \ -X github.com/openshift/microshift/pkg/version.gitTreeState=$(EMBEDDED_GIT_TREE_STATE) \ @@ -116,6 +118,7 @@ etcd: GO_LD_FLAGS="$(GC_FLAGS) -ldflags \"\ -X main.majorFromGit=$(MAJOR) \ -X main.minorFromGit=$(MINOR) \ + -X main.patchFromGit=$(PATCH) \ -X main.versionFromGit=$(EMBEDDED_GIT_TAG) \ -X main.commitFromGit=$(EMBEDDED_GIT_COMMIT) \ -X main.gitTreeState=$(EMBEDDED_GIT_TREE_STATE) \ diff --git a/assets/version/microshift-version.yaml b/assets/version/microshift-version.yaml index ecadf04f83..4c644a84d8 100644 --- a/assets/version/microshift-version.yaml +++ b/assets/version/microshift-version.yaml @@ -7,4 +7,5 @@ metadata: data: major: "" minor: "" + patch: "" version: "" diff --git a/etcd/cmd/microshift-etcd/version.go b/etcd/cmd/microshift-etcd/version.go index 3bc93f4ac5..29ea462b3f 100644 --- a/etcd/cmd/microshift-etcd/version.go +++ b/etcd/cmd/microshift-etcd/version.go @@ -24,12 +24,19 @@ var ( majorFromGit string // minor version minorFromGit string + // patch version + patchFromGit string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') buildDate string // state of git tree, either "clean" or "dirty" gitTreeState string ) +type Info struct { + version.Info + Patch string `json:"patch"` +} + type VersionOptions struct { Output string @@ -58,16 +65,19 @@ func NewVersionCommand(ioStreams genericclioptions.IOStreams) *cobra.Command { } func (o *VersionOptions) Run() error { - versionInfo := version.Info{ - Major: majorFromGit, - Minor: minorFromGit, - GitCommit: commitFromGit, - GitVersion: versionFromGit, - GitTreeState: gitTreeState, - BuildDate: buildDate, - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + versionInfo := Info{ + Info: version.Info{ + Major: majorFromGit, + Minor: minorFromGit, + GitCommit: commitFromGit, + GitVersion: versionFromGit, + GitTreeState: gitTreeState, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + }, + Patch: patchFromGit, } switch o.Output { diff --git a/pkg/admin/prerun/version.go b/pkg/admin/prerun/version.go index d3c995d7e4..7b9dffdf1a 100644 --- a/pkg/admin/prerun/version.go +++ b/pkg/admin/prerun/version.go @@ -54,37 +54,42 @@ func CreateOrValidateDataVersion() error { } type versionMetadata struct { - Major, Minor int + Major, Minor, Patch int } func (v versionMetadata) String() string { - return fmt.Sprintf("%d.%d", v.Major, v.Minor) + return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) } -// versionMetadataFromString creates versionMetadata object from "major.minor" string where X and Y are integers -func versionMetadataFromString(majorMinor string) (versionMetadata, error) { - majorMinor = strings.TrimSpace(majorMinor) - majorMinorSplit := strings.Split(majorMinor, ".") - if len(majorMinorSplit) != 2 { - return versionMetadata{}, fmt.Errorf("invalid version string (%s): expected X.Y", majorMinor) +// versionMetadataFromString creates versionMetadata object from "major.minor.patch" string where major, minor, and patch are integers +func versionMetadataFromString(majorMinorPatch string) (versionMetadata, error) { + majorMinorPatch = strings.TrimSpace(majorMinorPatch) + split := strings.Split(majorMinorPatch, ".") + if len(split) != 3 { + return versionMetadata{}, fmt.Errorf("invalid version string (%s): expected Major.Minor.Patch", majorMinorPatch) } - major, err := strconv.Atoi(majorMinorSplit[0]) + major, err := strconv.Atoi(split[0]) if err != nil { - return versionMetadata{}, fmt.Errorf("converting '%s' to an int failed: %w", majorMinorSplit[0], err) + return versionMetadata{}, fmt.Errorf("converting %q to an int failed: %w", split[0], err) } - minor, err := strconv.Atoi(majorMinorSplit[1]) + minor, err := strconv.Atoi(split[1]) if err != nil { - return versionMetadata{}, fmt.Errorf("converting '%s' to an int failed: %w", majorMinorSplit[1], err) + return versionMetadata{}, fmt.Errorf("converting %q to an int failed: %w", split[1], err) } - return versionMetadata{Major: major, Minor: minor}, nil + patch, err := strconv.Atoi(split[2]) + if err != nil { + return versionMetadata{}, fmt.Errorf("converting %q to an int failed: %w", split[2], err) + } + + return versionMetadata{Major: major, Minor: minor, Patch: patch}, nil } func getVersionOfExecutable() (versionMetadata, error) { ver := version.Get() - return versionMetadataFromString(fmt.Sprintf("%s.%s", ver.Major, ver.Minor)) + return versionMetadataFromString(fmt.Sprintf("%s.%s.%s", ver.Major, ver.Minor, ver.Patch)) } func getVersionOfData() (versionMetadata, error) { diff --git a/pkg/controllers/version.go b/pkg/controllers/version.go index cd45d862c5..70afda1c26 100644 --- a/pkg/controllers/version.go +++ b/pkg/controllers/version.go @@ -49,6 +49,7 @@ func (s *VersionManager) Run(ctx context.Context, ready chan<- struct{}, stopped var data = map[string]string{ "major": versionInfo.Major, "minor": versionInfo.Minor, + "patch": versionInfo.Patch, "version": versionInfo.String(), } diff --git a/pkg/version/version.go b/pkg/version/version.go index 2fe259b702..45f3f366e7 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -21,25 +21,35 @@ var ( majorFromGit string // minor version minorFromGit string + // patch version + patchFromGit string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') buildDate string // state of git tree, either "clean" or "dirty" gitTreeState string ) +type Info struct { + version.Info + Patch string `json:"patch"` +} + // 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, - GitTreeState: gitTreeState, - BuildDate: buildDate, - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), +func Get() Info { + return Info{ + Info: version.Info{ + Major: majorFromGit, + Minor: minorFromGit, + GitCommit: commitFromGit, + GitVersion: versionFromGit, + GitTreeState: gitTreeState, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + }, + Patch: patchFromGit, } } diff --git a/test/suites/version.robot b/test/suites/version.robot index 9761b31afb..ff5c5a1361 100755 --- a/test/suites/version.robot +++ b/test/suites/version.robot @@ -23,6 +23,7 @@ ConfigMap Contents ${configmap}= Oc Get configmap kube-public microshift-version Should Be Equal As Integers ${configmap.data.major} ${MAJOR_VERSION} Should Be Equal As Integers ${configmap.data.minor} ${MINOR_VERSION} + Should Be Equal As Integers ${configmap.data.patch} ${PATCH_VERSION} CLI Output [Documentation] Check the version reported by the process @@ -30,6 +31,7 @@ CLI Output ${version}= MicroShift Version Should Be Equal As Integers ${version.major} ${MAJOR_VERSION} Should Be Equal As Integers ${version.minor} ${MINOR_VERSION} + Should Be Equal As Integers ${version.patch} ${PATCH_VERSION} Should Start With ${version.gitVersion} ${Y_STREAM} ConfigMap Matches CLI @@ -83,3 +85,10 @@ Read Expected Versions # robocop: disable=too-many-calls-in-keyword # 4.14 ${ystream}= Format String {}.{} ${major} ${minor} Set Suite Variable \${Y_STREAM} ${ystream} + + # 0-0.nightly-arm64-2023-05-04-012046 + ${without_majorminor}= Get Substring ${without_major} 3 + + # 0 + ${patch}= Fetch From Left ${without_majorminor} - + Set Suite Variable \${PATCH_VERSION} ${patch}