From fd6b80b02b6d29957c23fd96b0b6a38c2b785a3e Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Fri, 8 Nov 2019 14:50:39 -0800 Subject: [PATCH 1/6] added removeorphanlockfile function --- cni/network/plugin/main.go | 2 ++ common/utils.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/cni/network/plugin/main.go b/cni/network/plugin/main.go index 211a763995..123e3477fc 100644 --- a/cni/network/plugin/main.go +++ b/cni/network/plugin/main.go @@ -187,6 +187,8 @@ func main() { reportPluginError(reportManager, tb, err) tb.Close() } + + //removeOrphanLockFile(platform.CNIRuntimePath + name + ".json" + ".lock") return } diff --git a/common/utils.go b/common/utils.go index 7c5b7b7b6b..5e8e044bd9 100644 --- a/common/utils.go +++ b/common/utils.go @@ -14,6 +14,7 @@ import ( "net" "net/http" "os" + "strconv" "time" "github.com/Azure/azure-container-networking/log" @@ -258,3 +259,4 @@ func SaveHostMetadata(metadata Metadata, fileName string) error { return err } + From 559594831d00358af0751b899e8e87a7d08c2ce1 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 19 Nov 2019 15:48:44 -0800 Subject: [PATCH 2/6] remove lock file if process holding that exited --- cni/ipam/plugin/main.go | 10 ++++++++- cni/network/plugin/main.go | 30 +++++++++++++++----------- cni/plugin.go | 40 +++++++++++++++++++++++++++++++++-- common/utils.go | 2 -- platform/os_linux.go | 39 ++++++++++++++++++++++++++++++++++ platform/os_test.go | 25 ++++++++++++++++++++++ platform/os_windows.go | 43 +++++++++++++++++++++++++++++++++++++- store/json.go | 4 ++++ store/store.go | 1 + 9 files changed, 176 insertions(+), 18 deletions(-) diff --git a/cni/ipam/plugin/main.go b/cni/ipam/plugin/main.go index 6ab1e49f3c..4795d72251 100644 --- a/cni/ipam/plugin/main.go +++ b/cni/ipam/plugin/main.go @@ -28,11 +28,19 @@ func main() { if err := ipamPlugin.Plugin.InitializeKeyValueStore(&config); err != nil { fmt.Printf("Failed to initialize key-value store of ipam plugin, err:%v.\n", err) + + if ipamPlugin.Plugin.IsSafeToRemoveLock(ipamPlugin.Plugin.Name) { + log.Printf("[CNI] Removing lock file as process holding lock exited.") + if errUninit := ipamPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { + log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) + } + } + os.Exit(1) } defer func() { - if errUninit := ipamPlugin.Plugin.UninitializeKeyValueStore(); errUninit != nil { + if errUninit := ipamPlugin.Plugin.UninitializeKeyValueStore(false); errUninit != nil { fmt.Printf("Failed to uninitialize key-value store of ipam plugin, err:%v.\n", err) } diff --git a/cni/network/plugin/main.go b/cni/network/plugin/main.go index 123e3477fc..f604313380 100644 --- a/cni/network/plugin/main.go +++ b/cni/network/plugin/main.go @@ -188,21 +188,18 @@ func main() { tb.Close() } - //removeOrphanLockFile(platform.CNIRuntimePath + name + ".json" + ".lock") + if netPlugin.Plugin.IsSafeToRemoveLock(name) { + log.Printf("[CNI] Removing lock file as process holding lock exited.") + if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { + log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) + } + } + return } - // Start telemetry process if not already started. This should be done inside lock, otherwise multiple process - // end up creating/killing telemetry process results in undesired state. - tb := telemetry.NewTelemetryBuffer("") - tb.ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds) - defer tb.Close() - - t := time.Now() - cniReport.Timestamp = t.Format("2006-01-02 15:04:05") - defer func() { - if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(); errUninit != nil { + if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(false); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } @@ -211,6 +208,15 @@ func main() { } }() + // Start telemetry process if not already started. This should be done inside lock, otherwise multiple process + // end up creating/killing telemetry process results in undesired state. + tb := telemetry.NewTelemetryBuffer("") + tb.ConnectToTelemetryService(telemetryNumRetries, telemetryWaitTimeInMilliseconds) + defer tb.Close() + + t := time.Now() + cniReport.Timestamp = t.Format("2006-01-02 15:04:05") + if err = netPlugin.Start(&config); err != nil { log.Errorf("Failed to start network plugin, err:%v.\n", err) reportPluginError(reportManager, tb, err) @@ -230,7 +236,7 @@ func main() { netPlugin.Stop() // release cni lock - if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(); errUninit != nil { + if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(false); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } diff --git a/cni/plugin.go b/cni/plugin.go index 0a6cb7f0cb..179577c916 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -6,8 +6,10 @@ package cni import ( "context" "fmt" + "io/ioutil" "os" "runtime" + "strings" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/log" @@ -186,9 +188,9 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error } // Uninitialize key-value store -func (plugin *Plugin) UninitializeKeyValueStore() error { +func (plugin *Plugin) UninitializeKeyValueStore(forceUnlock bool) error { if plugin.Store != nil { - err := plugin.Store.Unlock(false) + err := plugin.Store.Unlock(forceUnlock) if err != nil { log.Printf("[cni] Failed to unlock store: %v.", err) return err @@ -198,3 +200,37 @@ func (plugin *Plugin) UninitializeKeyValueStore() error { return nil } + +// check if safe to remove lockfile +func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { + if plugin.Store != nil { + lockFileName := plugin.Store.GetLockFileName() + + content, err := ioutil.ReadFile(lockFileName) + if err != nil { + log.Errorf("Failed to read lock file :%v, ", err) + return false + } + + if len(content) <= 0 { + log.Errorf("Num bytes read from lock file is 0") + return false + } + + pid := string(content) + pid = strings.Trim(pid, "\n") + + pName, err := platform.GetProcessNameByID(pid) + if err != nil { + return true + } + + log.Printf("[CNI] Process name for pid %s is %s", pid, pName) + + if pName != processName { + return true + } + } + + return false +} diff --git a/common/utils.go b/common/utils.go index 5e8e044bd9..7c5b7b7b6b 100644 --- a/common/utils.go +++ b/common/utils.go @@ -14,7 +14,6 @@ import ( "net" "net/http" "os" - "strconv" "time" "github.com/Azure/azure-container-networking/log" @@ -259,4 +258,3 @@ func SaveHostMetadata(metadata Metadata, fileName string) error { return err } - diff --git a/platform/os_linux.go b/platform/os_linux.go index fc8cbaf898..26c7fd85f6 100644 --- a/platform/os_linux.go +++ b/platform/os_linux.go @@ -7,8 +7,11 @@ import ( "bytes" "fmt" "io/ioutil" + "os" "os/exec" + "strconv" "strings" + "syscall" "time" "github.com/Azure/azure-container-networking/common" @@ -127,3 +130,39 @@ func GetOSDetails() (map[string]string, error) { return osInfoArr, nil } + +func IsProcessRunning(pidstr string) bool { + pid, err := strconv.Atoi(pidstr) + if err != nil { + log.Errorf("Cannot convert processid to int: %v", err) + return false + } + + process, err := os.FindProcess(pid) + if err != nil { + log.Printf("Failed to find process: %s", err) + return false + } + + err = process.Signal(syscall.Signal(0)) + log.Printf("process.Signal on pid %d returned: %v", pid, err) + if err != nil { + return false + } + + return true +} + +func GetProcessNameByID(pidstr string) (string, error) { + cmd := fmt.Sprintf("ps -p %s -o comm=", pidstr) + out, err := ExecuteCommand(cmd) + if err != nil { + log.Printf("GetProcessNameByID returned error: %v", err) + return "", err + } + + out = strings.Trim(out, "\n") + out = strings.TrimSpace(out) + + return out, nil +} diff --git a/platform/os_test.go b/platform/os_test.go index 124f6153b7..55417bdbd2 100644 --- a/platform/os_test.go +++ b/platform/os_test.go @@ -2,6 +2,8 @@ package platform import ( "os" + "strconv" + "strings" "testing" ) @@ -23,3 +25,26 @@ func TestGetOSDetails(t *testing.T) { t.Errorf("GetOSDetails failed :%v", err) } } + +func TestIsProcessRunning(t *testing.T) { + status := IsProcessRunning(strconv.Itoa(os.Getpid())) + if !status { + t.Errorf("IsProcessRunning failed. Expected: process should be running") + } + + status = IsProcessRunning("-1") + if status { + t.Errorf("IsProcessRunning failed. Expected: Process should not run") + } +} + +func TestGetProcessNameByID(t *testing.T) { + pName, err := GetProcessNameByID(strconv.Itoa(os.Getpid())) + if err != nil { + t.Errorf("GetProcessNameByID failed: %v", err) + } + + if !strings.Contains(pName, "platform.test") { + t.Errorf("Incorrect process name:%v\n", pName) + } +} diff --git a/platform/os_windows.go b/platform/os_windows.go index e8bc2c6f4a..01ceb9d0d2 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -150,7 +150,11 @@ func executePowershellCommand(command string) (string, error) { var stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr - cmd.Run() + + err = cmd.Run() + if err != nil { + return "", fmt.Errorf("%s:%s", err.Error(), stderr.String()) + } return strings.TrimSpace(stdout.String()), nil } @@ -186,3 +190,40 @@ func SetSdnRemoteArpMacAddress() error { func GetOSDetails() (map[string]string, error) { return nil, nil } + +func IsProcessRunning(pidstr string) bool { + cmd := fmt.Sprintf("Get-Process -Id %s", pidstr) + out, err := executePowershellCommand(cmd) + if err != nil { + log.Printf("Process is not running. Output:%v, Error %v", out, err) + return false + } + + return true +} + +func GetProcessNameByID(pidstr string) (string, error) { + cmd := fmt.Sprintf("Get-Process -Id %s|Format-List", pidstr) + out, err := executePowershellCommand(cmd) + if err != nil { + log.Printf("Process is not running. Output:%v, Error %v", out, err) + return "", err + } + + if len(out) <= 0 { + log.Printf("Output length is 0") + return "", fmt.Errorf("get-process output length is 0") + } + + lines := strings.Split(out, "\n") + for _, line := range lines { + if strings.Contains(line, "Name") { + pName := strings.Split(line, ":") + if len(pName) > 1 { + return strings.TrimSpace(pName[1]), nil + } + } + } + + return "", fmt.Errorf("Process not found") +} diff --git a/store/json.go b/store/json.go index 65f18b4d55..8945ad26ee 100644 --- a/store/json.go +++ b/store/json.go @@ -241,3 +241,7 @@ func (kvs *jsonFileStore) GetLockFileModificationTime() (time.Time, error) { return info.ModTime().UTC(), nil } + +func (kvs *jsonFileStore) GetLockFileName() string { + return kvs.fileName + lockExtension +} diff --git a/store/store.go b/store/store.go index 2e505ba2ed..ef648a161c 100644 --- a/store/store.go +++ b/store/store.go @@ -17,6 +17,7 @@ type KeyValueStore interface { Unlock(forceUnlock bool) error GetModificationTime() (time.Time, error) GetLockFileModificationTime() (time.Time, error) + GetLockFileName() string } var ( From 6d2bcb95ff6ef5f25b3970d11bc07ab74c139865 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Thu, 21 Nov 2019 17:51:07 -0800 Subject: [PATCH 3/6] addressed comments --- cni/ipam/plugin/main.go | 1 + cni/plugin.go | 9 ++++----- platform/os_linux.go | 4 ++++ platform/os_windows.go | 5 +++++ 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cni/ipam/plugin/main.go b/cni/ipam/plugin/main.go index 4795d72251..78a0fb10db 100644 --- a/cni/ipam/plugin/main.go +++ b/cni/ipam/plugin/main.go @@ -10,6 +10,7 @@ import ( "github.com/Azure/azure-container-networking/cni" "github.com/Azure/azure-container-networking/cni/ipam" "github.com/Azure/azure-container-networking/common" + "github.com/Azure/azure-container-networking/log" ) // Version is populated by make during build. diff --git a/cni/plugin.go b/cni/plugin.go index 179577c916..7c02ca3c01 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -188,9 +188,9 @@ func (plugin *Plugin) InitializeKeyValueStore(config *common.PluginConfig) error } // Uninitialize key-value store -func (plugin *Plugin) UninitializeKeyValueStore(forceUnlock bool) error { +func (plugin *Plugin) UninitializeKeyValueStore(force bool) error { if plugin.Store != nil { - err := plugin.Store.Unlock(forceUnlock) + err := plugin.Store.Unlock(force) if err != nil { log.Printf("[cni] Failed to unlock store: %v.", err) return err @@ -203,7 +203,7 @@ func (plugin *Plugin) UninitializeKeyValueStore(forceUnlock bool) error { // check if safe to remove lockfile func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { - if plugin.Store != nil { + if plugin != nil && plugin.Store != nil { lockFileName := plugin.Store.GetLockFileName() content, err := ioutil.ReadFile(lockFileName) @@ -217,8 +217,7 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { return false } - pid := string(content) - pid = strings.Trim(pid, "\n") + pid := strings.Trim(string(content), "\n") pName, err := platform.GetProcessNameByID(pid) if err != nil { diff --git a/platform/os_linux.go b/platform/os_linux.go index 26c7fd85f6..3008a93aa1 100644 --- a/platform/os_linux.go +++ b/platform/os_linux.go @@ -47,6 +47,10 @@ func GetOSInfo() string { return string(info) } +func Init() error { + return nil +} + // GetLastRebootTime returns the last time the system rebooted. func GetLastRebootTime() (time.Time, error) { // Query last reboot time. diff --git a/platform/os_windows.go b/platform/os_windows.go index 01ceb9d0d2..2170b8c922 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -60,6 +60,11 @@ func GetOSInfo() string { return "windows" } +func Init() error { + _, err := executePowershellCommand("Get-Process -Id 0") + return err +} + // GetLastRebootTime returns the last time the system rebooted. func GetLastRebootTime() (time.Time, error) { out, err := exec.Command("cmd", "/c", "wmic os get lastbootuptime").Output() From 3c3ec753790250b4afd7f94153627fb33727e8ae Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Fri, 22 Nov 2019 12:45:25 -0800 Subject: [PATCH 4/6] addressed comments and added a condition to check get process cmd is supported --- cni/plugin.go | 11 +++++++++-- platform/os_linux.go | 6 ++++-- platform/os_windows.go | 6 ++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cni/plugin.go b/cni/plugin.go index 7c02ca3c01..07e6e3fc06 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -204,8 +204,14 @@ func (plugin *Plugin) UninitializeKeyValueStore(force bool) error { // check if safe to remove lockfile func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { if plugin != nil && plugin.Store != nil { - lockFileName := plugin.Store.GetLockFileName() + // check if get process command supported + if cmdErr := platform.GetProcessSupport(); cmdErr != nil { + log.Errorf("Get process cmd not supported. Error %v", cmdErr) + return false + } + // Read pid from lockfile + lockFileName := plugin.Store.GetLockFileName() content, err := ioutil.ReadFile(lockFileName) if err != nil { log.Errorf("Failed to read lock file :%v, ", err) @@ -217,8 +223,9 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { return false } + // Get the process name if running and + // check if that matches with our expected process pid := strings.Trim(string(content), "\n") - pName, err := platform.GetProcessNameByID(pid) if err != nil { return true diff --git a/platform/os_linux.go b/platform/os_linux.go index 3008a93aa1..425ed5fe34 100644 --- a/platform/os_linux.go +++ b/platform/os_linux.go @@ -47,8 +47,10 @@ func GetOSInfo() string { return string(info) } -func Init() error { - return nil +func GetProcessSupport() error { + cmd := fmt.Sprintf("ps -p %v -o comm=", os.Getpid()) + _, err := ExecuteCommand(cmd) + return err } // GetLastRebootTime returns the last time the system rebooted. diff --git a/platform/os_windows.go b/platform/os_windows.go index 2170b8c922..dbc829f879 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -6,6 +6,7 @@ package platform import ( "bytes" "fmt" + "os" "os/exec" "strings" "time" @@ -60,8 +61,9 @@ func GetOSInfo() string { return "windows" } -func Init() error { - _, err := executePowershellCommand("Get-Process -Id 0") +func GetProcessSupport() error { + cmd := fmt.Sprintf("Get-Process -Id %v", os.Getpid()) + _, err := executePowershellCommand(cmd) return err } From 11526d0f7852e55e30d136764c9f19521eff496a Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Mon, 2 Dec 2019 17:11:46 -0800 Subject: [PATCH 5/6] Addressed comments fixed trim line ending --- cni/ipam/plugin/main.go | 4 ++-- cni/network/plugin/main.go | 4 ++-- cni/plugin.go | 21 ++++++++++----------- platform/os_linux.go | 25 +------------------------ platform/os_test.go | 12 ------------ platform/os_windows.go | 14 +++----------- 6 files changed, 18 insertions(+), 62 deletions(-) diff --git a/cni/ipam/plugin/main.go b/cni/ipam/plugin/main.go index 78a0fb10db..8e06082085 100644 --- a/cni/ipam/plugin/main.go +++ b/cni/ipam/plugin/main.go @@ -30,8 +30,8 @@ func main() { if err := ipamPlugin.Plugin.InitializeKeyValueStore(&config); err != nil { fmt.Printf("Failed to initialize key-value store of ipam plugin, err:%v.\n", err) - if ipamPlugin.Plugin.IsSafeToRemoveLock(ipamPlugin.Plugin.Name) { - log.Printf("[CNI] Removing lock file as process holding lock exited.") + if isSafe, err := ipamPlugin.Plugin.IsSafeToRemoveLock(ipamPlugin.Plugin.Name); isSafe { + log.Printf("[IPAM] Removing lock file as process holding lock exited. Error:%v", err) if errUninit := ipamPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } diff --git a/cni/network/plugin/main.go b/cni/network/plugin/main.go index f604313380..22540634b2 100644 --- a/cni/network/plugin/main.go +++ b/cni/network/plugin/main.go @@ -188,8 +188,8 @@ func main() { tb.Close() } - if netPlugin.Plugin.IsSafeToRemoveLock(name) { - log.Printf("[CNI] Removing lock file as process holding lock exited.") + if isSafe, err := netPlugin.Plugin.IsSafeToRemoveLock(name); isSafe { + log.Printf("[CNI] Removing lock file as process holding lock exited. Error:%v", err) if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } diff --git a/cni/plugin.go b/cni/plugin.go index 07e6e3fc06..f840c7078e 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -9,7 +9,6 @@ import ( "io/ioutil" "os" "runtime" - "strings" "github.com/Azure/azure-container-networking/common" "github.com/Azure/azure-container-networking/log" @@ -202,12 +201,12 @@ func (plugin *Plugin) UninitializeKeyValueStore(force bool) error { } // check if safe to remove lockfile -func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { +func (plugin *Plugin) IsSafeToRemoveLock(processName string) (bool, error) { if plugin != nil && plugin.Store != nil { // check if get process command supported if cmdErr := platform.GetProcessSupport(); cmdErr != nil { log.Errorf("Get process cmd not supported. Error %v", cmdErr) - return false + return false, cmdErr } // Read pid from lockfile @@ -215,28 +214,28 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) bool { content, err := ioutil.ReadFile(lockFileName) if err != nil { log.Errorf("Failed to read lock file :%v, ", err) - return false + return false, err } if len(content) <= 0 { log.Errorf("Num bytes read from lock file is 0") - return false + return false, fmt.Errorf("Num bytes read from lock file is 0") } + log.Printf("Read from Lock file:%s", content) // Get the process name if running and // check if that matches with our expected process - pid := strings.Trim(string(content), "\n") - pName, err := platform.GetProcessNameByID(pid) + pName, err := platform.GetProcessNameByID(string(content)) if err != nil { - return true + return true, nil } - log.Printf("[CNI] Process name for pid %s is %s", pid, pName) + log.Printf("[CNI] Process name is %s", pName) if pName != processName { - return true + return true, nil } } - return false + return false, fmt.Errorf("plugin store nil") } diff --git a/platform/os_linux.go b/platform/os_linux.go index 425ed5fe34..1a00609ddb 100644 --- a/platform/os_linux.go +++ b/platform/os_linux.go @@ -9,9 +9,7 @@ import ( "io/ioutil" "os" "os/exec" - "strconv" "strings" - "syscall" "time" "github.com/Azure/azure-container-networking/common" @@ -137,29 +135,8 @@ func GetOSDetails() (map[string]string, error) { return osInfoArr, nil } -func IsProcessRunning(pidstr string) bool { - pid, err := strconv.Atoi(pidstr) - if err != nil { - log.Errorf("Cannot convert processid to int: %v", err) - return false - } - - process, err := os.FindProcess(pid) - if err != nil { - log.Printf("Failed to find process: %s", err) - return false - } - - err = process.Signal(syscall.Signal(0)) - log.Printf("process.Signal on pid %d returned: %v", pid, err) - if err != nil { - return false - } - - return true -} - func GetProcessNameByID(pidstr string) (string, error) { + pidstr = strings.Trim(pidstr, "\n") cmd := fmt.Sprintf("ps -p %s -o comm=", pidstr) out, err := ExecuteCommand(cmd) if err != nil { diff --git a/platform/os_test.go b/platform/os_test.go index 55417bdbd2..38048eaecd 100644 --- a/platform/os_test.go +++ b/platform/os_test.go @@ -26,18 +26,6 @@ func TestGetOSDetails(t *testing.T) { } } -func TestIsProcessRunning(t *testing.T) { - status := IsProcessRunning(strconv.Itoa(os.Getpid())) - if !status { - t.Errorf("IsProcessRunning failed. Expected: process should be running") - } - - status = IsProcessRunning("-1") - if status { - t.Errorf("IsProcessRunning failed. Expected: Process should not run") - } -} - func TestGetProcessNameByID(t *testing.T) { pName, err := GetProcessNameByID(strconv.Itoa(os.Getpid())) if err != nil { diff --git a/platform/os_windows.go b/platform/os_windows.go index dbc829f879..0900e74462 100644 --- a/platform/os_windows.go +++ b/platform/os_windows.go @@ -152,6 +152,8 @@ func executePowershellCommand(command string) (string, error) { return "", fmt.Errorf("Failed to find powershell executable") } + log.Printf("[Azure-Utils] %s", command) + cmd := exec.Command(ps, command) var stdout bytes.Buffer var stderr bytes.Buffer @@ -198,18 +200,8 @@ func GetOSDetails() (map[string]string, error) { return nil, nil } -func IsProcessRunning(pidstr string) bool { - cmd := fmt.Sprintf("Get-Process -Id %s", pidstr) - out, err := executePowershellCommand(cmd) - if err != nil { - log.Printf("Process is not running. Output:%v, Error %v", out, err) - return false - } - - return true -} - func GetProcessNameByID(pidstr string) (string, error) { + pidstr = strings.Trim(pidstr, "\r\n") cmd := fmt.Sprintf("Get-Process -Id %s|Format-List", pidstr) out, err := executePowershellCommand(cmd) if err != nil { From eca0cb605be852bb30ddc7c445b0b663debb9389 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 3 Dec 2019 15:00:06 -0800 Subject: [PATCH 6/6] updated log --- cni/ipam/plugin/main.go | 2 +- cni/network/plugin/main.go | 2 +- cni/plugin.go | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cni/ipam/plugin/main.go b/cni/ipam/plugin/main.go index 69329a42f9..15602aee93 100644 --- a/cni/ipam/plugin/main.go +++ b/cni/ipam/plugin/main.go @@ -44,7 +44,7 @@ func main() { fmt.Printf("Failed to initialize key-value store of ipam plugin, err:%v.\n", err) if isSafe, err := ipamPlugin.Plugin.IsSafeToRemoveLock(ipamPlugin.Plugin.Name); isSafe { - log.Printf("[IPAM] Removing lock file as process holding lock exited. Error:%v", err) + log.Printf("[IPAM] Removing lock file as process holding lock exited") if errUninit := ipamPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } diff --git a/cni/network/plugin/main.go b/cni/network/plugin/main.go index 22540634b2..14c6fc84c2 100644 --- a/cni/network/plugin/main.go +++ b/cni/network/plugin/main.go @@ -189,7 +189,7 @@ func main() { } if isSafe, err := netPlugin.Plugin.IsSafeToRemoveLock(name); isSafe { - log.Printf("[CNI] Removing lock file as process holding lock exited. Error:%v", err) + log.Printf("[CNI] Removing lock file as process holding lock exited") if errUninit := netPlugin.Plugin.UninitializeKeyValueStore(true); errUninit != nil { log.Errorf("Failed to uninitialize key-value store of network plugin, err:%v.\n", errUninit) } diff --git a/cni/plugin.go b/cni/plugin.go index f840c7078e..a1ed3fe338 100644 --- a/cni/plugin.go +++ b/cni/plugin.go @@ -237,5 +237,6 @@ func (plugin *Plugin) IsSafeToRemoveLock(processName string) (bool, error) { } } + log.Errorf("Plugin store is nil") return false, fmt.Errorf("plugin store nil") }