|
@@ -1,10 +1,6 @@
|
|
|
/*
|
|
|
Package client is a Go client for the Docker Engine API.
|
|
|
|
|
|
-The "docker" command uses this package to communicate with the daemon. It can also
|
|
|
-be used by your own Go applications to do anything the command-line interface does
|
|
|
-- running containers, pulling images, managing swarms, etc.
|
|
|
-
|
|
|
For more information about the Engine API, see the documentation:
|
|
|
https://docs.docker.com/engine/reference/api/
|
|
|
|
|
@@ -160,7 +156,7 @@ func NewEnvClient() (*Client, error) {
|
|
|
// highly recommended that you set a version or your client may break if the
|
|
|
// server is upgraded.
|
|
|
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error) {
|
|
|
- proto, addr, basePath, err := ParseHost(host)
|
|
|
+ hostURL, err := ParseHostURL(host)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
@@ -171,7 +167,7 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
|
|
|
}
|
|
|
} else {
|
|
|
transport := new(http.Transport)
|
|
|
- sockets.ConfigureTransport(transport, proto, addr)
|
|
|
+ sockets.ConfigureTransport(transport, hostURL.Scheme, hostURL.Host)
|
|
|
client = &http.Client{
|
|
|
Transport: transport,
|
|
|
CheckRedirect: CheckRedirect,
|
|
@@ -189,28 +185,24 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
|
|
|
scheme = "https"
|
|
|
}
|
|
|
|
|
|
+ // TODO: store URL instead of proto/addr/basePath
|
|
|
return &Client{
|
|
|
scheme: scheme,
|
|
|
host: host,
|
|
|
- proto: proto,
|
|
|
- addr: addr,
|
|
|
- basePath: basePath,
|
|
|
+ proto: hostURL.Scheme,
|
|
|
+ addr: hostURL.Host,
|
|
|
+ basePath: hostURL.Path,
|
|
|
client: client,
|
|
|
version: version,
|
|
|
customHTTPHeaders: httpHeaders,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-// Close ensures that transport.Client is closed
|
|
|
-// especially needed while using NewClient with *http.Client = nil
|
|
|
-// for example
|
|
|
-// client.NewClient("unix:///var/run/docker.sock", nil, "v1.18", map[string]string{"User-Agent": "engine-api-cli-1.0"})
|
|
|
+// Close the transport used by the client
|
|
|
func (cli *Client) Close() error {
|
|
|
-
|
|
|
if t, ok := cli.client.Transport.(*http.Transport); ok {
|
|
|
t.CloseIdleConnections()
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -234,23 +226,20 @@ func (cli *Client) getAPIPath(p string, query url.Values) string {
|
|
|
return u.String()
|
|
|
}
|
|
|
|
|
|
-// ClientVersion returns the version string associated with this
|
|
|
-// instance of the Client. Note that this value can be changed
|
|
|
-// via the DOCKER_API_VERSION env var.
|
|
|
-// This operation doesn't acquire a mutex.
|
|
|
+// ClientVersion returns the API version used by this client.
|
|
|
func (cli *Client) ClientVersion() string {
|
|
|
return cli.version
|
|
|
}
|
|
|
|
|
|
-// NegotiateAPIVersion updates the version string associated with this
|
|
|
-// instance of the Client to match the latest version the server supports
|
|
|
+// NegotiateAPIVersion queries the API and updates the version to match the
|
|
|
+// API version. Any errors are silently ignored.
|
|
|
func (cli *Client) NegotiateAPIVersion(ctx context.Context) {
|
|
|
ping, _ := cli.Ping(ctx)
|
|
|
cli.NegotiateAPIVersionPing(ping)
|
|
|
}
|
|
|
|
|
|
-// NegotiateAPIVersionPing updates the version string associated with this
|
|
|
-// instance of the Client to match the latest version the server supports
|
|
|
+// NegotiateAPIVersionPing updates the client version to match the Ping.APIVersion
|
|
|
+// if the ping version is less than the default version.
|
|
|
func (cli *Client) NegotiateAPIVersionPing(p types.Ping) {
|
|
|
if cli.manualOverride {
|
|
|
return
|
|
@@ -272,17 +261,28 @@ func (cli *Client) NegotiateAPIVersionPing(p types.Ping) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// DaemonHost returns the host associated with this instance of the Client.
|
|
|
-// This operation doesn't acquire a mutex.
|
|
|
+// DaemonHost returns the host address used by the client
|
|
|
func (cli *Client) DaemonHost() string {
|
|
|
return cli.host
|
|
|
}
|
|
|
|
|
|
-// ParseHost verifies that the given host strings is valid.
|
|
|
+// ParseHost parses a url string, validates the strings is a host url, and returns
|
|
|
+// the parsed host as: protocol, address, and base path
|
|
|
+// Deprecated: use ParseHostURL
|
|
|
func ParseHost(host string) (string, string, string, error) {
|
|
|
+ hostURL, err := ParseHostURL(host)
|
|
|
+ if err != nil {
|
|
|
+ return "", "", "", err
|
|
|
+ }
|
|
|
+ return hostURL.Scheme, hostURL.Host, hostURL.Path, nil
|
|
|
+}
|
|
|
+
|
|
|
+// ParseHostURL parses a url string, validates the string is a host url, and
|
|
|
+// returns the parsed URL
|
|
|
+func ParseHostURL(host string) (*url.URL, error) {
|
|
|
protoAddrParts := strings.SplitN(host, "://", 2)
|
|
|
if len(protoAddrParts) == 1 {
|
|
|
- return "", "", "", fmt.Errorf("unable to parse docker host `%s`", host)
|
|
|
+ return nil, fmt.Errorf("unable to parse docker host `%s`", host)
|
|
|
}
|
|
|
|
|
|
var basePath string
|
|
@@ -290,16 +290,19 @@ func ParseHost(host string) (string, string, string, error) {
|
|
|
if proto == "tcp" {
|
|
|
parsed, err := url.Parse("tcp://" + addr)
|
|
|
if err != nil {
|
|
|
- return "", "", "", err
|
|
|
+ return nil, err
|
|
|
}
|
|
|
addr = parsed.Host
|
|
|
basePath = parsed.Path
|
|
|
}
|
|
|
- return proto, addr, basePath, nil
|
|
|
+ return &url.URL{
|
|
|
+ Scheme: proto,
|
|
|
+ Host: addr,
|
|
|
+ Path: basePath,
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
-// CustomHTTPHeaders returns the custom http headers associated with this
|
|
|
-// instance of the Client. This operation doesn't acquire a mutex.
|
|
|
+// CustomHTTPHeaders returns the custom http headers stored by the client.
|
|
|
func (cli *Client) CustomHTTPHeaders() map[string]string {
|
|
|
m := make(map[string]string)
|
|
|
for k, v := range cli.customHTTPHeaders {
|
|
@@ -308,8 +311,7 @@ func (cli *Client) CustomHTTPHeaders() map[string]string {
|
|
|
return m
|
|
|
}
|
|
|
|
|
|
-// SetCustomHTTPHeaders updates the custom http headers associated with this
|
|
|
-// instance of the Client. This operation doesn't acquire a mutex.
|
|
|
+// SetCustomHTTPHeaders that will be set on every HTTP request made by the client.
|
|
|
func (cli *Client) SetCustomHTTPHeaders(headers map[string]string) {
|
|
|
cli.customHTTPHeaders = headers
|
|
|
}
|