Skip to content

Commit

Permalink
add version info to the binary via ldflags. (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Güttler <[email protected]>
  • Loading branch information
guettli committed Oct 12, 2023
1 parent e7ad502 commit 4fda66b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down Expand Up @@ -49,6 +48,10 @@ tilt_config.json
*.tmp
tmp/
.DS_Store
.vscode/settings.json

# pre-commit.com
.pre-commit-config.yaml

# Sample config files auto-generated by kubebuilder
config/samples
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ CTLPTL := $(abspath $(TOOLS_BIN_DIR)/ctlptl)
ctlptl: $(CTLPTL) ## Build a local copy of ctlptl
$(CTLPTL):
go install github.com/tilt-dev/ctlptl/cmd/[email protected]

CLUSTERCTL := $(abspath $(TOOLS_BIN_DIR)/clusterctl)
clusterctl: $(CLUSTERCTL) ## Build a local copy of clusterctl
$(CLUSTERCTL):
Expand Down Expand Up @@ -352,7 +352,7 @@ ALL_GENERATE_MODULES = core
# support go modules
generate-modules: ## Generates missing go modules
ifeq ($(BUILD_IN_CONTAINER),true)
docker run --rm -t -i \
docker run --rm \
-v $(shell go env GOPATH)/pkg:/go/pkg$(MOUNT_FLAGS) \
-v $(shell pwd):/src/cluster-stack-operator$(MOUNT_FLAGS) \
$(BUILDER_IMAGE):$(BUILDER_IMAGE_VERSION) $@;
Expand Down Expand Up @@ -497,7 +497,7 @@ ifeq ($(BUILD_IN_CONTAINER),true)
-v $(shell pwd):/src/cluster-stack-operator$(MOUNT_FLAGS) \
$(BUILDER_IMAGE):$(BUILDER_IMAGE_VERSION) $@;
else
lychee --config .lychee.toml ./*.md
lychee --config .lychee.toml ./*.md
endif

##@ Main Targets
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
//+kubebuilder:scaffold:imports
csov1alpha1 "github.com/SovereignCloudStack/cluster-stack-operator/api/v1alpha1"
"github.com/SovereignCloudStack/cluster-stack-operator/internal/controller"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/csoversion"
githubclient "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/kube"
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/utillog"
Expand Down Expand Up @@ -145,7 +146,7 @@ func main() {
os.Exit(1)
}

setupLog.Info("starting manager")
setupLog.Info("starting manager", "version", csoversion.Get().String())
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion hack/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ version::ldflags() {
local key=${1}
local val=${2}
ldflags+=(
"-X 'github.com/sovereigncloudstack/cluster-stack-operator/version.${key}=${val}'"
"-X 'github.com/SovereignCloudStack/cluster-stack-operator/pkg/csoversion.${key}=${val}'"
)
}

Expand Down
65 changes: 65 additions & 0 deletions pkg/csoversion/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package csoversion populates all the variables that are used further with ldflags.
package csoversion

import (
"fmt"
"runtime"
)

var (
gitMajor string // major version, always numeric
gitMinor string // minor version, numeric possibly followed by "+"
gitVersion string // semantic version, derived by build scripts
gitCommit string // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState string // state of git tree, either "clean" or "dirty"
buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// Info exposes information about the version used for the current running code.
type Info struct {
Major string `json:"major,omitempty"`
Minor string `json:"minor,omitempty"`
GitVersion string `json:"gitVersion,omitempty"`
GitCommit string `json:"gitCommit,omitempty"`
GitTreeState string `json:"gitTreeState,omitempty"`
BuildDate string `json:"buildDate,omitempty"`
GoVersion string `json:"goVersion,omitempty"`
Compiler string `json:"compiler,omitempty"`
Platform string `json:"platform,omitempty"`
}

// Get returns an Info object with all the information about the current running code.
func Get() *Info {
return &Info{
Major: gitMajor,
Minor: gitMinor,
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

// String returns info as a human-friendly version string.
func (info *Info) String() string {
return info.GitVersion
}

0 comments on commit 4fda66b

Please sign in to comment.