Call plugins with custom transports.
Small refactor to be able to use custom transports to call remote plugins. Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
parent
98943aafae
commit
44005e59d4
5 changed files with 123 additions and 23 deletions
|
@ -6,17 +6,17 @@ import (
|
|||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/docker/pkg/plugins/transport"
|
||||
"github.com/docker/go-connections/sockets"
|
||||
"github.com/docker/go-connections/tlsconfig"
|
||||
)
|
||||
|
||||
const (
|
||||
versionMimetype = "application/vnd.docker.plugins.v1.2+json"
|
||||
defaultTimeOut = 30
|
||||
defaultTimeOut = 30
|
||||
)
|
||||
|
||||
// NewClient creates a new plugin client (http).
|
||||
|
@ -29,23 +29,38 @@ func NewClient(addr string, tlsConfig tlsconfig.Options) (*Client, error) {
|
|||
}
|
||||
tr.TLSClientConfig = c
|
||||
|
||||
protoAndAddr := strings.Split(addr, "://")
|
||||
if err := sockets.ConfigureTransport(tr, protoAndAddr[0], protoAndAddr[1]); err != nil {
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
scheme := protoAndAddr[0]
|
||||
if scheme != "https" {
|
||||
scheme = "http"
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
scheme := httpScheme(u)
|
||||
|
||||
clientTransport := transport.NewHTTPTransport(tr, scheme, socket)
|
||||
return NewClientWithTransport(clientTransport), nil
|
||||
}
|
||||
|
||||
// NewClientWithTransport creates a new plugin client with a given transport.
|
||||
func NewClientWithTransport(tr transport.Transport) *Client {
|
||||
return &Client{
|
||||
http: &http.Client{
|
||||
Transport: tr,
|
||||
},
|
||||
requestFactory: tr,
|
||||
}
|
||||
return &Client{&http.Client{Transport: tr}, scheme, protoAndAddr[1]}, nil
|
||||
}
|
||||
|
||||
// Client represents a plugin client.
|
||||
type Client struct {
|
||||
http *http.Client // http client to use
|
||||
scheme string // scheme protocol of the plugin
|
||||
addr string // http address of the plugin
|
||||
http *http.Client // http client to use
|
||||
requestFactory transport.RequestFactory
|
||||
}
|
||||
|
||||
// Call calls the specified method with the specified arguments for the plugin.
|
||||
|
@ -94,13 +109,10 @@ func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{})
|
|||
}
|
||||
|
||||
func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool) (io.ReadCloser, error) {
|
||||
req, err := http.NewRequest("POST", "/"+serviceMethod, data)
|
||||
req, err := c.requestFactory.NewRequest(serviceMethod, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", versionMimetype)
|
||||
req.URL.Scheme = c.scheme
|
||||
req.URL.Host = c.addr
|
||||
|
||||
var retries int
|
||||
start := time.Now()
|
||||
|
@ -117,7 +129,7 @@ func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool)
|
|||
return nil, err
|
||||
}
|
||||
retries++
|
||||
logrus.Warnf("Unable to connect to plugin: %s, retrying in %v", c.addr, timeOff)
|
||||
logrus.Warnf("Unable to connect to plugin: %s, retrying in %v", req.URL, timeOff)
|
||||
time.Sleep(timeOff)
|
||||
continue
|
||||
}
|
||||
|
@ -163,3 +175,11 @@ func backoff(retries int) time.Duration {
|
|||
func abort(start time.Time, timeOff time.Duration) bool {
|
||||
return timeOff+time.Since(start) >= time.Duration(defaultTimeOut)*time.Second
|
||||
}
|
||||
|
||||
func httpScheme(u *url.URL) string {
|
||||
scheme := u.Scheme
|
||||
if scheme != "https" {
|
||||
scheme = "http"
|
||||
}
|
||||
return scheme
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue