Skip to content

Commit 0a4277a

Browse files
committed
api/types: move stats-types to api/types/container
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent fe60fa2 commit 0a4277a

11 files changed

Lines changed: 128 additions & 67 deletions

File tree

api/types/container/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CopyToContainerOptions struct {
3737
// The OSType field is set to the server's platform to allow
3838
// platform-specific handling of the response.
3939
//
40-
// TODO(thaJeztah): remove this wrapper, and make OSType part of [StatsJSON].
40+
// TODO(thaJeztah): remove this wrapper, and make OSType part of [StatsResponse].
4141
type StatsResponseReader struct {
4242
Body io.ReadCloser `json:"body"`
4343
OSType string `json:"ostype"`
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Package types is used for API stability in the types and response to the
2-
// consumers of the API stats endpoint.
3-
package types // import "github.com/docker/docker/api/types"
1+
package container
42

53
import "time"
64

@@ -169,8 +167,10 @@ type Stats struct {
169167
MemoryStats MemoryStats `json:"memory_stats,omitempty"`
170168
}
171169

172-
// StatsJSON is newly used Networks
173-
type StatsJSON struct {
170+
// StatsResponse is newly used Networks.
171+
//
172+
// TODO(thaJeztah): unify with [Stats]. This wrapper was to account for pre-api v1.21 changes, see https://github.com/moby/moby/commit/d3379946ec96fb6163cb8c4517d7d5a067045801
173+
type StatsResponse struct {
174174
Stats
175175

176176
Name string `json:"name,omitempty"`

api/types/types_deprecated.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,67 @@ type CopyToContainerOptions = container.CopyToContainerOptions
114114
// Deprecated: use [container.StatsResponseReader].
115115
type ContainerStats = container.StatsResponseReader
116116

117+
// ThrottlingData stores CPU throttling stats of one running container.
118+
// Not used on Windows.
119+
//
120+
// Deprecated: use [container.ThrottlingData].
121+
type ThrottlingData = container.ThrottlingData
122+
123+
// CPUUsage stores All CPU stats aggregated since container inception.
124+
//
125+
// Deprecated: use [container.CPUUsage].
126+
type CPUUsage = container.CPUUsage
127+
128+
// CPUStats aggregates and wraps all CPU related info of container
129+
//
130+
// Deprecated: use [container.CPUStats].
131+
type CPUStats = container.CPUStats
132+
133+
// MemoryStats aggregates all memory stats since container inception on Linux.
134+
// Windows returns stats for commit and private working set only.
135+
//
136+
// Deprecated: use [container.MemoryStats].
137+
type MemoryStats = container.MemoryStats
138+
139+
// BlkioStatEntry is one small entity to store a piece of Blkio stats
140+
// Not used on Windows.
141+
//
142+
// Deprecated: use [container.BlkioStatEntry].
143+
type BlkioStatEntry = container.BlkioStatEntry
144+
145+
// BlkioStats stores All IO service stats for data read and write.
146+
// This is a Linux specific structure as the differences between expressing
147+
// block I/O on Windows and Linux are sufficiently significant to make
148+
// little sense attempting to morph into a combined structure.
149+
//
150+
// Deprecated: use [container.BlkioStats].
151+
type BlkioStats = container.BlkioStats
152+
153+
// StorageStats is the disk I/O stats for read/write on Windows.
154+
//
155+
// Deprecated: use [container.StorageStats].
156+
type StorageStats = container.StorageStats
157+
158+
// NetworkStats aggregates the network stats of one container
159+
//
160+
// Deprecated: use [container.NetworkStats].
161+
type NetworkStats = container.NetworkStats
162+
163+
// PidsStats contains the stats of a container's pids
164+
//
165+
// Deprecated: use [container.PidsStats].
166+
type PidsStats = container.PidsStats
167+
168+
// Stats is Ultimate struct aggregating all types of stats of one container
169+
//
170+
// Deprecated: use [container.Stats].
171+
type Stats = container.Stats
172+
173+
// StatsJSON is newly used Networks
174+
//
175+
// Deprecated: use [container.StatsResponse].
176+
type StatsJSON = container.StatsResponse
177+
117178
// EventsOptions holds parameters to filter events with.
118179
//
119180
// Deprecated: use [events.ListOptions].

daemon/stats.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/containerd/log"
11-
"github.com/docker/docker/api/types"
1211
"github.com/docker/docker/api/types/backend"
12+
containertypes "github.com/docker/docker/api/types/container"
1313
"github.com/docker/docker/container"
1414
"github.com/docker/docker/errdefs"
1515
)
@@ -30,7 +30,7 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
3030

3131
// If the container is either not running or restarting and requires no stream, return an empty stats.
3232
if (!ctr.IsRunning() || ctr.IsRestarting()) && !config.Stream {
33-
return enc.Encode(&types.StatsJSON{
33+
return enc.Encode(&containertypes.StatsResponse{
3434
Name: ctr.Name,
3535
ID: ctr.ID,
3636
})
@@ -45,10 +45,10 @@ func (daemon *Daemon) ContainerStats(ctx context.Context, prefixOrName string, c
4545
return enc.Encode(stats)
4646
}
4747

48-
var preCPUStats types.CPUStats
48+
var preCPUStats containertypes.CPUStats
4949
var preRead time.Time
50-
getStatJSON := func(v interface{}) *types.StatsJSON {
51-
ss := v.(types.StatsJSON)
50+
getStatJSON := func(v interface{}) *containertypes.StatsResponse {
51+
ss := v.(containertypes.StatsResponse)
5252
ss.Name = ctr.Name
5353
ss.ID = ctr.ID
5454
ss.PreCPUStats = preCPUStats
@@ -99,7 +99,7 @@ func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch cha
9999
}
100100

101101
// GetContainerStats collects all the stats published by a container
102-
func (daemon *Daemon) GetContainerStats(container *container.Container) (*types.StatsJSON, error) {
102+
func (daemon *Daemon) GetContainerStats(container *container.Container) (*containertypes.StatsResponse, error) {
103103
stats, err := daemon.stats(container)
104104
if err != nil {
105105
goto done
@@ -124,7 +124,7 @@ done:
124124
return stats, nil
125125
case errdefs.ErrConflict, errdefs.ErrNotFound:
126126
// return empty stats containing only name and ID if not running or not found
127-
return &types.StatsJSON{
127+
return &containertypes.StatsResponse{
128128
Name: container.Name,
129129
ID: container.ID,
130130
}, nil

daemon/stats/collector.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"sync"
55
"time"
66

7-
"github.com/docker/docker/api/types"
7+
containertypes "github.com/docker/docker/api/types/container"
88
"github.com/docker/docker/container"
99
"github.com/moby/pubsub"
1010
)
@@ -31,7 +31,7 @@ func NewCollector(supervisor supervisor, interval time.Duration) *Collector {
3131

3232
type supervisor interface {
3333
// GetContainerStats collects all the stats related to a container
34-
GetContainerStats(container *container.Container) (*types.StatsJSON, error)
34+
GetContainerStats(container *container.Container) (*containertypes.StatsResponse, error)
3535
}
3636

3737
// Collect registers the container with the collector and adds it to
@@ -105,7 +105,7 @@ func (s *Collector) Run() {
105105
for _, pair := range pairs {
106106
stats, err := s.supervisor.GetContainerStats(pair.container)
107107
if err != nil {
108-
stats = &types.StatsJSON{
108+
stats = &containertypes.StatsResponse{
109109
Name: pair.container.Name,
110110
ID: pair.container.ID,
111111
}

daemon/stats_unix.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212

1313
statsV1 "github.com/containerd/cgroups/v3/cgroup1/stats"
1414
statsV2 "github.com/containerd/cgroups/v3/cgroup2/stats"
15-
"github.com/docker/docker/api/types"
15+
containertypes "github.com/docker/docker/api/types/container"
1616
"github.com/docker/docker/container"
1717
"github.com/pkg/errors"
1818
)
1919

20-
func copyBlkioEntry(entries []*statsV1.BlkIOEntry) []types.BlkioStatEntry {
21-
out := make([]types.BlkioStatEntry, len(entries))
20+
func copyBlkioEntry(entries []*statsV1.BlkIOEntry) []containertypes.BlkioStatEntry {
21+
out := make([]containertypes.BlkioStatEntry, len(entries))
2222
for i, re := range entries {
23-
out[i] = types.BlkioStatEntry{
23+
out[i] = containertypes.BlkioStatEntry{
2424
Major: re.Major,
2525
Minor: re.Minor,
2626
Op: re.Op,
@@ -30,7 +30,7 @@ func copyBlkioEntry(entries []*statsV1.BlkIOEntry) []types.BlkioStatEntry {
3030
return out
3131
}
3232

33-
func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
33+
func (daemon *Daemon) stats(c *container.Container) (*containertypes.StatsResponse, error) {
3434
c.Lock()
3535
task, err := c.GetRunningTask()
3636
c.Unlock()
@@ -44,7 +44,7 @@ func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
4444
}
4545
return nil, err
4646
}
47-
s := &types.StatsJSON{}
47+
s := &containertypes.StatsResponse{}
4848
s.Read = cs.Read
4949
stats := cs.Metrics
5050
switch t := stats.(type) {
@@ -57,9 +57,9 @@ func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
5757
}
5858
}
5959

60-
func (daemon *Daemon) statsV1(s *types.StatsJSON, stats *statsV1.Metrics) (*types.StatsJSON, error) {
60+
func (daemon *Daemon) statsV1(s *containertypes.StatsResponse, stats *statsV1.Metrics) (*containertypes.StatsResponse, error) {
6161
if stats.Blkio != nil {
62-
s.BlkioStats = types.BlkioStats{
62+
s.BlkioStats = containertypes.BlkioStats{
6363
IoServiceBytesRecursive: copyBlkioEntry(stats.Blkio.IoServiceBytesRecursive),
6464
IoServicedRecursive: copyBlkioEntry(stats.Blkio.IoServicedRecursive),
6565
IoQueuedRecursive: copyBlkioEntry(stats.Blkio.IoQueuedRecursive),
@@ -71,14 +71,14 @@ func (daemon *Daemon) statsV1(s *types.StatsJSON, stats *statsV1.Metrics) (*type
7171
}
7272
}
7373
if stats.CPU != nil {
74-
s.CPUStats = types.CPUStats{
75-
CPUUsage: types.CPUUsage{
74+
s.CPUStats = containertypes.CPUStats{
75+
CPUUsage: containertypes.CPUUsage{
7676
TotalUsage: stats.CPU.Usage.Total,
7777
PercpuUsage: stats.CPU.Usage.PerCPU,
7878
UsageInKernelmode: stats.CPU.Usage.Kernel,
7979
UsageInUsermode: stats.CPU.Usage.User,
8080
},
81-
ThrottlingData: types.ThrottlingData{
81+
ThrottlingData: containertypes.ThrottlingData{
8282
Periods: stats.CPU.Throttling.Periods,
8383
ThrottledPeriods: stats.CPU.Throttling.ThrottledPeriods,
8484
ThrottledTime: stats.CPU.Throttling.ThrottledTime,
@@ -122,15 +122,15 @@ func (daemon *Daemon) statsV1(s *types.StatsJSON, stats *statsV1.Metrics) (*type
122122
"total_unevictable": stats.Memory.TotalUnevictable,
123123
}
124124
if stats.Memory.Usage != nil {
125-
s.MemoryStats = types.MemoryStats{
125+
s.MemoryStats = containertypes.MemoryStats{
126126
Stats: raw,
127127
Usage: stats.Memory.Usage.Usage,
128128
MaxUsage: stats.Memory.Usage.Max,
129129
Limit: stats.Memory.Usage.Limit,
130130
Failcnt: stats.Memory.Usage.Failcnt,
131131
}
132132
} else {
133-
s.MemoryStats = types.MemoryStats{
133+
s.MemoryStats = containertypes.MemoryStats{
134134
Stats: raw,
135135
}
136136
}
@@ -142,7 +142,7 @@ func (daemon *Daemon) statsV1(s *types.StatsJSON, stats *statsV1.Metrics) (*type
142142
}
143143

144144
if stats.Pids != nil {
145-
s.PidsStats = types.PidsStats{
145+
s.PidsStats = containertypes.PidsStats{
146146
Current: stats.Pids.Current,
147147
Limit: stats.Pids.Limit,
148148
}
@@ -151,40 +151,40 @@ func (daemon *Daemon) statsV1(s *types.StatsJSON, stats *statsV1.Metrics) (*type
151151
return s, nil
152152
}
153153

154-
func (daemon *Daemon) statsV2(s *types.StatsJSON, stats *statsV2.Metrics) (*types.StatsJSON, error) {
154+
func (daemon *Daemon) statsV2(s *containertypes.StatsResponse, stats *statsV2.Metrics) (*containertypes.StatsResponse, error) {
155155
if stats.Io != nil {
156-
var isbr []types.BlkioStatEntry
156+
var isbr []containertypes.BlkioStatEntry
157157
for _, re := range stats.Io.Usage {
158158
isbr = append(isbr,
159-
types.BlkioStatEntry{
159+
containertypes.BlkioStatEntry{
160160
Major: re.Major,
161161
Minor: re.Minor,
162162
Op: "read",
163163
Value: re.Rbytes,
164164
},
165-
types.BlkioStatEntry{
165+
containertypes.BlkioStatEntry{
166166
Major: re.Major,
167167
Minor: re.Minor,
168168
Op: "write",
169169
Value: re.Wbytes,
170170
},
171171
)
172172
}
173-
s.BlkioStats = types.BlkioStats{
173+
s.BlkioStats = containertypes.BlkioStats{
174174
IoServiceBytesRecursive: isbr,
175175
// Other fields are unsupported
176176
}
177177
}
178178

179179
if stats.CPU != nil {
180-
s.CPUStats = types.CPUStats{
181-
CPUUsage: types.CPUUsage{
180+
s.CPUStats = containertypes.CPUStats{
181+
CPUUsage: containertypes.CPUUsage{
182182
TotalUsage: stats.CPU.UsageUsec * 1000,
183183
// PercpuUsage is not supported
184184
UsageInKernelmode: stats.CPU.SystemUsec * 1000,
185185
UsageInUsermode: stats.CPU.UserUsec * 1000,
186186
},
187-
ThrottlingData: types.ThrottlingData{
187+
ThrottlingData: containertypes.ThrottlingData{
188188
Periods: stats.CPU.NrPeriods,
189189
ThrottledPeriods: stats.CPU.NrThrottled,
190190
ThrottledTime: stats.CPU.ThrottledUsec * 1000,
@@ -193,7 +193,7 @@ func (daemon *Daemon) statsV2(s *types.StatsJSON, stats *statsV2.Metrics) (*type
193193
}
194194

195195
if stats.Memory != nil {
196-
s.MemoryStats = types.MemoryStats{
196+
s.MemoryStats = containertypes.MemoryStats{
197197
// Stats is not compatible with v1
198198
Stats: map[string]uint64{
199199
"anon": stats.Memory.Anon,
@@ -244,7 +244,7 @@ func (daemon *Daemon) statsV2(s *types.StatsJSON, stats *statsV2.Metrics) (*type
244244
}
245245

246246
if stats.Pids != nil {
247-
s.PidsStats = types.PidsStats{
247+
s.PidsStats = containertypes.PidsStats{
248248
Current: stats.Pids.Current,
249249
Limit: stats.Pids.Limit,
250250
}
@@ -267,7 +267,7 @@ func (daemon *Daemon) getNetworkSandboxID(c *container.Container) (string, error
267267
return curr.NetworkSettings.SandboxID, nil
268268
}
269269

270-
func (daemon *Daemon) getNetworkStats(c *container.Container) (map[string]types.NetworkStats, error) {
270+
func (daemon *Daemon) getNetworkStats(c *container.Container) (map[string]containertypes.NetworkStats, error) {
271271
sandboxID, err := daemon.getNetworkSandboxID(c)
272272
if err != nil {
273273
return nil, err
@@ -283,10 +283,10 @@ func (daemon *Daemon) getNetworkStats(c *container.Container) (map[string]types.
283283
return nil, err
284284
}
285285

286-
stats := make(map[string]types.NetworkStats)
286+
stats := make(map[string]containertypes.NetworkStats)
287287
// Convert libnetwork nw stats into api stats
288288
for ifName, ifStats := range lnstats {
289-
stats[ifName] = types.NetworkStats{
289+
stats[ifName] = containertypes.NetworkStats{
290290
RxBytes: ifStats.RxBytes,
291291
RxPackets: ifStats.RxPackets,
292292
RxErrors: ifStats.RxErrors,

0 commit comments

Comments
 (0)