Skip to content

Commit 3d352eb

Browse files
committed
Newtype OverlayMedium
1 parent d30c58e commit 3d352eb

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

runsc/boot/mount_hints_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestRootfsHintHappy(t *testing.T) {
257257
Annotations: map[string]string{
258258
RootfsPrefix + "source": imagePath,
259259
RootfsPrefix + "type": erofs.Name,
260-
RootfsPrefix + "overlay": config.MemoryOverlay.String(),
260+
RootfsPrefix + "overlay": config.OverlayMediumMemory().String(),
261261
},
262262
}
263263
hint, err := NewRootfsHint(spec)
@@ -272,7 +272,7 @@ func TestRootfsHintHappy(t *testing.T) {
272272
if hint.Mount.Type != erofs.Name {
273273
t.Errorf("rootfs type, want: %q, got: %q", erofs.Name, hint.Mount.Type)
274274
}
275-
if hint.Overlay != config.MemoryOverlay {
275+
if hint.Overlay.MediumType() != config.MemoryOverlay {
276276
t.Errorf("rootfs overlay, want: %q, got: %q", config.MemoryOverlay, hint.Overlay)
277277
}
278278
}
@@ -317,23 +317,23 @@ func TestRootfsHintErrors(t *testing.T) {
317317
RootfsPrefix + "invalid": "invalid",
318318
RootfsPrefix + "source": imagePath,
319319
RootfsPrefix + "type": erofs.Name,
320-
RootfsPrefix + "overlay": config.MemoryOverlay.String(),
320+
RootfsPrefix + "overlay": config.OverlayMediumMemory().String(),
321321
},
322322
error: "invalid rootfs annotation",
323323
},
324324
{
325325
name: "missing source",
326326
annotations: map[string]string{
327327
RootfsPrefix + "type": erofs.Name,
328-
RootfsPrefix + "overlay": config.MemoryOverlay.String(),
328+
RootfsPrefix + "overlay": config.OverlayMediumMemory().String(),
329329
},
330330
error: "rootfs annotations missing required field",
331331
},
332332
{
333333
name: "missing type",
334334
annotations: map[string]string{
335335
RootfsPrefix + "source": imagePath,
336-
RootfsPrefix + "overlay": config.MemoryOverlay.String(),
336+
RootfsPrefix + "overlay": config.OverlayMediumMemory().String(),
337337
},
338338
error: "rootfs annotations missing required field",
339339
},

runsc/config/config.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func (c *Config) GetOverlay2() Overlay2 {
493493
panic(fmt.Sprintf("Overlay2 cannot be set when --overlay=true"))
494494
}
495495
// Using a deprecated flag, honor it to avoid breaking users.
496-
return Overlay2{rootMount: true, subMounts: true, medium: "memory"}
496+
return Overlay2{rootMount: true, subMounts: true, medium: OverlayMediumMemory()}
497497
}
498498
return c.Overlay2
499499
}
@@ -844,49 +844,82 @@ func (g HostFifo) AllowOpen() bool {
844844
return g&HostFifoOpen != 0
845845
}
846846

847-
// OverlayMedium describes how overlay medium is configured.
848-
type OverlayMedium string
847+
// OverlayMediumType describes types of overlay medium.
848+
type OverlayMediumType string
849849

850850
const (
851851
// NoOverlay indicates that no overlay will be applied.
852-
NoOverlay = OverlayMedium("")
852+
NoOverlay = OverlayMediumType("")
853853

854854
// MemoryOverlay indicates that the overlay is backed by app memory.
855-
MemoryOverlay = OverlayMedium("memory")
855+
MemoryOverlay = OverlayMediumType("memory")
856856

857857
// SelfOverlay indicates that the overlaid mount is backed by itself.
858-
SelfOverlay = OverlayMedium("self")
858+
SelfOverlay = OverlayMediumType("self")
859+
860+
AnonOverlay = OverlayMediumType("dir")
859861

860-
// AnonOverlayPrefix is the prefix that users should specify in the
862+
// anonOverlayPrefix is the prefix that users should specify in the
861863
// config for the anonymous overlay.
862-
AnonOverlayPrefix = "dir="
864+
anonOverlayPrefix = "dir="
863865
)
864866

867+
// OverlayMedium describes how overlay medium is configured.
868+
type OverlayMedium struct {
869+
medium OverlayMediumType
870+
// Set iff medium is AnonOverlay.
871+
dir string
872+
}
873+
874+
func OverlayMediumNoOverlay() OverlayMedium {
875+
return OverlayMedium{medium: NoOverlay}
876+
}
877+
878+
func OverlayMediumMemory() OverlayMedium {
879+
return OverlayMedium{medium: MemoryOverlay}
880+
}
881+
865882
// String returns a human-readable string representing the overlay medium config.
866883
func (m OverlayMedium) String() string {
867-
return string(m)
884+
switch m.medium {
885+
case NoOverlay, MemoryOverlay, SelfOverlay:
886+
return string(m.medium)
887+
case AnonOverlay:
888+
return fmt.Sprintf("%s=%s", AnonOverlay, m.dir)
889+
default:
890+
panic(fmt.Sprintf("Invalid overlay medium %q", m.medium))
891+
}
868892
}
869893

870894
// Set sets the value. Set(String()) should be idempotent.
871895
func (m *OverlayMedium) Set(v string) error {
872-
switch OverlayMedium(v) {
873-
case NoOverlay, MemoryOverlay, SelfOverlay: // OK
896+
switch OverlayMediumType(v) {
897+
case NoOverlay, MemoryOverlay, SelfOverlay:
898+
{
899+
*m = OverlayMedium{medium: OverlayMediumType(v), dir: ""}
900+
return nil
901+
}
874902
default:
875-
if !strings.HasPrefix(v, AnonOverlayPrefix) {
903+
if !strings.HasPrefix(v, anonOverlayPrefix) {
876904
return fmt.Errorf("unexpected medium: %q", v)
877905
}
878-
if hostFileDir := strings.TrimPrefix(v, AnonOverlayPrefix); !filepath.IsAbs(hostFileDir) {
906+
hostFileDir := strings.TrimPrefix(v, anonOverlayPrefix)
907+
if !filepath.IsAbs(hostFileDir) {
879908
return fmt.Errorf("overlay host file directory should be an absolute path, got %q", hostFileDir)
880909
}
910+
*m = OverlayMedium{medium: OverlayMediumType(v), dir: hostFileDir}
881911
}
882-
*m = OverlayMedium(v)
883912
return nil
884913
}
885914

915+
func (m OverlayMedium) MediumType() OverlayMediumType {
916+
return m.medium
917+
}
918+
886919
// IsBackedByAnon indicates whether the overlaid mount is backed by a host file
887920
// in an anonymous directory.
888921
func (m OverlayMedium) IsBackedByAnon() bool {
889-
return strings.HasPrefix(string(m), AnonOverlayPrefix)
922+
return m.medium == AnonOverlay
890923
}
891924

892925
// HostFileDir indicates the directory in which the overlay-backing host file
@@ -895,9 +928,9 @@ func (m OverlayMedium) IsBackedByAnon() bool {
895928
// Precondition: m.IsBackedByAnon().
896929
func (m OverlayMedium) HostFileDir() string {
897930
if !m.IsBackedByAnon() {
898-
panic(fmt.Sprintf("anonymous overlay medium = %q does not have %v prefix", m, AnonOverlayPrefix))
931+
panic(fmt.Sprintf("anonymous overlay medium = %q does not have %v prefix", m, anonOverlayPrefix))
899932
}
900-
return strings.TrimPrefix(string(m), AnonOverlayPrefix)
933+
return m.dir
901934
}
902935

903936
// Overlay2 holds the configuration for setting up overlay filesystems for the
@@ -910,15 +943,15 @@ type Overlay2 struct {
910943

911944
func defaultOverlay2() *Overlay2 {
912945
// Rootfs overlay is enabled by default and backed by a file in rootfs itself.
913-
return &Overlay2{rootMount: true, subMounts: false, medium: SelfOverlay}
946+
return &Overlay2{rootMount: true, subMounts: false, medium: OverlayMedium{medium: SelfOverlay}}
914947
}
915948

916949
// Set implements flag.Value. Set(String()) should be idempotent.
917950
func (o *Overlay2) Set(v string) error {
918951
if v == "none" {
919952
o.rootMount = false
920953
o.subMounts = false
921-
o.medium = NoOverlay
954+
o.medium = OverlayMediumNoOverlay()
922955
return nil
923956
}
924957
vs := strings.Split(v, ":")
@@ -963,21 +996,21 @@ func (o Overlay2) String() string {
963996

964997
// Enabled returns true if the overlay option is enabled for any mounts.
965998
func (o *Overlay2) Enabled() bool {
966-
return o.medium != NoOverlay
999+
return o.medium.medium != NoOverlay
9671000
}
9681001

9691002
// RootOverlayMedium returns the overlay medium config of the root mount.
9701003
func (o *Overlay2) RootOverlayMedium() OverlayMedium {
9711004
if !o.rootMount {
972-
return NoOverlay
1005+
return OverlayMediumNoOverlay()
9731006
}
9741007
return o.medium
9751008
}
9761009

9771010
// SubMountOverlayMedium returns the overlay medium config of submounts.
9781011
func (o *Overlay2) SubMountOverlayMedium() OverlayMedium {
9791012
if !o.subMounts {
980-
return NoOverlay
1013+
return OverlayMediumNoOverlay()
9811014
}
9821015
return o.medium
9831016
}

runsc/config/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func checkOverlay2(name string, value string) error {
193193
if err := o.Set(value); err != nil {
194194
return fmt.Errorf("invalid overlay2 annotation: %w", err)
195195
}
196-
switch o.medium {
196+
switch o.medium.MediumType() {
197197
case NoOverlay, MemoryOverlay, SelfOverlay:
198198
return nil
199199
default:

runsc/container/container.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ func createGoferConf(overlayMedium config.OverlayMedium, mountType string, mount
911911
default:
912912
return boot.GoferMountConf{}, fmt.Errorf("unsupported mount type %q in mount hint", mountType)
913913
}
914-
switch overlayMedium {
914+
switch overlayMedium.MediumType() {
915915
case config.NoOverlay:
916916
return boot.GoferMountConf{Lower: lower, Upper: boot.NoOverlay}, nil
917917
case config.MemoryOverlay:
@@ -947,7 +947,7 @@ func (c *Container) initGoferConfs(ovlConf config.Overlay2, mountHints *boot.Pod
947947
}
948948
}
949949
if c.Spec.Root.Readonly {
950-
overlayMedium = config.NoOverlay
950+
overlayMedium = config.OverlayMediumNoOverlay()
951951
}
952952
goferConf, err := createGoferConf(overlayMedium, mountType, c.Spec.Root.Path)
953953
if err != nil {
@@ -963,12 +963,12 @@ func (c *Container) initGoferConfs(ovlConf config.Overlay2, mountHints *boot.Pod
963963
overlayMedium = ovlConf.SubMountOverlayMedium()
964964
mountType = boot.Bind
965965
if specutils.IsReadonlyMount(c.Spec.Mounts[i].Options) {
966-
overlayMedium = config.NoOverlay
966+
overlayMedium = config.OverlayMediumNoOverlay()
967967
}
968968
if hint := mountHints.FindMount(c.Spec.Mounts[i].Source); hint != nil {
969969
// Note that we want overlayMedium=self even if this is a read-only mount so that
970970
// the shared mount is created correctly. Future containers may mount this writably.
971-
overlayMedium = config.SelfOverlay
971+
overlayMedium = config.OverlayMediumNoOverlay()
972972
if !specutils.IsGoferMount(hint.Mount) {
973973
mountType = hint.Mount.Type
974974
}

runsc/container/container_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ func TestRootfsEROFS(t *testing.T) {
34863486
spec.Annotations[boot.RootfsPrefix+"source"] = rootfsImage
34873487
// Disable the overlay, as we want to be sure that rootfs will always be
34883488
// shown as EROFS in mountinfo.
3489-
spec.Annotations[boot.RootfsPrefix+"overlay"] = config.NoOverlay.String()
3489+
spec.Annotations[boot.RootfsPrefix+"overlay"] = config.OverlayMediumNoOverlay().String()
34903490

34913491
conf := testutil.TestConfig(t)
34923492

@@ -3563,7 +3563,7 @@ func TestCheckpointRestoreEROFS(t *testing.T) {
35633563
// a writeable and savable overlay for rootfs, which allows the sentry to
35643564
// create the mount point for the bind mount of the temporary directory shared
35653565
// between host and test container.
3566-
spec.Annotations[boot.RootfsPrefix+"overlay"] = config.MemoryOverlay.String()
3566+
spec.Annotations[boot.RootfsPrefix+"overlay"] = config.OverlayMediumMemory().String()
35673567
return spec
35683568
})
35693569
})

0 commit comments

Comments
 (0)