-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
154 lines (132 loc) · 4.11 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
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 gceGCEDriver
import (
"errors"
"fmt"
"context"
csi "github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc"
"k8s.io/klog"
)
var ProbeCSIFullMethod = "/csi.v1.Identity/Probe"
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) {
if info.FullMethod == ProbeCSIFullMethod {
return handler(ctx, req)
}
klog.V(4).Infof("%s called with request: %+v", info.FullMethod, req)
resp, err := handler(ctx, req)
if err != nil {
klog.Errorf("%s returned with error: %v", info.FullMethod, err)
} else {
klog.V(4).Infof("%s returned with response: %+v", info.FullMethod, resp)
}
return resp, err
}
func validateVolumeCapabilities(vcs []*csi.VolumeCapability) error {
isMnt := false
isBlk := false
if vcs == nil {
return errors.New("volume capabilities is nil")
}
for _, vc := range vcs {
if err := validateVolumeCapability(vc); err != nil {
return err
}
if blk := vc.GetBlock(); blk != nil {
isBlk = true
}
if mnt := vc.GetMount(); mnt != nil {
isMnt = true
}
}
if isBlk && isMnt {
return errors.New("both mount and block volume capabilities specified")
}
return nil
}
func validateVolumeCapability(vc *csi.VolumeCapability) error {
if err := validateAccessMode(vc.GetAccessMode()); err != nil {
return err
}
blk := vc.GetBlock()
mnt := vc.GetMount()
mod := vc.GetAccessMode().GetMode()
if mnt == nil && blk == nil {
return errors.New("must specify an access type")
}
if mnt != nil && blk != nil {
return errors.New("specified both mount and block access types")
}
if mnt != nil && mod == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER {
return errors.New("specified multi writer with mount access type")
}
return nil
}
func validateAccessMode(am *csi.VolumeCapability_AccessMode) error {
if am == nil {
return errors.New("access mode is nil")
}
switch am.GetMode() {
case csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER:
case csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY:
case csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY:
case csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER:
default:
return fmt.Errorf("%v access mode is not supported for for PD", am.GetMode())
}
return nil
}
func getMultiWriterFromCapability(vc *csi.VolumeCapability) (bool, error) {
if vc.GetAccessMode() == nil {
return false, errors.New("access mode is nil")
}
mode := vc.GetAccessMode().GetMode()
return (mode == csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER), nil
}
func getMultiWriterFromCapabilities(vcs []*csi.VolumeCapability) (bool, error) {
if vcs == nil {
return false, errors.New("volume capabilities is nil")
}
for _, vc := range vcs {
multiWriter, err := getMultiWriterFromCapability(vc)
if err != nil {
return false, err
}
if multiWriter {
return true, nil
}
}
return false, nil
}