Skip to content

Commit cbd1a73

Browse files
committed
overlay: use function to check for overlay-based mounts
Factorize code that check for an overlay mount type by using a function instead. This will allow supporting other overlay-based mount types more easily in the future (for example fuse-overlayfs). Signed-off-by: Alexis Murzeau <[email protected]>
1 parent b0c05cd commit cbd1a73

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

cache/manager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/moby/buildkit/util/contentutil"
4646
"github.com/moby/buildkit/util/iohelper"
4747
"github.com/moby/buildkit/util/leaseutil"
48+
"github.com/moby/buildkit/util/overlay"
4849
"github.com/moby/buildkit/util/winlayers"
4950
digest "github.com/opencontainers/go-digest"
5051
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -2701,7 +2702,7 @@ func isReadOnly(mnt mount.Mount) bool {
27012702
hasUpperdir = true
27022703
}
27032704
}
2704-
if mnt.Type == "overlay" {
2705+
if overlay.IsOverlayMountType(mnt) {
27052706
return !hasUpperdir
27062707
}
27072708
return false

cache/refs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/moby/buildkit/util/compression"
2828
"github.com/moby/buildkit/util/flightcontrol"
2929
"github.com/moby/buildkit/util/leaseutil"
30+
"github.com/moby/buildkit/util/overlay"
3031
"github.com/moby/buildkit/util/progress"
3132
rootlessmountopts "github.com/moby/buildkit/util/rootless/mountopts"
3233
"github.com/moby/buildkit/util/winlayers"
@@ -1513,7 +1514,7 @@ func (m *readOnlyMounter) Mount() ([]mount.Mount, func() error, error) {
15131514
return nil, nil, err
15141515
}
15151516
for i, m := range mounts {
1516-
if m.Type == "overlay" {
1517+
if overlay.IsOverlayMountType(m) {
15171518
mounts[i].Options = readonlyOverlay(m.Options)
15181519
continue
15191520
}
@@ -1623,7 +1624,7 @@ func (sm *sharableMountable) Mount() (_ []mount.Mount, _ func() error, retErr er
16231624
}()
16241625
var isOverlay bool
16251626
for _, m := range mounts {
1626-
if m.Type == "overlay" {
1627+
if overlay.IsOverlayMountType(m) {
16271628
isOverlay = true
16281629
break
16291630
}

snapshot/diffapply_unix.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ func applierFor(dest Mountable, tryCrossSnapshotLink, userxattr bool) (_ *applie
167167
}
168168
mnt := mnts[0]
169169

170-
switch mnt.Type {
171-
case "overlay":
170+
if overlay.IsOverlayMountType(mnt) {
172171
for _, opt := range mnt.Options {
173172
if strings.HasPrefix(opt, "upperdir=") {
174173
a.root = strings.TrimPrefix(opt, "upperdir=")
@@ -183,9 +182,9 @@ func applierFor(dest Mountable, tryCrossSnapshotLink, userxattr bool) (_ *applie
183182
return nil, errors.Errorf("could not find lowerdir in mount options %v", mnt.Options)
184183
}
185184
a.createWhiteoutDelete = true
186-
case "bind", "rbind":
185+
} else if mnt.Type == "bind" || mnt.Type == "rbind" {
187186
a.root = mnt.Source
188-
default:
187+
} else {
189188
mnter := LocalMounter(dest)
190189
root, err := mnter.Mount()
191190
if err != nil {
@@ -570,10 +569,9 @@ func differFor(lowerMntable, upperMntable Mountable) (_ *differ, rerr error) {
570569
}
571570

572571
if len(upperMnts) == 1 {
573-
switch upperMnts[0].Type {
574-
case "bind", "rbind":
572+
if upperMnts[0].Type == "bind" || upperMnts[0].Type == "rbind" {
575573
d.upperBindSource = upperMnts[0].Source
576-
case "overlay":
574+
} else if overlay.IsOverlayMountType(upperMnts[0]) {
577575
overlayDirs, err := overlay.GetOverlayLayers(upperMnts[0])
578576
if err != nil {
579577
return nil, errors.Wrapf(err, "failed to get overlay layers from mount %+v", upperMnts[0])

snapshot/snapshotter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ func setRedirectDir(mounts []mount.Mount, redirectDirOption string) (ret []mount
167167
return mounts
168168
}
169169
for _, m := range mounts {
170+
// Replace redirect_dir options, but only for overlay.
171+
// redirect_dir is not supported by fuse-overlayfs.
170172
if m.Type == "overlay" {
171173
var opts []string
172174
for _, o := range m.Options {

snapshot/snapshotter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/containerd/continuity/fs/fstest"
2424
"github.com/moby/buildkit/identity"
2525
"github.com/moby/buildkit/util/leaseutil"
26+
overlayutil "github.com/moby/buildkit/util/overlay"
2627
"github.com/pkg/errors"
2728
"github.com/stretchr/testify/require"
2829
bolt "go.etcd.io/bbolt"
@@ -602,7 +603,7 @@ func pathCallback[T any](ctx context.Context, t *testing.T, sn *mergeSnapshotter
602603
require.Len(t, mounts, 1)
603604
mnt := mounts[0]
604605

605-
if mnt.Type == "overlay" {
606+
if overlayutil.IsOverlayMountType(mnt) {
606607
var upperdir string
607608
var lowerdirs []string
608609
for _, opt := range mnt.Options {

util/overlay/overlay.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package overlay
2+
3+
import "github.com/containerd/containerd/mount"
4+
5+
// IsOverlayMountType returns true if the mount type is overlay-based
6+
func IsOverlayMountType(mnt mount.Mount) bool {
7+
return mnt.Type == "overlay"
8+
}

util/overlay/overlay_linux.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,23 @@ func GetUpperdir(lower, upper []mount.Mount) (string, error) {
3838
// Get layer directories of lower snapshot
3939
var lowerlayers []string
4040
lowerM := lower[0]
41-
switch lowerM.Type {
42-
case "bind":
41+
if lowerM.Type == "bind" {
4342
// lower snapshot is a bind mount of one layer
4443
lowerlayers = []string{lowerM.Source}
45-
case "overlay":
44+
} else if IsOverlayMountType(lowerM) {
4645
// lower snapshot is an overlay mount of multiple layers
4746
var err error
4847
lowerlayers, err = GetOverlayLayers(lowerM)
4948
if err != nil {
5049
return "", err
5150
}
52-
default:
51+
} else {
5352
return "", errors.Errorf("cannot get layer information from mount option (type = %q)", lowerM.Type)
5453
}
5554

5655
// Get layer directories of upper snapshot
5756
upperM := upper[0]
58-
if upperM.Type != "overlay" {
57+
if !IsOverlayMountType(upperM) {
5958
return "", errors.Errorf("upper snapshot isn't overlay mounted (type = %q)", upperM.Type)
6059
}
6160
upperlayers, err := GetOverlayLayers(upperM)

0 commit comments

Comments
 (0)