Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hostpath driver implements the ControllerModityVolume feature #481

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/hostpathplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func main() {
flag.BoolVar(&cfg.EnableVolumeExpansion, "node-expand-required", true, "Enables volume expansion capability of the plugin(Deprecated). Please use enable-volume-expansion flag.")

flag.BoolVar(&cfg.EnableVolumeExpansion, "enable-volume-expansion", true, "Enables volume expansion feature.")
flag.BoolVar(&cfg.EnableControllerModifyVolume, "enable-controller-modify-volume", false, "Enables Controller modify volume feature.")
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.")
flag.BoolVar(&cfg.DisableControllerExpansion, "disable-controller-expansion", false, "Disables Controller volume expansion capability.")
flag.BoolVar(&cfg.DisableNodeExpansion, "disable-node-expansion", false, "Disables Node volume expansion capability.")
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.")
Expand Down
66 changes: 64 additions & 2 deletions pkg/hostpath/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"google.golang.org/grpc/status"

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

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

if len(req.GetMutableParameters()) > 0 {
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_MODIFY_VOLUME); err != nil {
glog.V(3).Infof("invalid create volume req: %v", req)
return nil, err
}
// Check if the mutable parameters are in the accepted list
if err := hp.validateVolumeMutableParameters(req.MutableParameters); err != nil {
return nil, err
}
}

// Check arguments
if len(req.GetName()) == 0 {
return nil, status.Error(codes.InvalidArgument, "Name missing in request")
Expand Down Expand Up @@ -493,8 +505,36 @@ func (hp *hostPath) ControllerGetVolume(ctx context.Context, req *csi.Controller
}, nil
}

func (hp *hostPath) ControllerModifyVolume(ctx context.Context, request *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
return nil, fmt.Errorf("unimplemented")
func (hp *hostPath) ControllerModifyVolume(ctx context.Context, req *csi.ControllerModifyVolumeRequest) (*csi.ControllerModifyVolumeResponse, error) {
carlory marked this conversation as resolved.
Show resolved Hide resolved
if err := hp.validateControllerServiceRequest(csi.ControllerServiceCapability_RPC_MODIFY_VOLUME); err != nil {
return nil, err
}

// Check arguments
if len(req.VolumeId) == 0 {
return nil, status.Error(codes.InvalidArgument, "Volume ID cannot be empty")
}
if len(req.MutableParameters) == 0 {
return nil, status.Error(codes.InvalidArgument, "Mutable parameters cannot be empty")
}

// Check if the mutable parameters are in the accepted list
if err := hp.validateVolumeMutableParameters(req.MutableParameters); err != nil {
return nil, err
}

// Lock before acting on global state. A production-quality
// driver might use more fine-grained locking.
hp.mutex.Lock()
defer hp.mutex.Unlock()

// Get the volume
_, err := hp.state.GetVolumeByID(req.VolumeId)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

Copy link
Member Author

@carlory carlory Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @sunnylovestiramisu @msau42

I'm not sure whether mutable parameters should be store in the volume state? It seems useless to do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a test parameter here but make it really clear that it is for testing(ModifyVolumeTestingField etc.), also comment out the field so that others do not use it.

return &csi.ControllerModifyVolumeResponse{}, nil
}

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

// validateVolumeMutableParameters is a helper function to check if the mutable parameters are in the accepted list
func (hp *hostPath) validateVolumeMutableParameters(params map[string]string) error {
if len(hp.config.AcceptedMutableParameterNames) == 0 {
return nil
}

accepts := sets.New(hp.config.AcceptedMutableParameterNames...)
unsupported := []string{}
for k := range params {
if !accepts.Has(k) {
unsupported = append(unsupported, k)
}
}
if len(unsupported) > 0 {
return status.Errorf(codes.InvalidArgument, "invalid parameters: %v", unsupported)
}
return nil
}

func (hp *hostPath) validateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
return nil
Expand Down Expand Up @@ -815,6 +874,9 @@ func (hp *hostPath) getControllerServiceCapabilities() []*csi.ControllerServiceC
if hp.config.EnableAttach {
cl = append(cl, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME)
}
if hp.config.EnableControllerModifyVolume {
cl = append(cl, csi.ControllerServiceCapability_RPC_MODIFY_VOLUME)
}
}

var csc []*csi.ControllerServiceCapability
Expand Down
Loading