/
utils.go
79 lines (67 loc) · 2.38 KB
/
utils.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
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package driver
import (
"fmt"
"net"
csi "github.com/container-storage-interface/spec/lib/go/csi"
pbSanitizer "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"golang.org/x/net/context"
"google.golang.org/grpc"
"k8s.io/klog/v2"
)
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
return &csi.VolumeCapability_AccessMode{Mode: mode}
}
func NewControllerServiceCapability(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
}
}
func NewNodeServiceCapability(cap csi.NodeServiceCapability_RPC_Type) *csi.NodeServiceCapability {
return &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: cap,
},
},
}
}
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
klog.V(3).Infof("GRPC call: %s", info.FullMethod)
klog.V(5).Infof("GRPC request: %+v", pbSanitizer.StripSecretsCSI03(req).String())
resp, err := handler(ctx, req)
if err != nil {
klog.Errorf("GRPC error: %v", err.Error())
} else {
klog.V(5).Infof("GRPC response: %+v", resp)
}
return resp, err
}
// IsIpWithinRange checks if an ip address is within the given ip range.
func IsIpWithinRange(ipAddress, ipRange string) (bool, error) {
_, ipnet, err := net.ParseCIDR(ipRange)
if err != nil {
return false, fmt.Errorf("failed to parse cidr range %s: %w", ipRange, err)
}
return ipnet.Contains(net.ParseIP(ipAddress)), nil
}
// IsCIDR verifies if the given ip range is a valid CIDR value.
func IsCIDR(ipRange string) bool {
_, _, err := net.ParseCIDR(ipRange)
return err == nil
}