ping.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "context"
  4. "net/http"
  5. "path"
  6. "strings"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/errdefs"
  10. )
  11. // Ping pings the server and returns the value of the "Docker-Experimental",
  12. // "Builder-Version", "OS-Type" & "API-Version" headers. It attempts to use
  13. // a HEAD request on the endpoint, but falls back to GET if HEAD is not supported
  14. // by the daemon. It ignores internal server errors returned by the API, which
  15. // may be returned if the daemon is in an unhealthy state, but returns errors
  16. // for other non-success status codes, failing to connect to the API, or failing
  17. // to parse the API response.
  18. func (cli *Client) Ping(ctx context.Context) (types.Ping, error) {
  19. var ping types.Ping
  20. // Using cli.buildRequest() + cli.doRequest() instead of cli.sendRequest()
  21. // because ping requests are used during API version negotiation, so we want
  22. // to hit the non-versioned /_ping endpoint, not /v1.xx/_ping
  23. req, err := cli.buildRequest(ctx, http.MethodHead, path.Join(cli.basePath, "/_ping"), nil, nil)
  24. if err != nil {
  25. return ping, err
  26. }
  27. serverResp, err := cli.doRequest(req)
  28. if err == nil {
  29. defer ensureReaderClosed(serverResp)
  30. switch serverResp.statusCode {
  31. case http.StatusOK, http.StatusInternalServerError:
  32. // Server handled the request, so parse the response
  33. return parsePingResponse(cli, serverResp)
  34. }
  35. } else if IsErrConnectionFailed(err) {
  36. return ping, err
  37. }
  38. // HEAD failed; fallback to GET.
  39. req.Method = http.MethodGet
  40. serverResp, err = cli.doRequest(req)
  41. defer ensureReaderClosed(serverResp)
  42. if err != nil {
  43. return ping, err
  44. }
  45. return parsePingResponse(cli, serverResp)
  46. }
  47. func parsePingResponse(cli *Client, resp serverResponse) (types.Ping, error) {
  48. var ping types.Ping
  49. if resp.header == nil {
  50. err := cli.checkResponseErr(resp)
  51. return ping, errdefs.FromStatusCode(err, resp.statusCode)
  52. }
  53. ping.APIVersion = resp.header.Get("API-Version")
  54. ping.OSType = resp.header.Get("OSType")
  55. if resp.header.Get("Docker-Experimental") == "true" {
  56. ping.Experimental = true
  57. }
  58. if bv := resp.header.Get("Builder-Version"); bv != "" {
  59. ping.BuilderVersion = types.BuilderVersion(bv)
  60. }
  61. if si := resp.header.Get("Swarm"); si != "" {
  62. state, role, _ := strings.Cut(si, "/")
  63. ping.SwarmStatus = &swarm.Status{
  64. NodeState: swarm.LocalNodeState(state),
  65. ControlAvailable: role == "manager",
  66. }
  67. }
  68. err := cli.checkResponseErr(resp)
  69. return ping, errdefs.FromStatusCode(err, resp.statusCode)
  70. }