Skip to content

Commit 001a375

Browse files
committed
Add other common functions
1 parent bd468a0 commit 001a375

File tree

2 files changed

+384
-20
lines changed

2 files changed

+384
-20
lines changed

connection/connection.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func LogGRPC(ctx context.Context, method string, req, reply interface{}, cc *grp
163163
return err
164164
}
165165

166+
// GetDriverName returns name of CSI driver.
166167
func GetDriverName(ctx context.Context, conn *grpc.ClientConn) (string, error) {
167168
client := csi.NewIdentityClient(conn)
168169

@@ -177,3 +178,68 @@ func GetDriverName(ctx context.Context, conn *grpc.ClientConn) (string, error) {
177178
}
178179
return name, nil
179180
}
181+
182+
// PluginCapabilitySet is set of CSI plugin capabilities. Only supported capabilities are in the map.
183+
type PluginCapabilitySet map[csi.PluginCapability_Service_Type]bool
184+
185+
// GetPluginCapabilities returns set of supported capabilities of CSI driver.
186+
func GetPluginCapabilities(ctx context.Context, conn *grpc.ClientConn) (PluginCapabilitySet, error) {
187+
client := csi.NewIdentityClient(conn)
188+
req := csi.GetPluginCapabilitiesRequest{}
189+
rsp, err := client.GetPluginCapabilities(ctx, &req)
190+
if err != nil {
191+
return nil, err
192+
}
193+
caps := PluginCapabilitySet{}
194+
for _, cap := range rsp.GetCapabilities() {
195+
if cap == nil {
196+
continue
197+
}
198+
srv := cap.GetService()
199+
if srv == nil {
200+
continue
201+
}
202+
t := srv.GetType()
203+
caps[t] = true
204+
}
205+
return caps, nil
206+
}
207+
208+
// ControllerCapabilitySet is set of CSI controller capabilities. Only supported capabilities are in the map.
209+
type ControllerCapabilitySet map[csi.ControllerServiceCapability_RPC_Type]bool
210+
211+
// GetControllerCapabilities returns set of supported controller capabilities of CSI driver.
212+
func GetControllerCapabilities(ctx context.Context, conn *grpc.ClientConn) (ControllerCapabilitySet, error) {
213+
client := csi.NewControllerClient(conn)
214+
req := csi.ControllerGetCapabilitiesRequest{}
215+
rsp, err := client.ControllerGetCapabilities(ctx, &req)
216+
if err != nil {
217+
return nil, err
218+
}
219+
220+
caps := ControllerCapabilitySet{}
221+
for _, cap := range rsp.GetCapabilities() {
222+
if cap == nil {
223+
continue
224+
}
225+
rpc := cap.GetRpc()
226+
if rpc == nil {
227+
continue
228+
}
229+
t := rpc.GetType()
230+
caps[t] = true
231+
}
232+
return caps, nil
233+
}
234+
235+
// Probe calls Probe() of a CSI driver and waits until the driver becomes ready.
236+
// TODO: wait until the driver is ready? It may require timeout instead of context as a parameter.
237+
func Probe(ctx context.Context, conn *grpc.ClientConn) error {
238+
client := csi.NewIdentityClient(conn)
239+
req := csi.ProbeRequest{}
240+
_, err := client.Probe(ctx, &req)
241+
if err != nil {
242+
return err
243+
}
244+
return nil
245+
}

0 commit comments

Comments
 (0)