Skip to content

Commit bb4fc00

Browse files
committed
hostpath driver implements the ControllerModityVolume feature
1 parent 60f5b16 commit bb4fc00

File tree

5 files changed

+416
-21
lines changed

5 files changed

+416
-21
lines changed

cmd/hostpathplugin/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func main() {
5858
flag.BoolVar(&cfg.EnableVolumeExpansion, "node-expand-required", true, "Enables volume expansion capability of the plugin(Deprecated). Please use enable-volume-expansion flag.")
5959

6060
flag.BoolVar(&cfg.EnableVolumeExpansion, "enable-volume-expansion", true, "Enables volume expansion feature.")
61+
flag.BoolVar(&cfg.EnableControllerModifyVolume, "enable-controller-modify-volume", false, "Enables Controller modify volume feature.")
62+
flag.Var(&cfg.AcceptedMutableParameterNames, "accepted-mutable-parameter-names", "Comma separated list of parameter names that can be modified on a persistent volume. This is only used when enable-controller-modify-volume is true. If unset, all parameters are mutable.")
6163
flag.BoolVar(&cfg.DisableControllerExpansion, "disable-controller-expansion", false, "Disables Controller volume expansion capability.")
6264
flag.BoolVar(&cfg.DisableNodeExpansion, "disable-node-expansion", false, "Disables Node volume expansion capability.")
6365
flag.Int64Var(&cfg.MaxVolumeExpansionSizeNode, "max-volume-size-node", 0, "Maximum allowed size of volume when expanded on the node. Defaults to same size as max-volume-size.")

pkg/hostpath/controllerserver.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"google.golang.org/grpc/status"
3434

3535
"github.com/container-storage-interface/spec/lib/go/csi"
36+
"k8s.io/apimachinery/pkg/util/sets"
3637
"k8s.io/klog/v2"
3738

3839
"github.com/kubernetes-csi/csi-driver-host-path/pkg/state"
@@ -48,6 +49,17 @@ func (hp *hostPath) CreateVolume(ctx context.Context, req *csi.CreateVolumeReque
4849
return nil, err
4950
}
5051

52+
if len(req.GetMutableParameters()) > 0 {
53+
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_MODIFY_VOLUME); err != nil {
54+
glog.V(3).Infof("invalid create volume req: %v", req)
55+
return nil, err
56+
}
57+
// Check if the mutable parameters are in the accepted list
58+
if err := hp.validateVolumeMutableParameters(req.MutableParameters); err != nil {
59+
return nil, err
60+
}
61+
}
62+
5163
// Check arguments
5264
if len(req.GetName()) == 0 {
5365
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
@@ -493,8 +505,36 @@ func (hp *hostPath) ControllerGetVolume(ctx context.Context, req *csi.Controller
493505
}, nil
494506
}
495507

496-
func (hp *hostPath) ControllerModifyVolume(ctx context.Context, request *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
497-
return nil, fmt.Errorf("unimplemented")
508+
func (hp *hostPath) ControllerModifyVolume(ctx context.Context, req *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
509+
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_MODIFY_VOLUME); err != nil {
510+
return nil, err
511+
}
512+
513+
// Check arguments
514+
if len(req.VolumeId) == 0 {
515+
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
516+
}
517+
if len(req.MutableParameters) == 0 {
518+
return nil, status.Error(codes.InvalidArgument, "Mutable parameters cannot be empty")
519+
}
520+
521+
// Check if the mutable parameters are in the accepted list
522+
if err := hp.validateVolumeMutableParameters(req.MutableParameters); err != nil {
523+
return nil, err
524+
}
525+
526+
// Lock before acting on global state. A production-quality
527+
// driver might use more fine-grained locking.
528+
hp.mutex.Lock()
529+
defer hp.mutex.Unlock()
530+
531+
// Get the volume
532+
_, err := hp.state.GetVolumeByID(req.VolumeId)
533+
if err != nil {
534+
return nil, status.Error(codes.NotFound, err.Error())
535+
}
536+
537+
return &csi.ControllerModifyVolumeResponse{}, nil
498538
}
499539

500540
// CreateSnapshot uses tar command to create snapshot for hostpath volume. The tar command can quickly create
@@ -782,6 +822,25 @@ func convertSnapshot(snap state.Snapshot) *csi.ListSnapshotsResponse {
782822
return rsp
783823
}
784824

825+
// validateVolumeMutableParameters is a helper function to check if the mutable parameters are in the accepted list
826+
func (hp *hostPath) validateVolumeMutableParameters(params map[string]string) error {
827+
if len(hp.config.AcceptedMutableParameterNames) == 0 {
828+
return nil
829+
}
830+
831+
accepts := sets.New(hp.config.AcceptedMutableParameterNames...)
832+
unsupported := []string{}
833+
for k := range params {
834+
if !accepts.Has(k) {
835+
unsupported = append(unsupported, k)
836+
}
837+
}
838+
if len(unsupported) > 0 {
839+
return status.Errorf(codes.InvalidArgument, "invalid parameters: %v", unsupported)
840+
}
841+
return nil
842+
}
843+
785844
func (hp *hostPath) validateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
786845
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
787846
return nil
@@ -815,6 +874,9 @@ func (hp *hostPath) getControllerServiceCapabilities() []*csi.ControllerServiceC
815874
if hp.config.EnableAttach {
816875
cl = append(cl, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME)
817876
}
877+
if hp.config.EnableControllerModifyVolume {
878+
cl = append(cl, csi.ControllerServiceCapability_RPC_MODIFY_VOLUME)
879+
}
818880
}
819881

820882
var csc []*csi.ControllerServiceCapability

0 commit comments

Comments
 (0)