k3s/pkg/version/version.go

64 lines
2.0 KiB
Go
Raw Normal View History

2014-07-02 21:57:26 +00:00
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
2014-07-02 21:57:26 +00:00
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 version
2015-09-23 08:18:05 +00:00
import "github.com/prometheus/client_golang/prometheus"
// Info contains versioning information.
// TODO: Add []string of api versions supported? It's still unclear
// how we'll want to distribute that information.
type Info struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
}
// Get returns the overall codebase version. It's for detecting
// what code a binary was built from.
func Get() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the settings in pkg/version/base.go
return Info{
Major: gitMajor,
Minor: gitMinor,
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
}
2014-07-02 21:57:26 +00:00
}
// String returns info as a human-friendly version string.
func (info Info) String() string {
Use just GitVersion for the -version output It turns out that that's simply the most useful information about the version that was built. For a git tree, it includes information about the closest release, number of commits since the release, enough of the SHA1 to find the exact commit and whether the tree was clean or dirty at build time. For a build from tarball, it will include information of which release was built and, when using a tarball from a not released commit (e.g. downloading "master.tar.gz" from GitHub, not recommended!) it will include the -dev prefix to indicate that was not an official release. (That's as good as we can get with it.) If additional information is required, it can be found with -version=raw. Tested: - Built from the git tree: $ _output/go/bin/kubecfg -version Kubernetes v0.2-29-gd916051e9db865 $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"2+", GitVersion:"v0.2-29-gd916051e9db865", GitCommit:"d916051e9db8650899acb9415a4e285e3e3a1f87", GitTreeState:"clean"} - Built it from a dirty git tree: $ { echo 'package version'; echo 'var X = 1'; } >pkg/version/sillyvar.go $ make $ _output/go/bin/kubecfg -version Kubernetes v0.2-29-gd916051e9db865-dirty $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"2+", GitVersion:"v0.2-29-gd916051e9db865-dirty", GitCommit:"d916051e9db8650899acb9415a4e285e3e3a1f87", GitTreeState:"dirty"} - Tag it v0.3 and build it: $ git tag -a v0.3 -m 'Release Kubernetes v0.3' $ make $ _output/go/bin/kubecfg -version Kubernetes v0.3 $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"3", GitVersion:"v0.3", GitCommit:"d916051e9db8650899acb9415a4e285e3e3a1f87", GitTreeState:"clean"} - Built it from a tarball: $ wget -O kubernetes.tgz https://api.github.com/repos/filbranden/kubernetes/tarball/version_string1 $ tar xvf kubernetes.tgz $ cd filbranden-kubernetes-*/ $ make $ _output/go/bin/kubecfg -version Kubernetes v0.2-dev $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"2+", GitVersion:"v0.2-dev", GitCommit:"", GitTreeState:"not a git tree"} - Built it with `go get`: # I need to prefetch the tree to replace the official tree with mine: $ mkdir -p ${TMPDIR}/gopath/src/github.com/ $ ln -sf filbranden ${TMPDIR}/gopath/src/github.com/GoogleCloudPlatform # Now run `go get` to build kubecfg: $ GOPATH=${TMPDIR}/gopath go get github.com/filbranden/kubernetes/cmd/kubecfg # Check the version: $ ${TMPDIR}/gopath/bin/kubecfg -version Kubernetes v0.2-dev $ ${TMPDIR}/gopath/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"2+", GitVersion:"v0.2-dev", GitCommit:"", GitTreeState:"not a git tree"} Signed-off-by: Filipe Brandenburger <filbranden@google.com>
2014-09-09 22:51:54 +00:00
return info.GitVersion
}
2015-09-23 08:18:05 +00:00
func init() {
buildInfo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "kubernetes_build_info",
Help: "A metric with a constant '1' value labeled by major, minor, git version, git commit and git tree state from which Kubernetes was built.",
},
[]string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState"},
)
info := Get()
buildInfo.WithLabelValues(info.Major, info.Minor, info.GitVersion, info.GitCommit, info.GitTreeState).Set(1)
prometheus.MustRegister(buildInfo)
}