mirror of
https://github.com/kubernetes-csi/csi-driver-nvmf.git
synced 2025-09-08 16:40:01 +00:00
init csi-driver-nvmf
Signed-off-by: zhouenhua <zhouenhua@bytedance.com>
This commit is contained in:
84
pkg/nvmf/nvmf_utils.go
Normal file
84
pkg/nvmf/nvmf_utils.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package nvmf
|
||||
|
||||
import (
|
||||
"csi-driver-nvmf/pkg/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"k8s.io/klog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func waitForPathToExist(devicePath string, maxRetries, intervalSeconds int, deviceTransport string) (bool, error) {
|
||||
var err error
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
err = nil
|
||||
if deviceTransport == "tcp" {
|
||||
exist := utils.IsFileExisting(devicePath)
|
||||
if exist {
|
||||
return true, nil
|
||||
}
|
||||
} else {
|
||||
return false, fmt.Errorf("connect only support tcp")
|
||||
}
|
||||
|
||||
if i == maxRetries-1 {
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Second * time.Duration(intervalSeconds))
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func GetDeviceNameByVolumeID(volumeID string) (deviceName string, err error) {
|
||||
volumeLinkPath := strings.Join([]string{"/dev/disk/by-id/nvmf-uuid", volumeID}, ".")
|
||||
stat, err := os.Lstat(volumeLinkPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("volumeID link path %q not found", volumeLinkPath)
|
||||
} else {
|
||||
return "", fmt.Errorf("error getting stat of %q: %v", volumeLinkPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
if stat.Mode()&os.ModeSymlink != os.ModeSymlink {
|
||||
klog.Errorf("volumeID link file %q found, but was not a symlink", volumeLinkPath)
|
||||
return "", fmt.Errorf("volumeID link file %q found, but was not a symlink", volumeLinkPath)
|
||||
}
|
||||
|
||||
resolved, err := filepath.EvalSymlinks(volumeLinkPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error reading target of symlink %q: %v", volumeLinkPath, err)
|
||||
}
|
||||
if !strings.HasPrefix(resolved, "/dev") {
|
||||
return "", fmt.Errorf("resolved symlink for %q was unexpected: %q", volumeLinkPath, resolved)
|
||||
}
|
||||
log.Infof("Device Link Info: %s link to %s", volumeLinkPath, resolved)
|
||||
tmp := strings.Split(resolved, "/")
|
||||
return tmp[len(tmp)-1], nil
|
||||
}
|
||||
|
||||
func parseDeviceToControllerPath(deviceName string) string {
|
||||
nvmfControllerPrefix := "/sys/class/block"
|
||||
index := strings.LastIndex(deviceName, "n")
|
||||
parsed := deviceName[:index] + "c0" + deviceName[index:]
|
||||
scanPath := filepath.Join(nvmfControllerPrefix, parsed, "device/rescan_controller")
|
||||
return scanPath
|
||||
}
|
||||
|
||||
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
klog.Infof("GRPC call: %s", info.FullMethod)
|
||||
klog.Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
|
||||
resp, err := handler(ctx, req)
|
||||
if err != nil {
|
||||
klog.Errorf("GRPC error: %v", err)
|
||||
} else {
|
||||
klog.Infof("GRPC response: %s", protosanitizer.StripSecrets(resp))
|
||||
}
|
||||
return resp, err
|
||||
}
|
Reference in New Issue
Block a user