@@ -2,7 +2,9 @@ package mounter
22
33import (
44 "context"
5+ "fmt"
56
7+ "k8s.io/klog/v2"
68 mountutils "k8s.io/mount-utils"
79)
810
@@ -16,13 +18,17 @@ type MountOperation struct {
1618 Target string
1719 FsType string
1820 Options []string
21+ Args []string
1922 Secrets map [string ]string
2023 MetricsPath string
2124 VolumeID string
25+
26+ MountResult any
2227}
2328
2429type MountInterceptor interface {
25- BeforeMount (op * MountOperation ) (* MountOperation , error )
30+ BeforeMount (op * MountOperation , err error ) (* MountOperation , error )
31+ AfterMount (op * MountOperation , err error ) error
2632}
2733
2834type MountWorkflow struct {
@@ -32,13 +38,29 @@ type MountWorkflow struct {
3238
3339var _ Mounter = & MountWorkflow {}
3440
35- func (w * MountWorkflow ) ExtendedMount (ctx context.Context , op * MountOperation ) (err error ) {
41+ func (w * MountWorkflow ) ExtendedMount (ctx context.Context , op * MountOperation ) error {
42+ var err error
43+ for i , interceptor := range w .interceptors {
44+ if op , err = interceptor .BeforeMount (op , err ); err != nil && i != len (w .interceptors )- 1 {
45+ // Log error but continue to the next interceptor, since some interceptors may
46+ // want to handle the error, e.g. the OssMonitorInterceptor.
47+ // Only log for the intermediate interceptors, otherwise the final error will be printed twice.
48+ klog .ErrorS (err , "error executing BeforeMount interceptor" )
49+ }
50+ }
51+ if err != nil {
52+ return fmt .Errorf ("error executing BeforeMount interceptor: %w" , err )
53+ }
54+
55+ err = w .Mounter .ExtendedMount (ctx , op )
3656 for _ , interceptor := range w .interceptors {
37- if op , err = interceptor .BeforeMount (op ); err != nil {
38- return err
57+ if afterErr := interceptor .AfterMount (op , err ); afterErr != nil {
58+ // Log error but continue to the next interceptor.
59+ // Do not override the original error from mount operation.
60+ klog .ErrorS (afterErr , "error executing AfterMount interceptor" )
3961 }
4062 }
41- return w . Mounter . ExtendedMount ( ctx , op )
63+ return err
4264}
4365
4466func NewForMounter (m Mounter , interceptors ... MountInterceptor ) Mounter {
0 commit comments