diff --git a/agent/Makefile b/agent/Makefile index 51a1b2bdc..18c72b8d9 100644 --- a/agent/Makefile +++ b/agent/Makefile @@ -17,14 +17,15 @@ GOMOD := $(shell go env GOMOD) GOSUM := $(GOMOD:.mod=.sum) SOURCES:=$(shell find . -name '*.go') DOCKER_IMAGE_TAG?=latest +VERSION := $(shell git describe --always) all: agent agent: *.go $(GOMOD) $(GOSUM) ifneq ($(STATIC_AGENT),) - CGO_ENABLED=0 go build -installsuffix cgo -a -ldflags "-s" $(EXTRAGOARGS) -o agent + CGO_ENABLED=0 go build -installsuffix cgo -a -ldflags "-s -X main.version=$(VERSION)" $(EXTRAGOARGS) -o agent else - go build $(EXTRAGOARGS) -o agent + go build $(EXTRAGOARGS) -ldflags "-X main.version=$(VERSION)" -o agent endif test: diff --git a/agent/main.go b/agent/main.go index 1557c1ee4..407823731 100644 --- a/agent/main.go +++ b/agent/main.go @@ -16,6 +16,7 @@ package main import ( "context" "flag" + "fmt" "os" "os/signal" "syscall" @@ -47,20 +48,31 @@ const ( enableSubreaper = 1 ) +var ( + version string +) + func main() { var ( port int debug bool + version bool ) flag.IntVar(&port, "port", defaultPort, "Vsock port to listen to") flag.BoolVar(&debug, "debug", false, "Turn on debug mode") + flag.BoolVar(&version, "version", false, "Show the version") flag.Parse() if debug { logrus.SetLevel(logrus.DebugLevel) } + if version { + showVersion() + return + } + signals := make(chan os.Signal, 32) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, unix.SIGCHLD) @@ -155,3 +167,9 @@ func main() { panic(err) } } + +func showVersion() { + // After Go 1.12, We can use runtime/debug.BuildInfo instead. + // https://github.com/golang/go/issues/22147 + fmt.Printf("containerd Firecracker agent %s\n", version) +}