File: ping.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (81 lines) | stat: -rw-r--r-- 2,529 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package client

import (
	"context"
	"net/http"
	"path"
	"strings"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/build"
	"github.com/docker/docker/api/types/swarm"
)

// Ping pings the server and returns the value of the "Docker-Experimental",
// "Builder-Version", "OS-Type" & "API-Version" headers. It attempts to use
// a HEAD request on the endpoint, but falls back to GET if HEAD is not supported
// by the daemon. It ignores internal server errors returned by the API, which
// may be returned if the daemon is in an unhealthy state, but returns errors
// for other non-success status codes, failing to connect to the API, or failing
// to parse the API response.
func (cli *Client) Ping(ctx context.Context) (types.Ping, error) {
	var ping types.Ping

	// Using cli.buildRequest() + cli.doRequest() instead of cli.sendRequest()
	// because ping requests are used during API version negotiation, so we want
	// to hit the non-versioned /_ping endpoint, not /v1.xx/_ping
	req, err := cli.buildRequest(ctx, http.MethodHead, path.Join(cli.basePath, "/_ping"), nil, nil)
	if err != nil {
		return ping, err
	}
	resp, err := cli.doRequest(req)
	if err != nil {
		if IsErrConnectionFailed(err) {
			return ping, err
		}
		// We managed to connect, but got some error; continue and try GET request.
	} else {
		defer ensureReaderClosed(resp)
		switch resp.StatusCode {
		case http.StatusOK, http.StatusInternalServerError:
			// Server handled the request, so parse the response
			return parsePingResponse(cli, resp)
		}
	}

	// HEAD failed; fallback to GET.
	req.Method = http.MethodGet
	resp, err = cli.doRequest(req)
	defer ensureReaderClosed(resp)
	if err != nil {
		return ping, err
	}
	return parsePingResponse(cli, resp)
}

func parsePingResponse(cli *Client, resp *http.Response) (types.Ping, error) {
	if resp == nil {
		return types.Ping{}, nil
	}

	var ping types.Ping
	if resp.Header == nil {
		return ping, cli.checkResponseErr(resp)
	}
	ping.APIVersion = resp.Header.Get("Api-Version")
	ping.OSType = resp.Header.Get("Ostype")
	if resp.Header.Get("Docker-Experimental") == "true" {
		ping.Experimental = true
	}
	if bv := resp.Header.Get("Builder-Version"); bv != "" {
		ping.BuilderVersion = build.BuilderVersion(bv)
	}
	if si := resp.Header.Get("Swarm"); si != "" {
		state, role, _ := strings.Cut(si, "/")
		ping.SwarmStatus = &swarm.Status{
			NodeState:        swarm.LocalNodeState(state),
			ControlAvailable: role == "manager",
		}
	}
	return ping, cli.checkResponseErr(resp)
}