From 2aa268050d60c61db8720246c0455d3ac9ccf519 Mon Sep 17 00:00:00 2001 From: Christopher Desiniotis Date: Fri, 22 Dec 2023 13:16:18 -0800 Subject: [PATCH] Log version and git_commit info Signed-off-by: Christopher Desiniotis --- Makefile | 1 + cmd/nvidia-k8s-vgpu-dm/main.go | 6 ++++ cmd/nvidia-vgpu-dm/main.go | 5 +++- deployments/container/Dockerfile.ubi8 | 4 +-- deployments/container/Makefile | 1 + internal/info/version.go | 43 +++++++++++++++++++++++++++ versions.mk | 2 ++ 7 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 internal/info/version.go diff --git a/Makefile b/Makefile index 02550bf8..a1d089ec 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ DOCKER_TARGETS := $(patsubst %, docker-%, $(TARGETS)) .PHONY: $(TARGETS) $(DOCKER_TARGETS) GOOS := linux +VERSION_PKG=$(MODULE)/internal/info ifneq ($(PREFIX),) cmd-%: COMMAND_BUILD_OPTIONS = -o $(PREFIX)/$(*) diff --git a/cmd/nvidia-k8s-vgpu-dm/main.go b/cmd/nvidia-k8s-vgpu-dm/main.go index 3641efeb..a05ac3f6 100644 --- a/cmd/nvidia-k8s-vgpu-dm/main.go +++ b/cmd/nvidia-k8s-vgpu-dm/main.go @@ -36,6 +36,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/util/wait" + + "github.com/NVIDIA/vgpu-device-manager/internal/info" ) const ( @@ -102,8 +104,10 @@ func (m *SyncableVGPUConfig) Get() string { func main() { c := cli.NewApp() + c.Name = "nvidia-k8s-vgpu-dm" c.Before = validateFlags c.Action = start + c.Version = info.GetVersionString() c.Flags = []cli.Flag{ &cli.StringFlag{ @@ -147,6 +151,8 @@ func main() { }, } + log.Infof("version: %s", c.Version) + err := c.Run(os.Args) if err != nil { log.SetOutput(os.Stderr) diff --git a/cmd/nvidia-vgpu-dm/main.go b/cmd/nvidia-vgpu-dm/main.go index d6a23793..61d56218 100644 --- a/cmd/nvidia-vgpu-dm/main.go +++ b/cmd/nvidia-vgpu-dm/main.go @@ -24,6 +24,7 @@ import ( "github.com/NVIDIA/vgpu-device-manager/cmd/nvidia-vgpu-dm/apply" "github.com/NVIDIA/vgpu-device-manager/cmd/nvidia-vgpu-dm/assert" + "github.com/NVIDIA/vgpu-device-manager/internal/info" ) // Flags represents the top level flags that can be passed to the vgpu-dm CLI @@ -37,7 +38,7 @@ func main() { c := cli.NewApp() c.Name = "nvidia-vgpu-dm" c.Usage = "Manage NVIDIA vGPU devices" - c.Version = "0.2.0" + c.Version = info.GetVersionString() c.Flags = []cli.Flag{ &cli.BoolFlag{ @@ -66,6 +67,8 @@ func main() { return nil } + log.Infof("version: %s", c.Version) + err := c.Run(os.Args) if err != nil { log.Fatalf(err.Error()) diff --git a/deployments/container/Dockerfile.ubi8 b/deployments/container/Dockerfile.ubi8 index a93f3f14..a2d3adf0 100644 --- a/deployments/container/Dockerfile.ubi8 +++ b/deployments/container/Dockerfile.ubi8 @@ -14,8 +14,6 @@ ARG BASE_DIST=ubi8 ARG CUDA_VERSION -ARG GOLANG_VERSION=x.x.x -ARG VERSION="N/A" FROM nvcr.io/nvidia/cuda:${CUDA_VERSION}-base-${BASE_DIST} as build @@ -39,6 +37,8 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH WORKDIR /build COPY . . +ARG VERSION="N/A" +ARG GIT_COMMIT="unknown" RUN make PREFIX=/artifacts cmds diff --git a/deployments/container/Makefile b/deployments/container/Makefile index c3ebf108..96f8d49e 100644 --- a/deployments/container/Makefile +++ b/deployments/container/Makefile @@ -84,6 +84,7 @@ $(BUILD_TARGETS): build-%: --build-arg CUDA_VERSION="$(CUDA_VERSION)" \ --build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \ --build-arg VERSION="$(VERSION)" \ + --build-arg GIT_COMMIT="$(GIT_COMMIT)" \ --build-arg CVE_UPDATES="$(CVE_UPDATES)" \ --file $(DOCKERFILE) \ $(CURDIR) diff --git a/internal/info/version.go b/internal/info/version.go new file mode 100644 index 00000000..0b9c906e --- /dev/null +++ b/internal/info/version.go @@ -0,0 +1,43 @@ +/* +# Copyright (c) NVIDIA CORPORATION. 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 +# limitations under the License. +*/ + +package info + +import "strings" + +// version must be set by go build's -X main.version= option in the Makefile. +var version = "unknown" + +// gitCommit will be the hash that the binary was built from +// and will be populated by the Makefile +var gitCommit = "" + +// GetVersionParts returns the different version components +func GetVersionParts() []string { + v := []string{version} + + if gitCommit != "" { + v = append(v, "commit: "+gitCommit) + } + + return v +} + +// GetVersionString returns the string representation of the version +func GetVersionString(more ...string) string { + v := append(GetVersionParts(), more...) + return strings.Join(v, ", ") +} diff --git a/versions.mk b/versions.mk index 1dae1061..8e101bed 100644 --- a/versions.mk +++ b/versions.mk @@ -19,3 +19,5 @@ vVERSION := v$(VERSION:v%=%) CUDA_VERSION := 12.2.2 GOLANG_VERSION := 1.20.4 + +GIT_COMMIT ?= $(shell git describe --match="" --dirty --long --always --abbrev=40 2> /dev/null || echo "")