From 28d012ea007b3146cc7542277a097c8adc5504fd Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 4 Apr 2017 15:57:35 -0400 Subject: [PATCH 1/2] containerd: add access to default config when wanting to craft a custom config, but based on the default config, add a route to output the containerd config to a tempfile. Signed-off-by: Vincent Batts --- cmd/containerd/config.go | 16 +++++++++++++++- cmd/containerd/main.go | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/cmd/containerd/config.go b/cmd/containerd/config.go index 65e4018..82187f7 100644 --- a/cmd/containerd/config.go +++ b/cmd/containerd/config.go @@ -1,6 +1,11 @@ package main -import "github.com/BurntSushi/toml" +import ( + "bytes" + "io" + + "github.com/BurntSushi/toml" +) func defaultConfig() *config { return &config{ @@ -58,6 +63,15 @@ func (c *config) decodePlugin(name string, v interface{}) error { return c.md.PrimitiveDecode(p, v) } +func (c *config) WriteTo(w io.Writer) (int64, error) { + buf := bytes.NewBuffer(nil) + e := toml.NewEncoder(buf) + if err := e.Encode(c); err != nil { + return 0, err + } + return io.Copy(w, buf) +} + type grpcConfig struct { Socket string `toml:"socket"` Uid int `toml:"uid"` diff --git a/cmd/containerd/main.go b/cmd/containerd/main.go index fa0bffd..f9b5f5c 100644 --- a/cmd/containerd/main.go +++ b/cmd/containerd/main.go @@ -3,6 +3,7 @@ package main import ( _ "expvar" "fmt" + "io/ioutil" "net/http" _ "net/http/pprof" "os" @@ -60,7 +61,7 @@ func main() { app.Flags = []cli.Flag{ cli.StringFlag{ Name: "config,c", - Usage: "path to the configuration file", + Usage: "path to the configuration file (Use 'default' to output the default toml)", Value: "/etc/containerd/config.toml", }, cli.StringFlag{ @@ -148,6 +149,19 @@ func main() { } func before(context *cli.Context) error { + if context.GlobalString("config") == "default" { + fh, err := ioutil.TempFile("", "containerd-config.toml.") + if err != nil { + return err + } + if _, err := conf.WriteTo(fh); err != nil { + fh.Close() + return err + } + fh.Close() + log.G(global).Infof("containerd default config written to %q", fh.Name()) + os.Exit(0) + } if err := loadConfig(context.GlobalString("config")); err != nil && !os.IsNotExist(err) { return err From 93417d446c82626ebdf59014b109086cd24a8304 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 4 Apr 2017 15:31:35 -0400 Subject: [PATCH 2/2] containerd: missing config creates a default This fixes silently ignore the config file not existing as well. ```bash sudo ./bin/containerd --config farts $ sudo ./bin/containerd --config farts INFO[0000] config "farts" does not exist. Creating it. module=containerd INFO[0000] starting containerd boot... module=containerd INFO[0000] starting debug API... debug="/run/containerd/debug.sock" module=containerd INFO[0000] loading monitor plugin "cgroups"... module=containerd INFO[0000] loading runtime plugin "linux"... module=containerd INFO[0000] loading snapshot plugin "snapshot-overlay"... module=containerd INFO[0000] loading grpc service plugin "content-grpc"... module=containerd INFO[0000] loading grpc service plugin "metrics-grpc"... module=containerd INFO[0000] loading grpc service plugin "runtime-grpc"... module=containerd INFO[0000] loading grpc service plugin "healthcheck-grpc"... module=containerd INFO[0000] loading grpc service plugin "rootfs-grpc"... module=containerd INFO[0000] starting GRPC API server... module=containerd INFO[0000] containerd successfully booted in 0.001465s module=containerd ^C$ cat farts state = "/run/containerd" root = "/var/lib/containerd" snapshotter = "overlay" subreaper = false [grpc] socket = "/run/containerd/containerd.sock" uid = 0 gid = 0 [debug] socket = "/run/containerd/debug.sock" level = "info" [metrics] address = "" ``` Signed-off-by: Vincent Batts --- cmd/containerd/main.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/containerd/main.go b/cmd/containerd/main.go index f9b5f5c..2a4135c 100644 --- a/cmd/containerd/main.go +++ b/cmd/containerd/main.go @@ -162,10 +162,22 @@ func before(context *cli.Context) error { log.G(global).Infof("containerd default config written to %q", fh.Name()) os.Exit(0) } - if err := loadConfig(context.GlobalString("config")); err != nil && - !os.IsNotExist(err) { + err := loadConfig(context.GlobalString("config")) + if err != nil && !os.IsNotExist(err) { return err + } else if err != nil && os.IsNotExist(err) { + log.G(global).Infof("config %q does not exist. Creating it.", context.GlobalString("config")) + fh, err := os.Create(context.GlobalString("config")) + if err != nil { + return err + } + if _, err := conf.WriteTo(fh); err != nil { + fh.Close() + return err + } + fh.Close() } + // the order for config vs flag values is that flags will always override // the config values if they are set if err := setLevel(context); err != nil {