Skip to content

Commit

Permalink
develop csi.NodeServer NodePublishVolume and NodeUnpublishVolume
Browse files Browse the repository at this point in the history
  • Loading branch information
chensijie committed Jun 16, 2023
1 parent 0b37db0 commit 9512dbb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 18 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ require (
k8s.io/client-go v0.24.8
k8s.io/code-generator v0.24.9
k8s.io/klog/v2 v2.90.1
k8s.io/mount-utils v0.24.9
sigs.k8s.io/controller-runtime v0.12.3
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,8 @@ k8s.io/klog/v2 v2.90.1 h1:m4bYOKall2MmOiRaR1J+We67Do7vm9KiQVlT96lnHUw=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 h1:Gii5eqf+GmIEwGNKQYQClCayuJCe2/4fZUvF7VG99sU=
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42/go.mod h1:Z/45zLw8lUo4wdiUkI+v/ImEGAvu3WatcZl3lPMR4Rk=
k8s.io/mount-utils v0.24.9 h1:YHAPoL/VqFmSvzbGgW2pGd5/xL7lj4YG0CMt9TL5P94=
k8s.io/mount-utils v0.24.9/go.mod h1:OSv/4Aw211WK3Kl7bVPSde0BK66c96j4fOjsfwW3K48=
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 h1:HNSDgDCrr/6Ly3WEGKZftiE7IY19Vz2GdbOCyI4qqhc=
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
Expand Down
113 changes: 95 additions & 18 deletions pkg/localstorage/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@ package localstorage

import (
"context"

"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
"k8s.io/mount-utils"
"os"
)

// NodePublishVolume mount the volume
func (ls *localStorage) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
return &csi.NodePublishVolumeResponse{}, nil
}

// NodeUnpublishVolume unmount the volume
func (ls *localStorage) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
return &csi.NodeUnpublishVolumeResponse{}, nil
// NodeGetCapabilities return the capabilities of the Node plugin
func (ls *localStorage) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{Capabilities: []*csi.NodeServiceCapability{
{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: csi.NodeServiceCapability_RPC_UNKNOWN,
},
},
},
}}, nil
}

// NodeGetInfo return info of the node on which this plugin is running
Expand All @@ -41,24 +46,96 @@ func (ls *localStorage) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoReq
}, nil
}

// NodeGetCapabilities return the capabilities of the Node plugin
func (ls *localStorage) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
return &csi.NodeGetCapabilitiesResponse{}, nil
// NodePublishVolume mount the volume
func (ls *localStorage) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
// TODO for debug
klog.Infof("param %v\n", req)

if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "missing VolumeId")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "missing TargetPath")
}

targetPath := req.TargetPath
klog.Infof("NodePublishVolume target path %s ", targetPath)

if _, err := os.Stat(targetPath); err != nil {
if os.IsNotExist(err) {
if req.GetReadonly() {
os.MkdirAll(targetPath, 0310)
} else {
os.MkdirAll(targetPath, 0750)
}
}
return nil, status.Error(codes.Internal, err.Error())
}

localstorage, err := ls.getLocalStorageByNode(ls.GetNode())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

sourcePath := ""
for _, volume := range localstorage.Status.Volumes {
if req.GetVolumeId() == volume.VolID {
sourcePath = volume.VolPath
}
}
if len(sourcePath) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "can't found volume, id %s", req.GetVolumeId())
}

mounter := mount.New("")
if err := mounter.Mount(sourcePath, targetPath, "", nil); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &csi.NodePublishVolumeResponse{}, nil
}

// NodeGetVolumeStats get volume stats
func (ls *localStorage) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, nil
// NodeUnpublishVolume unmount the volume
func (ls *localStorage) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
// TODO for debug
klog.Infof("param %v\n", req)

if len(req.GetVolumeId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "missing VolumeId")
}
if len(req.GetTargetPath()) == 0 {
return nil, status.Error(codes.InvalidArgument, "missing TargetPath")
}

targetPath := req.TargetPath
klog.Infof("NodeUnpublishVolume target path %s ", targetPath)

if _, err := os.Stat(targetPath); err != nil {
if os.IsNotExist(err) {
return nil, status.Error(codes.Internal, "volume path not found")
}
}

mounter := mount.New("")
if err := mounter.Unmount(targetPath); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}

// NodeStageVolume stage volume
func (ls *localStorage) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

// NodeUnstageVolume unstage volume
func (ls *localStorage) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

// NodeStageVolume stage volume
func (ls *localStorage) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
// NodeGetVolumeStats get volume stats
func (ls *localStorage) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, nil
}

// NodeExpandVolume node expand volume
Expand Down

0 comments on commit 9512dbb

Please sign in to comment.