Skip to content

Commit

Permalink
Merge pull request #77 from imeoer/unify-filesystem
Browse files Browse the repository at this point in the history
Filesystem: refactor estargz support
  • Loading branch information
jiangliu authored Jun 25, 2022
2 parents 615dda8 + 1224c99 commit a1cf218
Show file tree
Hide file tree
Showing 26 changed files with 860 additions and 1,350 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nydus
package fs

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nydus
package fs

import (
"context"
Expand Down
58 changes: 37 additions & 21 deletions pkg/filesystem/nydus/config.go → pkg/filesystem/fs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
* SPDX-License-Identifier: Apache-2.0
*/

package nydus
package fs

import (
"os"
"strings"

"github.com/containerd/nydus-snapshotter/config"
"github.com/containerd/nydus-snapshotter/pkg/cache"
"github.com/containerd/nydus-snapshotter/pkg/filesystem/fs"
"github.com/containerd/nydus-snapshotter/pkg/filesystem/fs/stargz"
"github.com/containerd/nydus-snapshotter/pkg/filesystem/meta"
"github.com/containerd/nydus-snapshotter/pkg/process"
"github.com/containerd/nydus-snapshotter/pkg/signature"
"github.com/pkg/errors"
)

type NewFSOpt func(d *filesystem) error
type NewFSOpt func(d *Filesystem) error

func WithMeta(root string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if root == "" {
return errors.New("rootDir is required")
}
Expand All @@ -34,7 +34,7 @@ func WithMeta(root string) NewFSOpt {
}

func WithNydusdBinaryPath(p string, daemonMode string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if daemonMode != config.DaemonModeNone && p == "" {
return errors.New("nydusd binary path is required")
}
Expand All @@ -43,8 +43,15 @@ func WithNydusdBinaryPath(p string, daemonMode string) NewFSOpt {
}
}

func WithNydusImageBinaryPath(p string) NewFSOpt {
return func(d *Filesystem) error {
d.nydusImageBinaryPath = p
return nil
}
}

func WithProcessManager(pm *process.Manager) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if pm == nil {
return errors.New("process manager cannot be nil")
}
Expand All @@ -55,7 +62,7 @@ func WithProcessManager(pm *process.Manager) NewFSOpt {
}

func WithCacheManager(cm *cache.Manager) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if cm == nil {
return errors.New("cache manager cannot be nil")
}
Expand All @@ -66,14 +73,14 @@ func WithCacheManager(cm *cache.Manager) NewFSOpt {
}

func WithVerifier(verifier *signature.Verifier) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
d.verifier = verifier
return nil
}
}

func WithDaemonConfig(cfg config.DaemonConfig) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if (config.DaemonConfig{}) == cfg {
return errors.New("daemon config is empty")
}
Expand All @@ -83,33 +90,33 @@ func WithDaemonConfig(cfg config.DaemonConfig) NewFSOpt {
}

func WithVPCRegistry(vpcRegistry bool) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
d.vpcRegistry = vpcRegistry
return nil
}
}

func WithDaemonMode(daemonMode string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
mode := strings.ToLower(daemonMode)
switch mode {
case config.DaemonModeNone:
d.mode = fs.NoneInstance
d.mode = NoneInstance
case config.DaemonModeShared:
d.mode = fs.SharedInstance
d.mode = SharedInstance
case config.DaemonModePrefetch:
d.mode = fs.PrefetchInstance
d.mode = PrefetchInstance
case config.DaemonModeMultiple:
fallthrough
default:
d.mode = fs.MultiInstance
d.mode = MultiInstance
}
return nil
}
}

func WithDaemonBackend(daemonBackend string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
switch daemonBackend {
case config.DaemonBackendFscache:
d.daemonBackend = config.DaemonBackendFscache
Expand All @@ -121,7 +128,7 @@ func WithDaemonBackend(daemonBackend string) NewFSOpt {
}

func WithLogLevel(logLevel string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if logLevel == "" {
d.logLevel = config.DefaultLogLevel
} else {
Expand All @@ -132,7 +139,7 @@ func WithLogLevel(logLevel string) NewFSOpt {
}

func WithLogDir(dir string) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Errorf("failed to create logDir %s: %v", dir, err)
}
Expand All @@ -142,25 +149,34 @@ func WithLogDir(dir string) NewFSOpt {
}

func WithLogToStdout(logToStdout bool) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
d.logToStdout = logToStdout
return nil
}
}

func WithNydusdThreadNum(nydusdThreadNum int) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
d.nydusdThreadNum = nydusdThreadNum
return nil
}
}

func WithImageMode(cfg config.DaemonConfig) NewFSOpt {
return func(d *filesystem) error {
return func(d *Filesystem) error {
if cfg.Device.Backend.BackendType == "localfs" &&
len(cfg.Device.Backend.Config.Dir) != 0 {
d.imageMode = PreLoad
}
return nil
}
}

func WithEnableStargz(enable bool) NewFSOpt {
return func(d *Filesystem) error {
if enable {
d.stargzResolver = stargz.NewResolver()
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package nydus
package fs

import (
"testing"
Expand All @@ -14,7 +14,7 @@ import (
)

func TestWithNydusdBinaryPath(t *testing.T) {
var fs filesystem
var fs Filesystem
opt := WithNydusdBinaryPath("/bin/nydusd", config.DaemonModeMultiple)
err := opt(&fs)
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit a1cf218

Please sign in to comment.