2018-02-05 21:05:59 +00:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2015-05-14 17:05:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-11-08 16:46:46 +00:00
|
|
|
"context"
|
2015-05-14 17:05:39 +00:00
|
|
|
"encoding/json"
|
2015-06-05 20:09:53 +00:00
|
|
|
"io"
|
2015-05-14 17:05:39 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-03-02 01:16:40 +00:00
|
|
|
"net/url"
|
2015-05-14 17:05:39 +00:00
|
|
|
"time"
|
|
|
|
|
2017-11-08 16:46:46 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-03-02 01:16:40 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins/transport"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-05-14 17:05:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-09-16 10:34:34 +00:00
|
|
|
defaultTimeOut = 30
|
2015-05-14 17:05:39 +00:00
|
|
|
)
|
|
|
|
|
2016-11-21 17:24:01 +00:00
|
|
|
func newTransport(addr string, tlsConfig *tlsconfig.Options) (transport.Transport, error) {
|
2015-05-14 17:05:39 +00:00
|
|
|
tr := &http.Transport{}
|
2015-05-27 22:21:18 +00:00
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
c, err := tlsconfig.Client(*tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tr.TLSClientConfig = c
|
2015-05-27 22:21:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-02 01:16:40 +00:00
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
socket := u.Host
|
|
|
|
if socket == "" {
|
|
|
|
// valid local socket addresses have the host empty.
|
|
|
|
socket = u.Path
|
|
|
|
}
|
|
|
|
if err := sockets.ConfigureTransport(tr, u.Scheme, socket); err != nil {
|
2016-02-16 18:05:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-02 01:16:40 +00:00
|
|
|
scheme := httpScheme(u)
|
2015-09-22 19:54:29 +00:00
|
|
|
|
2016-11-21 17:24:01 +00:00
|
|
|
return transport.NewHTTPTransport(tr, scheme, socket), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new plugin client (http).
|
|
|
|
func NewClient(addr string, tlsConfig *tlsconfig.Options) (*Client, error) {
|
|
|
|
clientTransport, err := newTransport(addr, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newClientWithTransport(clientTransport, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClientWithTimeout creates a new plugin client (http).
|
2017-05-22 09:38:23 +00:00
|
|
|
func NewClientWithTimeout(addr string, tlsConfig *tlsconfig.Options, timeout time.Duration) (*Client, error) {
|
2016-11-21 17:24:01 +00:00
|
|
|
clientTransport, err := newTransport(addr, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-22 09:38:23 +00:00
|
|
|
return newClientWithTransport(clientTransport, timeout), nil
|
2016-03-02 01:16:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 17:24:01 +00:00
|
|
|
// newClientWithTransport creates a new plugin client with a given transport.
|
2017-05-22 09:38:23 +00:00
|
|
|
func newClientWithTransport(tr transport.Transport, timeout time.Duration) *Client {
|
2016-03-02 01:16:40 +00:00
|
|
|
return &Client{
|
|
|
|
http: &http.Client{
|
|
|
|
Transport: tr,
|
2017-05-22 09:38:23 +00:00
|
|
|
Timeout: timeout,
|
2016-03-02 01:16:40 +00:00
|
|
|
},
|
|
|
|
requestFactory: tr,
|
2015-09-22 19:54:29 +00:00
|
|
|
}
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Client represents a plugin client.
|
2015-05-14 17:05:39 +00:00
|
|
|
type Client struct {
|
2016-03-02 01:16:40 +00:00
|
|
|
http *http.Client // http client to use
|
|
|
|
requestFactory transport.RequestFactory
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 16:46:46 +00:00
|
|
|
// RequestOpts is the set of options that can be passed into a request
|
|
|
|
type RequestOpts struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRequestTimeout sets a timeout duration for plugin requests
|
|
|
|
func WithRequestTimeout(t time.Duration) func(*RequestOpts) {
|
|
|
|
return func(o *RequestOpts) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Call calls the specified method with the specified arguments for the plugin.
|
|
|
|
// It will retry for 30 seconds if a failure occurs when calling.
|
2017-11-08 16:46:46 +00:00
|
|
|
func (c *Client) Call(serviceMethod string, args, ret interface{}) error {
|
|
|
|
return c.CallWithOptions(serviceMethod, args, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallWithOptions is just like call except it takes options
|
|
|
|
func (c *Client) CallWithOptions(serviceMethod string, args interface{}, ret interface{}, opts ...func(*RequestOpts)) error {
|
2015-06-05 20:09:53 +00:00
|
|
|
var buf bytes.Buffer
|
2015-12-05 07:55:50 +00:00
|
|
|
if args != nil {
|
|
|
|
if err := json.NewEncoder(&buf).Encode(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-05 20:09:53 +00:00
|
|
|
}
|
2017-11-08 16:46:46 +00:00
|
|
|
body, err := c.callWithRetry(serviceMethod, &buf, true, opts...)
|
2015-06-05 20:09:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer body.Close()
|
2015-12-05 07:55:50 +00:00
|
|
|
if ret != nil {
|
|
|
|
if err := json.NewDecoder(body).Decode(&ret); err != nil {
|
|
|
|
logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
|
|
|
|
return err
|
|
|
|
}
|
2015-10-08 15:51:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
2015-05-19 20:05:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:09:53 +00:00
|
|
|
// Stream calls the specified method with the specified arguments for the plugin and returns the response body
|
|
|
|
func (c *Client) Stream(serviceMethod string, args interface{}) (io.ReadCloser, error) {
|
2015-05-14 17:05:39 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(args); err != nil {
|
2015-06-05 20:09:53 +00:00
|
|
|
return nil, err
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2015-06-05 20:09:53 +00:00
|
|
|
return c.callWithRetry(serviceMethod, &buf, true)
|
|
|
|
}
|
2015-05-14 17:05:39 +00:00
|
|
|
|
2015-06-05 20:09:53 +00:00
|
|
|
// SendFile calls the specified method, and passes through the IO stream
|
|
|
|
func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{}) error {
|
|
|
|
body, err := c.callWithRetry(serviceMethod, data, true)
|
2015-05-14 17:05:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-16 15:38:13 +00:00
|
|
|
defer body.Close()
|
2015-10-08 15:51:41 +00:00
|
|
|
if err := json.NewDecoder(body).Decode(&ret); err != nil {
|
|
|
|
logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-05 20:09:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 16:46:46 +00:00
|
|
|
func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool, reqOpts ...func(*RequestOpts)) (io.ReadCloser, error) {
|
2015-05-14 17:05:39 +00:00
|
|
|
var retries int
|
|
|
|
start := time.Now()
|
|
|
|
|
2017-11-08 16:46:46 +00:00
|
|
|
var opts RequestOpts
|
|
|
|
for _, o := range reqOpts {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
for {
|
2017-05-27 01:02:31 +00:00
|
|
|
req, err := c.requestFactory.NewRequest(serviceMethod, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-08 16:46:46 +00:00
|
|
|
cancelRequest := func() {}
|
|
|
|
if opts.Timeout > 0 {
|
|
|
|
var ctx context.Context
|
|
|
|
ctx, cancelRequest = context.WithTimeout(req.Context(), opts.Timeout)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
resp, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
2017-11-08 16:46:46 +00:00
|
|
|
cancelRequest()
|
2015-05-19 20:05:25 +00:00
|
|
|
if !retry {
|
2015-06-05 20:09:53 +00:00
|
|
|
return nil, err
|
2015-05-19 20:05:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
timeOff := backoff(retries)
|
2015-05-19 20:05:25 +00:00
|
|
|
if abort(start, timeOff) {
|
2015-06-05 20:09:53 +00:00
|
|
|
return nil, err
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
|
|
|
retries++
|
2016-06-12 15:23:19 +00:00
|
|
|
logrus.Warnf("Unable to connect to plugin: %s%s: %v, retrying in %v", req.URL.Host, req.URL.Path, err, timeOff)
|
2015-05-14 17:05:39 +00:00
|
|
|
time.Sleep(timeOff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-12-15 16:51:48 +00:00
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
2016-02-25 01:09:51 +00:00
|
|
|
resp.Body.Close()
|
2017-11-08 16:46:46 +00:00
|
|
|
cancelRequest()
|
2015-05-14 17:05:39 +00:00
|
|
|
if err != nil {
|
2016-01-21 03:31:46 +00:00
|
|
|
return nil, &statusError{resp.StatusCode, serviceMethod, err.Error()}
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2015-12-15 16:51:48 +00:00
|
|
|
|
|
|
|
// Plugins' Response(s) should have an Err field indicating what went
|
|
|
|
// wrong. Try to unmarshal into ResponseErr. Otherwise fallback to just
|
|
|
|
// return the string(body)
|
|
|
|
type responseErr struct {
|
|
|
|
Err string
|
|
|
|
}
|
|
|
|
remoteErr := responseErr{}
|
2016-01-06 16:29:40 +00:00
|
|
|
if err := json.Unmarshal(b, &remoteErr); err == nil {
|
|
|
|
if remoteErr.Err != "" {
|
2016-01-21 03:31:46 +00:00
|
|
|
return nil, &statusError{resp.StatusCode, serviceMethod, remoteErr.Err}
|
2016-01-06 16:29:40 +00:00
|
|
|
}
|
2015-12-15 16:51:48 +00:00
|
|
|
}
|
|
|
|
// old way...
|
2016-01-21 03:31:46 +00:00
|
|
|
return nil, &statusError{resp.StatusCode, serviceMethod, string(b)}
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2017-11-08 16:46:46 +00:00
|
|
|
return ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
err := resp.Body.Close()
|
|
|
|
cancelRequest()
|
|
|
|
return err
|
|
|
|
}), nil
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func backoff(retries int) time.Duration {
|
2015-05-19 20:05:25 +00:00
|
|
|
b, max := 1, defaultTimeOut
|
2015-05-14 17:05:39 +00:00
|
|
|
for b < max && retries > 0 {
|
|
|
|
b *= 2
|
|
|
|
retries--
|
|
|
|
}
|
|
|
|
if b > max {
|
|
|
|
b = max
|
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
return time.Duration(b) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func abort(start time.Time, timeOff time.Duration) bool {
|
2015-07-09 23:31:43 +00:00
|
|
|
return timeOff+time.Since(start) >= time.Duration(defaultTimeOut)*time.Second
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2016-03-02 01:16:40 +00:00
|
|
|
|
|
|
|
func httpScheme(u *url.URL) string {
|
|
|
|
scheme := u.Scheme
|
|
|
|
if scheme != "https" {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
return scheme
|
|
|
|
}
|