/
interceptor.go
48 lines (40 loc) · 1.3 KB
/
interceptor.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
package sched
import (
"context"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// FilterInterceptor is a wrapper for the filter
// to be used an interceptor
type FilterInterceptor struct {
Filter
}
// SchedUnaryInterceptor calls the filter function based on the req
func (fi *FilterInterceptor) SchedUnaryInterceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
var err error
switch req.(type) {
case *csi.CreateVolumeRequest:
csiReq := req.(*csi.CreateVolumeRequest)
req, err = fi.Filter.PreVolumeCreate(csiReq)
if err != nil {
logrus.WithContext(ctx).Errorf("CSI pre-create filter failed: %v", err)
// Return an aborted code to retry from the csi-provisioner.
// We cannot ignore this error or else a volume will be created w/
// incorrect locator.VolumeLabels.
return nil, status.Error(codes.Aborted, "pre-create filter failed: %v")
} else {
logrus.WithContext(ctx).Tracef("K8s-CSI filter: Filter applied successfully for request %T", req)
}
default:
logrus.WithContext(ctx).Tracef("K8s-CSI filter: Ignoring filter for this request: %T", req)
}
return handler(ctx, req)
}