From d8d56f79da742759d97b200486d81bf0d61461c8 Mon Sep 17 00:00:00 2001 From: Mauricio Poppe Date: Mon, 21 Nov 2022 13:29:19 -0800 Subject: [PATCH] Revert "Remove mkdir call while creating the registration probe file" --- pkg/util/util_test.go | 5 ++--- pkg/util/util_unix.go | 6 ++++++ pkg/util/util_windows.go | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 6708d63ad..87d54554d 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -28,6 +28,7 @@ import ( ) var socketFileName = "reg.sock" +var kubeletRegistrationPath = "/var/lib/kubelet/plugins/csi-dummy/registration" // TestSocketFileDoesNotExist - Test1: file does not exist. So clean up should be successful. func TestSocketFileDoesNotExist(t *testing.T) { @@ -183,9 +184,7 @@ func TestTouchFile(t *testing.T) { } defer os.RemoveAll(testDir) - // In real life, testDir would be the kubeletRegistration socket dirname - // e.g. /var/lib/kubelet/plugins// - filePath := filepath.Join(testDir, "registration") + filePath := filepath.Join(testDir, kubeletRegistrationPath) fileExists, err := DoesFileExist(filePath) if err != nil { t.Fatalf("Failed to execute file exist: %+v", err) diff --git a/pkg/util/util_unix.go b/pkg/util/util_unix.go index cc3c946c9..3a2da755f 100644 --- a/pkg/util/util_unix.go +++ b/pkg/util/util_unix.go @@ -22,6 +22,7 @@ package util import ( "fmt" "os" + "path/filepath" "golang.org/x/sys/unix" ) @@ -87,6 +88,11 @@ func TouchFile(filePath string) error { return err } if !exists { + err := os.MkdirAll(filepath.Dir(filePath), 0755) + if err != nil { + return err + } + file, err := os.Create(filePath) if err != nil { return err diff --git a/pkg/util/util_windows.go b/pkg/util/util_windows.go index e80b89490..e8f2a236e 100644 --- a/pkg/util/util_windows.go +++ b/pkg/util/util_windows.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "os" + "path/filepath" ) func Umask(mask int) (int, error) { @@ -85,6 +86,11 @@ func TouchFile(filePath string) error { return err } if !exists { + err := os.MkdirAll(filepath.Dir(filePath), 0755) + if err != nil { + return err + } + file, err := os.Create(filePath) if err != nil { return err