@@ -9,29 +9,52 @@ import (
99
1010// totalReadyImageBytesFromMetadata sums ready image sizes directly from metadata.json files.
1111// This is conservative for admission control and disk accounting: if metadata says an
12- // image is ready, we count its recorded size without re-validating the rootfs path.
12+ // image is ready, we count its recorded size without re-validating the rootfs path. If
13+ // the metadata file is unreadable or malformed, we fall back to counting any rootfs disk
14+ // files found in the digest directory so we do not undercount host disk usage.
1315func totalReadyImageBytesFromMetadata (imagesDir string ) (int64 , error ) {
1416 var total int64
1517
1618 err := filepath .Walk (imagesDir , func (path string , info os.FileInfo , err error ) error {
1719 if err != nil {
18- return nil
20+ if os .IsNotExist (err ) {
21+ return nil
22+ }
23+ return err
1924 }
2025 if info .IsDir () || info .Name () != "metadata.json" {
2126 return nil
2227 }
2328
2429 data , err := os .ReadFile (path )
2530 if err != nil {
26- return nil
31+ rootfsBytes , fallbackErr := totalRootfsBytesInDigestDir (filepath .Dir (path ))
32+ if fallbackErr == nil {
33+ total += rootfsBytes
34+ return nil
35+ }
36+ return fmt .Errorf ("read image metadata %s: %w" , path , err )
2737 }
2838
2939 var meta imageMetadata
3040 if err := json .Unmarshal (data , & meta ); err != nil {
31- return nil
41+ rootfsBytes , fallbackErr := totalRootfsBytesInDigestDir (filepath .Dir (path ))
42+ if fallbackErr == nil {
43+ total += rootfsBytes
44+ return nil
45+ }
46+ return fmt .Errorf ("unmarshal image metadata %s: %w" , path , err )
3247 }
3348 if meta .Status == StatusReady && meta .SizeBytes > 0 {
3449 total += meta .SizeBytes
50+ return nil
51+ }
52+ if meta .Status == StatusReady {
53+ rootfsBytes , err := totalRootfsBytesInDigestDir (filepath .Dir (path ))
54+ if err != nil {
55+ return fmt .Errorf ("stat ready image rootfs for %s: %w" , path , err )
56+ }
57+ total += rootfsBytes
3558 }
3659 return nil
3760 })
@@ -50,7 +73,10 @@ func totalOCICacheBlobBytesFromFilesystem(blobDir string) (int64, error) {
5073
5174 err := filepath .Walk (blobDir , func (path string , info os.FileInfo , err error ) error {
5275 if err != nil {
53- return nil
76+ if os .IsNotExist (err ) {
77+ return nil
78+ }
79+ return err
5480 }
5581 if info .IsDir () {
5682 return nil
@@ -117,3 +143,32 @@ func (m *manager) computeDiskUsageTotals() (int64, int64, error) {
117143 }
118144 return readyImageBytes , ociCacheBytes , nil
119145}
146+
147+ func totalRootfsBytesInDigestDir (digestDir string ) (int64 , error ) {
148+ rootfsPaths , err := filepath .Glob (filepath .Join (digestDir , "rootfs.*" ))
149+ if err != nil {
150+ return 0 , err
151+ }
152+ if len (rootfsPaths ) == 0 {
153+ return 0 , os .ErrNotExist
154+ }
155+
156+ var total int64
157+ for _ , rootfsPath := range rootfsPaths {
158+ info , err := os .Stat (rootfsPath )
159+ if err != nil {
160+ if os .IsNotExist (err ) {
161+ continue
162+ }
163+ return 0 , err
164+ }
165+ if info .IsDir () {
166+ continue
167+ }
168+ total += info .Size ()
169+ }
170+ if total == 0 {
171+ return 0 , os .ErrNotExist
172+ }
173+ return total , nil
174+ }
0 commit comments