Skip to content

Commit 5bb1119

Browse files
authored
Merge pull request #888 from bitoku/fix-false-layer-missing-ro-parent
Fix false ErrLayerMissing for RW layers with RO parents
2 parents d2d26a4 + 0aa1065 commit 5bb1119

2 files changed

Lines changed: 50 additions & 21 deletions

File tree

storage/check.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -414,32 +414,33 @@ func (s *store) Check(options *CheckOptions) (CheckReport, error) {
414414
}
415415
}
416416
}
417-
// Check that we don't have any dangling parent layer references.
418-
for id, parent := range report.layerParentsByLayerID {
419-
// If this layer doesn't have a parent, no problem.
420-
if parent == "" {
421-
continue
422-
}
423-
// If we've already seen a layer with this parent ID, skip it.
424-
if _, checked := referencedLayers[parent]; checked {
425-
continue
426-
}
427-
if _, checked := referencedROLayers[parent]; checked {
428-
continue
429-
}
430-
// We haven't seen a layer with the ID that this layer's record
431-
// says is its parent's ID.
432-
433-
// Not recordError2 because we are recording for layer "id"
434-
// but the text talks about layer "parent".
435-
report.recordLayerErrors(id,
436-
fmt.Errorf("%slayer %s: %w", readWriteDesc, parent, ErrLayerMissing))
437-
}
438417
return struct{}{}, false, nil
439418
}); err != nil {
440419
return CheckReport{}, err
441420
}
442421

422+
// Check that we don't have any dangling parent layer references.
423+
// This runs after all layer stores (RW and RO) have been visited,
424+
// so both referencedLayers and referencedROLayers are fully populated.
425+
for id, parent := range report.layerParentsByLayerID {
426+
if parent == "" {
427+
continue
428+
}
429+
if _, checked := referencedLayers[parent]; checked {
430+
continue
431+
}
432+
if _, checked := referencedROLayers[parent]; checked {
433+
continue
434+
}
435+
if _, isRO := referencedROLayers[id]; isRO {
436+
report.recordROLayerErrors(id,
437+
fmt.Errorf("read-only layer %s: %w", parent, ErrLayerMissing))
438+
} else {
439+
report.recordLayerErrors(id,
440+
fmt.Errorf("layer %s: %w", parent, ErrLayerMissing))
441+
}
442+
}
443+
443444
// This map will track examined images. If we have multiple stores, read-only ones can
444445
// contain copies of images that are also in the read-write store, or the read-write store
445446
// may contain a duplicate entry that refers to layers in the read-only stores, but when

storage/check_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package storage
22

33
import (
44
"archive/tar"
5+
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -66,6 +67,33 @@ func TestCheckDirectory(t *testing.T) {
6667
}
6768
}
6869

70+
func TestCheckParentLayerInROStore(t *testing.T) {
71+
roStore := newTestStore(t, StoreOptions{})
72+
_, err := roStore.CreateLayer("ro-parent-layer", "", nil, "", false, nil)
73+
require.NoError(t, err)
74+
roStoreRoot := roStore.GraphRoot()
75+
_, err = roStore.Shutdown(false)
76+
require.NoError(t, err)
77+
78+
// Copy the graph root to a fresh path so the global lock file cache
79+
// doesn't conflict (the first store registered these locks as RW).
80+
roStoreCopy := t.TempDir()
81+
require.NoError(t, os.CopyFS(roStoreCopy, os.DirFS(roStoreRoot)))
82+
83+
rwStore := newTestStore(t, StoreOptions{
84+
GraphDriverOptions: []string{"imagestore=" + roStoreCopy},
85+
})
86+
t.Cleanup(func() { _, _ = rwStore.Shutdown(true) })
87+
88+
_, err = rwStore.CreateLayer("rw-child-layer", "ro-parent-layer", nil, "", true, nil)
89+
require.NoError(t, err)
90+
91+
report, err := rwStore.Check(nil)
92+
require.NoError(t, err)
93+
assert.Empty(t, report.Layers, "RW child layer with RO parent should not be flagged")
94+
assert.Empty(t, report.ROLayers, "RO parent layer should not be flagged")
95+
}
96+
6997
func TestCheckDetectWriteable(t *testing.T) {
7098
var sawRWlayers, sawRWimages bool
7199
stoar, err := GetStore(StoreOptions{

0 commit comments

Comments
 (0)