Skip to content

Commit 5e2e086

Browse files
committed
Support for configuring volume attachable limit on a node.
1 parent 184a6a7 commit 5e2e086

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

cmd/hostpathplugin/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func main() {
5151
flag.BoolVar(&cfg.EnableAttach, "enable-attach", false, "Enables RPC_PUBLISH_UNPUBLISH_VOLUME capability.")
5252
flag.BoolVar(&cfg.EnableTopology, "enable-topology", true, "Enables PluginCapability_Service_VOLUME_ACCESSIBILITY_CONSTRAINTS capability.")
5353
flag.BoolVar(&cfg.EnableVolumeExpansion, "node-expand-required", true, "Enables NodeServiceCapability_RPC_EXPAND_VOLUME capacity.")
54+
flag.Int64Var(&cfg.AttachLimit, "attach-limit", 0, "Maximum number of attachable volumes on a node. Zero refers to no limit.")
5455
showVersion := flag.Bool("version", false, "Show version.")
5556
// The proxy-endpoint option is intended to used by the Kubernetes E2E test suite
5657
// for proxying incoming calls to the embedded mock CSI driver.

pkg/hostpath/controllerserver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ func (hp *hostPath) ControllerPublishVolume(ctx context.Context, req *csi.Contro
304304
}, nil
305305
}
306306

307+
// Check attach limit before publishing.
308+
if hp.config.AttachLimit > 0 && hp.getAttachCount() >= hp.config.AttachLimit {
309+
return nil, status.Errorf(codes.ResourceExhausted, "Cannot attach any more volumes to this node ('%s')", hp.config.NodeID)
310+
}
311+
307312
vol.IsAttached = true
308313
vol.ReadOnlyAttach = req.GetReadonly()
309314
if err := hp.updateVolume(vol.VolID, vol); err != nil {

pkg/hostpath/hostpath.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type Config struct {
9696
NodeID string
9797
VendorVersion string
9898
MaxVolumesPerNode int64
99+
AttachLimit int64
99100
Capacity Capacity
100101
Ephemeral bool
101102
ShowVersion bool
@@ -503,6 +504,16 @@ func (hp *hostPath) getSortedVolumeIDs() []string {
503504
return ids
504505
}
505506

507+
func (hp *hostPath) getAttachCount() int64 {
508+
count := int64(0)
509+
for _, vol := range hp.volumes {
510+
if vol.IsAttached {
511+
count++
512+
}
513+
}
514+
return count
515+
}
516+
506517
func filterVolumeName(targetPath string) string {
507518
pathItems := strings.Split(targetPath, "kubernetes.io~csi/")
508519
if len(pathItems) < 2 {

pkg/hostpath/nodeserver.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ func (hp *hostPath) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest
313313
}
314314
}
315315

316+
if hp.config.AttachLimit > 0 {
317+
resp.MaxVolumesPerNode = hp.config.AttachLimit
318+
}
319+
316320
return resp, nil
317321
}
318322

0 commit comments

Comments
 (0)