forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
85 lines (68 loc) · 1.92 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2019 Huawei Corporation
// Copyright (c) 2020 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package persist
import (
"fmt"
exp "github.com/kata-containers/runtime/virtcontainers/experimental"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/kata-containers/runtime/virtcontainers/persist/fs"
"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
)
type initFunc (func() (persistapi.PersistDriver, error))
const (
RootFSName = "fs"
RootlessFSName = "rootlessfs"
)
var (
// NewStoreFeature is an experimental feature
NewStoreFeature = exp.Feature{
Name: "newstore",
Description: "This is a new storage driver which reorganized disk data structures, it has to be an experimental feature since it breaks backward compatibility.",
ExpRelease: "2.0",
}
expErr error
supportedDrivers = map[string]initFunc{
RootFSName: fs.Init,
RootlessFSName: fs.RootlessInit,
}
mockTesting = false
)
func init() {
expErr = exp.Register(NewStoreFeature)
}
func EnableMockTesting() {
mockTesting = true
}
// GetDriver returns new PersistDriver according to driver name
func GetDriverByName(name string) (persistapi.PersistDriver, error) {
if expErr != nil {
return nil, expErr
}
if f, ok := supportedDrivers[name]; ok {
return f()
}
return nil, fmt.Errorf("failed to get storage driver %q", name)
}
// GetDriver returns new PersistDriver according to current needs.
// For example, a rootless FS driver is returned if the process is running
// as unprivileged process.
func GetDriver() (persistapi.PersistDriver, error) {
if expErr != nil {
return nil, expErr
}
if mockTesting {
return fs.MockFSInit()
}
if rootless.IsRootless() {
if f, ok := supportedDrivers[RootlessFSName]; ok {
return f()
}
}
if f, ok := supportedDrivers[RootFSName]; ok {
return f()
}
return nil, fmt.Errorf("Could not find a FS driver")
}