Skip to content

Commit a44b0a3

Browse files
authored
Merge pull request kubernetes#131787 from rata/automated-cherry-pick-of-#130800-upstream-release-1.31
Automated cherry pick of kubernetes#130800: Fix unit tests on windows
2 parents ec21688 + c9dbae7 commit a44b0a3

File tree

7 files changed

+97
-16
lines changed

7 files changed

+97
-16
lines changed

pkg/kubelet/kubelet_pods.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ func (kl *Kubelet) getKubeletMappings() (uint32, uint32, error) {
143143
}
144144
}
145145

146+
// Windows doesn't support user namespaces, let's return the default mappings.
147+
if runtime.GOOS == "windows" {
148+
return defaultFirstID, defaultLen, nil
149+
}
150+
146151
_, err := user.Lookup(kubeletUser)
147152
if err != nil {
148153
var unknownUserErr user.UnknownUserError

pkg/kubelet/userns/types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package userns
18+
19+
import "k8s.io/apimachinery/pkg/types"
20+
21+
// Here go types that are common for all supported OS (windows, linux).
22+
23+
type userNsPodsManager interface {
24+
HandlerSupportsUserNamespaces(runtimeHandler string) (bool, error)
25+
GetPodDir(podUID types.UID) string
26+
ListPodsFromDisk() ([]types.UID, error)
27+
GetKubeletMappings() (uint32, uint32, error)
28+
GetMaxPods() int
29+
}

pkg/kubelet/userns/userns_manager.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2022 The Kubernetes Authors.
36
@@ -43,14 +46,6 @@ const userNsLength = (1 << 16)
4346
// since Go maps never free memory.
4447
const mapReInitializeThreshold = 1000
4548

46-
type userNsPodsManager interface {
47-
HandlerSupportsUserNamespaces(runtimeHandler string) (bool, error)
48-
GetPodDir(podUID types.UID) string
49-
ListPodsFromDisk() ([]types.UID, error)
50-
GetKubeletMappings() (uint32, uint32, error)
51-
GetMaxPods() int
52-
}
53-
5449
type UsernsManager struct {
5550
used *allocator.AllocationBitmap
5651
usedBy map[types.UID]uint32 // Map pod.UID to range used
@@ -132,7 +127,7 @@ func (m *UsernsManager) readMappingsFromFile(pod types.UID) ([]byte, error) {
132127
func MakeUserNsManager(kl userNsPodsManager) (*UsernsManager, error) {
133128
kubeletMappingID, kubeletMappingLen, err := kl.GetKubeletMappings()
134129
if err != nil {
135-
return nil, err
130+
return nil, fmt.Errorf("kubelet mappings: %w", err)
136131
}
137132

138133
if kubeletMappingID%userNsLength != 0 {

pkg/kubelet/userns/userns_manager_disabled_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2022 The Kubernetes Authors.
36

pkg/kubelet/userns/userns_manager_switch_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2024 The Kubernetes Authors.
36

pkg/kubelet/userns/userns_manager_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright 2022 The Kubernetes Authors.
36
@@ -20,7 +23,6 @@ import (
2023
"errors"
2124
"fmt"
2225
"os"
23-
goruntime "runtime"
2426
"testing"
2527

2628
"github.com/stretchr/testify/assert"
@@ -289,7 +291,6 @@ func TestGetOrCreateUserNamespaceMappings(t *testing.T) {
289291
runtimeUserns bool
290292
runtimeHandler string
291293
success bool
292-
skipOnWindows bool
293294
}{
294295
{
295296
name: "no user namespace",
@@ -323,7 +324,6 @@ func TestGetOrCreateUserNamespaceMappings(t *testing.T) {
323324
expMode: runtimeapi.NamespaceMode_POD,
324325
runtimeUserns: true,
325326
success: true,
326-
skipOnWindows: true,
327327
},
328328
{
329329
name: "user namespace, but no runtime support",
@@ -348,10 +348,6 @@ func TestGetOrCreateUserNamespaceMappings(t *testing.T) {
348348

349349
for _, tc := range cases {
350350
t.Run(tc.name, func(t *testing.T) {
351-
if tc.skipOnWindows && goruntime.GOOS == "windows" {
352-
// TODO: remove skip once the failing test has been fixed.
353-
t.Skip("Skip failing test on Windows.")
354-
}
355351
// These tests will create the userns file, so use an existing podDir.
356352
testUserNsPodsManager := &testUserNsPodsManager{
357353
podDir: t.TempDir(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package userns
18+
19+
import (
20+
v1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/types"
22+
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
23+
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
24+
)
25+
26+
type UsernsManager struct{}
27+
28+
func MakeUserNsManager(kl userNsPodsManager) (*UsernsManager, error) {
29+
return nil, nil
30+
}
31+
32+
// Release releases the user namespace allocated to the specified pod.
33+
func (m *UsernsManager) Release(podUID types.UID) {
34+
return
35+
}
36+
37+
func (m *UsernsManager) GetOrCreateUserNamespaceMappings(pod *v1.Pod, runtimeHandler string) (*runtimeapi.UserNamespace, error) {
38+
return nil, nil
39+
}
40+
41+
// CleanupOrphanedPodUsernsAllocations reconciliates the state of user namespace
42+
// allocations with the pods actually running. It frees any user namespace
43+
// allocation for orphaned pods.
44+
func (m *UsernsManager) CleanupOrphanedPodUsernsAllocations(pods []*v1.Pod, runningPods []*kubecontainer.Pod) error {
45+
return nil
46+
}
47+
48+
func EnabledUserNamespacesSupport() bool {
49+
return false
50+
}

0 commit comments

Comments
 (0)