Skip to content

Commit

Permalink
Merge pull request #1577 from shaloulcy/notify_systemd
Browse files Browse the repository at this point in the history
feature: add systemd notify functionality
  • Loading branch information
Wei Fu authored Jul 5, 2018
2 parents c456006 + 270bbc5 commit 26e93e1
Show file tree
Hide file tree
Showing 17 changed files with 1,063 additions and 8 deletions.
7 changes: 6 additions & 1 deletion apis/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Server struct {
}

// Start setup route table and listen to specified address which currently only supports unix socket and tcp address.
func (s *Server) Start() (err error) {
func (s *Server) Start(readyCh chan bool) (err error) {
router := initRoute(s)
errCh := make(chan error)

Expand All @@ -50,6 +50,7 @@ func (s *Server) Start() (err error) {
if s.Config.TLS.Key != "" && s.Config.TLS.Cert != "" {
tlsConfig, err = httputils.GenTLSConfig(s.Config.TLS.Key, s.Config.TLS.Cert, s.Config.TLS.CA)
if err != nil {
readyCh <- false
return err
}
if s.Config.TLS.VerifyRemote {
Expand All @@ -61,6 +62,7 @@ func (s *Server) Start() (err error) {
for _, one := range s.Config.Listen {
l, err := getListener(one, tlsConfig)
if err != nil {
readyCh <- false
return err
}
logrus.Infof("start to listen to: %s", one)
Expand All @@ -71,6 +73,9 @@ func (s *Server) Start() (err error) {
}(l)
}

// the http server has set up, send Ready
readyCh <- true

// not error, will block and run forever.
return <-errCh
}
Expand Down
23 changes: 18 additions & 5 deletions cri/criservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,42 @@ import (
)

// RunCriService start cri service if pouchd is specified with --enable-cri.
func RunCriService(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, stopCh chan error) {
func RunCriService(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, stopCh chan error, readyCh chan bool) {
var err error

defer func() {
stopCh <- err
close(stopCh)
}()
if !daemonconfig.IsCriEnabled {
// the CriService has been disabled, so send Ready
readyCh <- true
return
}
switch daemonconfig.CriConfig.CriVersion {
case "v1alpha1":
err = runv1alpha1(daemonconfig, containerMgr, imageMgr)
err = runv1alpha1(daemonconfig, containerMgr, imageMgr, readyCh)
case "v1alpha2":
err = runv1alpha2(daemonconfig, containerMgr, imageMgr)
err = runv1alpha2(daemonconfig, containerMgr, imageMgr, readyCh)
default:
readyCh <- false
err = fmt.Errorf("invalid CRI version,failed to start CRI service")
}
return
}

// Start CRI service with CRI version: v1alpha1
func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr) error {
func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, readyCh chan bool) error {
logrus.Infof("Start CRI service with CRI version: v1alpha1")
criMgr, err := criv1alpha1.NewCriManager(daemonconfig, containerMgr, imageMgr)
if err != nil {
readyCh <- false
return fmt.Errorf("failed to get CriManager with error: %v", err)
}

service, err := servicev1alpha1.NewService(daemonconfig, criMgr)
if err != nil {
readyCh <- false
return fmt.Errorf("failed to start CRI service with error: %v", err)
}

Expand All @@ -59,6 +64,9 @@ func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, ima
logrus.Infof("CRI Stream server stopped")
}()

// the criservice has set up, send Ready
readyCh <- true

// Check for error
for i := 0; i < cap(errChan); i++ {
if err := <-errChan; err != nil {
Expand All @@ -71,15 +79,17 @@ func runv1alpha1(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, ima
}

// Start CRI service with CRI version: v1alpha2
func runv1alpha2(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr) error {
func runv1alpha2(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, imageMgr mgr.ImageMgr, readyCh chan bool) error {
logrus.Infof("Start CRI service with CRI version: v1alpha2")
criMgr, err := criv1alpha2.NewCriManager(daemonconfig, containerMgr, imageMgr)
if err != nil {
readyCh <- false
return fmt.Errorf("failed to get CriManager with error: %v", err)
}

service, err := servicev1alpha2.NewService(daemonconfig, criMgr)
if err != nil {
readyCh <- false
return fmt.Errorf("failed to start CRI service with error: %v", err)
}

Expand All @@ -94,6 +104,9 @@ func runv1alpha2(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, ima
logrus.Infof("CRI Stream server stopped")
}()

// the criservice has set up, send Ready
readyCh <- true

// Check for error
for i := 0; i < cap(errChan); i++ {
if err := <-errChan; err != nil {
Expand Down
35 changes: 33 additions & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/alibaba/pouch/pkg/meta"
"github.com/alibaba/pouch/pkg/system"

systemddaemon "github.com/coreos/go-systemd/daemon"
systemdutil "github.com/coreos/go-systemd/util"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -203,16 +205,30 @@ func (d *Daemon) Run() error {
// set image proxy
ctrd.SetImageProxy(d.config.ImageProxy)

httpReadyCh := make(chan bool)
criReadyCh := make(chan bool)

httpServerCloseCh := make(chan struct{})
go func() {
if err := d.server.Start(); err != nil {
if err := d.server.Start(httpReadyCh); err != nil {
logrus.Errorf("failed to start http server: %v", err)
}
close(httpServerCloseCh)
}()

criStopCh := make(chan error)
go criservice.RunCriService(d.config, d.containerMgr, d.imageMgr, criStopCh)
go criservice.RunCriService(d.config, d.containerMgr, d.imageMgr, criStopCh, criReadyCh)

httpReady := <-httpReadyCh
criReady := <-criReadyCh

if httpReady && criReady {
notifySystemd()
}

// close the ready channel
close(httpReadyCh)
close(criReadyCh)

err = <-criStopCh
if err != nil {
Expand Down Expand Up @@ -322,3 +338,18 @@ func (d *Daemon) addSystemLabels() error {

return nil
}

func notifySystemd() {
if !systemdutil.IsRunningSystemd() {
return
}

sent, err := systemddaemon.SdNotify(false, "READY=1")
if err != nil {
logrus.Errorf("failed to notify systemd for readiness: %v", err)
}

if !sent {
logrus.Errorf("forgot to set Type=notify in systemd service file?")
}
}
1 change: 1 addition & 0 deletions hack/package/deb/systemd/pouch.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Description=pouch
[Service]
ExecStart=/usr/bin/pouchd
ExecReload=/bin/kill -HUP $MAINPID
Type=notify

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
Expand Down
1 change: 1 addition & 0 deletions hack/package/rpm/service/pouch.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Description=pouch
[Service]
ExecStart=/usr/local/bin/pouchd
ExecReload=/bin/kill -HUP $MAINPID
Type=notify

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
Expand Down
191 changes: 191 additions & 0 deletions vendor/github.com/coreos/go-systemd/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/coreos/go-systemd/NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 26e93e1

Please sign in to comment.