Refactors kubelet's plugin watcher

Refactors platform specific code into separate files (*_linux.go / *_windows.go / *_others.go)

Adds unit tests for the pluginwatcher changes.
This commit is contained in:
sorkinl 2022-05-02 12:44:36 -04:00 committed by Claudiu Belu
parent 6bdda2da16
commit 71d44a9348
5 changed files with 161 additions and 11 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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))
}

View File

@ -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

View File

@ -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)
}