github.com/lalkh/containerd@v1.4.3/cmd/containerd/command/config.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package command 18 19 import ( 20 gocontext "context" 21 "io" 22 "os" 23 24 "github.com/BurntSushi/toml" 25 "github.com/containerd/containerd/pkg/timeout" 26 "github.com/containerd/containerd/services/server" 27 srvconfig "github.com/containerd/containerd/services/server/config" 28 "github.com/urfave/cli" 29 ) 30 31 // Config is a wrapper of server config for printing out. 32 type Config struct { 33 *srvconfig.Config 34 // Plugins overrides `Plugins map[string]toml.Primitive` in server config. 35 Plugins map[string]interface{} `toml:"plugins"` 36 } 37 38 // WriteTo marshals the config to the provided writer 39 func (c *Config) WriteTo(w io.Writer) (int64, error) { 40 return 0, toml.NewEncoder(w).Encode(c) 41 } 42 43 func outputConfig(cfg *srvconfig.Config) error { 44 config := &Config{ 45 Config: cfg, 46 } 47 48 plugins, err := server.LoadPlugins(gocontext.Background(), config.Config) 49 if err != nil { 50 return err 51 } 52 if len(plugins) != 0 { 53 config.Plugins = make(map[string]interface{}) 54 for _, p := range plugins { 55 if p.Config == nil { 56 continue 57 } 58 59 pc, err := config.Decode(p) 60 if err != nil { 61 return err 62 } 63 64 config.Plugins[p.URI()] = pc 65 } 66 } 67 68 if config.Timeouts == nil { 69 config.Timeouts = make(map[string]string) 70 } 71 timeouts := timeout.All() 72 for k, v := range timeouts { 73 if config.Timeouts[k] == "" { 74 config.Timeouts[k] = v.String() 75 } 76 } 77 78 // for the time being, keep the defaultConfig's version set at 1 so that 79 // when a config without a version is loaded from disk and has no version 80 // set, we assume it's a v1 config. But when generating new configs via 81 // this command, generate the v2 config 82 config.Config.Version = 2 83 84 // remove overridden Plugins type to avoid duplication in output 85 config.Config.Plugins = nil 86 87 _, err = config.WriteTo(os.Stdout) 88 return err 89 } 90 91 var configCommand = cli.Command{ 92 Name: "config", 93 Usage: "information on the containerd config", 94 Subcommands: []cli.Command{ 95 { 96 Name: "default", 97 Usage: "see the output of the default config", 98 Action: func(context *cli.Context) error { 99 return outputConfig(defaultConfig()) 100 }, 101 }, 102 { 103 Name: "dump", 104 Usage: "see the output of the final main config with imported in subconfig files", 105 Action: func(context *cli.Context) error { 106 config := defaultConfig() 107 if err := srvconfig.LoadConfig(context.GlobalString("config"), config); err != nil && !os.IsNotExist(err) { 108 return err 109 } 110 111 return outputConfig(config) 112 }, 113 }, 114 }, 115 }