diff --git a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher.go b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher.go index 35c51683472..44a08012c0d 100644 --- a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher.go +++ b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher.go @@ -19,7 +19,6 @@ package pluginwatcher import ( "fmt" "os" - "runtime" "strings" "github.com/fsnotify/fsnotify" @@ -159,13 +158,7 @@ func (w *Watcher) traversePluginDir(dir string) error { func (w *Watcher) handleCreateEvent(event fsnotify.Event) error { klog.V(6).InfoS("Handling create event", "event", event) - fi, err := os.Stat(event.Name) - // TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see - // microsoft/Windows-Containers#97 for details. - // Once the issue is resvolved, the following os.Lstat() is not needed. - if err != nil && runtime.GOOS == "windows" { - fi, err = os.Lstat(event.Name) - } + fi, err := getStat(event) if err != nil { return fmt.Errorf("stat file %s failed: %v", event.Name, err) } @@ -192,9 +185,7 @@ func (w *Watcher) handleCreateEvent(event fsnotify.Event) error { } func (w *Watcher) handlePluginRegistration(socketPath string) error { - if runtime.GOOS == "windows" { - socketPath = util.NormalizePath(socketPath) - } + socketPath = getSocketPath(socketPath) // Update desired state of world list of plugins // If the socket path does exist in the desired world cache, there's still // a possibility that it has been deleted and recreated again before it is diff --git a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others.go b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others.go new file mode 100644 index 00000000000..2ecb570238e --- /dev/null +++ b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others.go @@ -0,0 +1,34 @@ +//go:build !windows +// +build !windows + +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pluginwatcher + +import ( + "os" + + "github.com/fsnotify/fsnotify" +) + +func getStat(event fsnotify.Event) (os.FileInfo, error) { + return os.Stat(event.Name) +} + +func getSocketPath(socketPath string) string { + return socketPath +} diff --git a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others_test.go b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others_test.go new file mode 100644 index 00000000000..ea09b6791bb --- /dev/null +++ b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_others_test.go @@ -0,0 +1,42 @@ +//go:build !windows +// +build !windows + +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pluginwatcher + +import ( + "os" + "testing" + + "github.com/fsnotify/fsnotify" + "github.com/stretchr/testify/assert" +) + +func TestGetStat(t *testing.T) { + event := fsnotify.Event{Name: "name", Op: fsnotify.Create} + fi, err := getStat(event) + fiExpected, errExpected := os.Stat(event.Name) + + assert.Equal(t, fi, fiExpected) + assert.Equal(t, err, errExpected) +} + +func TestGetSocketPath(t *testing.T) { + socketPath := "/tmp/foo/lish.sock" + assert.Equal(t, socketPath, getSocketPath(socketPath)) +} diff --git a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows.go b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows.go new file mode 100644 index 00000000000..476ae4fc0c7 --- /dev/null +++ b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows.go @@ -0,0 +1,40 @@ +//go:build windows +// +build windows + +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pluginwatcher + +import ( + "github.com/fsnotify/fsnotify" + "k8s.io/kubernetes/pkg/kubelet/util" + "os" +) + +func getStat(event fsnotify.Event) (os.FileInfo, error) { + fi, err := os.Stat(event.Name) + // TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see + // microsoft/Windows-Containers#97 for details. + // Once the issue is resvolved, the following os.Lstat() is not needed. + if err != nil { + fi, err = os.Lstat(event.Name) + } + + return fi, err +} + +var getSocketPath = util.NormalizePath diff --git a/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows_test.go b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows_test.go new file mode 100644 index 00000000000..ea4586eb1cd --- /dev/null +++ b/pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher_windows_test.go @@ -0,0 +1,43 @@ +//go:build windows +// +build windows + +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pluginwatcher + +import ( + "os" + "testing" + + "github.com/fsnotify/fsnotify" + "github.com/stretchr/testify/assert" +) + +func TestGetStatWindows(t *testing.T) { + event := fsnotify.Event{Name: "name", Op: fsnotify.Create} + fi, err := getStat(event) + fiExpected, errExpected := os.Stat(event.Name) + // TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see + // microsoft/Windows-Containers#97 for details. + // Once the issue is resvolved, the following os.Lstat() is not needed. + if errExpected != nil { + fiExpected, errExpected = os.Lstat(event.Name) + } + + assert.Equal(t, fi, fiExpected) + assert.Equal(t, err, errExpected) +}