Skip to content

Commit 4fda66b

Browse files
authored
add version info to the binary via ldflags. (#24)
Signed-off-by: Thomas Güttler <[email protected]>
1 parent e7ad502 commit 4fda66b

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Binaries for programs and plugins
32
*.exe
43
*.exe~
@@ -49,6 +48,10 @@ tilt_config.json
4948
*.tmp
5049
tmp/
5150
.DS_Store
51+
.vscode/settings.json
52+
53+
# pre-commit.com
54+
.pre-commit-config.yaml
5255

5356
# Sample config files auto-generated by kubebuilder
5457
config/samples

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ CTLPTL := $(abspath $(TOOLS_BIN_DIR)/ctlptl)
115115
ctlptl: $(CTLPTL) ## Build a local copy of ctlptl
116116
$(CTLPTL):
117117
go install github.com/tilt-dev/ctlptl/cmd/[email protected]
118-
118+
119119
CLUSTERCTL := $(abspath $(TOOLS_BIN_DIR)/clusterctl)
120120
clusterctl: $(CLUSTERCTL) ## Build a local copy of clusterctl
121121
$(CLUSTERCTL):
@@ -352,7 +352,7 @@ ALL_GENERATE_MODULES = core
352352
# support go modules
353353
generate-modules: ## Generates missing go modules
354354
ifeq ($(BUILD_IN_CONTAINER),true)
355-
docker run --rm -t -i \
355+
docker run --rm \
356356
-v $(shell go env GOPATH)/pkg:/go/pkg$(MOUNT_FLAGS) \
357357
-v $(shell pwd):/src/cluster-stack-operator$(MOUNT_FLAGS) \
358358
$(BUILDER_IMAGE):$(BUILDER_IMAGE_VERSION) $@;
@@ -497,7 +497,7 @@ ifeq ($(BUILD_IN_CONTAINER),true)
497497
-v $(shell pwd):/src/cluster-stack-operator$(MOUNT_FLAGS) \
498498
$(BUILDER_IMAGE):$(BUILDER_IMAGE_VERSION) $@;
499499
else
500-
lychee --config .lychee.toml ./*.md
500+
lychee --config .lychee.toml ./*.md
501501
endif
502502

503503
##@ Main Targets

cmd/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
//+kubebuilder:scaffold:imports
2828
csov1alpha1 "github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1"
2929
"github.com/SovereignCloudStack/cluster-stack-operator/internal/controller"
30+
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/csoversion"
3031
githubclient "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client"
3132
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/kube"
3233
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/utillog"
@@ -145,7 +146,7 @@ func main() {
145146
os.Exit(1)
146147
}
147148

148-
setupLog.Info("starting manager")
149+
setupLog.Info("starting manager", "version", csoversion.Get().String())
149150
if err := mgr.Start(ctx); err != nil {
150151
setupLog.Error(err, "problem running manager")
151152
os.Exit(1)

hack/version.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ version::ldflags() {
7777
local key=${1}
7878
local val=${2}
7979
ldflags+=(
80-
"-X 'github.com/sovereigncloudstack/cluster-stack-operator/version.${key}=${val}'"
80+
"-X 'github.com/SovereignCloudStack/cluster-stack-operator/pkg/csoversion.${key}=${val}'"
8181
)
8282
}
8383

pkg/csoversion/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 csoversion populates all the variables that are used further with ldflags.
18+
package csoversion
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)