Browse code

api/types: rename container.StatsResponse to StatsResponseReader

commit 17c3269a370331d32d153d67975501caf6f0f29b moved the ContainerStats
type to the container package, and renamed it to StatsResponse. However,
this name is chosen poorly, as it documents it to be the response of
the API endpoint, but is more accurately a wrapper around a reader,
used to read a (stream of) StatsJSON. We want to change StatsJSON
to StatsResponse, as it's more consistent with other response types.

As 17c3269a370331d32d153d67975501caf6f0f29b did not make it into a
non-pre-release, we can still change this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/06/20 10:04:35
Showing 5 changed files
... ...
@@ -31,9 +31,14 @@ type CopyToContainerOptions struct {
31 31
 	CopyUIDGID                bool
32 32
 }
33 33
 
34
-// StatsResponse contains response of Engine API:
35
-// GET "/stats"
36
-type StatsResponse struct {
34
+// StatsResponseReader wraps an io.ReadCloser to read (a stream of) stats
35
+// for a container, as produced by the GET "/stats" endpoint.
36
+//
37
+// The OSType field is set to the server's platform to allow
38
+// platform-specific handling of the response.
39
+//
40
+// TODO(thaJeztah): remove this wrapper, and make OSType part of [StatsJSON].
41
+type StatsResponseReader struct {
37 42
 	Body   io.ReadCloser `json:"body"`
38 43
 	OSType string        `json:"ostype"`
39 44
 }
... ...
@@ -111,8 +111,8 @@ type CopyToContainerOptions = container.CopyToContainerOptions
111 111
 // ContainerStats contains response of Engine API:
112 112
 // GET "/stats"
113 113
 //
114
-// Deprecated: use [container.StatsResponse].
115
-type ContainerStats = container.StatsResponse
114
+// Deprecated: use [container.StatsResponseReader].
115
+type ContainerStats = container.StatsResponseReader
116 116
 
117 117
 // EventsOptions holds parameters to filter events with.
118 118
 //
... ...
@@ -9,7 +9,7 @@ import (
9 9
 
10 10
 // ContainerStats returns near realtime stats for a given container.
11 11
 // It's up to the caller to close the io.ReadCloser returned.
12
-func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (container.StatsResponse, error) {
12
+func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (container.StatsResponseReader, error) {
13 13
 	query := url.Values{}
14 14
 	query.Set("stream", "0")
15 15
 	if stream {
... ...
@@ -18,10 +18,10 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
18 18
 
19 19
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
20 20
 	if err != nil {
21
-		return container.StatsResponse{}, err
21
+		return container.StatsResponseReader{}, err
22 22
 	}
23 23
 
24
-	return container.StatsResponse{
24
+	return container.StatsResponseReader{
25 25
 		Body:   resp.body,
26 26
 		OSType: getDockerOS(resp.header.Get("Server")),
27 27
 	}, nil
... ...
@@ -29,17 +29,17 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
29 29
 
30 30
 // ContainerStatsOneShot gets a single stat entry from a container.
31 31
 // It differs from `ContainerStats` in that the API should not wait to prime the stats
32
-func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (container.StatsResponse, error) {
32
+func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (container.StatsResponseReader, error) {
33 33
 	query := url.Values{}
34 34
 	query.Set("stream", "0")
35 35
 	query.Set("one-shot", "1")
36 36
 
37 37
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
38 38
 	if err != nil {
39
-		return container.StatsResponse{}, err
39
+		return container.StatsResponseReader{}, err
40 40
 	}
41 41
 
42
-	return container.StatsResponse{
42
+	return container.StatsResponseReader{
43 43
 		Body:   resp.body,
44 44
 		OSType: getDockerOS(resp.header.Get("Server")),
45 45
 	}, nil
... ...
@@ -67,8 +67,8 @@ type ContainerAPIClient interface {
67 67
 	ContainerResize(ctx context.Context, container string, options container.ResizeOptions) error
68 68
 	ContainerRestart(ctx context.Context, container string, options container.StopOptions) error
69 69
 	ContainerStatPath(ctx context.Context, container, path string) (container.PathStat, error)
70
-	ContainerStats(ctx context.Context, container string, stream bool) (container.StatsResponse, error)
71
-	ContainerStatsOneShot(ctx context.Context, container string) (container.StatsResponse, error)
70
+	ContainerStats(ctx context.Context, container string, stream bool) (container.StatsResponseReader, error)
71
+	ContainerStatsOneShot(ctx context.Context, container string) (container.StatsResponseReader, error)
72 72
 	ContainerStart(ctx context.Context, container string, options container.StartOptions) error
73 73
 	ContainerStop(ctx context.Context, container string, options container.StopOptions) error
74 74
 	ContainerTop(ctx context.Context, container string, arguments []string) (container.ContainerTopOKBody, error)
... ...
@@ -151,7 +151,7 @@ func (s *DockerAPISuite) TestGetContainerStats(c *testing.T) {
151 151
 	runSleepingContainer(c, "--name", name)
152 152
 
153 153
 	type b struct {
154
-		stats container.StatsResponse
154
+		stats container.StatsResponseReader
155 155
 		err   error
156 156
 	}
157 157
 
... ...
@@ -255,7 +255,7 @@ func (s *DockerAPISuite) TestGetContainerStatsStream(c *testing.T) {
255 255
 	runSleepingContainer(c, "--name", name)
256 256
 
257 257
 	type b struct {
258
-		stats container.StatsResponse
258
+		stats container.StatsResponseReader
259 259
 		err   error
260 260
 	}
261 261
 
... ...
@@ -296,7 +296,7 @@ func (s *DockerAPISuite) TestGetContainerStatsNoStream(c *testing.T) {
296 296
 	runSleepingContainer(c, "--name", name)
297 297
 
298 298
 	type b struct {
299
-		stats container.StatsResponse
299
+		stats container.StatsResponseReader
300 300
 		err   error
301 301
 	}
302 302
 
... ...
@@ -1419,7 +1419,7 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
1419 1419
 	cli.WaitRun(c, name)
1420 1420
 
1421 1421
 	type b struct {
1422
-		stats container.StatsResponse
1422
+		stats container.StatsResponseReader
1423 1423
 		err   error
1424 1424
 	}
1425 1425
 	bc := make(chan b, 1)