Skip to content

Commit 2a2f994

Browse files
authored
Merge pull request kubernetes#114187 from claudiubelu/refactor-platform-deps-3
Refactors kubelet's plugin watcher
2 parents 564f438 + 71d44a9 commit 2a2f994

File tree

5 files changed

+161
-11
lines changed

5 files changed

+161
-11
lines changed

pkg/kubelet/pluginmanager/pluginwatcher/plugin_watcher.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package pluginwatcher
1919
import (
2020
"fmt"
2121
"os"
22-
"runtime"
2322
"strings"
2423

2524
"github.com/fsnotify/fsnotify"
@@ -159,13 +158,7 @@ func (w *Watcher) traversePluginDir(dir string) error {
159158
func (w *Watcher) handleCreateEvent(event fsnotify.Event) error {
160159
klog.V(6).InfoS("Handling create event", "event", event)
161160

162-
fi, err := os.Stat(event.Name)
163-
// TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see
164-
// microsoft/Windows-Containers#97 for details.
165-
// Once the issue is resvolved, the following os.Lstat() is not needed.
166-
if err != nil && runtime.GOOS == "windows" {
167-
fi, err = os.Lstat(event.Name)
168-
}
161+
fi, err := getStat(event)
169162
if err != nil {
170163
return fmt.Errorf("stat file %s failed: %v", event.Name, err)
171164
}
@@ -192,9 +185,7 @@ func (w *Watcher) handleCreateEvent(event fsnotify.Event) error {
192185
}
193186

194187
func (w *Watcher) handlePluginRegistration(socketPath string) error {
195-
if runtime.GOOS == "windows" {
196-
socketPath = util.NormalizePath(socketPath)
197-
}
188+
socketPath = getSocketPath(socketPath)
198189
// Update desired state of world list of plugins
199190
// If the socket path does exist in the desired world cache, there's still
200191
// a possibility that it has been deleted and recreated again before it is
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2023 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package pluginwatcher
21+
22+
import (
23+
"os"
24+
25+
"github.com/fsnotify/fsnotify"
26+
)
27+
28+
func getStat(event fsnotify.Event) (os.FileInfo, error) {
29+
return os.Stat(event.Name)
30+
}
31+
32+
func getSocketPath(socketPath string) string {
33+
return socketPath
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
/*
5+
Copyright 2023 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package pluginwatcher
21+
22+
import (
23+
"os"
24+
"testing"
25+
26+
"github.com/fsnotify/fsnotify"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func TestGetStat(t *testing.T) {
31+
event := fsnotify.Event{Name: "name", Op: fsnotify.Create}
32+
fi, err := getStat(event)
33+
fiExpected, errExpected := os.Stat(event.Name)
34+
35+
assert.Equal(t, fi, fiExpected)
36+
assert.Equal(t, err, errExpected)
37+
}
38+
39+
func TestGetSocketPath(t *testing.T) {
40+
socketPath := "/tmp/foo/lish.sock"
41+
assert.Equal(t, socketPath, getSocketPath(socketPath))
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2023 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package pluginwatcher
21+
22+
import (
23+
"github.com/fsnotify/fsnotify"
24+
"k8s.io/kubernetes/pkg/kubelet/util"
25+
"os"
26+
)
27+
28+
func getStat(event fsnotify.Event) (os.FileInfo, error) {
29+
fi, err := os.Stat(event.Name)
30+
// TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see
31+
// microsoft/Windows-Containers#97 for details.
32+
// Once the issue is resvolved, the following os.Lstat() is not needed.
33+
if err != nil {
34+
fi, err = os.Lstat(event.Name)
35+
}
36+
37+
return fi, err
38+
}
39+
40+
var getSocketPath = util.NormalizePath
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build windows
2+
// +build windows
3+
4+
/*
5+
Copyright 2023 The Kubernetes Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package pluginwatcher
21+
22+
import (
23+
"os"
24+
"testing"
25+
26+
"github.com/fsnotify/fsnotify"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func TestGetStatWindows(t *testing.T) {
31+
event := fsnotify.Event{Name: "name", Op: fsnotify.Create}
32+
fi, err := getStat(event)
33+
fiExpected, errExpected := os.Stat(event.Name)
34+
// TODO: This is a workaround for Windows 20H2 issue for os.Stat(). Please see
35+
// microsoft/Windows-Containers#97 for details.
36+
// Once the issue is resvolved, the following os.Lstat() is not needed.
37+
if errExpected != nil {
38+
fiExpected, errExpected = os.Lstat(event.Name)
39+
}
40+
41+
assert.Equal(t, fi, fiExpected)
42+
assert.Equal(t, err, errExpected)
43+
}

0 commit comments

Comments
 (0)