From 14898dd3c70025c149a00dcdda293add16bf1a9d Mon Sep 17 00:00:00 2001 From: "long.wang" <42292669+wlooppy@users.noreply.github.com> Date: Thu, 1 Sep 2022 09:53:53 +0800 Subject: [PATCH] cni should handler unmont volume, when delete pod. (#1873) Co-authored-by: wanglong --- go.mod | 1 + go.sum | 1 + pkg/daemon/handler_linux.go | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/go.mod b/go.mod index f1635129337..931a6c907bb 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/greenpau/ovsdb v1.0.2 github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.3.0 github.com/mdlayher/arp v0.0.0-20220512170110-6706a2966875 + github.com/moby/sys/mountinfo v0.6.0 github.com/neverlee/keymutex v0.0.0-20171121013845-f593aa834bf9 github.com/oilbeater/go-ping v0.0.0-20200413021620-332b7197c5b5 github.com/onsi/ginkgo/v2 v2.1.5 diff --git a/go.sum b/go.sum index 0bf834d933b..ff923753a57 100644 --- a/go.sum +++ b/go.sum @@ -910,6 +910,7 @@ github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0Gq github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= +github.com/moby/sys/mountinfo v0.6.0 h1:gUDhXQx58YNrpHlK4nSL+7y2pxFZkUcXqzFDKWdC0Oo= github.com/moby/sys/mountinfo v0.6.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ= github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw= diff --git a/pkg/daemon/handler_linux.go b/pkg/daemon/handler_linux.go index 4b0ea81c97c..aa3e83f0a14 100644 --- a/pkg/daemon/handler_linux.go +++ b/pkg/daemon/handler_linux.go @@ -1,7 +1,9 @@ package daemon import ( + "errors" "fmt" + "io/fs" "os" "strings" "syscall" @@ -12,6 +14,7 @@ import ( "github.com/kubeovn/kube-ovn/pkg/request" "github.com/kubeovn/kube-ovn/pkg/util" + "github.com/moby/sys/mountinfo" ) func (csh cniServerHandler) validatePodRequest(req *request.CniRequest) error { @@ -63,6 +66,12 @@ func removeShortSharedDir(pod *v1.Pod, volumeName string) (err error) { klog.Errorf("shared directory %s does not exist to unmount, %s", sharedDir, err) return nil } + + foundMount, err := mountinfo.Mounted(sharedDir) + if errors.Is(err, fs.ErrNotExist) || (err == nil && !foundMount) { + klog.Infof("volume: %s not mounted, no need to unmount", sharedDir) + return nil + } err = unix.Unmount(sharedDir, 0) if err != nil { klog.Errorf("Failed to unmount dir: %v", err)