Skip to content

Commit e339d21

Browse files
authored
Merge pull request #26 from mlmhl/node_expand_only
Use trivial resizer if only node expansion supported
2 parents f34fcae + 289e556 commit e339d21

File tree

186 files changed

+16118
-109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+16118
-109
lines changed

Gopkg.lock

+46-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/csi-resizer/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func main() {
8080

8181
informerFactory := informers.NewSharedInformerFactory(kubeClient, *resyncPeriod)
8282

83-
csiResizer, err := resizer.NewCSIResizer(*csiAddress, *csiTimeout, kubeClient, informerFactory)
83+
csiResizer, err := resizer.NewResizer(*csiAddress, *csiTimeout, kubeClient, informerFactory)
8484
if err != nil {
8585
klog.Fatal(err.Error())
8686
}

pkg/client/client.go

-59
This file was deleted.

pkg/csi/client.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

pkg/csi/mock_client.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package csi
2+
3+
import "context"
4+
5+
func NewMockClient(
6+
supportsNodeResize bool,
7+
supportsControllerResize bool,
8+
supportsPluginControllerService bool) *MockClient {
9+
return &MockClient{
10+
name: "mock",
11+
supportsNodeResize: supportsNodeResize,
12+
supportsControllerResize: supportsControllerResize,
13+
supportsPluginControllerService: supportsPluginControllerService,
14+
}
15+
}
16+
17+
type MockClient struct {
18+
name string
19+
supportsNodeResize bool
20+
supportsControllerResize bool
21+
supportsPluginControllerService bool
22+
}
23+
24+
func (c *MockClient) GetDriverName(context.Context) (string, error) {
25+
return c.name, nil
26+
}
27+
28+
func (c *MockClient) SupportsPluginControllerService(context.Context) (bool, error) {
29+
return c.supportsPluginControllerService, nil
30+
}
31+
32+
func (c *MockClient) SupportsControllerResize(context.Context) (bool, error) {
33+
return c.supportsControllerResize, nil
34+
}
35+
36+
func (c *MockClient) SupportsNodeResize(context.Context) (bool, error) {
37+
return c.supportsNodeResize, nil
38+
}
39+
40+
func (c *MockClient) Expand(
41+
ctx context.Context,
42+
volumeID string,
43+
requestBytes int64,
44+
secrets map[string]string) (int64, bool, error) {
45+
// TODO: Determine whether the operation succeeds or fails by parameters.
46+
return requestBytes, c.supportsNodeResize, nil
47+
}

0 commit comments

Comments
 (0)