Add Closer in plugin.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2018-01-23 02:33:33 +00:00
parent dc5964ccc2
commit d8f87a5a65

View File

@ -2,6 +2,7 @@ package server
import ( import (
"expvar" "expvar"
"io"
"net" "net"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
@ -102,6 +103,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
if service, ok := instance.(plugin.Service); ok { if service, ok := instance.(plugin.Service); ok {
services = append(services, service) services = append(services, service)
} }
s.plugins = append(s.plugins, result)
} }
// register services after all plugins have been initialized // register services after all plugins have been initialized
for _, service := range services { for _, service := range services {
@ -114,9 +116,10 @@ func New(ctx context.Context, config *Config) (*Server, error) {
// Server is the containerd main daemon // Server is the containerd main daemon
type Server struct { type Server struct {
rpc *grpc.Server rpc *grpc.Server
events *exchange.Exchange events *exchange.Exchange
config *Config config *Config
plugins []*plugin.Plugin
} }
// ServeGRPC provides the containerd grpc APIs on the provided listener // ServeGRPC provides the containerd grpc APIs on the provided listener
@ -156,6 +159,23 @@ func (s *Server) ServeDebug(l net.Listener) error {
// Stop the containerd server canceling any open connections // Stop the containerd server canceling any open connections
func (s *Server) Stop() { func (s *Server) Stop() {
s.rpc.Stop() s.rpc.Stop()
for i := len(s.plugins) - 1; i >= 0; i-- {
p := s.plugins[i]
instance, err := p.Instance()
if err != nil {
log.L.WithError(err).WithField("id", p.Registration.ID).
Errorf("could not get plugin instance")
continue
}
closer, ok := instance.(io.Closer)
if !ok {
continue
}
if err := closer.Close(); err != nil {
log.L.WithError(err).WithField("id", p.Registration.ID).
Errorf("failed to close plugin")
}
}
} }
// LoadPlugins loads all plugins into containerd and generates an ordered graph // LoadPlugins loads all plugins into containerd and generates an ordered graph