Skip to content

Commit f66e17d

Browse files
committed
refactor: split non-mounting actions into interceptors
1 parent be083ec commit f66e17d

File tree

11 files changed

+203
-94
lines changed

11 files changed

+203
-94
lines changed

pkg/mounter/cmd_mounter.go

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,32 @@ import (
77
"os/exec"
88
"time"
99

10-
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
1110
"k8s.io/mount-utils"
1211
)
1312

1413
const timeout = time.Second * 10
1514

1615
type OssCmdMounter struct {
17-
mount.Interface
18-
volumeId string
1916
execPath string
17+
volumeID string
18+
mount.Interface
2019
}
2120

22-
func NewOssCmdMounter(execPath, volumeId string, inner mount.Interface) Mounter {
21+
var _ Mounter = &OssCmdMounter{}
22+
23+
func NewOssCmdMounter(execPath, volumeID string, inner mount.Interface) Mounter {
2324
return &OssCmdMounter{
2425
execPath: execPath,
25-
volumeId: volumeId,
26+
volumeID: volumeID,
2627
Interface: inner,
2728
}
2829
}
2930

30-
func (m *OssCmdMounter) ExtendedMount(source, target, fstype string, options []string, params *ExtendedMountParams) error {
31+
func (m *OssCmdMounter) ExtendedMount(_ context.Context, op *MountOperation) error {
3132
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(timeout))
3233
defer cancel()
3334

34-
// Parameters in ExtendedMountParams are optional
35-
if params == nil {
36-
params = &ExtendedMountParams{}
37-
}
38-
passwd, err := utils.SaveOssSecretsToFile(params.Secrets, fstype)
39-
if err != nil {
40-
return err
41-
}
42-
43-
args := getArgs(source, target, fstype, passwd, options)
44-
cmd := exec.CommandContext(ctx, m.execPath, args...)
35+
cmd := exec.CommandContext(ctx, m.execPath, getArgs(op)...)
4536
cmd.Stdout = os.Stdout
4637
cmd.Stderr = os.Stderr
4738

@@ -51,20 +42,20 @@ func (m *OssCmdMounter) ExtendedMount(source, target, fstype string, options []s
5142
return nil
5243
}
5344

54-
func getArgs(source, target, fstype, passwdFile string, options []string) []string {
55-
if fstype == "ossfs" {
56-
if passwdFile != "" {
57-
options = append(options, "passwd_file="+passwdFile)
58-
}
59-
return mount.MakeMountArgs(source, target, "", options)
45+
func getArgs(op *MountOperation) []string {
46+
if op == nil {
47+
return nil
6048
}
61-
// ossfs2
62-
args := []string{"mount", target}
63-
if passwdFile != "" {
64-
args = append(args, []string{"-c", passwdFile}...)
65-
}
66-
for _, o := range options {
67-
args = append(args, fmt.Sprintf("--%s", o))
49+
switch op.FsType {
50+
case "ossfs":
51+
return mount.MakeMountArgs(op.Source, op.Target, "", op.Options)
52+
case "ossfs2":
53+
args := []string{"mount", op.Target}
54+
for _, o := range op.Options {
55+
args = append(args, fmt.Sprintf("--%s", o))
56+
}
57+
return args
58+
default:
59+
return nil
6860
}
69-
return args
7061
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package interceptors
2+
3+
import (
4+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
5+
)
6+
7+
type AlinasSecretInterceptor struct{}
8+
9+
var _ mounter.MountInterceptor = AlinasSecretInterceptor{}
10+
11+
func NewAlinasSecretInterceptor() mounter.MountInterceptor {
12+
return AlinasSecretInterceptor{}
13+
}
14+
15+
func (AlinasSecretInterceptor) BeforeMount(req *mounter.MountOperation) (*mounter.MountOperation, error) {
16+
return req, nil
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package interceptors
2+
3+
import (
4+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
5+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
6+
)
7+
8+
type OssfsSecretInterceptor struct {
9+
credFileKey string
10+
}
11+
12+
var _ mounter.MountInterceptor = OssfsSecretInterceptor{}
13+
14+
func NewOssfsSecretInterceptor() mounter.MountInterceptor {
15+
return OssfsSecretInterceptor{
16+
credFileKey: "passwd_file",
17+
}
18+
}
19+
20+
func NewOssfs2SecretInterceptor() mounter.MountInterceptor {
21+
return OssfsSecretInterceptor{
22+
credFileKey: "-c",
23+
}
24+
}
25+
26+
func (i OssfsSecretInterceptor) BeforeMount(req *mounter.MountOperation) (*mounter.MountOperation, error) {
27+
filePath, err := utils.SaveOssSecretsToFile(req.Secrets, req.FsType)
28+
if err != nil {
29+
return req, err
30+
}
31+
if filePath != "" {
32+
req.Options = append(req.Options, i.credFileKey+"="+filePath)
33+
}
34+
return req, nil
35+
}

pkg/mounter/mounter.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,49 @@
11
package mounter
22

33
import (
4+
"context"
5+
46
mountutils "k8s.io/mount-utils"
57
)
68

7-
type ExtendedMountParams struct {
9+
type Mounter interface {
10+
mountutils.Interface
11+
ExtendedMount(ctx context.Context, op *MountOperation) error
12+
}
13+
14+
type MountOperation struct {
15+
Source string
16+
Target string
17+
FsType string
18+
Options []string
819
Secrets map[string]string
920
MetricsPath string
21+
VolumeID string
1022
}
1123

12-
type Mounter interface {
13-
mountutils.Interface
14-
ExtendedMount(source, target, fstype string, options []string, params *ExtendedMountParams) error
24+
type MountInterceptor interface {
25+
BeforeMount(op *MountOperation) (*MountOperation, error)
26+
}
27+
28+
type MountWorkflow struct {
29+
Mounter
30+
interceptors []MountInterceptor
31+
}
32+
33+
var _ Mounter = &MountWorkflow{}
34+
35+
func (w *MountWorkflow) ExtendedMount(ctx context.Context, op *MountOperation) (err error) {
36+
for _, interceptor := range w.interceptors {
37+
if op, err = interceptor.BeforeMount(op); err != nil {
38+
return err
39+
}
40+
}
41+
return w.Mounter.ExtendedMount(ctx, op)
42+
}
43+
44+
func NewForMounter(m Mounter, interceptors ...MountInterceptor) Mounter {
45+
return &MountWorkflow{
46+
Mounter: m,
47+
interceptors: interceptors,
48+
}
1549
}

pkg/mounter/proxy/protocol.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ type MountRequest struct {
4343
MountFlags []string `json:"mountFlags,omitempty"`
4444
Secrets map[string]string `json:"secrets,omitempty"`
4545
MetricsPath string `json:"metricsPath,omitempty"`
46+
VolumeID string `json:"volumeID,omitempty"`
4647
}

pkg/mounter/proxy/server/alinas/driver.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"syscall"
1212
"time"
1313

14+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
15+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/interceptors"
1416
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/proxy"
1517
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/proxy/server"
16-
mounter "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
18+
mounterutils "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
1719
"k8s.io/apimachinery/pkg/util/wait"
1820
"k8s.io/klog/v2"
1921
"k8s.io/mount-utils"
@@ -25,11 +27,16 @@ const (
2527
)
2628

2729
func init() {
28-
server.RegisterDriver(&Driver{mounter: mount.New("")})
30+
server.RegisterDriver(&Driver{
31+
Mounter: mounter.NewForMounter(
32+
&extendedMounter{Interface: mount.New("")},
33+
interceptors.NewAlinasSecretInterceptor(),
34+
),
35+
})
2936
}
3037

3138
type Driver struct {
32-
mounter mount.Interface
39+
mounter.Mounter
3340
}
3441

3542
func (h *Driver) Name() string {
@@ -41,15 +48,15 @@ func (h *Driver) Fstypes() []string {
4148
}
4249

4350
func (h *Driver) Mount(ctx context.Context, req *proxy.MountRequest) error {
44-
klog.InfoS("Mounting", "fstype", req.Fstype, "source", req.Source, "target", req.Target, "options", req.Options)
45-
options := append(req.Options, "no_start_watchdog")
46-
if req.Fstype == fstypeAlinas {
47-
// options = append(options, "no_atomic_move", "auto_fallback_nfs")
48-
options = append(options, "no_atomic_move")
49-
options = addAutoFallbackNFSMountOptions(options)
50-
}
51-
52-
return h.mounter.Mount(req.Source, req.Target, req.Fstype, options)
51+
return h.ExtendedMount(ctx, &mounter.MountOperation{
52+
Source: req.Source,
53+
Target: req.Target,
54+
FsType: req.Fstype,
55+
Options: req.Options,
56+
Secrets: req.Secrets,
57+
MetricsPath: req.MetricsPath,
58+
VolumeID: req.VolumeID,
59+
})
5360
}
5461

5562
func (h *Driver) Init() {
@@ -79,7 +86,7 @@ func addAutoFallbackNFSMountOptions(mountOptions []string) []string {
7986
isEFC := false
8087
isVSC := false
8188
for _, options := range mountOptions {
82-
for _, option := range mounter.SplitMountOptions(options) {
89+
for _, option := range mounterutils.SplitMountOptions(options) {
8390
if option == "" {
8491
continue
8592
}
@@ -158,3 +165,20 @@ func copyFile(src, dst string) error {
158165

159166
return dstFile.Sync()
160167
}
168+
169+
type extendedMounter struct {
170+
mount.Interface
171+
}
172+
173+
var _ mounter.Mounter = &extendedMounter{}
174+
175+
func (m *extendedMounter) ExtendedMount(ctx context.Context, op *mounter.MountOperation) error {
176+
klog.InfoS("Mounting", "fstype", op.FsType, "source", op.Source, "target", op.Target, "options", op.Options)
177+
op.Options = append(op.Options, "no_start_watchdog")
178+
if op.FsType == fstypeAlinas {
179+
// options = append(options, "no_atomic_move", "auto_fallback_nfs")
180+
op.Options = append(op.Options, "no_atomic_move")
181+
op.Options = addAutoFallbackNFSMountOptions(op.Options)
182+
}
183+
return m.Mount(op.Source, op.Target, op.FsType, op.Options)
184+
}

pkg/mounter/proxy_mounter.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mounter
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67

@@ -14,27 +15,25 @@ type ProxyMounter struct {
1415
mountutils.Interface
1516
}
1617

18+
var _ Mounter = &ProxyMounter{}
19+
1720
func NewProxyMounter(socketPath string, inner mountutils.Interface) Mounter {
1821
return &ProxyMounter{
1922
socketPath: socketPath,
2023
Interface: inner,
2124
}
2225
}
2326

24-
func (m *ProxyMounter) ExtendedMount(source, target, fstype string, options []string, params *ExtendedMountParams) error {
25-
// Parameters in ExtendedMountParams are optional
26-
if params == nil {
27-
params = &ExtendedMountParams{}
28-
}
29-
27+
func (m *ProxyMounter) ExtendedMount(ctx context.Context, op *MountOperation) error {
3028
dclient := client.NewClient(m.socketPath)
3129
resp, err := dclient.Mount(&proxy.MountRequest{
32-
Source: source,
33-
Target: target,
34-
Fstype: fstype,
35-
Options: options,
36-
Secrets: params.Secrets,
37-
MetricsPath: params.MetricsPath,
30+
Source: op.Source,
31+
Target: op.Target,
32+
Fstype: op.FsType,
33+
Options: op.Options,
34+
Secrets: op.Secrets,
35+
MetricsPath: op.MetricsPath,
36+
VolumeID: op.VolumeID,
3837
})
3938
if err != nil {
4039
return fmt.Errorf("call mounter daemon: %w", err)
@@ -43,7 +42,7 @@ func (m *ProxyMounter) ExtendedMount(source, target, fstype string, options []st
4342
if err != nil {
4443
return fmt.Errorf("failed to mount: %w", err)
4544
}
46-
notMnt, err := m.IsLikelyNotMountPoint(target)
45+
notMnt, err := m.IsLikelyNotMountPoint(op.Target)
4746
if err != nil {
4847
return err
4948
}
@@ -54,5 +53,10 @@ func (m *ProxyMounter) ExtendedMount(source, target, fstype string, options []st
5453
}
5554

5655
func (m *ProxyMounter) Mount(source string, target string, fstype string, options []string) error {
57-
return m.ExtendedMount(source, target, fstype, options, nil)
56+
return m.ExtendedMount(context.Background(), &MountOperation{
57+
Source: source,
58+
Target: target,
59+
FsType: fstype,
60+
Options: options,
61+
})
5862
}

pkg/nas/mounter.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@ func (m *NasMounter) Mount(source string, target string, fstype string, options
3232
return err
3333
}
3434

35-
func newNasMounter(agentMode bool) mountutils.Interface {
35+
func newNasMounter(agentMode bool, socketPath string) mountutils.Interface {
3636
inner := mountutils.NewWithoutSystemd("")
3737
m := &NasMounter{
3838
Interface: inner,
3939
alinasMounter: inner,
4040
}
41-
if !agentMode {
41+
switch {
42+
case socketPath != "":
43+
m.alinasMounter = mounter.NewProxyMounter(socketPath, inner)
44+
case !agentMode: // normal case, use connector mounter to ensure backward compatability
4245
m.alinasMounter = mounter.NewConnectorMounter(inner, "")
4346
}
4447
return m
4548
}
46-
47-
func newNasMounterWithProxy(socketPath string) mountutils.Interface {
48-
inner := mountutils.NewWithoutSystemd("")
49-
return &NasMounter{
50-
Interface: inner,
51-
alinasMounter: mounter.NewProxyMounter(socketPath, inner),
52-
}
53-
}

pkg/nas/mounter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (m *errorMockMounter) Mount(source string, target string, fstype string, op
2525
}
2626

2727
func TestNewNasMounter(t *testing.T) {
28-
actual := newNasMounter(true)
28+
actual := newNasMounter(true, "")
2929
assert.NotNil(t, actual)
3030
}
3131

0 commit comments

Comments
 (0)