@@ -40,6 +40,7 @@ import (
40
40
"k8s.io/kubernetes/pkg/controller"
41
41
"k8s.io/kubernetes/pkg/controller/resourceclaim/metrics"
42
42
"k8s.io/kubernetes/test/utils/ktesting"
43
+ "k8s.io/utils/ptr"
43
44
)
44
45
45
46
var (
@@ -61,13 +62,15 @@ var (
61
62
testClaimReserved = reserveClaim (testClaimAllocated , testPodWithResource )
62
63
testClaimReservedTwice = reserveClaim (testClaimReserved , otherTestPod )
63
64
64
- generatedTestClaim = makeGeneratedClaim (podResourceClaimName , testPodName + "-" + podResourceClaimName + "-" , testNamespace , className , 1 , makeOwnerReference (testPodWithResource , true ))
65
+ generatedTestClaim = makeGeneratedClaim (podResourceClaimName , testPodName + "-" + podResourceClaimName + "-" , testNamespace , className , 1 , makeOwnerReference (testPodWithResource , true ), nil )
66
+ generatedTestClaimWithAdmin = makeGeneratedClaim (podResourceClaimName , testPodName + "-" + podResourceClaimName + "-" , testNamespace , className , 1 , makeOwnerReference (testPodWithResource , true ), ptr .To (true ))
65
67
generatedTestClaimAllocated = allocateClaim (generatedTestClaim )
66
68
generatedTestClaimReserved = reserveClaim (generatedTestClaimAllocated , testPodWithResource )
67
69
68
- conflictingClaim = makeClaim (testPodName + "-" + podResourceClaimName , testNamespace , className , nil )
69
- otherNamespaceClaim = makeClaim (testPodName + "-" + podResourceClaimName , otherNamespace , className , nil )
70
- template = makeTemplate (templateName , testNamespace , className )
70
+ conflictingClaim = makeClaim (testPodName + "-" + podResourceClaimName , testNamespace , className , nil )
71
+ otherNamespaceClaim = makeClaim (testPodName + "-" + podResourceClaimName , otherNamespace , className , nil )
72
+ template = makeTemplate (templateName , testNamespace , className , nil )
73
+ templateWithAdminAccess = makeTemplate (templateName , testNamespace , className , ptr .To (true ))
71
74
72
75
testPodWithNodeName = func () * v1.Pod {
73
76
pod := testPodWithResource .DeepCopy ()
78
81
})
79
82
return pod
80
83
}()
84
+ adminAccessFeatureOffError = "admin access is requested, but the feature is disabled"
81
85
)
82
86
83
87
func TestSyncHandler (t * testing.T ) {
@@ -93,7 +97,7 @@ func TestSyncHandler(t *testing.T) {
93
97
templates []* resourceapi.ResourceClaimTemplate
94
98
expectedClaims []resourceapi.ResourceClaim
95
99
expectedStatuses map [string ][]v1.PodResourceClaimStatus
96
- expectedError bool
100
+ expectedError string
97
101
expectedMetrics expectedMetrics
98
102
}{
99
103
{
@@ -109,6 +113,27 @@ func TestSyncHandler(t *testing.T) {
109
113
},
110
114
expectedMetrics : expectedMetrics {1 , 0 },
111
115
},
116
+ {
117
+ name : "create with admin and feature gate off" ,
118
+ pods : []* v1.Pod {testPodWithResource },
119
+ templates : []* resourceapi.ResourceClaimTemplate {templateWithAdminAccess },
120
+ key : podKey (testPodWithResource ),
121
+ expectedError : adminAccessFeatureOffError ,
122
+ },
123
+ {
124
+ name : "create with admin and feature gate on" ,
125
+ pods : []* v1.Pod {testPodWithResource },
126
+ templates : []* resourceapi.ResourceClaimTemplate {templateWithAdminAccess },
127
+ key : podKey (testPodWithResource ),
128
+ expectedClaims : []resourceapi.ResourceClaim {* generatedTestClaimWithAdmin },
129
+ expectedStatuses : map [string ][]v1.PodResourceClaimStatus {
130
+ testPodWithResource .Name : {
131
+ {Name : testPodWithResource .Spec .ResourceClaims [0 ].Name , ResourceClaimName : & generatedTestClaimWithAdmin .Name },
132
+ },
133
+ },
134
+ adminAccessEnabled : true ,
135
+ expectedMetrics : expectedMetrics {1 , 0 },
136
+ },
112
137
{
113
138
name : "nop" ,
114
139
pods : []* v1.Pod {func () * v1.Pod {
@@ -153,7 +178,7 @@ func TestSyncHandler(t *testing.T) {
153
178
pods : []* v1.Pod {testPodWithResource },
154
179
templates : nil ,
155
180
key : podKey (testPodWithResource ),
156
- expectedError : true ,
181
+ expectedError : "resource claim template \" my-template \" : resourceclaimtemplate.resource.k8s.io \" my-template \" not found" ,
157
182
},
158
183
{
159
184
name : "find-existing-claim-by-label" ,
@@ -219,15 +244,15 @@ func TestSyncHandler(t *testing.T) {
219
244
key : podKey (testPodWithResource ),
220
245
claims : []* resourceapi.ResourceClaim {conflictingClaim },
221
246
expectedClaims : []resourceapi.ResourceClaim {* conflictingClaim },
222
- expectedError : true ,
247
+ expectedError : "resource claim template \" my-template \" : resourceclaimtemplate.resource.k8s.io \" my-template \" not found" ,
223
248
},
224
249
{
225
250
name : "create-conflict" ,
226
251
pods : []* v1.Pod {testPodWithResource },
227
252
templates : []* resourceapi.ResourceClaimTemplate {template },
228
253
key : podKey (testPodWithResource ),
229
254
expectedMetrics : expectedMetrics {1 , 1 },
230
- expectedError : true ,
255
+ expectedError : "create ResourceClaim : Operation cannot be fulfilled on resourceclaims.resource.k8s.io \" fake name \" : fake conflict" ,
231
256
},
232
257
{
233
258
name : "stay-reserved-seen" ,
@@ -424,11 +449,12 @@ func TestSyncHandler(t *testing.T) {
424
449
}
425
450
426
451
err = ec .syncHandler (tCtx , tc .key )
427
- if err != nil && ! tc .expectedError {
428
- t .Fatalf ("unexpected error while running handler: %v" , err )
452
+ if err != nil {
453
+ assert .ErrorContains (t , err , tc .expectedError , "the error message should have contained the expected error message" )
454
+ return
429
455
}
430
- if err == nil && tc .expectedError {
431
- t .Fatalf ("unexpected success " )
456
+ if tc .expectedError != "" {
457
+ t .Fatalf ("expected error, got none " )
432
458
}
433
459
434
460
claims , err := fakeKubeClient .ResourceV1beta1 ().ResourceClaims ("" ).List (tCtx , metav1.ListOptions {})
@@ -558,7 +584,7 @@ func makeClaim(name, namespace, classname string, owner *metav1.OwnerReference)
558
584
return claim
559
585
}
560
586
561
- func makeGeneratedClaim (podClaimName , generateName , namespace , classname string , createCounter int , owner * metav1.OwnerReference ) * resourceapi.ResourceClaim {
587
+ func makeGeneratedClaim (podClaimName , generateName , namespace , classname string , createCounter int , owner * metav1.OwnerReference , adminAccess * bool ) * resourceapi.ResourceClaim {
562
588
claim := & resourceapi.ResourceClaim {
563
589
ObjectMeta : metav1.ObjectMeta {
564
590
Name : fmt .Sprintf ("%s-%d" , generateName , createCounter ),
@@ -570,6 +596,19 @@ func makeGeneratedClaim(podClaimName, generateName, namespace, classname string,
570
596
if owner != nil {
571
597
claim .OwnerReferences = []metav1.OwnerReference {* owner }
572
598
}
599
+ if adminAccess != nil {
600
+ claim .Spec = resourceapi.ResourceClaimSpec {
601
+ Devices : resourceapi.DeviceClaim {
602
+ Requests : []resourceapi.DeviceRequest {
603
+ {
604
+ Name : "req-0" ,
605
+ DeviceClassName : "class" ,
606
+ AdminAccess : adminAccess ,
607
+ },
608
+ },
609
+ },
610
+ }
611
+ }
573
612
574
613
return claim
575
614
}
@@ -618,10 +657,25 @@ func makePod(name, namespace string, uid types.UID, podClaims ...v1.PodResourceC
618
657
return pod
619
658
}
620
659
621
- func makeTemplate (name , namespace , classname string ) * resourceapi.ResourceClaimTemplate {
660
+ func makeTemplate (name , namespace , classname string , adminAccess * bool ) * resourceapi.ResourceClaimTemplate {
622
661
template := & resourceapi.ResourceClaimTemplate {
623
662
ObjectMeta : metav1.ObjectMeta {Name : name , Namespace : namespace },
624
663
}
664
+ if adminAccess != nil {
665
+ template .Spec = resourceapi.ResourceClaimTemplateSpec {
666
+ Spec : resourceapi.ResourceClaimSpec {
667
+ Devices : resourceapi.DeviceClaim {
668
+ Requests : []resourceapi.DeviceRequest {
669
+ {
670
+ Name : "req-0" ,
671
+ DeviceClassName : "class" ,
672
+ AdminAccess : adminAccess ,
673
+ },
674
+ },
675
+ },
676
+ },
677
+ }
678
+ }
625
679
return template
626
680
}
627
681
0 commit comments