Browse code

api/types/container.StatsResponseReader: move to client

This type was only used in the client, and needs a rewrite; let's
move it to the client first.

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

Sebastiaan van Stijn authored on 2025/07/26 18:11:11
Showing 7 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 package container
2 2
 
3 3
 import (
4
-	"io"
5 4
 	"os"
6 5
 	"time"
7 6
 
... ...
@@ -35,18 +34,6 @@ type CopyToContainerOptions struct {
35 35
 	CopyUIDGID                bool
36 36
 }
37 37
 
38
-// StatsResponseReader wraps an io.ReadCloser to read (a stream of) stats
39
-// for a container, as produced by the GET "/stats" endpoint.
40
-//
41
-// The OSType field is set to the server's platform to allow
42
-// platform-specific handling of the response.
43
-//
44
-// TODO(thaJeztah): remove this wrapper, and make OSType part of [StatsResponse].
45
-type StatsResponseReader struct {
46
-	Body   io.ReadCloser `json:"body"`
47
-	OSType string        `json:"ostype"`
48
-}
49
-
50 38
 // MountPoint represents a mount point configuration inside the container.
51 39
 // This is used for reporting the mountpoints in use by a container.
52 40
 type MountPoint struct {
... ...
@@ -85,8 +85,8 @@ type ContainerAPIClient interface {
85 85
 	ContainerResize(ctx context.Context, container string, options container.ResizeOptions) error
86 86
 	ContainerRestart(ctx context.Context, container string, options container.StopOptions) error
87 87
 	ContainerStatPath(ctx context.Context, container, path string) (container.PathStat, error)
88
-	ContainerStats(ctx context.Context, container string, stream bool) (container.StatsResponseReader, error)
89
-	ContainerStatsOneShot(ctx context.Context, container string) (container.StatsResponseReader, error)
88
+	ContainerStats(ctx context.Context, container string, stream bool) (StatsResponseReader, error)
89
+	ContainerStatsOneShot(ctx context.Context, container string) (StatsResponseReader, error)
90 90
 	ContainerStart(ctx context.Context, container string, options container.StartOptions) error
91 91
 	ContainerStop(ctx context.Context, container string, options container.StopOptions) error
92 92
 	ContainerTop(ctx context.Context, container string, arguments []string) (container.TopResponse, error)
... ...
@@ -2,17 +2,28 @@ package client
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"io"
5 6
 	"net/url"
6
-
7
-	"github.com/moby/moby/api/types/container"
8 7
 )
9 8
 
9
+// StatsResponseReader wraps an io.ReadCloser to read (a stream of) stats
10
+// for a container, as produced by the GET "/stats" endpoint.
11
+//
12
+// The OSType field is set to the server's platform to allow
13
+// platform-specific handling of the response.
14
+//
15
+// TODO(thaJeztah): remove this wrapper, and make OSType part of [github.com/moby/moby/api/types/container.StatsResponse].
16
+type StatsResponseReader struct {
17
+	Body   io.ReadCloser `json:"body"`
18
+	OSType string        `json:"ostype"`
19
+}
20
+
10 21
 // ContainerStats returns near realtime stats for a given container.
11 22
 // 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.StatsResponseReader, error) {
23
+func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (StatsResponseReader, error) {
13 24
 	containerID, err := trimID("container", containerID)
14 25
 	if err != nil {
15
-		return container.StatsResponseReader{}, err
26
+		return StatsResponseReader{}, err
16 27
 	}
17 28
 
18 29
 	query := url.Values{}
... ...
@@ -23,10 +34,10 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
23 23
 
24 24
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
25 25
 	if err != nil {
26
-		return container.StatsResponseReader{}, err
26
+		return StatsResponseReader{}, err
27 27
 	}
28 28
 
29
-	return container.StatsResponseReader{
29
+	return StatsResponseReader{
30 30
 		Body:   resp.Body,
31 31
 		OSType: resp.Header.Get("Ostype"),
32 32
 	}, nil
... ...
@@ -34,10 +45,10 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
34 34
 
35 35
 // ContainerStatsOneShot gets a single stat entry from a container.
36 36
 // It differs from `ContainerStats` in that the API should not wait to prime the stats
37
-func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (container.StatsResponseReader, error) {
37
+func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (StatsResponseReader, error) {
38 38
 	containerID, err := trimID("container", containerID)
39 39
 	if err != nil {
40
-		return container.StatsResponseReader{}, err
40
+		return StatsResponseReader{}, err
41 41
 	}
42 42
 
43 43
 	query := url.Values{}
... ...
@@ -46,10 +57,10 @@ func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string
46 46
 
47 47
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
48 48
 	if err != nil {
49
-		return container.StatsResponseReader{}, err
49
+		return StatsResponseReader{}, err
50 50
 	}
51 51
 
52
-	return container.StatsResponseReader{
52
+	return StatsResponseReader{
53 53
 		Body:   resp.Body,
54 54
 		OSType: resp.Header.Get("Ostype"),
55 55
 	}, nil
... ...
@@ -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.StatsResponseReader
154
+		stats client.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.StatsResponseReader
258
+		stats client.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.StatsResponseReader
299
+		stats client.StatsResponseReader
300 300
 		err   error
301 301
 	}
302 302
 
... ...
@@ -1332,7 +1332,7 @@ func (s *DockerAPISuite) TestContainerAPIStatsWithNetworkDisabled(c *testing.T)
1332 1332
 	cli.WaitRun(c, name)
1333 1333
 
1334 1334
 	type b struct {
1335
-		stats container.StatsResponseReader
1335
+		stats client.StatsResponseReader
1336 1336
 		err   error
1337 1337
 	}
1338 1338
 	bc := make(chan b, 1)
... ...
@@ -1,7 +1,6 @@
1 1
 package container
2 2
 
3 3
 import (
4
-	"io"
5 4
 	"os"
6 5
 	"time"
7 6
 
... ...
@@ -35,18 +34,6 @@ type CopyToContainerOptions struct {
35 35
 	CopyUIDGID                bool
36 36
 }
37 37
 
38
-// StatsResponseReader wraps an io.ReadCloser to read (a stream of) stats
39
-// for a container, as produced by the GET "/stats" endpoint.
40
-//
41
-// The OSType field is set to the server's platform to allow
42
-// platform-specific handling of the response.
43
-//
44
-// TODO(thaJeztah): remove this wrapper, and make OSType part of [StatsResponse].
45
-type StatsResponseReader struct {
46
-	Body   io.ReadCloser `json:"body"`
47
-	OSType string        `json:"ostype"`
48
-}
49
-
50 38
 // MountPoint represents a mount point configuration inside the container.
51 39
 // This is used for reporting the mountpoints in use by a container.
52 40
 type MountPoint struct {
... ...
@@ -85,8 +85,8 @@ type ContainerAPIClient interface {
85 85
 	ContainerResize(ctx context.Context, container string, options container.ResizeOptions) error
86 86
 	ContainerRestart(ctx context.Context, container string, options container.StopOptions) error
87 87
 	ContainerStatPath(ctx context.Context, container, path string) (container.PathStat, error)
88
-	ContainerStats(ctx context.Context, container string, stream bool) (container.StatsResponseReader, error)
89
-	ContainerStatsOneShot(ctx context.Context, container string) (container.StatsResponseReader, error)
88
+	ContainerStats(ctx context.Context, container string, stream bool) (StatsResponseReader, error)
89
+	ContainerStatsOneShot(ctx context.Context, container string) (StatsResponseReader, error)
90 90
 	ContainerStart(ctx context.Context, container string, options container.StartOptions) error
91 91
 	ContainerStop(ctx context.Context, container string, options container.StopOptions) error
92 92
 	ContainerTop(ctx context.Context, container string, arguments []string) (container.TopResponse, error)
... ...
@@ -2,17 +2,28 @@ package client
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"io"
5 6
 	"net/url"
6
-
7
-	"github.com/moby/moby/api/types/container"
8 7
 )
9 8
 
9
+// StatsResponseReader wraps an io.ReadCloser to read (a stream of) stats
10
+// for a container, as produced by the GET "/stats" endpoint.
11
+//
12
+// The OSType field is set to the server's platform to allow
13
+// platform-specific handling of the response.
14
+//
15
+// TODO(thaJeztah): remove this wrapper, and make OSType part of [github.com/moby/moby/api/types/container.StatsResponse].
16
+type StatsResponseReader struct {
17
+	Body   io.ReadCloser `json:"body"`
18
+	OSType string        `json:"ostype"`
19
+}
20
+
10 21
 // ContainerStats returns near realtime stats for a given container.
11 22
 // 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.StatsResponseReader, error) {
23
+func (cli *Client) ContainerStats(ctx context.Context, containerID string, stream bool) (StatsResponseReader, error) {
13 24
 	containerID, err := trimID("container", containerID)
14 25
 	if err != nil {
15
-		return container.StatsResponseReader{}, err
26
+		return StatsResponseReader{}, err
16 27
 	}
17 28
 
18 29
 	query := url.Values{}
... ...
@@ -23,10 +34,10 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
23 23
 
24 24
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
25 25
 	if err != nil {
26
-		return container.StatsResponseReader{}, err
26
+		return StatsResponseReader{}, err
27 27
 	}
28 28
 
29
-	return container.StatsResponseReader{
29
+	return StatsResponseReader{
30 30
 		Body:   resp.Body,
31 31
 		OSType: resp.Header.Get("Ostype"),
32 32
 	}, nil
... ...
@@ -34,10 +45,10 @@ func (cli *Client) ContainerStats(ctx context.Context, containerID string, strea
34 34
 
35 35
 // ContainerStatsOneShot gets a single stat entry from a container.
36 36
 // It differs from `ContainerStats` in that the API should not wait to prime the stats
37
-func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (container.StatsResponseReader, error) {
37
+func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string) (StatsResponseReader, error) {
38 38
 	containerID, err := trimID("container", containerID)
39 39
 	if err != nil {
40
-		return container.StatsResponseReader{}, err
40
+		return StatsResponseReader{}, err
41 41
 	}
42 42
 
43 43
 	query := url.Values{}
... ...
@@ -46,10 +57,10 @@ func (cli *Client) ContainerStatsOneShot(ctx context.Context, containerID string
46 46
 
47 47
 	resp, err := cli.get(ctx, "/containers/"+containerID+"/stats", query, nil)
48 48
 	if err != nil {
49
-		return container.StatsResponseReader{}, err
49
+		return StatsResponseReader{}, err
50 50
 	}
51 51
 
52
-	return container.StatsResponseReader{
52
+	return StatsResponseReader{
53 53
 		Body:   resp.Body,
54 54
 		OSType: resp.Header.Get("Ostype"),
55 55
 	}, nil