Skip to content

Commit c9272a1

Browse files
committed
use trivial resizer if only node expansion supported
1 parent 95bd287 commit c9272a1

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

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/csi/client.go

+26
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ type Client interface {
4141
// in ControllerGetCapabilities() gRPC call.
4242
SupportsControllerResize(ctx context.Context) (bool, error)
4343

44+
// SupportsNodeResize returns whether the CSI driver reports EXPAND_VOLUME
45+
// in NodeGetCapabilities() gRPC call.
46+
SupportsNodeResize(ctx context.Context) (bool, error)
47+
4448
// Expand expands the volume to a new size at least as big as requestBytes.
4549
// It returns the new size and whether the volume need expand operation on the node.
4650
Expand(ctx context.Context, volumeID string, requestBytes int64, secrets map[string]string) (int64, bool, error)
@@ -60,12 +64,14 @@ func New(address string, timeout time.Duration) (Client, error) {
6064

6165
return &client{
6266
conn: conn,
67+
nodeClient: csi.NewNodeClient(conn),
6368
ctrlClient: csi.NewControllerClient(conn),
6469
}, nil
6570
}
6671

6772
type client struct {
6873
conn *grpc.ClientConn
74+
nodeClient csi.NodeClient
6975
ctrlClient csi.ControllerClient
7076
}
7177

@@ -89,6 +95,26 @@ func (c *client) SupportsControllerResize(ctx context.Context) (bool, error) {
8995
return caps[csi.ControllerServiceCapability_RPC_EXPAND_VOLUME], nil
9096
}
9197

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+
92118
func (c *client) Expand(
93119
ctx context.Context,
94120
volumeID string,

pkg/resizer/csi_resizer.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const (
4141
resizerSecretNamespaceKey = "csi.storage.k8s.io/resizer-secret-namespace"
4242
)
4343

44-
// NewCSIResizer creates a new resizer responsible for resizing CSI volumes.
45-
func NewCSIResizer(
44+
// NewResizer creates a new resizer responsible for resizing CSI volumes.
45+
func NewResizer(
4646
address string, timeout time.Duration,
4747
k8sClient kubernetes.Interface, informerFactory informers.SharedInformerFactory) (Resizer, error) {
4848
csiClient, err := csi.New(address, timeout)
@@ -55,22 +55,30 @@ func NewCSIResizer(
5555
return nil, fmt.Errorf("get driver name failed: %v", err)
5656
}
5757

58-
supports, err := supportsPluginControllerService(csiClient, timeout)
58+
supportControllerService, err := supportsPluginControllerService(csiClient, timeout)
5959
if err != nil {
6060
return nil, fmt.Errorf("failed to check if plugin supports controller service: %v", err)
6161
}
6262

63-
if !supports {
63+
if !supportControllerService {
6464
return nil, errors.New("CSI driver does not support controller service")
6565
}
6666

67-
supports, err = supportsControllerResize(csiClient, timeout)
67+
supportControllerResize, err := supportsControllerResize(csiClient, timeout)
6868
if err != nil {
6969
return nil, fmt.Errorf("failed to check if plugin supports controller resize: %v", err)
7070
}
7171

72-
if !supports {
73-
return nil, fmt.Errorf("CSI driver does not support controller resize")
72+
if !supportControllerResize {
73+
supportsNodeResize, err := supportsNodeResize(csiClient, timeout)
74+
if err != nil {
75+
return nil, fmt.Errorf("failed to check if plugin supports node resize: %v", err)
76+
}
77+
if supportsNodeResize {
78+
klog.Info("The CSI driver supports node resize only, using trivial resizer to handle resize requests")
79+
return newTrivialResizer(driverName), nil
80+
}
81+
return nil, fmt.Errorf("CSI driver neither supports controller resize nor node resize")
7482
}
7583

7684
return &csiResizer{
@@ -166,6 +174,12 @@ func supportsControllerResize(client csi.Client, timeout time.Duration) (bool, e
166174
return client.SupportsControllerResize(ctx)
167175
}
168176

177+
func supportsNodeResize(client csi.Client, timeout time.Duration) (bool, error) {
178+
ctx, cancel := timeoutCtx(timeout)
179+
defer cancel()
180+
return client.SupportsNodeResize(ctx)
181+
}
182+
169183
func timeoutCtx(timeout time.Duration) (context.Context, context.CancelFunc) {
170184
return context.WithTimeout(context.Background(), timeout)
171185
}

pkg/resizer/trivial_resizer.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ import (
2121
"k8s.io/apimachinery/pkg/api/resource"
2222
)
2323

24-
const exampleResizerName = "csi/example-resizer"
25-
2624
// newTrivialResizer returns a trivial resizer which will mark all pvs' resize process as finished.
27-
func newTrivialResizer() Resizer {
28-
return &trivialResizer{}
25+
func newTrivialResizer(name string) Resizer {
26+
return &trivialResizer{name: name}
2927
}
3028

31-
type trivialResizer struct{}
29+
type trivialResizer struct {
30+
name string
31+
}
3232

3333
func (r *trivialResizer) Name() string {
34-
return exampleResizerName
34+
return r.name
3535
}
3636

3737
func (r *trivialResizer) CanSupport(pv *v1.PersistentVolume) bool {
3838
return true
3939
}
4040

4141
func (r *trivialResizer) Resize(pv *v1.PersistentVolume, requestSize resource.Quantity) (newSize resource.Quantity, fsResizeRequired bool, err error) {
42-
return requestSize, false, nil
42+
return requestSize, true, nil
4343
}

0 commit comments

Comments
 (0)