Skip to content

Commit

Permalink
Include Go version, platform, and other build info in version string
Browse files Browse the repository at this point in the history
Additionally update MatchesServerVersion to only check GitVersion,
GitCommit, and GitTreeState.
  • Loading branch information
ixdy committed Mar 14, 2016
1 parent 4c2d129 commit fb663f2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hack/lib/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ kube::version::ldflag() {
kube::version::ldflags() {
kube::version::get_version_vars

local -a ldflags=()
local -a ldflags=($(kube::version::ldflag "buildDate" "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"))
if [[ -n ${KUBE_GIT_COMMIT-} ]]; then
ldflags+=($(kube::version::ldflag "gitCommit" "${KUBE_GIT_COMMIT}"))
ldflags+=($(kube::version::ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}"))
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/unversioned/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package unversioned

import (
"fmt"
"reflect"

"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
Expand Down Expand Up @@ -98,13 +97,14 @@ func MatchesServerVersion(client *Client, c *restclient.Config) error {
return err
}
}
clientVersion := version.Get()
serverVersion, err := client.Discovery().ServerVersion()
cVer := version.Get()
sVer, err := client.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("couldn't read version from server: %v\n", err)
}
if s := *serverVersion; !reflect.DeepEqual(clientVersion, s) {
return fmt.Errorf("server version (%#v) differs from client version (%#v)!\n", s, clientVersion)
// GitVersion includes GitCommit and GitTreeState, but best to be safe?
if cVer.GitVersion != sVer.GitVersion || cVer.GitCommit != sVer.GitCommit || cVer.GitTreeState != cVer.GitTreeState {
return fmt.Errorf("server version (%#v) differs from client version (%#v)!\n", sVer, cVer)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/generic_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var CommonMetrics = map[string][]string{
"http_response_size_bytes": {"handler", "quantile"},
"http_response_size_bytes_count": {"handler"},
"http_response_size_bytes_sum": {"handler"},
"kubernetes_build_info": {"major", "minor", "gitCommit", "gitTreeState", "gitVersion"},
"kubernetes_build_info": {"major", "minor", "gitCommit", "gitTreeState", "gitVersion", "buildDate", "goVersion", "compiler", "platform"},
"process_cpu_seconds_total": {},
"process_max_fds": {},
"process_open_fds": {},
Expand Down
2 changes: 2 additions & 0 deletions pkg/version/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ var (
gitVersion string = "v0.0.0-master+$Format:%h$"
gitCommit string = "$Format:%H$" // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState string = "not a git tree" // state of git tree, either "clean" or "dirty"

buildDate string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)
21 changes: 17 additions & 4 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

package version

import "github.com/prometheus/client_golang/prometheus"
import (
"fmt"
"runtime"

"github.com/prometheus/client_golang/prometheus"
)

// Info contains versioning information.
// TODO: Add []string of api versions supported? It's still unclear
Expand All @@ -27,6 +32,10 @@ type Info struct {
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}

// Get returns the overall codebase version. It's for detecting
Expand All @@ -40,6 +49,10 @@ func Get() Info {
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

Expand All @@ -52,12 +65,12 @@ 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.",
Help: "A metric with a constant '1' value labeled by major, minor, git version, git commit, git tree state, build date, Go version, and compiler from which Kubernetes was built, and platform on which it is running.",
},
[]string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState"},
[]string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState", "buildDate", "goVersion", "compiler", "platform"},
)
info := Get()
buildInfo.WithLabelValues(info.Major, info.Minor, info.GitVersion, info.GitCommit, info.GitTreeState).Set(1)
buildInfo.WithLabelValues(info.Major, info.Minor, info.GitVersion, info.GitCommit, info.GitTreeState, info.BuildDate, info.GoVersion, info.Compiler, info.Platform).Set(1)

prometheus.MustRegister(buildInfo)
}

0 comments on commit fb663f2

Please sign in to comment.