diff --git a/build/lib/csiplugin-connector.go b/build/lib/csiplugin-connector.go deleted file mode 100644 index ab0b801a8..000000000 --- a/build/lib/csiplugin-connector.go +++ /dev/null @@ -1,251 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "log" - "net" - "os" - "os/exec" - "path/filepath" - "strings" - "sync" - "time" -) - -const ( - // OSSSocketPath socket path - OSSSocketPath = "/run/csi-tool/connector/connector.sock" -) - -func main() { - log.Print("OSS Connector Daemon Is Starting...") - - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - EnsureSocketPath(OSSSocketPath) - log.Printf("Socket path is ready: %s", OSSSocketPath) - ln, err := net.Listen("unix", OSSSocketPath) - if err != nil { - log.Fatalf("Server Listen error: %s", err.Error()) - } - log.Print("Daemon Started ...") - defer ln.Close() - - go watchDogCheck() - // Handler to process the command - for { - fd, err := ln.Accept() - if err != nil { - log.Printf("Server Accept error: %s", err.Error()) - continue - } - go echoServer(fd) - } - }() - wg.Wait() -} - -func watchDogCheck() { - // watchdog of UNIX Domain Socket - var socketsPath []string - if os.Getenv("WATCHDOG_SOCKETS_PATH") != "" { - socketsPath = strings.Split(os.Getenv("WATCHDOG_SOCKETS_PATH"), ",") - } - socketNotAliveCount := make(map[string]int) - if len(socketsPath) == 0 { - return - } - for { - deadSockets := 0 - for _, path := range socketsPath { - if err := isUnixDomainSocketLive(path); err != nil { - log.Printf("socket %s is not alive: %v", path, err) - socketNotAliveCount[path]++ - } else { - socketNotAliveCount[path] = 0 - } - if socketNotAliveCount[path] >= 6 { - deadSockets++ - } - } - if deadSockets >= len(socketsPath) { - log.Printf("watchdog find too many dead sockets, csiplugin-connector will exit(0)") - os.Exit(0) - } - time.Sleep(time.Second * 10) - } -} - -func echoServer(c net.Conn) { - buf := make([]byte, 2048) - nr, err := c.Read(buf) - if err != nil { - log.Print("Server Read error: ", err.Error()) - return - } - - cmdStr := string(buf[0:nr]) - // '\x00' is chosen as the delimiter because it is the only character that is not vaild in the command line arguments. - // The rationale is the same as `xargs -0`. - args := strings.Split(cmdStr, "\x00") - log.Printf("Server receive mount cmd: %q", args) - - // Used when removing shell usage while be compatible with old code - // Should be removed eventually - cmd := strings.Join(args, " ") - - if strings.Contains(cmd, "/usr/local/bin/ossfs") { - err = checkOssfsCmd(cmd) - } else if strings.Contains(cmd, "mount -t alinas") { - err = checkRichNasClientCmd(cmd) - } - - if err != nil { - out := "Fail: " + err.Error() - log.Printf("Check user space mount is failed, err: %s", out) - if _, err := c.Write([]byte(out)); err != nil { - log.Printf("Check user space mount write is failed, err: %s", err.Error()) - } - return - } - // run command - if out, err := run(args...); err != nil { - reply := "Fail: " + cmd + ", error: " + err.Error() - _, err = c.Write([]byte(reply)) - log.Print("Server Fail to run cmd:", reply) - } else { - out = "Success:" + out - _, err = c.Write([]byte(out)) - log.Printf("Success: %s", out) - } -} - -// systemd-run --scope -- mount -t alinas -o unas -o client_owner=podUID nfsServer:nfsPath mountPoint -func checkRichNasClientCmd(cmd string) error { - parameteList := strings.Split(cmd, " ") - if len(parameteList) <= 2 { - return fmt.Errorf("Nas rich client mount command is format wrong:%+v", parameteList) - } - mountPoint := parameteList[len(parameteList)-1] - if !IsFileExisting(mountPoint) { - return errors.New("Nas rich client option: mountpoint not exist " + mountPoint) - } - nfsInfo := strings.Split(parameteList[len(parameteList)-2], ":") - if len(nfsInfo) != 2 { - return errors.New("Nas rich client option: nfsServer:nfsPath is wrong format " + parameteList[len(parameteList)-2]) - } - return nil -} - -// systemd-run --scope -- /usr/local/bin/ossfs shenzhen -// /var/lib/kubelet/pods/070d1a40-16a4-11ea-842e-00163e062fe1/volumes/kubernetes.io~csi/oss-csi-pv/mount -// -ourl=oss-cn-shenzhen-internal.aliyuncs.com -// -o max_stat_cache_size=0 -o allow_other -func checkOssfsCmd(cmd string) error { - ossCmdPrefixList := []string{"systemd-run --scope -- /usr/local/bin/ossfs", "systemd-run --scope -- ossfs", "ossfs"} - ossCmdPrefix := "" - for _, cmdPrefix := range ossCmdPrefixList { - if strings.HasPrefix(cmd, cmdPrefix) { - ossCmdPrefix = cmdPrefix - break - } - } - - // check oss command options - if ossCmdPrefix != "" { - cmdParameters := strings.TrimPrefix(cmd, ossCmdPrefix) - cmdParameters = strings.TrimSpace(cmdParameters) - cmdParameters = strings.Join(strings.Fields(cmdParameters), " ") - - parameteList := strings.Split(cmdParameters, " ") - if len(parameteList) < 3 { - return errors.New("Oss Options: parameters less than 3: " + cmd) - } - if !IsFileExisting(parameteList[1]) { - return errors.New("Oss Options: mountpoint not exist " + parameteList[1]) - } - if !strings.HasPrefix(parameteList[2], "-ourl=") { - return errors.New("Oss Options: url should start with -ourl: " + parameteList[2]) - } - oFlag := false - for index, value := range parameteList { - if index < 3 { - continue - } - if value == "-s" || value == "-d" || value == "--debug" { - if oFlag { - return errors.New("Oss Options: no expect string follow -o " + value) - } - continue - } - if strings.HasPrefix(value, "-o") && len(value) > 2 { - if oFlag { - return errors.New("Oss Options: no expect string follow -o " + value) - } - continue - } - if value == "-o" { - if oFlag == true { - return errors.New("Oss Options: inputs must -o string, 2 -o now ") - } - oFlag = true - continue - } - if oFlag == true { - oFlag = false - } else { - return errors.New("Oss Options: inputs must -o string, 2 string now ") - } - } - return nil - } - return errors.New("Oss Options: options with error prefix: " + cmd) -} - -func run(args ...string) (string, error) { - out, err := exec.Command(args[0], args[1:]...).CombinedOutput() - if err != nil { - return "", fmt.Errorf("failed to run cmd: %q, with out: %q, with error: %v", args, string(out), err) - } - return string(out), nil -} - -// IsFileExisting checks file exist in volume driver or not -func IsFileExisting(filename string) bool { - _, err := os.Stat(filename) - if err == nil { - return true - } - if os.IsNotExist(err) { - return false - } - return true -} - -func isUnixDomainSocketLive(socketPath string) error { - fileInfo, err := os.Stat(socketPath) - if err != nil || (fileInfo.Mode()&os.ModeSocket == 0) { - return fmt.Errorf("socket file %s is invalid", socketPath) - } - conn, err := net.Dial("unix", socketPath) - if err != nil { - return err - } - conn.Close() - return nil -} - -// EnsureSocketPath ... -func EnsureSocketPath(socketPath string) { - if IsFileExisting(socketPath) { - os.Remove(socketPath) - } else { - pathDir := filepath.Dir(socketPath) - if !IsFileExisting(pathDir) { - os.MkdirAll(pathDir, os.ModePerm) - } - } -} diff --git a/build/lib/csiplugin-connector.service b/build/lib/csiplugin-connector.service deleted file mode 100644 index b0942615d..000000000 --- a/build/lib/csiplugin-connector.service +++ /dev/null @@ -1,13 +0,0 @@ -[Unit] -Description=csiplugin connector -After=network.target remote-fs.target nss-lookup.target - -[Service] -Type=simple -RuntimeDirectory=csi-tool/connector -ExecStart=/etc/csi-tool/csiplugin-connector -Restart=always -RestartSec=5s - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/pkg/mounter/connector_mounter.go b/pkg/mounter/connector_mounter.go deleted file mode 100644 index 050d8522a..000000000 --- a/pkg/mounter/connector_mounter.go +++ /dev/null @@ -1,35 +0,0 @@ -package mounter - -import ( - "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils" - "k8s.io/klog/v2" - mountutils "k8s.io/mount-utils" -) - -type ConnectorMounter struct { - mounterPath string - mountutils.Interface -} - -func (m *ConnectorMounter) Mount(source string, target string, fstype string, options []string) error { - args := mountutils.MakeMountArgs(source, target, fstype, options) - mntCmd := []string{"systemd-run", "--scope", "--"} - if m.mounterPath == "" { - mntCmd = append(mntCmd, "mount") - } else { - mntCmd = append(mntCmd, m.mounterPath) - } - mntCmd = append(mntCmd, args...) - out, err := utils.ConnectorRun(mntCmd...) - if len(out) > 0 { - klog.Infof("ConnectorRun: %q, output: %s", mntCmd, string(out)) - } - return err -} - -func NewConnectorMounter(inner mountutils.Interface, mounterPath string) mountutils.Interface { - return &ConnectorMounter{ - mounterPath: mounterPath, - Interface: inner, - } -} diff --git a/pkg/nas/mounter.go b/pkg/nas/mounter.go index 954fc50ff..43253eea9 100644 --- a/pkg/nas/mounter.go +++ b/pkg/nas/mounter.go @@ -32,14 +32,6 @@ func (m *NasMounter) Mount(source string, target string, fstype string, options return err } -func newNasMounter() mountutils.Interface { - inner := mountutils.NewWithoutSystemd("") - return &NasMounter{ - Interface: inner, - alinasMounter: mounter.NewConnectorMounter(inner, ""), - } -} - func newNasMounterWithProxy(socketPath string) mountutils.Interface { inner := mountutils.NewWithoutSystemd("") return &NasMounter{ diff --git a/pkg/nas/mounter_test.go b/pkg/nas/mounter_test.go index 9fa59bb65..95cf288e3 100644 --- a/pkg/nas/mounter_test.go +++ b/pkg/nas/mounter_test.go @@ -24,11 +24,6 @@ func (m *errorMockMounter) Mount(source string, target string, fstype string, op return errors.New("") } -func TestNewNasMounter(t *testing.T) { - actual := newNasMounter() - assert.NotNil(t, actual) -} - func TestNasMounter_MountSuccess(t *testing.T) { nasMounter := &NasMounter{ Interface: &successMockMounter{}, diff --git a/pkg/nas/nodeserver.go b/pkg/nas/nodeserver.go index 3f7c82883..b5a832d7a 100644 --- a/pkg/nas/nodeserver.go +++ b/pkg/nas/nodeserver.go @@ -69,12 +69,8 @@ func newNodeServer(config *internal.NodeConfig) *nodeServer { NodeID: config.NodeName, }, } - if config.MountProxySocket == "" { - ns.mounter = newNasMounter() - } else { - ns.recorder = utils.NewEventRecorder() - ns.mounter = newNasMounterWithProxy(config.MountProxySocket) - } + ns.recorder = utils.NewEventRecorder() + ns.mounter = newNasMounterWithProxy(config.MountProxySocket) return ns } diff --git a/pkg/utils/util.go b/pkg/utils/util.go index ffcda70b4..5cebe098f 100644 --- a/pkg/utils/util.go +++ b/pkg/utils/util.go @@ -83,8 +83,6 @@ const ( fsckErrorsCorrected = 1 // fsckErrorsUncorrected tag fsckErrorsUncorrected = 4 - // socketPath is path of connector sock - socketPath = "/host/run/csi-tool/connector/connector.sock" // GiB ... GiB = 1024 * 1024 * 1024 @@ -530,32 +528,6 @@ func GetPvNameFormPodMnt(mntPath string) string { return "" } -// ConnectorRun Run shell command with host connector -// host connector is daemon running in host. -func ConnectorRun(cmd ...string) (string, error) { - c, err := net.Dial("unix", socketPath) - if err != nil { - klog.Errorf("Oss connector Dial error: %s", err.Error()) - return err.Error(), err - } - defer c.Close() - - _, err = c.Write([]byte(strings.Join(cmd, "\x00"))) - if err != nil { - klog.Errorf("Oss connector write error: %s", err.Error()) - return err.Error(), err - } - - buf := make([]byte, 2048) - n, _ := c.Read(buf[:]) - response := string(buf[0:n]) - if strings.HasPrefix(response, "Success") { - respstr := response[8:] - return respstr, nil - } - return response, errors.New("Exec command error:" + response) -} - // AppendJSONData append map data to json file. func AppendJSONData(dataFilePath string, appData map[string]string) error { curData, err := LoadJSONData(dataFilePath)