add config file for cri-containerd

fix #182

Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
This commit is contained in:
yason
2017-09-05 16:24:30 +08:00
committed by yanxuean
9 changed files with 67 additions and 89 deletions

View File

@@ -20,12 +20,13 @@ import (
"os"
"github.com/golang/glog"
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
"github.com/kubernetes-incubator/cri-containerd/pkg/server"
"github.com/kubernetes-incubator/cri-containerd/pkg/version"
"github.com/opencontainers/selinux/go-selinux"
"github.com/spf13/pflag"
"k8s.io/kubernetes/pkg/util/interrupt"
"github.com/kubernetes-incubator/cri-containerd/cmd/cri-containerd/options"
"github.com/kubernetes-incubator/cri-containerd/pkg/server"
"github.com/kubernetes-incubator/cri-containerd/pkg/version"
)
func main() {
@@ -35,7 +36,7 @@ func main() {
glog.Exitf("Failed to init CRI containerd flags: %v", err)
}
glog.V(2).Infof("Run cri-containerd %#v", o)
glog.V(2).Infof("Run cri-containerd %+v", o)
if o.PrintVersion {
version.PrintVersion()
os.Exit(0)
@@ -56,6 +57,7 @@ func main() {
o.StreamServerAddress,
o.StreamServerPort,
o.CgroupPath,
o.SandboxImage,
)
if err != nil {
glog.Exitf("Failed to create CRI containerd service %+v: %v", o, err)

View File

@@ -18,53 +18,61 @@ package options
import (
"flag"
"os"
"github.com/BurntSushi/toml"
"github.com/containerd/containerd"
"github.com/spf13/pflag"
"os"
"time"
)
const (
// "default path to the config file"
configFilePathArgName = "config-file-path"
)
// configFilePathArgName is the path to the config file.
const configFilePathArgName = "config"
//Config contains cri-containerd toml config
// ContainerdConfig contains config related to containerd
type ContainerdConfig struct {
// ContainerdSnapshotter is the snapshotter used by containerd.
ContainerdSnapshotter string `toml:"snapshotter"`
// ContainerdEndpoint is the containerd endpoint path.
ContainerdEndpoint string `toml:"endpoint"`
}
// CniConfig contains config related to cni
type CniConfig struct {
// NetworkPluginBinDir is the directory in which the binaries for the plugin is kept.
NetworkPluginBinDir string `toml:"bin_dir"`
// NetworkPluginConfDir is the directory in which the admin places a CNI conf.
NetworkPluginConfDir string `toml:"conf_dir"`
}
// Config contains cri-containerd toml config
type Config struct {
// ContainerdConfig contains config related to containerd
ContainerdConfig `toml:"containerd"`
// CniConfig contains config related to cni
CniConfig `toml:"cni"`
// SocketPath is the path to the socket which cri-containerd serves on.
SocketPath string `toml:"socketpath"`
SocketPath string `toml:"socket_path"`
// RootDir is the root directory path for managing cri-containerd files
// (metadata checkpoint etc.)
RootDir string `toml: "rootdir"`
// ContainerdSnapshotter is the snapshotter used by containerd.
ContainerdSnapshotter string `toml: "containerdsnapshotter"`
// ContainerdEndpoint is the containerd endpoint path.
ContainerdEndpoint string `toml:"containerdendpoint"`
// ContainerdConnectionTimeout is the connection timeout for containerd client.
ContainerdConnectionTimeout time.Duration `toml: "containerdconnectiontimeout"`
// NetworkPluginBinDir is the directory in which the binaries for the plugin is kept.
NetworkPluginBinDir string `toml:"networkpluginbindir"`
// NetworkPluginConfDir is the directory in which the admin places a CNI conf.
NetworkPluginConfDir string `toml:"networkpluginconfdir"`
RootDir string `toml:"root_dir"`
// StreamServerAddress is the ip address streaming server is listening on.
StreamServerAddress string `toml:"streamserveraddress"`
StreamServerAddress string `toml:"stream_server_address"`
// StreamServerPort is the port streaming server is listening on.
StreamServerPort string `toml: "streamserverport"`
StreamServerPort string `toml:"stream_server_port"`
// CgroupPath is the path for the cgroup that cri-containerd is placed in.
CgroupPath string `toml: "cgrouppath"`
CgroupPath string `toml:"cgroup_path"`
// EnableSelinux indicates to enable the selinux support
EnableSelinux bool `toml: "enableselinux"`
EnableSelinux bool `toml:"enable_selinux"`
// SandboxImage is the image used by sandbox container.
SandboxImage string `toml:"sandbox_image"`
}
// CRIContainerdOptions contains cri-containerd command line and toml options.
type CRIContainerdOptions struct {
//Config contains cri-containerd toml config
// Config contains cri-containerd toml config
Config
//Path to the TOML config file
// Path to the TOML config file
ConfigFilePath string
// PrintVersion indicates to print version information of cri-containerd.
PrintVersion bool
}
@@ -77,7 +85,7 @@ func NewCRIContainerdOptions() *CRIContainerdOptions {
// AddFlags adds cri-containerd command line options to pflag.
func (c *CRIContainerdOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.ConfigFilePath, configFilePathArgName,
"/etc/cri-containerd/config.toml", "Path to the config file")
"/etc/cri-containerd/config.toml", "Path to the config file.")
fs.StringVar(&c.SocketPath, "socket-path",
"/var/run/cri-containerd.sock", "Path to the socket which cri-containerd serves on.")
fs.StringVar(&c.RootDir, "root-dir",
@@ -99,6 +107,8 @@ func (c *CRIContainerdOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.CgroupPath, "cgroup-path", "", "The cgroup that cri-containerd is part of. By default cri-containerd is not placed in a cgroup")
fs.BoolVar(&c.EnableSelinux, "selinux-enabled",
false, "Enable selinux support.")
fs.StringVar(&c.SandboxImage, "sandbox-image",
"gcr.io/google_containers/pause:3.0", "The image used by sandbox container.")
}
// InitFlags must be called after adding all cli options flags are defined and
@@ -110,30 +120,28 @@ func (c *CRIContainerdOptions) InitFlags(fs *pflag.FlagSet) error {
fs.AddGoFlagSet(flag.CommandLine)
commandline := os.Args[1:]
err := fs.Parse(commandline) //this time: config = default + commandline(on top)
err := fs.Parse(commandline)
if err != nil {
return err
}
// will try default config file when user have not seted it in cli
err = loadConfigFile(c.ConfigFilePath, &c.Config) //config = default + commandline + configfile(on top)
// Load default config file if none provided
_, err = toml.DecodeFile(c.ConfigFilePath, &c.Config)
if err != nil {
//the absence of default config file is normal case.
// the absence of default config file is normal case.
if !fs.Changed(configFilePathArgName) && os.IsNotExist(err) {
return nil
}
return err
}
err = fs.Parse(commandline) //config = default + commandline + configfile + commandline(on top)
return err
}
func loadConfigFile(fpath string, v *Config) error {
if v == nil {
v = &Config{}
}
_, err := toml.DecodeFile(fpath, v)
return err
// What is the reason for applying the command line twice?
// Because the values from command line has the highest priority.
// So I must get the path of toml configuration file from command line,
// it trigger the first parse.
// The first parse generate the the default value and the value from command line at the same time.
// But the priority of toml config value is more higher than of default value,
// So I have not another way to insert toml config value between default value and command line value.
// So I trigger twice parses, one for default value, one for commandline value.
return fs.Parse(commandline)
}