ソースを参照

Add a version and commit to the binary

Daniel Ramich 3 年 前
コミット
a93e06d746
5 ファイル変更25 行追加4 行削除
  1. 8 1
      Dockerfile
  2. 2 1
      pkg/cmd/agent/agent.go
  3. 3 0
      pkg/cmd/costmodel/costmodel.go
  4. 0 2
      pkg/costmodel/router.go
  5. 12 0
      pkg/version/version.go

+ 8 - 1
Dockerfile

@@ -11,6 +11,9 @@ COPY go.sum .
 # golang:latest image does not contain GCC, while the AMD64 version does.
 ARG CGO_ENABLED=0
 
+ARG version=dev
+ARG	commit=HEAD
+
 # Get dependencies - will also be cached if we won't change mod/sum
 RUN go mod download
 # COPY the source code as the last step
@@ -21,7 +24,11 @@ RUN set -e ;\
     go test ./pkg/*;\
     cd cmd/costmodel;\
     GOOS=linux \
-    go build -a -installsuffix cgo -o /go/bin/app
+    go build -a -installsuffix cgo \
+    -ldflags \
+    "-X github.com/kubecost/opencost/pkg/version.Version=${version} \
+     -X github.com/kubecost/opencost/pkg/version.GitCommit=${commit}" \
+    -o /go/bin/app
 
 FROM alpine:latest
 RUN apk add --update --no-cache ca-certificates

+ 2 - 1
pkg/cmd/agent/agent.go

@@ -18,6 +18,7 @@ import (
 	"github.com/kubecost/opencost/pkg/metrics"
 	"github.com/kubecost/opencost/pkg/prom"
 	"github.com/kubecost/opencost/pkg/util/watcher"
+	"github.com/kubecost/opencost/pkg/version"
 
 	prometheus "github.com/prometheus/client_golang/api"
 	prometheusAPI "github.com/prometheus/client_golang/api/prometheus/v1"
@@ -125,7 +126,7 @@ func newPrometheusClient() (prometheus.Client, error) {
 }
 
 func Execute(opts *AgentOpts) error {
-	log.Infof("Starting Kubecost Agent")
+	log.Infof("Starting Kubecost Agent version %s", version.FriendlyVersion())
 
 	configWatchers := watcher.NewConfigMapWatchers()
 

+ 3 - 0
pkg/cmd/costmodel/costmodel.go

@@ -6,7 +6,9 @@ import (
 	"github.com/julienschmidt/httprouter"
 	"github.com/kubecost/opencost/pkg/costmodel"
 	"github.com/kubecost/opencost/pkg/errors"
+	"github.com/kubecost/opencost/pkg/log"
 	"github.com/kubecost/opencost/pkg/metrics"
+	"github.com/kubecost/opencost/pkg/version"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"github.com/rs/cors"
 )
@@ -23,6 +25,7 @@ func Healthz(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
 }
 
 func Execute(opts *CostModelOpts) error {
+	log.Infof("Starting cost-model version %s", version.FriendlyVersion())
 	a := costmodel.Initialize()
 
 	rootMux := http.NewServeMux()

+ 0 - 2
pkg/costmodel/router.go

@@ -1350,8 +1350,6 @@ func handlePanic(p errors.Panic) bool {
 }
 
 func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses {
-	log.Infof("Starting cost-model (git commit \"%s\")", env.GetAppVersion())
-
 	configWatchers := watcher.NewConfigMapWatchers(additionalConfigWatchers...)
 
 	var err error

+ 12 - 0
pkg/version/version.go

@@ -0,0 +1,12 @@
+package version
+
+import "fmt"
+
+var (
+	Version   = "dev"
+	GitCommit = "HEAD"
+)
+
+func FriendlyVersion() string {
+	return fmt.Sprintf("%s (%s)", Version, GitCommit)
+}