Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -213,6 +213,16 @@ type Info struct {
|
| 213 | 213 |
Warnings []string `json:",omitempty"` |
| 214 | 214 |
} |
| 215 | 215 |
|
| 216 |
+// Status provides information about the current swarm status and role, |
|
| 217 |
+// obtained from the "Swarm" header in the API response. |
|
| 218 |
+type Status struct {
|
|
| 219 |
+ // NodeState represents the state of the node. |
|
| 220 |
+ NodeState LocalNodeState |
|
| 221 |
+ |
|
| 222 |
+ // ControlAvailable indicates if the node is a swarm manager. |
|
| 223 |
+ ControlAvailable bool |
|
| 224 |
+} |
|
| 225 |
+ |
|
| 216 | 226 |
// Peer represents a peer. |
| 217 | 227 |
type Peer struct {
|
| 218 | 228 |
NodeID string |
| ... | ... |
@@ -188,6 +188,15 @@ type Ping struct {
|
| 188 | 188 |
OSType string |
| 189 | 189 |
Experimental bool |
| 190 | 190 |
BuilderVersion BuilderVersion |
| 191 |
+ |
|
| 192 |
+ // SwarmStatus provides information about the current swarm status of the |
|
| 193 |
+ // engine, obtained from the "Swarm" header in the API response. |
|
| 194 |
+ // |
|
| 195 |
+ // It can be a nil struct if the API version does not provide this header |
|
| 196 |
+ // in the ping response, or if an error occurred, in which case the client |
|
| 197 |
+ // should use other ways to get the current swarm status, such as the /swarm |
|
| 198 |
+ // endpoint. |
|
| 199 |
+ SwarmStatus *swarm.Status |
|
| 191 | 200 |
} |
| 192 | 201 |
|
| 193 | 202 |
// ComponentVersion describes the version information for a specific component. |
| ... | ... |
@@ -4,8 +4,10 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"net/http" |
| 6 | 6 |
"path" |
| 7 |
+ "strings" |
|
| 7 | 8 |
|
| 8 | 9 |
"github.com/docker/docker/api/types" |
| 10 |
+ "github.com/docker/docker/api/types/swarm" |
|
| 9 | 11 |
"github.com/docker/docker/errdefs" |
| 10 | 12 |
) |
| 11 | 13 |
|
| ... | ... |
@@ -61,6 +63,13 @@ func parsePingResponse(cli *Client, resp serverResponse) (types.Ping, error) {
|
| 61 | 61 |
if bv := resp.header.Get("Builder-Version"); bv != "" {
|
| 62 | 62 |
ping.BuilderVersion = types.BuilderVersion(bv) |
| 63 | 63 |
} |
| 64 |
+ if si := resp.header.Get("Swarm"); si != "" {
|
|
| 65 |
+ parts := strings.SplitN(si, "/", 2) |
|
| 66 |
+ ping.SwarmStatus = &swarm.Status{
|
|
| 67 |
+ NodeState: swarm.LocalNodeState(parts[0]), |
|
| 68 |
+ ControlAvailable: len(parts) == 2 && parts[1] == "manager", |
|
| 69 |
+ } |
|
| 70 |
+ } |
|
| 64 | 71 |
err := cli.checkResponseErr(resp) |
| 65 | 72 |
return ping, errdefs.FromStatusCode(err, resp.statusCode) |
| 66 | 73 |
} |
| ... | ... |
@@ -8,6 +8,7 @@ import ( |
| 8 | 8 |
"strings" |
| 9 | 9 |
"testing" |
| 10 | 10 |
|
| 11 |
+ "github.com/docker/docker/api/types/swarm" |
|
| 11 | 12 |
"gotest.tools/v3/assert" |
| 12 | 13 |
is "gotest.tools/v3/assert/cmp" |
| 13 | 14 |
) |
| ... | ... |
@@ -25,6 +26,7 @@ func TestPingFail(t *testing.T) {
|
| 25 | 25 |
resp.Header = http.Header{}
|
| 26 | 26 |
resp.Header.Set("API-Version", "awesome")
|
| 27 | 27 |
resp.Header.Set("Docker-Experimental", "true")
|
| 28 |
+ resp.Header.Set("Swarm", "inactive")
|
|
| 28 | 29 |
} |
| 29 | 30 |
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
|
| 30 | 31 |
return resp, nil |
| ... | ... |
@@ -35,12 +37,15 @@ func TestPingFail(t *testing.T) {
|
| 35 | 35 |
assert.ErrorContains(t, err, "some error with the server") |
| 36 | 36 |
assert.Check(t, is.Equal(false, ping.Experimental)) |
| 37 | 37 |
assert.Check(t, is.Equal("", ping.APIVersion))
|
| 38 |
+ var si *swarm.Status |
|
| 39 |
+ assert.Check(t, is.Equal(si, ping.SwarmStatus)) |
|
| 38 | 40 |
|
| 39 | 41 |
withHeader = true |
| 40 | 42 |
ping2, err := client.Ping(context.Background()) |
| 41 | 43 |
assert.ErrorContains(t, err, "some error with the server") |
| 42 | 44 |
assert.Check(t, is.Equal(true, ping2.Experimental)) |
| 43 | 45 |
assert.Check(t, is.Equal("awesome", ping2.APIVersion))
|
| 46 |
+ assert.Check(t, is.Equal(swarm.Status{NodeState: "inactive"}, *ping2.SwarmStatus))
|
|
| 44 | 47 |
} |
| 45 | 48 |
|
| 46 | 49 |
// TestPingWithError tests the case where there is a protocol error in the ping. |
| ... | ... |
@@ -52,6 +57,7 @@ func TestPingWithError(t *testing.T) {
|
| 52 | 52 |
resp.Header = http.Header{}
|
| 53 | 53 |
resp.Header.Set("API-Version", "awesome")
|
| 54 | 54 |
resp.Header.Set("Docker-Experimental", "true")
|
| 55 |
+ resp.Header.Set("Swarm", "active/manager")
|
|
| 55 | 56 |
resp.Body = io.NopCloser(strings.NewReader("some error with the server"))
|
| 56 | 57 |
return resp, errors.New("some error")
|
| 57 | 58 |
}), |
| ... | ... |
@@ -61,6 +67,8 @@ func TestPingWithError(t *testing.T) {
|
| 61 | 61 |
assert.ErrorContains(t, err, "some error") |
| 62 | 62 |
assert.Check(t, is.Equal(false, ping.Experimental)) |
| 63 | 63 |
assert.Check(t, is.Equal("", ping.APIVersion))
|
| 64 |
+ var si *swarm.Status |
|
| 65 |
+ assert.Check(t, is.Equal(si, ping.SwarmStatus)) |
|
| 64 | 66 |
} |
| 65 | 67 |
|
| 66 | 68 |
// TestPingSuccess tests that we are able to get the expected API headers/ping |
| ... | ... |
@@ -72,6 +80,7 @@ func TestPingSuccess(t *testing.T) {
|
| 72 | 72 |
resp.Header = http.Header{}
|
| 73 | 73 |
resp.Header.Set("API-Version", "awesome")
|
| 74 | 74 |
resp.Header.Set("Docker-Experimental", "true")
|
| 75 |
+ resp.Header.Set("Swarm", "active/manager")
|
|
| 75 | 76 |
resp.Body = io.NopCloser(strings.NewReader("OK"))
|
| 76 | 77 |
return resp, nil |
| 77 | 78 |
}), |
| ... | ... |
@@ -80,6 +89,7 @@ func TestPingSuccess(t *testing.T) {
|
| 80 | 80 |
assert.NilError(t, err) |
| 81 | 81 |
assert.Check(t, is.Equal(true, ping.Experimental)) |
| 82 | 82 |
assert.Check(t, is.Equal("awesome", ping.APIVersion))
|
| 83 |
+ assert.Check(t, is.Equal(swarm.Status{NodeState: "active", ControlAvailable: true}, *ping.SwarmStatus))
|
|
| 83 | 84 |
} |
| 84 | 85 |
|
| 85 | 86 |
// TestPingHeadFallback tests that the client falls back to GET if HEAD fails. |