@@ -16,7 +16,7 @@ import (
16
16
timetypes "github.com/docker/docker/api/types/time"
17
17
"github.com/docker/docker/api/types/versions"
18
18
"github.com/docker/docker/pkg/ioutils"
19
- pkgerrors "github.com/pkg/errors"
19
+ "github.com/pkg/errors"
20
20
"github.com/sirupsen/logrus"
21
21
"golang.org/x/sync/errgroup"
22
22
)
@@ -90,44 +90,83 @@ func (s *systemRouter) getVersion(ctx context.Context, w http.ResponseWriter, r
90
90
}
91
91
92
92
func (s * systemRouter ) getDiskUsage (ctx context.Context , w http.ResponseWriter , r * http.Request , vars map [string ]string ) error {
93
+ if err := httputils .ParseForm (r ); err != nil {
94
+ return err
95
+ }
96
+
97
+ var getContainers , getImages , getVolumes , getBuildCache bool
98
+ if typeStrs , ok := r .Form ["type" ]; ! ok {
99
+ getContainers , getImages , getVolumes , getBuildCache = true , true , true , true
100
+ } else {
101
+ for _ , typ := range typeStrs {
102
+ switch types .DiskUsageObject (typ ) {
103
+ case types .ContainerObject :
104
+ getContainers = true
105
+ case types .ImageObject :
106
+ getImages = true
107
+ case types .VolumeObject :
108
+ getVolumes = true
109
+ case types .BuildCacheObject :
110
+ getBuildCache = true
111
+ default :
112
+ return invalidRequestError {Err : fmt .Errorf ("unknown object type: %s" , typ )}
113
+ }
114
+ }
115
+ }
116
+
93
117
eg , ctx := errgroup .WithContext (ctx )
94
118
95
- var du * types.DiskUsage
96
- eg .Go (func () error {
97
- var err error
98
- du , err = s .backend .SystemDiskUsage (ctx )
99
- return err
100
- })
119
+ var systemDiskUsage * types.DiskUsage
120
+ if getContainers || getImages || getVolumes {
121
+ eg .Go (func () error {
122
+ var err error
123
+ systemDiskUsage , err = s .backend .SystemDiskUsage (ctx , DiskUsageOptions {
124
+ Containers : getContainers ,
125
+ Images : getImages ,
126
+ Volumes : getVolumes ,
127
+ })
128
+ return err
129
+ })
130
+ }
101
131
102
132
var buildCache []* types.BuildCache
103
- eg .Go (func () error {
104
- var err error
105
- buildCache , err = s .builder .DiskUsage (ctx )
106
- if err != nil {
107
- return pkgerrors .Wrap (err , "error getting build cache usage" )
108
- }
109
- return nil
110
- })
133
+ if getBuildCache {
134
+ eg .Go (func () error {
135
+ var err error
136
+ buildCache , err = s .builder .DiskUsage (ctx )
137
+ if err != nil {
138
+ return errors .Wrap (err , "error getting build cache usage" )
139
+ }
140
+ if buildCache == nil {
141
+ // Ensure empty `BuildCache` field is represented as empty JSON array(`[]`)
142
+ // instead of `null` to be consistent with `Images`, `Containers` etc.
143
+ buildCache = []* types.BuildCache {}
144
+ }
145
+ return nil
146
+ })
147
+ }
111
148
112
149
if err := eg .Wait (); err != nil {
113
150
return err
114
151
}
115
152
153
+ var builderSize int64
116
154
if versions .LessThan (httputils .VersionFromContext (ctx ), "1.42" ) {
117
- var builderSize int64
118
155
for _ , b := range buildCache {
119
156
builderSize += b .Size
120
157
}
121
- du .BuilderSize = builderSize
122
158
}
123
159
124
- du .BuildCache = buildCache
125
- if buildCache == nil {
126
- // Ensure empty `BuildCache` field is represented as empty JSON array(`[]`)
127
- // instead of `null` to be consistent with `Images`, `Containers` etc.
128
- du .BuildCache = []* types.BuildCache {}
160
+ du := types.DiskUsage {
161
+ BuildCache : buildCache ,
162
+ BuilderSize : builderSize ,
163
+ }
164
+ if systemDiskUsage != nil {
165
+ du .LayersSize = systemDiskUsage .LayersSize
166
+ du .Images = systemDiskUsage .Images
167
+ du .Containers = systemDiskUsage .Containers
168
+ du .Volumes = systemDiskUsage .Volumes
129
169
}
130
-
131
170
return httputils .WriteJSON (w , http .StatusOK , du )
132
171
}
133
172
0 commit comments