From 86f011359527e40ae559b494b0255c55c429cea5 Mon Sep 17 00:00:00 2001 From: Michael Wan Date: Thu, 16 Aug 2018 02:55:05 -0400 Subject: [PATCH] refactor: support specify Namespace by user Signed-off-by: Michael Wan --- daemon/config/config.go | 5 ++--- daemon/daemon.go | 8 +------- daemon/mgr/container.go | 2 +- main.go | 8 +++++--- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/daemon/config/config.go b/daemon/config/config.go index 6f02e1de7..ab8510bf7 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -112,9 +112,8 @@ type Config struct { // runtimes config Runtimes map[string]types.Runtime `json:"add-runtime,omitempty"` - // Namespace is passed to containerd, Namespace is not a daemon flag, - // do not marshal this field to config file. - Namespace string `json:"-"` + // DefaultNamespace is passed to containerd. + DefaultNamespace string `json:"default-namespace,omitempty"` } // Validate validates the user input config. diff --git a/daemon/daemon.go b/daemon/daemon.go index d98dc9ef1..2671361b5 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -10,7 +10,6 @@ import ( "github.com/alibaba/pouch/apis/plugins" "github.com/alibaba/pouch/apis/server" criservice "github.com/alibaba/pouch/cri" - criconfig "github.com/alibaba/pouch/cri/config" "github.com/alibaba/pouch/ctrd" "github.com/alibaba/pouch/daemon/config" "github.com/alibaba/pouch/daemon/events" @@ -72,18 +71,13 @@ func NewDaemon(cfg *config.Config) *Daemon { containerdBinaryFile = cfg.ContainerdPath } - // the default unix socket path to the containerd socket - if cfg.IsCriEnabled { - cfg.Namespace = criconfig.K8sNamespace - } - containerd, err := ctrd.NewClient(cfg.HomeDir, ctrd.WithDebugLog(cfg.Debug), ctrd.WithStartDaemon(true), ctrd.WithContainerdBinary(containerdBinaryFile), ctrd.WithRPCAddr(cfg.ContainerdAddr), ctrd.WithOOMScoreAdjust(cfg.OOMScoreAdjust), - ctrd.WithDefaultNamespace(cfg.Namespace), + ctrd.WithDefaultNamespace(cfg.DefaultNamespace), ) if err != nil { logrus.Errorf("failed to new containerd's client: %v", err) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index a01a4205e..4000bbc9f 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -1981,7 +1981,7 @@ func (mgr *ContainerManager) setBaseFS(ctx context.Context, c *Container, id str // io.containerd.runtime.v1.linux as a const used by runc c.Lock() - c.BaseFS = filepath.Join(mgr.Config.HomeDir, "containerd/state", "io.containerd.runtime.v1.linux", mgr.Config.Namespace, info.Name, "rootfs") + c.BaseFS = filepath.Join(mgr.Config.HomeDir, "containerd/state", "io.containerd.runtime.v1.linux", mgr.Config.DefaultNamespace, info.Name, "rootfs") c.Unlock() } diff --git a/main.go b/main.go index 28eadd0dd..1e4c0b6ac 100644 --- a/main.go +++ b/main.go @@ -121,6 +121,11 @@ func setupFlags(cmd *cobra.Command) { flagSet.StringVar(&cfg.Pidfile, "pidfile", "/var/run/pouch.pid", "Save daemon pid") flagSet.IntVar(&cfg.OOMScoreAdjust, "oom-score-adj", -500, "Set the oom_score_adj for the daemon") flagSet.Var(optscfg.NewRuntime(&cfg.Runtimes), "add-runtime", "register a OCI runtime to daemon") + + // Notes(ziren): default-namespace is passed to containerd, the default + // value is 'default'. So if IsCriEnabled is true for k8s, we should set the DefaultNamespace + // to k8s.io + flagSet.StringVar(&cfg.DefaultNamespace, "default-namespace", namespaces.Default, "default-namespace is passed to containerd, the default value is 'default'") } // runDaemon prepares configs, setups essential details and runs pouchd daemon. @@ -129,9 +134,6 @@ func runDaemon(cmd *cobra.Command) error { return fmt.Errorf("failed to load daemon file: %s", err) } - // set containerd namespace, we use containerd default namespace as pouch namespaces - cfg.Namespace = namespaces.Default - // parse log driver config logOptMap, err := opts.ParseLogOptions(cfg.DefaultLogConfig.LogDriver, logOpts) if err != nil {