From 9fd4c99bd1030756785dc2acf7d80bafe18f164c Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Thu, 12 Apr 2018 14:54:03 +0800 Subject: [PATCH] feature: add a flag of pouchd to specify whether enable CRI Signed-off-by: YaoZengzeng --- daemon/config/config.go | 3 ++ daemon/daemon.go | 86 ++++++++++++++++++++++++------------- hack/cri-test/test-utils.sh | 2 +- main.go | 1 + 4 files changed, 61 insertions(+), 31 deletions(-) diff --git a/daemon/config/config.go b/daemon/config/config.go index 936da48b5..6be89cab1 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -22,6 +22,9 @@ type Config struct { // Network config NetworkConfg network.Config + // Whether enable cri manager. + IsCriEnabled bool `json:"enable-cri,omitempty"` + // CRI config. CriConfig cri.Config diff --git a/daemon/daemon.go b/daemon/daemon.go index a758d1023..16f7e6492 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -167,17 +167,6 @@ func (d *Daemon) Run() error { } d.containerMgr = containerMgr - criMgr, err := internal.GenCriMgr(d) - if err != nil { - return err - } - d.criMgr = criMgr - - d.criService, err = cri.NewService(d.config, criMgr) - if err != nil { - return err - } - d.server = server.Server{ Config: d.config, ContainerMgr: containerMgr, @@ -205,29 +194,17 @@ func (d *Daemon) Run() error { close(httpServerCloseCh) }() - grpcServerCloseCh := make(chan struct{}) - go func() { - if err := d.criService.Serve(); err != nil { - logrus.Errorf("failed to start grpc server: %v", err) - } - close(grpcServerCloseCh) - }() + criStopCh := make(chan error) + go d.RunCriService(criStopCh) - streamServerCloseCh := make(chan struct{}) - go func() { - if d.criMgr.StreamServerStart(); err != nil { - logrus.Errorf("failed to start stream server: %v", err) - } - close(streamServerCloseCh) - }() + err = <-criStopCh + if err != nil { + return err + } - // Stop pouchd if both server stopped. + // Stop pouchd if the server stopped <-httpServerCloseCh logrus.Infof("HTTP server stopped") - <-grpcServerCloseCh - logrus.Infof("GRPC server stopped") - <-streamServerCloseCh - logrus.Infof("Stream server stopped") return nil } @@ -291,3 +268,52 @@ func (d *Daemon) ShutdownPlugin() error { } return nil } + +// RunCriService start cri service if pouchd is specified with --enable-cri. +func (d *Daemon) RunCriService(stopCh chan error) { + var err error + + defer func() { + stopCh <- err + close(stopCh) + }() + + if !d.config.IsCriEnabled { + return + } + + criMgr, err := internal.GenCriMgr(d) + if err != nil { + return + } + d.criMgr = criMgr + + d.criService, err = cri.NewService(d.config, criMgr) + if err != nil { + return + } + + grpcServerCloseCh := make(chan struct{}) + go func() { + if err := d.criService.Serve(); err != nil { + logrus.Errorf("failed to start grpc server: %v", err) + } + close(grpcServerCloseCh) + }() + + streamServerCloseCh := make(chan struct{}) + go func() { + if err := d.criMgr.StreamServerStart(); err != nil { + logrus.Errorf("failed to start stream server: %v", err) + } + close(streamServerCloseCh) + }() + + <-streamServerCloseCh + logrus.Infof("CRI Stream server stopped") + <-grpcServerCloseCh + logrus.Infof("CRI GRPC server stopped") + + logrus.Infof("CRI service stopped") + return +} diff --git a/hack/cri-test/test-utils.sh b/hack/cri-test/test-utils.sh index 8c9c86cbc..9e1d4abe8 100755 --- a/hack/cri-test/test-utils.sh +++ b/hack/cri-test/test-utils.sh @@ -49,7 +49,7 @@ test_setup() { pouch_pid_command=`pgrep pouchd` pouch_pid=${pouch_pid_command} if [ ! -n "${pouch_pid}" ]; then - keepalive "pouchd ${POUCH_FLAGS}" \ + keepalive "pouchd --enable-cri ${POUCH_FLAGS}" \ ${RESTART_WAIT_PERIOD} &> ${report_dir}/pouch.log & pouch_pid=$! fi diff --git a/main.go b/main.go index a015c363b..86294110d 100644 --- a/main.go +++ b/main.go @@ -73,6 +73,7 @@ func setupFlags(cmd *cobra.Command) { flagSet.StringVar(&cfg.HomeDir, "home-dir", "/var/lib/pouch", "Specify root dir of pouchd") flagSet.StringArrayVarP(&cfg.Listen, "listen", "l", []string{"unix:///var/run/pouchd.sock"}, "Specify listening addresses of Pouchd") + flagSet.BoolVar(&cfg.IsCriEnabled, "enable-cri", false, "Specify whether enable the cri part of pouchd which is used to support Kubernetes") flagSet.StringVar(&cfg.CriConfig.Listen, "listen-cri", "/var/run/pouchcri.sock", "Specify listening address of CRI") flagSet.StringVar(&cfg.CriConfig.NetworkPluginBinDir, "cni-bin-dir", "/opt/cni/bin", "The directory for putting cni plugin binaries.") flagSet.StringVar(&cfg.CriConfig.NetworkPluginConfDir, "cni-conf-dir", "/etc/cni/net.d", "The directory for putting cni plugin configuration files.")