Skip to content

Commit 8b4aab5

Browse files
committed
e2e_node: add a test to verify kubelet fails to create pod if userns isn't supported
and the pod requests a user namespace Signed-off-by: Peter Hunt <[email protected]>
1 parent aa35eff commit 8b4aab5

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

test/e2e_node/user_namespaces_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//go:build linux
2+
// +build linux
3+
4+
/*
5+
Copyright 2024 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 e2enode
21+
22+
import (
23+
"context"
24+
"fmt"
25+
"time"
26+
27+
"github.com/onsi/gomega"
28+
v1 "k8s.io/api/core/v1"
29+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
kubefeatures "k8s.io/kubernetes/pkg/features"
31+
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
32+
"k8s.io/kubernetes/test/e2e/feature"
33+
"k8s.io/kubernetes/test/e2e/framework"
34+
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
35+
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
36+
imageutils "k8s.io/kubernetes/test/utils/image"
37+
admissionapi "k8s.io/pod-security-admission/api"
38+
)
39+
40+
var _ = SIGDescribe("UserNamespaces", "[LinuxOnly]", feature.UserNamespacesSupport, framework.WithSerial(), func() {
41+
f := framework.NewDefaultFramework("user-namespace-off-test")
42+
f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
43+
f.Context("when UserNamespacesSupport=false in the kubelet", func() {
44+
// Turn off UserNamespacesSupport for this test
45+
// TODO: once the UserNamespacesSupport feature is removed, this test should be removed too
46+
tempSetCurrentKubeletConfig(f, func(ctx context.Context, initialConfig *kubeletconfig.KubeletConfiguration) {
47+
if initialConfig.FeatureGates == nil {
48+
initialConfig.FeatureGates = make(map[string]bool)
49+
}
50+
initialConfig.FeatureGates[string(kubefeatures.UserNamespacesSupport)] = false
51+
})
52+
f.It("will fail to create a hostUsers=false pod", func(ctx context.Context) {
53+
if on, ok := serviceFeatureGates[string(kubefeatures.UserNamespacesSupport)]; !ok || !on {
54+
e2eskipper.Skipf("services do not have user namespaces on")
55+
}
56+
falseVar := false
57+
podClient := e2epod.NewPodClient(f)
58+
pod, err := podClient.PodInterface.Create(ctx, &v1.Pod{
59+
ObjectMeta: metav1.ObjectMeta{Name: "userns-pod"},
60+
Spec: v1.PodSpec{
61+
Containers: []v1.Container{
62+
{
63+
Name: "test-container-1",
64+
Image: imageutils.GetE2EImage(imageutils.BusyBox),
65+
Command: []string{"/bin/sleep"},
66+
Args: []string{"10000"},
67+
},
68+
},
69+
HostUsers: &falseVar,
70+
},
71+
}, metav1.CreateOptions{})
72+
framework.ExpectNoError(err)
73+
74+
// Pod should stay in pending
75+
// Events would be a better way to tell this, as we could actually read the event,
76+
// but history proves events aren't reliable enough to base a test on.
77+
gomega.Consistently(ctx, func() error {
78+
p, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(ctx, pod.Name, metav1.GetOptions{})
79+
if err != nil {
80+
return err
81+
}
82+
if p.Status.Phase != v1.PodPending {
83+
return fmt.Errorf("Pod phase isn't pending")
84+
}
85+
return nil
86+
}, 30*time.Second, 5*time.Second).ShouldNot(gomega.HaveOccurred())
87+
})
88+
})
89+
})

0 commit comments

Comments
 (0)