Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Shutdown Method to sdk.Handler #124

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion authorization/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type Handler struct {

// NewHandler initializes the request handler with a plugin implementation.
func NewHandler(plugin Plugin) *Handler {
h := &Handler{plugin, sdk.NewHandler(manifest)}
h := &Handler{plugin, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down
2 changes: 1 addition & 1 deletion graphdriver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ type Handler struct {

// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, sdk.NewHandler(manifest)}
h := &Handler{driver, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down
2 changes: 1 addition & 1 deletion ipam/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Handler struct {

// NewHandler initializes the request handler with a driver implementation.
func NewHandler(ipam Ipam) *Handler {
h := &Handler{ipam, sdk.NewHandler(manifest)}
h := &Handler{ipam, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down
2 changes: 1 addition & 1 deletion network/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ type Handler struct {

// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, sdk.NewHandler(manifest)}
h := &Handler{driver, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down
32 changes: 21 additions & 11 deletions sdk/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdk

import (
"context"
"crypto/tls"
"fmt"
"net"
Expand All @@ -13,37 +14,46 @@ const activatePath = "/Plugin.Activate"
// Handler is the base to create plugin handlers.
// It initializes connections and sockets to listen to.
type Handler struct {
mux *http.ServeMux
mux *http.ServeMux
server *http.Server
}

// NewHandler creates a new Handler with an http mux.
func NewHandler(manifest string) Handler {
func NewHandler(manifest string) *Handler {
mux := http.NewServeMux()

mux.HandleFunc(activatePath, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", DefaultContentTypeV1_1)
fmt.Fprintln(w, manifest)
})

return Handler{mux: mux}
return &Handler{mux: mux}
}

// Serve sets up the handler to serve requests on the passed in listener
func (h Handler) Serve(l net.Listener) error {
func (h *Handler) Serve(l net.Listener) error {
server := http.Server{
Addr: l.Addr().String(),
Handler: h.mux,
}

h.server = &server
return server.Serve(l)
}

// Shutdown gracefully shuts down the http.Server serving requests after Serve or
// Serve{TCP,Unix,Windows} was called.
func (h *Handler) Shutdown(c context.Context) error {
return h.server.Shutdown(c)
}

// ServeTCP makes the handler to listen for request in a given TCP address.
// It also writes the spec file in the right directory for docker to read.
// Due to constrains for running Docker in Docker on Windows, data-root directory
// of docker daemon must be provided. To get default directory, use
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
func (h Handler) ServeTCP(pluginName, addr, daemonDir string, tlsConfig *tls.Config) error {
l, spec, err := newTCPListener(addr, pluginName, daemonDir, tlsConfig)
func (h *Handler) ServeTCP(pluginName, addr, daemonDir string, tlsConfig *tls.Config) error {
l, spec, err := NewTCPListener(addr, pluginName, daemonDir, tlsConfig)
if err != nil {
return err
}
Expand All @@ -55,8 +65,8 @@ func (h Handler) ServeTCP(pluginName, addr, daemonDir string, tlsConfig *tls.Con

// ServeUnix makes the handler to listen for requests in a unix socket.
// It also creates the socket file in the right directory for docker to read.
func (h Handler) ServeUnix(addr string, gid int) error {
l, spec, err := newUnixListener(addr, gid)
func (h *Handler) ServeUnix(addr string, gid int) error {
l, spec, err := NewUnixListener(addr, gid)
if err != nil {
return err
}
Expand All @@ -71,8 +81,8 @@ func (h Handler) ServeUnix(addr string, gid int) error {
// Due to constrains for running Docker in Docker on Windows, data-root directory
// of docker daemon must be provided. To get default directory, use
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
func (h Handler) ServeWindows(addr, pluginName, daemonDir string, pipeConfig *WindowsPipeConfig) error {
l, spec, err := newWindowsListener(addr, pluginName, daemonDir, pipeConfig)
func (h *Handler) ServeWindows(addr, pluginName, daemonDir string, pipeConfig *WindowsPipeConfig) error {
l, spec, err := NewWindowsListener(addr, pluginName, daemonDir, pipeConfig)
if err != nil {
return err
}
Expand All @@ -83,6 +93,6 @@ func (h Handler) ServeWindows(addr, pluginName, daemonDir string, pipeConfig *Wi
}

// HandleFunc registers a function to handle a request path with.
func (h Handler) HandleFunc(path string, fn func(w http.ResponseWriter, r *http.Request)) {
func (h *Handler) HandleFunc(path string, fn func(w http.ResponseWriter, r *http.Request)) {
h.mux.HandleFunc(path, fn)
}
8 changes: 7 additions & 1 deletion sdk/tcp_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (
"github.com/docker/go-connections/sockets"
)

func newTCPListener(address, pluginName, daemonDir string, tlsConfig *tls.Config) (net.Listener, string, error) {
// NewTCPListener constructs a net.Listener to use for serving requests at the given TCP address.
// It also writes the spec file in the right directory for docker to read.
//
// Due to constrains for running Docker in Docker on Windows, data-root directory
// of docker daemon must be provided. To get default directory, use
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
func NewTCPListener(address, pluginName, daemonDir string, tlsConfig *tls.Config) (net.Listener, string, error) {
listener, err := sockets.NewTCPSocket(address, tlsConfig)
if err != nil {
return nil, "", err
Expand Down
4 changes: 3 additions & 1 deletion sdk/unix_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (

const pluginSockDir = "/run/docker/plugins"

func newUnixListener(pluginName string, gid int) (net.Listener, string, error) {
// NewUnixListener constructs a net.Listener to use for serving requests at the given unix socket.
// It also creates the socket file in the right directory for docker to read.
func NewUnixListener(pluginName string, gid int) (net.Listener, string, error) {
path, err := fullSocketAddress(pluginName)
if err != nil {
return nil, "", err
Expand Down
8 changes: 7 additions & 1 deletion sdk/windows_listener_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ var (
errOnlySupportedOnWindows = errors.New("named pipe creation is only supported on Windows")
)

func newWindowsListener(address, pluginName, daemonRoot string, pipeConfig *WindowsPipeConfig) (net.Listener, string, error) {
// NewWindowsListener constructs a net.Listener to use for serving requests at the given Windows named pipe.
// It also creates the spec file in the right directory for docker to read.
//
// Due to constrains for running Docker in Docker on Windows, the data-root directory
// of docker daemon must be provided. To get default directory, use
// WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
func NewWindowsListener(address, pluginName, daemonRoot string, pipeConfig *WindowsPipeConfig) (net.Listener, string, error) {
return nil, "", errOnlySupportedOnWindows
}

Expand Down
2 changes: 1 addition & 1 deletion secrets/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Handler struct {

// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, sdk.NewHandler(manifest)}
h := &Handler{driver, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down
2 changes: 1 addition & 1 deletion volume/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type Handler struct {

// NewHandler initializes the request handler with a driver implementation.
func NewHandler(driver Driver) *Handler {
h := &Handler{driver, sdk.NewHandler(manifest)}
h := &Handler{driver, *sdk.NewHandler(manifest)}
h.initMux()
return h
}
Expand Down