|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package csi |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 25 | + "github.com/kubernetes-csi/csi-lib-utils/connection" |
| 26 | + csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc" |
| 27 | + "google.golang.org/grpc" |
| 28 | +) |
| 29 | + |
| 30 | +// Client is a gRPC client connect to remote CSI driver and abstracts all CSI calls. |
| 31 | +type Client interface { |
| 32 | + // GetDriverName returns driver name as discovered by GetPluginInfo() |
| 33 | + // gRPC call. |
| 34 | + GetDriverName(ctx context.Context) (string, error) |
| 35 | + |
| 36 | + // SupportsPluginControllerService return true if the CSI driver reports |
| 37 | + // CONTROLLER_SERVICE in GetPluginCapabilities() gRPC call. |
| 38 | + SupportsPluginControllerService(ctx context.Context) (bool, error) |
| 39 | + |
| 40 | + // SupportsControllerResize returns whether the CSI driver reports EXPAND_VOLUME |
| 41 | + // in ControllerGetCapabilities() gRPC call. |
| 42 | + SupportsControllerResize(ctx context.Context) (bool, error) |
| 43 | + |
| 44 | + // SupportsNodeResize returns whether the CSI driver reports EXPAND_VOLUME |
| 45 | + // in NodeGetCapabilities() gRPC call. |
| 46 | + SupportsNodeResize(ctx context.Context) (bool, error) |
| 47 | + |
| 48 | + // Expand expands the volume to a new size at least as big as requestBytes. |
| 49 | + // It returns the new size and whether the volume need expand operation on the node. |
| 50 | + Expand(ctx context.Context, volumeID string, requestBytes int64, secrets map[string]string) (int64, bool, error) |
| 51 | +} |
| 52 | + |
| 53 | +// New creates a new CSI client. |
| 54 | +func New(address string, timeout time.Duration) (Client, error) { |
| 55 | + conn, err := connection.Connect(address) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to connect to CSI driver: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + err = csirpc.ProbeForever(conn, timeout) |
| 61 | + if err != nil { |
| 62 | + return nil, fmt.Errorf("failed probing CSI driver: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + return &client{ |
| 66 | + conn: conn, |
| 67 | + nodeClient: csi.NewNodeClient(conn), |
| 68 | + ctrlClient: csi.NewControllerClient(conn), |
| 69 | + }, nil |
| 70 | +} |
| 71 | + |
| 72 | +type client struct { |
| 73 | + conn *grpc.ClientConn |
| 74 | + nodeClient csi.NodeClient |
| 75 | + ctrlClient csi.ControllerClient |
| 76 | +} |
| 77 | + |
| 78 | +func (c *client) GetDriverName(ctx context.Context) (string, error) { |
| 79 | + return csirpc.GetDriverName(ctx, c.conn) |
| 80 | +} |
| 81 | + |
| 82 | +func (c *client) SupportsPluginControllerService(ctx context.Context) (bool, error) { |
| 83 | + caps, err := csirpc.GetPluginCapabilities(ctx, c.conn) |
| 84 | + if err != nil { |
| 85 | + return false, fmt.Errorf("error getting controller capabilities: %v", err) |
| 86 | + } |
| 87 | + return caps[csi.PluginCapability_Service_CONTROLLER_SERVICE], nil |
| 88 | +} |
| 89 | + |
| 90 | +func (c *client) SupportsControllerResize(ctx context.Context) (bool, error) { |
| 91 | + caps, err := csirpc.GetControllerCapabilities(ctx, c.conn) |
| 92 | + if err != nil { |
| 93 | + return false, fmt.Errorf("error getting controller capabilities: %v", err) |
| 94 | + } |
| 95 | + return caps[csi.ControllerServiceCapability_RPC_EXPAND_VOLUME], nil |
| 96 | +} |
| 97 | + |
| 98 | +func (c *client) SupportsNodeResize(ctx context.Context) (bool, error) { |
| 99 | + rsp, err := c.nodeClient.NodeGetCapabilities(ctx, &csi.NodeGetCapabilitiesRequest{}) |
| 100 | + if err != nil { |
| 101 | + return false, fmt.Errorf("error getting node capabilities: %v", err) |
| 102 | + } |
| 103 | + for _, capacity := range rsp.GetCapabilities() { |
| 104 | + if capacity == nil { |
| 105 | + continue |
| 106 | + } |
| 107 | + rpc := capacity.GetRpc() |
| 108 | + if rpc == nil { |
| 109 | + continue |
| 110 | + } |
| 111 | + if rpc.GetType() == csi.NodeServiceCapability_RPC_EXPAND_VOLUME { |
| 112 | + return true, nil |
| 113 | + } |
| 114 | + } |
| 115 | + return false, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (c *client) Expand( |
| 119 | + ctx context.Context, |
| 120 | + volumeID string, |
| 121 | + requestBytes int64, |
| 122 | + secrets map[string]string) (int64, bool, error) { |
| 123 | + req := &csi.ControllerExpandVolumeRequest{ |
| 124 | + Secrets: secrets, |
| 125 | + VolumeId: volumeID, |
| 126 | + CapacityRange: &csi.CapacityRange{RequiredBytes: requestBytes}, |
| 127 | + } |
| 128 | + resp, err := c.ctrlClient.ControllerExpandVolume(ctx, req) |
| 129 | + if err != nil { |
| 130 | + return 0, false, err |
| 131 | + } |
| 132 | + return resp.CapacityBytes, resp.NodeExpansionRequired, nil |
| 133 | +} |
0 commit comments