Skip to content

Commit 3019702

Browse files
committed
add state to resource claim status
Signed-off-by: Varun Ramachandra Sekar <[email protected]>
1 parent 2fb2e00 commit 3019702

File tree

6 files changed

+156
-34
lines changed

6 files changed

+156
-34
lines changed

api/apps/v1alpha1/common_types.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,25 @@ type DRAResource struct {
295295

296296
// DRAResourceStatus defines the status of the DRAResource.
297297
type DRAResourceStatus struct {
298-
// Name is the generated name of the DRAResource referenced in the NIMService
299-
// pod template as `spec.resourceClaims[].name`.
300298
Name string `json:"name"`
301299
// ResourceClaimTemplateName is the name of the ResourceClaimTemplate that was
302300
// used to generate the ResourceClaim for an instance of NIMService.
303301
ResourceClaimTemplateName *string `json:"resourceClaimTemplateName,omitempty"`
304-
// ResourceClaims is the status of used resource claims.
305-
//
306-
// This list is empty if ResourceClaimTemplateName is not set.
302+
// ResourceClaims is the status of resource claims.
307303
ResourceClaims []DRAResourceClaimStatus `json:"resourceClaims,omitempty"`
308304
}
309305

310306
// DRAResourceClaimStatus defines the status of the DRAResourceClaim.
311307
type DRAResourceClaimStatus struct {
312308
// Name is the name of the ResourceClaim.
313309
Name string `json:"name"`
310+
// State is the state of the ResourceClaim.
311+
// * pending: the resource claim is pending allocation.
312+
// * deleted: the resource claim has a deletion timestamp set but is not yet finalized.
313+
// * allocated: the resource claim is allocated to a pod.
314+
// * reserved: the resource claim is consumed by a pod.
315+
// This field will have one or more of the above values depending on the status of the resource claim.
316+
//
317+
// +kubebuilder:validation:default=pending
318+
State string `json:"state"`
314319
}

bundle/manifests/apps.nvidia.com_nimservices.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,29 +2363,33 @@ spec:
23632363
description: DRAResourceStatus defines the status of the DRAResource.
23642364
properties:
23652365
name:
2366-
description: |-
2367-
Name is the generated name of the DRAResource referenced in the NIMService
2368-
pod template as `spec.resourceClaims[].name`.
23692366
type: string
23702367
resourceClaimTemplateName:
23712368
description: |-
23722369
ResourceClaimTemplateName is the name of the ResourceClaimTemplate that was
23732370
used to generate the ResourceClaim for an instance of NIMService.
23742371
type: string
23752372
resourceClaims:
2376-
description: |-
2377-
ResourceClaims is the status of used resource claims.
2378-
2379-
This list is empty if ResourceClaimTemplateName is not set.
2373+
description: ResourceClaims is the status of resource claims.
23802374
items:
23812375
description: DRAResourceClaimStatus defines the status of
23822376
the DRAResourceClaim.
23832377
properties:
23842378
name:
23852379
description: Name is the name of the ResourceClaim.
23862380
type: string
2381+
state:
2382+
description: |-
2383+
State is the state of the ResourceClaim.
2384+
* pending: the resource claim is pending allocation.
2385+
* deleted: the resource claim has a deletion timestamp set but is not yet finalized.
2386+
* allocated: the resource claim is allocated to a pod.
2387+
* reserved: the resource claim is consumed by a pod.
2388+
This field will have one or more of the above values depending on the status of the resource claim.
2389+
type: string
23872390
required:
23882391
- name
2392+
- state
23892393
type: object
23902394
type: array
23912395
required:

config/crd/bases/apps.nvidia.com_nimservices.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,29 +2363,33 @@ spec:
23632363
description: DRAResourceStatus defines the status of the DRAResource.
23642364
properties:
23652365
name:
2366-
description: |-
2367-
Name is the generated name of the DRAResource referenced in the NIMService
2368-
pod template as `spec.resourceClaims[].name`.
23692366
type: string
23702367
resourceClaimTemplateName:
23712368
description: |-
23722369
ResourceClaimTemplateName is the name of the ResourceClaimTemplate that was
23732370
used to generate the ResourceClaim for an instance of NIMService.
23742371
type: string
23752372
resourceClaims:
2376-
description: |-
2377-
ResourceClaims is the status of used resource claims.
2378-
2379-
This list is empty if ResourceClaimTemplateName is not set.
2373+
description: ResourceClaims is the status of resource claims.
23802374
items:
23812375
description: DRAResourceClaimStatus defines the status of
23822376
the DRAResourceClaim.
23832377
properties:
23842378
name:
23852379
description: Name is the name of the ResourceClaim.
23862380
type: string
2381+
state:
2382+
description: |-
2383+
State is the state of the ResourceClaim.
2384+
* pending: the resource claim is pending allocation.
2385+
* deleted: the resource claim has a deletion timestamp set but is not yet finalized.
2386+
* allocated: the resource claim is allocated to a pod.
2387+
* reserved: the resource claim is consumed by a pod.
2388+
This field will have one or more of the above values depending on the status of the resource claim.
2389+
type: string
23872390
required:
23882391
- name
2392+
- state
23892393
type: object
23902394
type: array
23912395
required:

deployments/helm/k8s-nim-operator/crds/apps.nvidia.com_nimservices.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,29 +2363,33 @@ spec:
23632363
description: DRAResourceStatus defines the status of the DRAResource.
23642364
properties:
23652365
name:
2366-
description: |-
2367-
Name is the generated name of the DRAResource referenced in the NIMService
2368-
pod template as `spec.resourceClaims[].name`.
23692366
type: string
23702367
resourceClaimTemplateName:
23712368
description: |-
23722369
ResourceClaimTemplateName is the name of the ResourceClaimTemplate that was
23732370
used to generate the ResourceClaim for an instance of NIMService.
23742371
type: string
23752372
resourceClaims:
2376-
description: |-
2377-
ResourceClaims is the status of used resource claims.
2378-
2379-
This list is empty if ResourceClaimTemplateName is not set.
2373+
description: ResourceClaims is the status of resource claims.
23802374
items:
23812375
description: DRAResourceClaimStatus defines the status of
23822376
the DRAResourceClaim.
23832377
properties:
23842378
name:
23852379
description: Name is the name of the ResourceClaim.
23862380
type: string
2381+
state:
2382+
description: |-
2383+
State is the state of the ResourceClaim.
2384+
* pending: the resource claim is pending allocation.
2385+
* deleted: the resource claim has a deletion timestamp set but is not yet finalized.
2386+
* allocated: the resource claim is allocated to a pod.
2387+
* reserved: the resource claim is consumed by a pod.
2388+
This field will have one or more of the above values depending on the status of the resource claim.
2389+
type: string
23872390
required:
23882391
- name
2392+
- state
23892393
type: object
23902394
type: array
23912395
required:

internal/shared/resourceclaims.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package shared
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
2223

2324
corev1 "k8s.io/api/core/v1"
25+
resourcev1beta2 "k8s.io/api/resource/v1beta2"
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527

2628
appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1"
@@ -150,9 +152,11 @@ func generateDRAResourceClaimStatus(ctx context.Context, client client.Client, n
150152
if err != nil {
151153
return nil, err
152154
}
153-
claimStatuses = append(claimStatuses, appsv1alpha1.DRAResourceClaimStatus{
154-
Name: claim.GetName(),
155-
})
155+
claimStatus := appsv1alpha1.DRAResourceClaimStatus{
156+
Name: claim.GetName(),
157+
State: getDRAResourceClaimState(claim),
158+
}
159+
claimStatuses = append(claimStatuses, claimStatus)
156160
return claimStatuses, nil
157161
}
158162

@@ -168,9 +172,28 @@ func generateDRAResourceClaimStatus(ctx context.Context, client client.Client, n
168172
continue
169173
}
170174
claimStatuses = append(claimStatuses, appsv1alpha1.DRAResourceClaimStatus{
171-
Name: claim.GetName(),
175+
Name: claim.GetName(),
176+
State: getDRAResourceClaimState(&claim),
172177
})
173178
}
174179
}
175180
return claimStatuses, nil
176181
}
182+
183+
func getDRAResourceClaimState(claim *resourcev1beta2.ResourceClaim) string {
184+
var states []string
185+
if claim.GetDeletionTimestamp() != nil {
186+
states = append(states, "deleted")
187+
}
188+
if claim.Status.Allocation == nil {
189+
if claim.GetDeletionTimestamp() == nil {
190+
states = append(states, "pending")
191+
}
192+
} else {
193+
states = append(states, "allocated")
194+
if len(claim.Status.ReservedFor) > 0 {
195+
states = append(states, "reserved")
196+
}
197+
}
198+
return strings.Join(states, ",")
199+
}

internal/shared/resourceclaims_test.go

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
resourcev1beta2 "k8s.io/api/resource/v1beta2"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212
"k8s.io/apimachinery/pkg/runtime"
13+
"k8s.io/apimachinery/pkg/types"
1314
"k8s.io/utils/ptr"
1415
"sigs.k8s.io/controller-runtime/pkg/client"
1516
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -395,7 +396,8 @@ var _ = Describe("DRA resourceclaim tests", func() {
395396
Expect(err).NotTo(HaveOccurred())
396397
Expect(statuses).To(Equal([]appsv1alpha1.DRAResourceClaimStatus{
397398
{
398-
Name: "test-claim",
399+
Name: "test-claim",
400+
State: "pending",
399401
},
400402
}))
401403
})
@@ -434,10 +436,12 @@ var _ = Describe("DRA resourceclaim tests", func() {
434436
Expect(err).NotTo(HaveOccurred())
435437
Expect(statuses).To(Equal([]appsv1alpha1.DRAResourceClaimStatus{
436438
{
437-
Name: "template-generated-claim-1",
439+
Name: "template-generated-claim-1",
440+
State: "pending",
438441
},
439442
{
440-
Name: "template-generated-claim-2",
443+
Name: "template-generated-claim-2",
444+
State: "pending",
441445
},
442446
}))
443447
})
@@ -480,7 +484,8 @@ var _ = Describe("DRA resourceclaim tests", func() {
480484
Expect(err).NotTo(HaveOccurred())
481485
Expect(statuses).To(Equal([]appsv1alpha1.DRAResourceClaimStatus{
482486
{
483-
Name: "template-generated-claim-1",
487+
Name: "template-generated-claim-1",
488+
State: "pending",
484489
},
485490
}))
486491
})
@@ -525,4 +530,81 @@ var _ = Describe("DRA resourceclaim tests", func() {
525530
Expect(err).To(HaveOccurred())
526531
})
527532
})
533+
534+
DescribeTable("getDRAResourceClaimState",
535+
func(claim *resourcev1beta2.ResourceClaim, expectedState string) {
536+
state := getDRAResourceClaimState(claim)
537+
Expect(state).To(Equal(expectedState))
538+
},
539+
Entry("pending claim",
540+
&resourcev1beta2.ResourceClaim{
541+
ObjectMeta: metav1.ObjectMeta{
542+
Name: "test-claim",
543+
Namespace: "test-ns",
544+
CreationTimestamp: metav1.Time{},
545+
},
546+
Status: resourcev1beta2.ResourceClaimStatus{},
547+
},
548+
"pending",
549+
),
550+
Entry("allocated and reserved claim",
551+
&resourcev1beta2.ResourceClaim{
552+
ObjectMeta: metav1.ObjectMeta{
553+
Name: "test-claim",
554+
Namespace: "test-ns",
555+
CreationTimestamp: metav1.Time{},
556+
},
557+
Status: resourcev1beta2.ResourceClaimStatus{
558+
Allocation: &resourcev1beta2.AllocationResult{},
559+
ReservedFor: []resourcev1beta2.ResourceClaimConsumerReference{
560+
{
561+
Name: "pod-test",
562+
Resource: "pods",
563+
UID: types.UID("pod-test"),
564+
},
565+
},
566+
},
567+
},
568+
"allocated,reserved",
569+
),
570+
Entry("deleted claim",
571+
&resourcev1beta2.ResourceClaim{
572+
ObjectMeta: metav1.ObjectMeta{
573+
Name: "test-claim",
574+
Namespace: "test-ns",
575+
CreationTimestamp: metav1.Time{},
576+
DeletionTimestamp: &metav1.Time{},
577+
Finalizers: []string{
578+
resourcev1beta2.Finalizer,
579+
},
580+
},
581+
Status: resourcev1beta2.ResourceClaimStatus{},
582+
},
583+
"deleted",
584+
),
585+
Entry("deleted, allocated and reserved claim",
586+
&resourcev1beta2.ResourceClaim{
587+
ObjectMeta: metav1.ObjectMeta{
588+
Name: "test-claim",
589+
Namespace: "test-ns",
590+
CreationTimestamp: metav1.Time{},
591+
DeletionTimestamp: &metav1.Time{},
592+
Finalizers: []string{
593+
resourcev1beta2.Finalizer,
594+
},
595+
},
596+
Status: resourcev1beta2.ResourceClaimStatus{
597+
Allocation: &resourcev1beta2.AllocationResult{},
598+
ReservedFor: []resourcev1beta2.ResourceClaimConsumerReference{
599+
{
600+
Name: "pod-test",
601+
Resource: "pods",
602+
UID: types.UID("pod-test"),
603+
},
604+
},
605+
},
606+
},
607+
"deleted,allocated,reserved",
608+
),
609+
)
528610
})

0 commit comments

Comments
 (0)