From cf43fd2556261fda378cf5fc36b29d8858526fb5 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Fri, 2 Oct 2015 10:26:33 +0100 Subject: [PATCH] Expose git revision as well as version in version info, add Makefile --- Makefile | 38 ++++++++++++++++++++++++++ build/build.sh | 42 +++++++++++++++++++++++++++++ cadvisor.go | 4 +-- info/v1/machine.go | 2 ++ manager/machine.go | 3 ++- metrics/prometheus.go | 4 +-- metrics/prometheus_test.go | 3 ++- metrics/testdata/prometheus_metrics | 2 +- version/VERSION | 1 + version/version.go | 21 +++++++++++++-- 10 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 Makefile create mode 100755 build/build.sh create mode 100644 version/VERSION diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..8bd9c445 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +# Copyright 2015 Google Inc. All rights reserved. +# +# 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 + +GO := godep go +pkgs = $(shell $(GO) list ./...) + +all: format build test + +test: + @echo ">> running tests" + @$(GO) test -short $(pkgs) + +format: + @echo ">> formatting code" + @$(GO) fmt $(pkgs) + +vet: + @echo ">> vetting code" + @$(GO) vet $(pkgs) + +build: + @echo ">> building binaries" + @./build/build.sh + +docker: + @docker build -t cadvisor:$(shell git rev-parse --short HEAD) -f deploy/Dockerfile . + +.PHONY: all format build test vet docker diff --git a/build/build.sh b/build/build.sh new file mode 100755 index 00000000..b1d5908c --- /dev/null +++ b/build/build.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +# Copyright 2015 Google Inc. All rights reserved. +# +# 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 + +set -e + +repo_path="github.com/google/cadvisor" + +version=$( cat version/VERSION ) +revision=$( git rev-parse --short HEAD 2> /dev/null || echo 'unknown' ) +branch=$( git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown' ) +host=$( hostname -f ) +build_date=$( date +%Y%m%d-%H:%M:%S ) +go_version=$( go version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/' ) + +if [ "$(go env GOOS)" = "windows" ]; then +› ext=".exe" +fi + +ldflags=" + -X ${repo_path}/version.Version=${version} + -X ${repo_path}/version.Revision=${revision} + -X ${repo_path}/version.Branch=${branch} + -X ${repo_path}/version.BuildUser=${USER}@${host} + -X ${repo_path}/version.BuildDate=${build_date} + -X ${repo_path}/version.GoVersion=${go_version}" + +echo " > cadvisor" +godep go build -ldflags "${ldflags}" -o cadvisor${ext} ${repo_path} + +exit 0 diff --git a/cadvisor.go b/cadvisor.go index 2fdd547d..3bf832e5 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -54,7 +54,7 @@ func main() { flag.Parse() if *versionFlag { - fmt.Printf("cAdvisor version %s\n", version.VERSION) + fmt.Printf("cAdvisor version %s (%s)\n", version.Info["version"], version.Info["revision"]) os.Exit(0) } @@ -91,7 +91,7 @@ func main() { // Install signal handler. installSignalHandler(containerManager) - glog.Infof("Starting cAdvisor version: %q on port %d", version.VERSION, *argPort) + glog.Infof("Starting cAdvisor version: %s-%s on port %d", version.Info["version"], version.Info["revision"], *argPort) addr := fmt.Sprintf("%s:%d", *argIp, *argPort) glog.Fatal(http.ListenAndServe(addr, nil)) diff --git a/info/v1/machine.go b/info/v1/machine.go index dc07ffa6..7f50ce3b 100644 --- a/info/v1/machine.go +++ b/info/v1/machine.go @@ -179,6 +179,8 @@ type VersionInfo struct { // cAdvisor version. CadvisorVersion string `json:"cadvisor_version"` + // cAdvisor git revision. + CadvisorRevision string `json:"cadvisor_revision"` } type MachineInfoFactory interface { diff --git a/manager/machine.go b/manager/machine.go index 5ff60b65..5e288bc6 100644 --- a/manager/machine.go +++ b/manager/machine.go @@ -122,7 +122,8 @@ func getVersionInfo() (*info.VersionInfo, error) { KernelVersion: kernel_version, ContainerOsVersion: container_os, DockerVersion: docker_version, - CadvisorVersion: version.VERSION, + CadvisorVersion: version.Info["version"], + CadvisorRevision: version.Info["revision"], }, nil } diff --git a/metrics/prometheus.go b/metrics/prometheus.go index 2e8ec06a..6d3b637e 100644 --- a/metrics/prometheus.go +++ b/metrics/prometheus.go @@ -446,7 +446,7 @@ func NewPrometheusCollector(infoProvider infoProvider) *PrometheusCollector { } var ( - versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion"}, nil) + versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion", "cadvisorRevision"}, nil) machineInfoCoresDesc = prometheus.NewDesc("machine_cpu_cores", "Number of CPU cores on the machine.", nil, nil) machineInfoMemoryDesc = prometheus.NewDesc("machine_memory_bytes", "Amount of memory installed on the machine.", nil, nil) ) @@ -527,7 +527,7 @@ func (c *PrometheusCollector) collectVersionInfo(ch chan<- prometheus.Metric) { glog.Warningf("Couldn't get version info: %s", err) return } - ch <- prometheus.MustNewConstMetric(versionInfoDesc, prometheus.GaugeValue, 1, []string{versionInfo.KernelVersion, versionInfo.ContainerOsVersion, versionInfo.DockerVersion, versionInfo.CadvisorVersion}...) + ch <- prometheus.MustNewConstMetric(versionInfoDesc, prometheus.GaugeValue, 1, []string{versionInfo.KernelVersion, versionInfo.ContainerOsVersion, versionInfo.DockerVersion, versionInfo.CadvisorVersion, versionInfo.CadvisorRevision}...) } func (c *PrometheusCollector) collectMachineInfo(ch chan<- prometheus.Metric) { diff --git a/metrics/prometheus_test.go b/metrics/prometheus_test.go index 2ca5fa81..84259b70 100644 --- a/metrics/prometheus_test.go +++ b/metrics/prometheus_test.go @@ -35,6 +35,7 @@ func (p testSubcontainersInfoProvider) GetVersionInfo() (*info.VersionInfo, erro ContainerOsVersion: "Fedora 22 (Twenty Two)", DockerVersion: "1.8.1", CadvisorVersion: "0.16.0", + CadvisorRevision: "abcdef", }, nil } @@ -170,7 +171,7 @@ func TestPrometheusCollector(t *testing.T) { // (https://github.com/prometheus/client_golang/issues/58), we simply compare // verbatim text-format metrics outputs, but ignore certain metric lines // whose value depends on the current time or local circumstances. - includeRe := regexp.MustCompile("^(# HELP |# TYPE |)container_") + includeRe := regexp.MustCompile("^(?:(?:# HELP |# TYPE)container_|cadvisor_version_info{)") ignoreRe := regexp.MustCompile("^container_last_seen{") for i, want := range wantLines { if !includeRe.MatchString(want) || ignoreRe.MatchString(want) { diff --git a/metrics/testdata/prometheus_metrics b/metrics/testdata/prometheus_metrics index b4d089bf..0f20e194 100644 --- a/metrics/testdata/prometheus_metrics +++ b/metrics/testdata/prometheus_metrics @@ -1,6 +1,6 @@ # HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version. # TYPE cadvisor_version_info gauge -cadvisor_version_info{cadvisorVersion="0.16.0",dockerVersion="1.8.1",kernelVersion="4.1.6-200.fc22.x86_64",osVersion="Fedora 22 (Twenty Two)"} 1 +cadvisor_version_info{cadvisorRevision="abcdef",cadvisorVersion="0.16.0",dockerVersion="1.8.1",kernelVersion="4.1.6-200.fc22.x86_64",osVersion="Fedora 22 (Twenty Two)"} 1 # HELP container_cpu_system_seconds_total Cumulative system cpu time consumed in seconds. # TYPE container_cpu_system_seconds_total counter container_cpu_system_seconds_total{id="testcontainer",image="test",name="testcontaineralias"} 7e-09 diff --git a/version/VERSION b/version/VERSION new file mode 100644 index 00000000..66333910 --- /dev/null +++ b/version/VERSION @@ -0,0 +1 @@ +0.18.0 diff --git a/version/version.go b/version/version.go index df46539a..965c2931 100644 --- a/version/version.go +++ b/version/version.go @@ -14,5 +14,22 @@ package version -// Version of cAdvisor. -const VERSION = "0.18.0" +// Build information. Populated at build-time. +var ( + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion string +) + +// Info provides the iterable version information. +var Info = map[string]string{ + "version": Version, + "revision": Revision, + "branch": Branch, + "buildUser": BuildUser, + "buildDate": BuildDate, + "goVersion": GoVersion, +}