@@ -36,6 +36,7 @@ import (
36
36
v1 "k8s.io/api/core/v1"
37
37
resourcealphaapi "k8s.io/api/resource/v1alpha3"
38
38
resourceapi "k8s.io/api/resource/v1beta1"
39
+ resourceapiv1beta2 "k8s.io/api/resource/v1beta2"
39
40
apierrors "k8s.io/apimachinery/pkg/api/errors"
40
41
"k8s.io/apimachinery/pkg/api/resource"
41
42
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -1265,6 +1266,88 @@ var _ = framework.SIGDescribe("node")("DRA", feature.DynamicResourceAllocation,
1265
1266
})
1266
1267
}
1267
1268
1269
+ v1beta2Tests := func () {
1270
+ nodes := NewNodes (f , 1 , 1 )
1271
+ maxAllocations := 1
1272
+ generateResources := func () Resources {
1273
+ return perNode (maxAllocations , nodes )()
1274
+ }
1275
+ driver := NewDriver (f , nodes , generateResources ) // All tests get their own driver instance.
1276
+ b := newBuilder (f , driver )
1277
+ // We have to set the parameters *before* creating the class.
1278
+ b .classParameters = `{"x":"y"}`
1279
+ expectedEnv := []string {"admin_x" , "y" }
1280
+ _ , expected := b .parametersEnv ()
1281
+ expectedEnv = append (expectedEnv , expected ... )
1282
+
1283
+ ginkgo .It ("supports simple ResourceClaim" , func (ctx context.Context ) {
1284
+ pod , template := b .podInlineWithV1beta2 ()
1285
+ b .create (ctx , pod , template )
1286
+ b .testPod (ctx , f , pod , expectedEnv ... )
1287
+ })
1288
+
1289
+ f .It ("supports requests with alternatives" , feature .DRAPrioritizedList , func (ctx context.Context ) {
1290
+ claimName := "external-multiclaim"
1291
+ parameters , _ := b .parametersEnv ()
1292
+ claim := & resourceapiv1beta2.ResourceClaim {
1293
+ ObjectMeta : metav1.ObjectMeta {
1294
+ Name : claimName ,
1295
+ },
1296
+ Spec : resourceapiv1beta2.ResourceClaimSpec {
1297
+ Devices : resourceapiv1beta2.DeviceClaim {
1298
+ Requests : []resourceapiv1beta2.DeviceRequest {{
1299
+ Name : "request-1" ,
1300
+ FirstAvailable : []resourceapiv1beta2.DeviceSubRequest {
1301
+ {
1302
+ Name : "sub-request-1" ,
1303
+ DeviceClassName : b .className (),
1304
+ AllocationMode : resourceapiv1beta2 .DeviceAllocationModeExactCount ,
1305
+ Count : 2 ,
1306
+ },
1307
+ {
1308
+ Name : "sub-request-2" ,
1309
+ DeviceClassName : b .className (),
1310
+ AllocationMode : resourceapiv1beta2 .DeviceAllocationModeExactCount ,
1311
+ Count : 1 ,
1312
+ },
1313
+ },
1314
+ }},
1315
+ Config : []resourceapiv1beta2.DeviceClaimConfiguration {{
1316
+ DeviceConfiguration : resourceapiv1beta2.DeviceConfiguration {
1317
+ Opaque : & resourceapiv1beta2.OpaqueDeviceConfiguration {
1318
+ Driver : b .driver .Name ,
1319
+ Parameters : runtime.RawExtension {
1320
+ Raw : []byte (parameters ),
1321
+ },
1322
+ },
1323
+ },
1324
+ }},
1325
+ },
1326
+ },
1327
+ }
1328
+ pod := b .podExternal ()
1329
+ podClaimName := "resource-claim"
1330
+ pod .Spec .ResourceClaims = []v1.PodResourceClaim {
1331
+ {
1332
+ Name : podClaimName ,
1333
+ ResourceClaimName : & claimName ,
1334
+ },
1335
+ }
1336
+ b .create (ctx , claim , pod )
1337
+ b .testPod (ctx , f , pod , expectedEnv ... )
1338
+
1339
+ var allocatedResourceClaim * resourceapi.ResourceClaim
1340
+ gomega .Eventually (ctx , func (ctx context.Context ) (* resourceapi.ResourceClaim , error ) {
1341
+ var err error
1342
+ allocatedResourceClaim , err = f .ClientSet .ResourceV1beta1 ().ResourceClaims (f .Namespace .Name ).Get (ctx , claim .Name , metav1.GetOptions {})
1343
+ return allocatedResourceClaim , err
1344
+ }).WithTimeout (f .Timeouts .PodDelete ).ShouldNot (gomega .HaveField ("Status.Allocation" , (* resourceapi .AllocationResult )(nil )))
1345
+ results := allocatedResourceClaim .Status .Allocation .Devices .Results
1346
+ gomega .Expect (results ).To (gomega .HaveLen (1 ))
1347
+ gomega .Expect (results [0 ].Request ).To (gomega .Equal ("request-1/sub-request-2" ))
1348
+ })
1349
+ }
1350
+
1268
1351
ginkgo .Context ("on single node" , func () {
1269
1352
singleNodeTests ()
1270
1353
})
@@ -1277,6 +1360,14 @@ var _ = framework.SIGDescribe("node")("DRA", feature.DynamicResourceAllocation,
1277
1360
prioritizedListTests ()
1278
1361
})
1279
1362
1363
+ ginkgo .Context ("with prioritized list" , func () {
1364
+ prioritizedListTests ()
1365
+ })
1366
+
1367
+ ginkgo .Context ("with v1beta2 API" , func () {
1368
+ v1beta2Tests ()
1369
+ })
1370
+
1280
1371
framework .Context ("with device taints" , feature .DRADeviceTaints , framework .WithFeatureGate (features .DRADeviceTaints ), func () {
1281
1372
nodes := NewNodes (f , 1 , 1 )
1282
1373
driver := NewDriver (f , nodes , func () Resources {
@@ -2100,6 +2191,34 @@ func (b *builder) claimSpec() resourceapi.ResourceClaimSpec {
2100
2191
return spec
2101
2192
}
2102
2193
2194
+ // claimSpecWithV1beta2 returns the device request for a claim or claim template
2195
+ // with the associated config using the v1beta2 API.
2196
+ func (b * builder ) claimSpecWithV1beta2 () resourceapiv1beta2.ResourceClaimSpec {
2197
+ parameters , _ := b .parametersEnv ()
2198
+ spec := resourceapiv1beta2.ResourceClaimSpec {
2199
+ Devices : resourceapiv1beta2.DeviceClaim {
2200
+ Requests : []resourceapiv1beta2.DeviceRequest {{
2201
+ Name : "my-request" ,
2202
+ Exactly : & resourceapiv1beta2.ExactDeviceRequest {
2203
+ DeviceClassName : b .className (),
2204
+ },
2205
+ }},
2206
+ Config : []resourceapiv1beta2.DeviceClaimConfiguration {{
2207
+ DeviceConfiguration : resourceapiv1beta2.DeviceConfiguration {
2208
+ Opaque : & resourceapiv1beta2.OpaqueDeviceConfiguration {
2209
+ Driver : b .driver .Name ,
2210
+ Parameters : runtime.RawExtension {
2211
+ Raw : []byte (parameters ),
2212
+ },
2213
+ },
2214
+ },
2215
+ }},
2216
+ },
2217
+ }
2218
+
2219
+ return spec
2220
+ }
2221
+
2103
2222
// parametersEnv returns the default user env variables as JSON (config) and key/value list (pod env).
2104
2223
func (b * builder ) parametersEnv () (string , []string ) {
2105
2224
return `{"a":"b"}` ,
@@ -2157,6 +2276,20 @@ func (b *builder) podInline() (*v1.Pod, *resourceapi.ResourceClaimTemplate) {
2157
2276
return pod , template
2158
2277
}
2159
2278
2279
+ func (b * builder ) podInlineWithV1beta2 () (* v1.Pod , * resourceapiv1beta2.ResourceClaimTemplate ) {
2280
+ pod , _ := b .podInline ()
2281
+ template := & resourceapiv1beta2.ResourceClaimTemplate {
2282
+ ObjectMeta : metav1.ObjectMeta {
2283
+ Name : pod .Name ,
2284
+ Namespace : pod .Namespace ,
2285
+ },
2286
+ Spec : resourceapiv1beta2.ResourceClaimTemplateSpec {
2287
+ Spec : b .claimSpecWithV1beta2 (),
2288
+ },
2289
+ }
2290
+ return pod , template
2291
+ }
2292
+
2160
2293
// podInlineMultiple returns a pod with inline resource claim referenced by 3 containers
2161
2294
func (b * builder ) podInlineMultiple () (* v1.Pod , * resourceapi.ResourceClaimTemplate ) {
2162
2295
pod , template := b .podInline ()
@@ -2211,8 +2344,12 @@ func (b *builder) create(ctx context.Context, objs ...klog.KMetadata) []klog.KMe
2211
2344
createdObj , err = b .f .ClientSet .CoreV1 ().ConfigMaps (b .f .Namespace .Name ).Create (ctx , obj , metav1.CreateOptions {})
2212
2345
case * resourceapi.ResourceClaim :
2213
2346
createdObj , err = b .f .ClientSet .ResourceV1beta1 ().ResourceClaims (b .f .Namespace .Name ).Create (ctx , obj , metav1.CreateOptions {})
2347
+ case * resourceapiv1beta2.ResourceClaim :
2348
+ createdObj , err = b .f .ClientSet .ResourceV1beta2 ().ResourceClaims (b .f .Namespace .Name ).Create (ctx , obj , metav1.CreateOptions {})
2214
2349
case * resourceapi.ResourceClaimTemplate :
2215
2350
createdObj , err = b .f .ClientSet .ResourceV1beta1 ().ResourceClaimTemplates (b .f .Namespace .Name ).Create (ctx , obj , metav1.CreateOptions {})
2351
+ case * resourceapiv1beta2.ResourceClaimTemplate :
2352
+ createdObj , err = b .f .ClientSet .ResourceV1beta2 ().ResourceClaimTemplates (b .f .Namespace .Name ).Create (ctx , obj , metav1.CreateOptions {})
2216
2353
case * resourceapi.ResourceSlice :
2217
2354
createdObj , err = b .f .ClientSet .ResourceV1beta1 ().ResourceSlices ().Create (ctx , obj , metav1.CreateOptions {})
2218
2355
ginkgo .DeferCleanup (func (ctx context.Context ) {
0 commit comments