github.com/demonoid81/containerd@v1.3.4/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  	timeouts := timeout.All()
    69  	config.Timeouts = make(map[string]string)
    70  	for k, v := range timeouts {
    71  		config.Timeouts[k] = v.String()
    72  	}
    73  
    74  	// for the time being, keep the defaultConfig's version set at 1 so that
    75  	// when a config without a version is loaded from disk and has no version
    76  	// set, we assume it's a v1 config.  But when generating new configs via
    77  	// this command, generate the v2 config
    78  	config.Config.Version = 2
    79  
    80  	// remove overridden Plugins type to avoid duplication in output
    81  	config.Config.Plugins = nil
    82  
    83  	_, err = config.WriteTo(os.Stdout)
    84  	return err
    85  }
    86  
    87  var configCommand = cli.Command{
    88  	Name:  "config",
    89  	Usage: "information on the containerd config",
    90  	Subcommands: []cli.Command{
    91  		{
    92  			Name:  "default",
    93  			Usage: "see the output of the default config",
    94  			Action: func(context *cli.Context) error {
    95  				return outputConfig(defaultConfig())
    96  			},
    97  		},
    98  		{
    99  			Name:  "dump",
   100  			Usage: "see the output of the final main config with imported in subconfig files",
   101  			Action: func(context *cli.Context) error {
   102  				config := defaultConfig()
   103  				if err := srvconfig.LoadConfig(context.GlobalString("config"), config); err != nil && !os.IsNotExist(err) {
   104  					return err
   105  				}
   106  
   107  				return outputConfig(config)
   108  			},
   109  		},
   110  	},
   111  }