@@ -22,7 +22,10 @@ import (
22
22
"github.com/stretchr/testify/require"
23
23
24
24
v1 "k8s.io/api/core/v1"
25
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
26
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
25
27
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
28
+ pkgfeatures "k8s.io/kubernetes/pkg/features"
26
29
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
27
30
kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
28
31
)
@@ -162,10 +165,25 @@ func TestPodSandboxChanged(t *testing.T) {
162
165
}
163
166
}
164
167
168
+ type fakeRuntimeHandlerResolver struct {}
169
+
170
+ func (* fakeRuntimeHandlerResolver ) LookupRuntimeHandler (s * string ) (string , error ) {
171
+ return "" , nil
172
+ }
173
+
165
174
func TestNamespacesForPod (t * testing.T ) {
175
+ usernsIDs := & runtimeapi.IDMapping {
176
+ HostId : 65536 ,
177
+ ContainerId : 0 ,
178
+ Length : 65536 ,
179
+ }
180
+
166
181
for desc , test := range map [string ]struct {
167
- input * v1.Pod
168
- expected * runtimeapi.NamespaceOption
182
+ input * v1.Pod
183
+ runtimeHandlers map [string ]kubecontainer.RuntimeHandler
184
+ usernsEnabled bool
185
+ expected * runtimeapi.NamespaceOption
186
+ expErr bool
169
187
}{
170
188
"nil pod -> default v1 namespaces" : {
171
189
input : nil ,
@@ -221,11 +239,84 @@ func TestNamespacesForPod(t *testing.T) {
221
239
Pid : runtimeapi .NamespaceMode_CONTAINER ,
222
240
},
223
241
},
242
+ "hostUsers: false and feature enabled" : {
243
+ input : & v1.Pod {
244
+ Spec : v1.PodSpec {
245
+ HostUsers : & []bool {false }[0 ],
246
+ },
247
+ },
248
+ usernsEnabled : true ,
249
+ runtimeHandlers : map [string ]kubecontainer.RuntimeHandler {
250
+ "" : {
251
+ SupportsUserNamespaces : true ,
252
+ },
253
+ },
254
+ expected : & runtimeapi.NamespaceOption {
255
+ Ipc : runtimeapi .NamespaceMode_POD ,
256
+ Network : runtimeapi .NamespaceMode_POD ,
257
+ Pid : runtimeapi .NamespaceMode_CONTAINER ,
258
+ UsernsOptions : & runtimeapi.UserNamespace {
259
+ Mode : runtimeapi .NamespaceMode_POD ,
260
+ Uids : []* runtimeapi.IDMapping {usernsIDs },
261
+ Gids : []* runtimeapi.IDMapping {usernsIDs },
262
+ },
263
+ },
264
+ },
265
+ // The hostUsers field can't be set to any value if the feature is disabled.
266
+ "hostUsers: false and feature disabled --> error" : {
267
+ input : & v1.Pod {
268
+ Spec : v1.PodSpec {
269
+ HostUsers : & []bool {false }[0 ],
270
+ },
271
+ },
272
+ usernsEnabled : false ,
273
+ expErr : true ,
274
+ },
275
+ // The hostUsers field can't be set to any value if the feature is disabled.
276
+ "hostUsers: true and feature disabled --> error" : {
277
+ input : & v1.Pod {
278
+ Spec : v1.PodSpec {
279
+ HostUsers : & []bool {true }[0 ],
280
+ },
281
+ },
282
+ usernsEnabled : false ,
283
+ expErr : true ,
284
+ },
285
+ "error if runtime handler not found" : {
286
+ input : & v1.Pod {
287
+ Spec : v1.PodSpec {
288
+ HostUsers : & []bool {false }[0 ],
289
+ },
290
+ },
291
+ usernsEnabled : true ,
292
+ runtimeHandlers : map [string ]kubecontainer.RuntimeHandler {
293
+ "other" : {},
294
+ },
295
+ expErr : true ,
296
+ },
297
+ "error if runtime handler does not support userns" : {
298
+ input : & v1.Pod {
299
+ Spec : v1.PodSpec {
300
+ HostUsers : & []bool {false }[0 ],
301
+ },
302
+ },
303
+ usernsEnabled : true ,
304
+ expErr : true ,
305
+ },
224
306
} {
225
307
t .Run (desc , func (t * testing.T ) {
226
- actual , err := NamespacesForPod (test .input , & kubecontainertest.FakeRuntimeHelper {}, nil )
227
- require .NoError (t , err )
228
- require .Equal (t , test .expected , actual )
308
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , pkgfeatures .UserNamespacesSupport , test .usernsEnabled )
309
+
310
+ fakeRuntimeHelper := kubecontainertest.FakeRuntimeHelper {
311
+ RuntimeHandlers : test .runtimeHandlers ,
312
+ }
313
+ actual , err := NamespacesForPod (test .input , & fakeRuntimeHelper , & fakeRuntimeHandlerResolver {})
314
+ if test .expErr {
315
+ require .Error (t , err )
316
+ } else {
317
+ require .NoError (t , err )
318
+ require .Equal (t , test .expected , actual )
319
+ }
229
320
})
230
321
}
231
322
}
0 commit comments