Skip to content

Commit ad2579d

Browse files
authored
Merge pull request kubernetes-sigs#639 from srm09/feature/add-version-info-to-starting-log
Adds version info to manager startup logs
2 parents c0d09b2 + e90c650 commit ad2579d

File tree

5 files changed

+169
-3
lines changed

5 files changed

+169
-3
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
!/exp/**
99
!/feature/**
1010
!/pkg/**
11+
!/version/**
1112
!/main.go
1213
!/go.mod
1314
!/go.sum

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ E2E_CONF_FILE_ENVSUBST := $(ROOT_DIR)/test/e2e/config/azure-dev-envsubst.yaml
8080
SKIP_CLEANUP ?= false
8181
SKIP_CREATE_MGMT_CLUSTER ?= false
8282

83+
# Build time versioning details.
84+
LDFLAGS := $(shell hack/version.sh)
85+
8386
CLUSTER_TEMPLATE ?= cluster-template.yaml
8487
MANAGED_CLUSTER_TEMPLATE ?= cluster-template-aks.yaml
8588

@@ -130,7 +133,7 @@ binaries: manager ## Builds and installs all binaries
130133

131134
.PHONY: manager
132135
manager: ## Build manager binary.
133-
go build -o $(BIN_DIR)/manager .
136+
go build -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/manager .
134137

135138
## --------------------------------------
136139
## Tooling Binaries
@@ -310,7 +313,7 @@ release-binary: $(RELEASE_DIR)
310313
-v "$$(pwd):/workspace" \
311314
-w /workspace \
312315
golang:1.13.8 \
313-
go build -a -ldflags '-extldflags "-static"' \
316+
go build -a -ldflags '$(LDFLAGS) -extldflags "-static"' \
314317
-o $(RELEASE_DIR)/$(notdir $(RELEASE_BINARY))-$(GOOS)-$(GOARCH) $(RELEASE_BINARY)
315318

316319
.PHONY: release-staging

hack/version.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
# Copyright 2020 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+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
version::get_version_vars() {
21+
GIT_COMMIT="$(git rev-parse HEAD^{commit})"
22+
23+
if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
24+
GIT_TREE_STATE="clean"
25+
else
26+
GIT_TREE_STATE="dirty"
27+
fi
28+
29+
# borrowed 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
32+
# This translates the "git describe" to an actual semver.org
33+
# compatible semantic version that looks something like this:
34+
# v1.1.0-alpha.0.6+84c76d1142ea4d
35+
DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g")
36+
if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
37+
# We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
38+
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/")
39+
elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
40+
# We have distance to base tag (v1.1.0-1-gCommitHash)
41+
GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/")
42+
fi
43+
if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then
44+
# git describe --dirty only considers changes to existing files, but
45+
# that is problematic since new untracked .go files affect the build,
46+
# so use our idea of "dirty" from git status instead.
47+
GIT_VERSION+="-dirty"
48+
fi
49+
50+
51+
# Try to match the "git describe" output to a regex to try to extract
52+
# the "major" and "minor" versions and whether this is the exact tagged
53+
# version or whether the tree is between two tagged versions.
54+
if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
55+
GIT_MAJOR=${BASH_REMATCH[1]}
56+
GIT_MINOR=${BASH_REMATCH[2]}
57+
fi
58+
59+
# If GIT_VERSION is not a valid Semantic Version, then refuse to build.
60+
if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
61+
echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}"
62+
echo "Please see more details here: https://semver.org"
63+
exit 1
64+
fi
65+
fi
66+
67+
GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags)
68+
}
69+
70+
# borrowed from k8s.io/hack/lib/version.sh and modified
71+
# Prints the value that needs to be passed to the -ldflags parameter of go build
72+
version::ldflags() {
73+
version::get_version_vars
74+
75+
local -a ldflags
76+
function add_ldflag() {
77+
local key=${1}
78+
local val=${2}
79+
ldflags+=(
80+
"-X 'sigs.k8s.io/cluster-api-provider-azure/version.${key}=${val}'"
81+
)
82+
}
83+
84+
add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')"
85+
add_ldflag "gitCommit" "${GIT_COMMIT}"
86+
add_ldflag "gitTreeState" "${GIT_TREE_STATE}"
87+
add_ldflag "gitMajor" "${GIT_MAJOR}"
88+
add_ldflag "gitMinor" "${GIT_MINOR}"
89+
add_ldflag "gitVersion" "${GIT_VERSION}"
90+
91+
# The -ldflags parameter takes a single string, so join the output.
92+
echo "${ldflags[*]-}"
93+
}
94+
95+
version::ldflags

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/cluster-api-provider-azure/controllers"
3939
infrav1alpha3exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha3"
4040
infrav1controllersexp "sigs.k8s.io/cluster-api-provider-azure/exp/controllers"
41+
version "sigs.k8s.io/cluster-api-provider-azure/version"
4142
capifeature "sigs.k8s.io/cluster-api/feature"
4243

4344
"sigs.k8s.io/cluster-api-provider-azure/feature"
@@ -278,7 +279,7 @@ func main() {
278279
os.Exit(1)
279280
}
280281

281-
setupLog.Info("starting manager")
282+
setupLog.Info("starting manager", "version", version.Get().String())
282283
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
283284
setupLog.Error(err, "problem running manager")
284285
os.Exit(1)

version/version.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2020 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
18+
19+
import (
20+
"fmt"
21+
"runtime"
22+
23+
azuresdkversion "github.com/Azure/azure-sdk-for-go/version"
24+
)
25+
26+
var (
27+
gitMajor string // major version, always numeric
28+
gitMinor string // minor version, numeric possibly followed by "+"
29+
gitVersion string // semantic version, derived by build scripts
30+
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
31+
gitTreeState string // state of git tree, either "clean" or "dirty"
32+
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
33+
)
34+
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+
AzureSdkVersion string `json:"azureSdkVersion,omitempty"`
46+
}
47+
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+
AzureSdkVersion: fmt.Sprintf("v%s", azuresdkversion.Number),
60+
}
61+
}
62+
63+
// String returns info as a human-friendly version string.
64+
func (info Info) String() string {
65+
return info.GitVersion
66+
}

0 commit comments

Comments
 (0)