Skip to content

Commit d6c50c3

Browse files
committed
node audience restriction: add unit tests to simulate intree inline_vol/pv to csi failure
Signed-off-by: Anish Ramasekar <[email protected]>
1 parent 82e3a67 commit d6c50c3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

plugin/pkg/admission/noderestriction/admission_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,19 @@ func Test_nodePlugin_Admit(t *testing.T) {
408408
},
409409
}
410410

411+
azureFileCSIDriver = &storagev1.CSIDriver{
412+
ObjectMeta: metav1.ObjectMeta{
413+
Name: "file.csi.azure.com",
414+
},
415+
Spec: storagev1.CSIDriverSpec{
416+
TokenRequests: []storagev1.TokenRequest{
417+
{
418+
Audience: "foo",
419+
},
420+
},
421+
},
422+
}
423+
411424
csiDriverIndex = cache.NewIndexer(cache.MetaNamespaceKeyFunc, nil)
412425
csiDriverLister = storagelisters.NewCSIDriverLister(csiDriverIndex)
413426

@@ -424,6 +437,16 @@ func Test_nodePlugin_Admit(t *testing.T) {
424437
},
425438
}
426439

440+
pvcWithIntreeAzureFile = &corev1.PersistentVolumeClaim{
441+
ObjectMeta: metav1.ObjectMeta{
442+
Name: "pvclaim-azurefile",
443+
Namespace: "ns",
444+
},
445+
Spec: corev1.PersistentVolumeClaimSpec{
446+
VolumeName: "pvname-azurefile",
447+
},
448+
}
449+
427450
ephemeralVolumePVCWithCSIDriver = &corev1.PersistentVolumeClaim{
428451
ObjectMeta: metav1.ObjectMeta{
429452
Name: "myephemeralpod-myvol",
@@ -451,6 +474,20 @@ func Test_nodePlugin_Admit(t *testing.T) {
451474
},
452475
}
453476

477+
pvWithIntreeAzureFile = &corev1.PersistentVolume{
478+
ObjectMeta: metav1.ObjectMeta{
479+
Name: "pvname-azurefile",
480+
},
481+
Spec: corev1.PersistentVolumeSpec{
482+
ClaimRef: &corev1.ObjectReference{
483+
Namespace: "ns",
484+
},
485+
PersistentVolumeSource: corev1.PersistentVolumeSource{
486+
AzureFile: &corev1.AzureFilePersistentVolumeSource{ShareName: "default", SecretName: "secret"},
487+
},
488+
},
489+
}
490+
454491
pvIndex = cache.NewIndexer(cache.MetaNamespaceKeyFunc, nil)
455492
pvLister = corev1lister.NewPersistentVolumeLister(pvIndex)
456493

@@ -463,6 +500,7 @@ func Test_nodePlugin_Admit(t *testing.T) {
463500
projectedVolumeSource := &corev1.ProjectedVolumeSource{Sources: []corev1.VolumeProjection{{ServiceAccountToken: &corev1.ServiceAccountTokenProjection{Audience: "foo"}}}}
464501
csiDriverVolumeSource := &corev1.CSIVolumeSource{Driver: "com.example.csi.mydriver"}
465502
persistentVolumeClaimVolumeSource := &corev1.PersistentVolumeClaimVolumeSource{ClaimName: "pvclaim"}
503+
persistentVolumeClaimVolumeSourceAzureFile := &corev1.PersistentVolumeClaimVolumeSource{ClaimName: "pvclaim-azurefile"}
466504
ephemeralVolumeSource := &corev1.EphemeralVolumeSource{VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{}}
467505

468506
coremypodWithProjectedServiceAccountEmptyAudience, v1mypodWithProjectedServiceAccountEmptyAudience := makeTestPod("ns", "mysapod", "mynode", false)
@@ -483,10 +521,19 @@ func Test_nodePlugin_Admit(t *testing.T) {
483521
coremypodWithPVCAndCSI, v1mypodWithPVCAndCSI := makeTestPod("ns", "mypvcandcsipod", "mynode", false)
484522
v1mypodWithPVCAndCSI.Spec.Volumes = []corev1.Volume{{VolumeSource: corev1.VolumeSource{PersistentVolumeClaim: persistentVolumeClaimVolumeSource}}, {VolumeSource: corev1.VolumeSource{CSI: csiDriverVolumeSource}}}
485523

524+
coremypodIntreeInlineVolToCSI, v1mypodIntreeInlineVolToCSI := makeTestPod("ns", "myintreeinlinevoltocsipod", "mynode", false)
525+
v1mypodIntreeInlineVolToCSI.Spec.Volumes = []corev1.Volume{{VolumeSource: corev1.VolumeSource{AzureFile: &corev1.AzureFileVolumeSource{ShareName: "default", SecretName: "secret"}}}}
526+
527+
coremypodIntreePVToCSI, v1mypodIntreePVToCSI := makeTestPod("ns", "myintreepvtocsipod", "mynode", false)
528+
v1mypodIntreePVToCSI.Spec.Volumes = []corev1.Volume{{VolumeSource: corev1.VolumeSource{PersistentVolumeClaim: persistentVolumeClaimVolumeSourceAzureFile}}}
529+
486530
checkNilError(t, csiDriverIndex.Add(csiDriverWithAudience))
531+
checkNilError(t, csiDriverIndex.Add(azureFileCSIDriver))
487532
checkNilError(t, pvcIndex.Add(pvcWithCSIDriver))
533+
checkNilError(t, pvcIndex.Add(pvcWithIntreeAzureFile))
488534
checkNilError(t, pvcIndex.Add(ephemeralVolumePVCWithCSIDriver))
489535
checkNilError(t, pvIndex.Add(pvWithCSIDriver))
536+
checkNilError(t, pvIndex.Add(pvWithIntreeAzureFile))
490537

491538
existingPodsIndex.Add(v1mymirrorpod)
492539
existingPodsIndex.Add(v1othermirrorpod)
@@ -501,6 +548,8 @@ func Test_nodePlugin_Admit(t *testing.T) {
501548
checkNilError(t, existingPodsIndex.Add(v1mypodWithPVCRefCSI))
502549
checkNilError(t, existingPodsIndex.Add(v1mypodWithEphemeralVolume))
503550
checkNilError(t, existingPodsIndex.Add(v1mypodWithPVCAndCSI))
551+
checkNilError(t, existingPodsIndex.Add(v1mypodIntreePVToCSI))
552+
checkNilError(t, existingPodsIndex.Add(v1mypodIntreeInlineVolToCSI))
504553

505554
existingNodesIndex.Add(&corev1.Node{ObjectMeta: mynodeObjMeta})
506555

@@ -1430,6 +1479,30 @@ func Test_nodePlugin_Admit(t *testing.T) {
14301479
},
14311480
attributes: admission.NewAttributesRecord(makeTokenRequest(coremypodWithCSI.Name, v1mypodWithCSI.UID, []string{"foo"}), nil, tokenrequestKind, coremypod.Namespace, "mysa", svcacctResource, "token", admission.Create, &metav1.CreateOptions{}, false, mynode),
14321481
},
1482+
{
1483+
name: "intree pv to csi, allow create of token when audience in pod --> csi --> driver --> tokenrequest with audience, ServiceAccountNodeAudienceRestriction=true",
1484+
podsGetter: existingPods,
1485+
csiDriverGetter: csiDriverLister,
1486+
pvcGetter: pvcLister,
1487+
pvGetter: pvLister,
1488+
features: feature.DefaultFeatureGate,
1489+
setupFunc: func(t *testing.T) {
1490+
t.Helper()
1491+
featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.ServiceAccountNodeAudienceRestriction, true)
1492+
},
1493+
attributes: admission.NewAttributesRecord(makeTokenRequest(coremypodIntreePVToCSI.Name, v1mypodIntreePVToCSI.UID, []string{"foo"}), nil, tokenrequestKind, coremypod.Namespace, "mysa", svcacctResource, "token", admission.Create, &metav1.CreateOptions{}, false, mynode),
1494+
},
1495+
{
1496+
name: "intree inline vol to csi, allow create of token when audience in pod --> csi --> driver --> tokenrequest with audience, ServiceAccountNodeAudienceRestriction=true",
1497+
podsGetter: existingPods,
1498+
csiDriverGetter: csiDriverLister,
1499+
features: feature.DefaultFeatureGate,
1500+
setupFunc: func(t *testing.T) {
1501+
t.Helper()
1502+
featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.ServiceAccountNodeAudienceRestriction, true)
1503+
},
1504+
attributes: admission.NewAttributesRecord(makeTokenRequest(coremypodIntreeInlineVolToCSI.Name, v1mypodIntreeInlineVolToCSI.UID, []string{"foo"}), nil, tokenrequestKind, coremypod.Namespace, "mysa", svcacctResource, "token", admission.Create, &metav1.CreateOptions{}, false, mynode),
1505+
},
14331506

14341507
// Unrelated objects
14351508
{

0 commit comments

Comments
 (0)