runsc: Refactor in how the version string is propagated in runsc.

This is helpful so that it can be imported form other packages without import
loops.

This will be used in a follow-up change to add the version string as a
per-sandbox metric metadata label.

PiperOrigin-RevId: 519002695
This commit is contained in:
Etienne Perot
2023-03-23 17:12:09 -07:00
committed by gVisor bot
parent f8b9824813
commit d0326a67da
7 changed files with 48 additions and 16 deletions
+10 -6
View File
@@ -9,15 +9,17 @@ go_binary(
name = "runsc",
srcs = [
"main.go",
"version.go",
],
pure = True,
tags = ["staging"],
visibility = [
"//visibility:public",
],
x_defs = {"main.version": "{STABLE_VERSION}"},
deps = ["//runsc/cli"],
x_defs = {"gvisor.dev/gvisor/runsc/version.version": "{STABLE_VERSION}"},
deps = [
"//runsc/cli",
"//runsc/version",
],
)
# The runsc-race target is a race-compatible BUILD target. This must be built
@@ -37,15 +39,17 @@ go_binary(
name = "runsc-race",
srcs = [
"main.go",
"version.go",
],
gotags = ["lockdep"],
static = True,
visibility = [
"//visibility:public",
],
x_defs = {"main.version": "{STABLE_VERSION}"},
deps = ["//runsc/cli"],
x_defs = {"gvisor.dev/gvisor/runsc/version.version": "{STABLE_VERSION}"},
deps = [
"//runsc/cli",
"//runsc/version",
],
)
sh_test(
+1
View File
@@ -24,6 +24,7 @@ go_library(
"//runsc/config",
"//runsc/flag",
"//runsc/specutils",
"//runsc/version",
"@com_github_google_subcommands//:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
+4 -3
View File
@@ -38,6 +38,7 @@ import (
"gvisor.dev/gvisor/runsc/config"
"gvisor.dev/gvisor/runsc/flag"
"gvisor.dev/gvisor/runsc/specutils"
"gvisor.dev/gvisor/runsc/version"
)
var (
@@ -56,7 +57,7 @@ var (
)
// Main is the main entrypoint.
func Main(version string) {
func Main() {
// Help and flags commands are generated automatically.
help := cmd.NewHelp(subcommands.DefaultCommander)
help.Register(new(cmd.Platforms))
@@ -118,7 +119,7 @@ func Main(version string) {
// Are we showing the version?
if *showVersion {
// The format here is the same as runc.
fmt.Fprintf(os.Stdout, "runsc version %s\n", version)
fmt.Fprintf(os.Stdout, "runsc version %s\n", version.Version())
fmt.Fprintf(os.Stdout, "spec: %s\n", specutils.Version)
os.Exit(0)
}
@@ -221,7 +222,7 @@ func Main(version string) {
log.Infof("***************************")
log.Infof("Args: %s", os.Args)
log.Infof("Version %s", version)
log.Infof("Version %s", version.Version())
log.Infof("GOOS: %s", runtime.GOOS)
log.Infof("GOARCH: %s", runtime.GOARCH)
log.Infof("PID: %d", os.Getpid())
+6 -1
View File
@@ -17,8 +17,13 @@ package main
import (
"gvisor.dev/gvisor/runsc/cli"
"gvisor.dev/gvisor/runsc/version"
)
// version.Version is set dynamically, but needs to be
// linked in the binary, so reference it here.
var _ = version.Version()
func main() {
cli.Main(version)
cli.Main()
}
+12
View File
@@ -0,0 +1,12 @@
load("//tools:defs.bzl", "go_library")
package(
default_applicable_licenses = ["//:license"],
licenses = ["notice"],
)
go_library(
name = "version",
srcs = ["version.go"],
visibility = ["//:sandbox"],
)
+12 -3
View File
@@ -1,4 +1,4 @@
// Copyright 2019 The gVisor Authors.
// Copyright 2023 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -15,7 +15,16 @@
//go:build go1.1
// +build go1.1
package main
// Package version holds a string containing version information for runsc.
// Other packages may import it to get this information while avoiding
// import loops.
package version
// version is set during linking.
// version is the version string.
// It is initialized by the runsc main() function.
var version = "VERSION_MISSING"
// Version returns the version string.
func Version() string {
return version
}
+3 -3
View File
@@ -1,6 +1,6 @@
#!/bin/bash
# Copyright 2018 The gVisor Authors.
# Copyright 2023 The gVisor Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@ set -euf -x -o pipefail
readonly runsc="$1"
readonly version=$($runsc --version)
# Version should should not match VERSION, which is the default and which will
# also appear if something is wrong with workspace_status.sh script.
# Version should should not match VERSION, which is the default and which
# will also appear if something is wrong with workspace_status.sh script.
if [[ $version =~ "VERSION" ]]; then
echo "FAIL: Got bad version $version"
exit 1