Skip to content

Commit 791c851

Browse files
authored
Merge pull request #346 from Fedosin/fix_version_sh
🐛 Fix version.sh script
2 parents 55b2c9c + 32e96e3 commit 791c851

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

cmd/plugin/cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/spf13/cobra"
2525
"sigs.k8s.io/yaml"
2626

27-
"sigs.k8s.io/cluster-api/version"
27+
"sigs.k8s.io/cluster-api-operator/version"
2828
)
2929

3030
// Version provides the version information of CAPI operator.

hack/version.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# Copyright 2022 The Kubernetes Authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +17,10 @@ set -o errexit
1717
set -o nounset
1818
set -o pipefail
1919

20+
if [[ "${TRACE-0}" == "1" ]]; then
21+
set -o xtrace
22+
fi
23+
2024
version::get_version_vars() {
2125
GIT_COMMIT="$(git rev-parse HEAD^{commit})"
2226

@@ -27,8 +31,8 @@ version::get_version_vars() {
2731
fi
2832

2933
# stolen from k8s.io/hack/lib/version.sh
30-
# Use git describe to find the version based on tags.
31-
if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then
34+
# Use git describe to find the version based on annotated tags.
35+
if [[ -n ${GIT_VERSION-} ]] || GIT_VERSION=$(git describe --abbrev=14 --match "v[0-9]*" 2>/dev/null); then
3236
# This translates the "git describe" to an actual semver.org
3337
# compatible semantic version that looks something like this:
3438
# v1.1.0-alpha.0.6+84c76d1142ea4d
@@ -68,7 +72,7 @@ version::get_version_vars() {
6872
fi
6973

7074
GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags)
71-
GIT_RELEASE_COMMIT=$(git rev-list -n 1 ${GIT_RELEASE_TAG})
75+
GIT_RELEASE_COMMIT=$(git rev-list -n 1 "${GIT_RELEASE_TAG}")
7276
}
7377

7478
# stolen from k8s.io/hack/lib/version.sh and modified
@@ -93,6 +97,8 @@ version::ldflags() {
9397
add_ldflag "gitVersion" "${GIT_VERSION}"
9498
add_ldflag "gitReleaseCommit" "${GIT_RELEASE_COMMIT}"
9599

96-
# The -ldflags parameter takes a single string, so join the output.
97-
echo "${ldflags[*]-}"
100+
# The -ldflags parameter takes a single string, so join the output.
101+
echo "${ldflags[*]-}"
98102
}
103+
104+
version::ldflags

version/version.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package version implements version handling code.
18+
package version
19+
20+
import (
21+
"fmt"
22+
"runtime"
23+
)
24+
25+
var (
26+
gitMajor string // major version, always numeric
27+
gitMinor string // minor version, numeric possibly followed by "+"
28+
gitVersion string // semantic version, derived by build scripts
29+
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
30+
gitTreeState string // state of git tree, either "clean" or "dirty"
31+
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
32+
)
33+
34+
// Info exposes information about the version used for the current running code.
35+
type Info struct {
36+
Major string `json:"major,omitempty"`
37+
Minor string `json:"minor,omitempty"`
38+
GitVersion string `json:"gitVersion,omitempty"`
39+
GitCommit string `json:"gitCommit,omitempty"`
40+
GitTreeState string `json:"gitTreeState,omitempty"`
41+
BuildDate string `json:"buildDate,omitempty"`
42+
GoVersion string `json:"goVersion,omitempty"`
43+
Compiler string `json:"compiler,omitempty"`
44+
Platform string `json:"platform,omitempty"`
45+
}
46+
47+
// Get returns an Info object with all the information about the current running code.
48+
func Get() Info {
49+
return Info{
50+
Major: gitMajor,
51+
Minor: gitMinor,
52+
GitVersion: gitVersion,
53+
GitCommit: gitCommit,
54+
GitTreeState: gitTreeState,
55+
BuildDate: buildDate,
56+
GoVersion: runtime.Version(),
57+
Compiler: runtime.Compiler,
58+
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
59+
}
60+
}
61+
62+
// String returns info as a human-friendly version string.
63+
func (info Info) String() string {
64+
return info.GitVersion
65+
}

0 commit comments

Comments
 (0)