From b8cadf7539f2ed429f42582f45b38b4f563f2633 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 27 Jan 2022 14:09:09 +0100 Subject: [PATCH 1/3] runtime/v2/shim: strip path information from version output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I noticed that path information showed up in the version output: ./bin/containerd-shim-runc-v1 -v ./bin/containerd-shim-runc-v1: Version: v1.6.0-rc.1 Revision: ad771115b82a70cfd8018d72ae489c707e63de16.m Go version: go1.17.2 POSIX guidelines describes; https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion > The program’s name should be a constant string; don’t compute it from argv[0]. > The idea is to state the standard or canonical name for the program, not its > file name. Unfortunately, this code is used by multiple binaries, so we can't fully remove the use of os.Args[0], but let's make a start and just remove the path info. Signed-off-by: Sebastiaan van Stijn --- runtime/v2/shim/shim.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/v2/shim/shim.go b/runtime/v2/shim/shim.go index 59e522ced..cc5b68440 100644 --- a/runtime/v2/shim/shim.go +++ b/runtime/v2/shim/shim.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "os" + "path/filepath" "runtime" "runtime/debug" "strings" @@ -240,7 +241,7 @@ func RunManager(ctx context.Context, manager Manager, opts ...BinaryOpts) { func run(ctx context.Context, manager Manager, initFunc Init, name string, config Config) error { parseFlags() if versionFlag { - fmt.Printf("%s:\n", os.Args[0]) + fmt.Printf("%s:\n", filepath.Base(os.Args[0])) fmt.Println(" Version: ", version.Version) fmt.Println(" Revision:", version.Revision) fmt.Println(" Go version:", version.GoVersion) From e79aba10d4c6c4eadd7dcc672a3d0d15e62b9969 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 31 Jan 2022 12:35:55 +0100 Subject: [PATCH 2/3] integration/images/volume-ownership: strip path information from usage output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POSIX guidelines describes; https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion > The program’s name should be a constant string; don’t compute it from argv[0]. > The idea is to state the standard or canonical name for the program, not its > file name. We don't have a const for this, but let's make a start and just remove the path info. Signed-off-by: Sebastiaan van Stijn --- integration/images/volume-ownership/tools/get_owner_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/images/volume-ownership/tools/get_owner_windows.go b/integration/images/volume-ownership/tools/get_owner_windows.go index 04322893e..b7870cd9a 100644 --- a/integration/images/volume-ownership/tools/get_owner_windows.go +++ b/integration/images/volume-ownership/tools/get_owner_windows.go @@ -26,7 +26,7 @@ import ( func main() { if len(os.Args) != 2 { - fmt.Printf("Usage: %s file_or_directory\n", os.Args[0]) + fmt.Println("Usage: get_owner_windows.exe file_or_directory") os.Exit(1) } From fdbfde5d8101360ad587f527030f25e8d108bfd4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 31 Jan 2022 12:37:02 +0100 Subject: [PATCH 3/3] cmd/containerd-shim: add -v (version) flag Unlike the other shims, containerd-shim did not have a -v (version) flag: ./bin/containerd-shim-runc-v1 -v ./bin/containerd-shim-runc-v1: Version: v1.6.0-rc.1 Revision: ad771115b82a70cfd8018d72ae489c707e63de16.m Go version: go1.17.2 ./bin/containerd-shim -v flag provided but not defined: -v Usage of ./bin/containerd-shim: This patch adds a `-v` flag to be consistent with the other shims. The code was slightly refactored to match the implementation in the other shims, taking the same approach as https://github.com/containerd/containerd/blob/77d53d2d230c3bcd3f02e6f493019a72905c875b/runtime/v2/shim/shim.go#L240-L256 Signed-off-by: Sebastiaan van Stijn --- cmd/containerd-shim/main_unix.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/containerd-shim/main_unix.go b/cmd/containerd-shim/main_unix.go index 024611bf3..49a3b12ee 100644 --- a/cmd/containerd-shim/main_unix.go +++ b/cmd/containerd-shim/main_unix.go @@ -42,6 +42,7 @@ import ( "github.com/containerd/containerd/runtime/v1/shim" shimapi "github.com/containerd/containerd/runtime/v1/shim/v1" "github.com/containerd/containerd/sys/reaper" + "github.com/containerd/containerd/version" "github.com/containerd/ttrpc" "github.com/containerd/typeurl" ptypes "github.com/gogo/protobuf/types" @@ -52,6 +53,7 @@ import ( var ( debugFlag bool + versionFlag bool namespaceFlag string socketFlag string addressFlag string @@ -68,8 +70,9 @@ var ( } ) -func init() { +func parseFlags() { flag.BoolVar(&debugFlag, "debug", false, "enable debug output in logs") + flag.BoolVar(&versionFlag, "v", false, "show the shim version and exit") flag.StringVar(&namespaceFlag, "namespace", "", "namespace that owns the shim") flag.StringVar(&socketFlag, "socket", "", "socket path to serve") flag.StringVar(&addressFlag, "address", "", "grpc address back to main containerd") @@ -83,23 +86,36 @@ func init() { flag.Parse() } -func main() { +func setRuntime() { debug.SetGCPercent(40) go func() { for range time.Tick(30 * time.Second) { debug.FreeOSMemory() } }() - - if debugFlag { - logrus.SetLevel(logrus.DebugLevel) - } - if os.Getenv("GOMAXPROCS") == "" { // If GOMAXPROCS hasn't been set, we default to a value of 2 to reduce // the number of Go stacks present in the shim. runtime.GOMAXPROCS(2) } +} + +func main() { + parseFlags() + if versionFlag { + fmt.Println("containerd-shim") + fmt.Println(" Version: ", version.Version) + fmt.Println(" Revision:", version.Revision) + fmt.Println(" Go version:", version.GoVersion) + fmt.Println("") + return + } + + setRuntime() + + if debugFlag { + logrus.SetLevel(logrus.DebugLevel) + } stdout, stderr, err := openStdioKeepAlivePipes(workdirFlag) if err != nil {