diff --git a/CHANGELOG.md b/CHANGELOG.md index 321013c..663d903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ needs a new checkpoint API version (with conversion), not just a new release. ## [Unreleased] +### Added + +- `api/checkpoint/v1`: the state of the NVIDIA GPU and ComputeDomain kubelet + plugins of dra-driver-nvidia-gpu that v1 could not hold: `Device.ShareID` and + `Device.Metadata`; per NVIDIA device group `timeSliceApplied`, `mpsApplied`, + `computeDomain` and `type`; `pciBusID` and `id` of NVIDIA devices and + `parentPCIBusID` of MIG partitions; `PrepareAborted` claims with `abortedAt`. + All optional, so existing v1 files stay valid. +- `checkpoint`: the legacy reader reads the checkpointmanager layouts of both + NVIDIA plugins, including the ComputeDomain V1+V2, V1 only and 25.3.0-rc.2 + layouts. + ## [0.1.0] - 2026-12-01 ### Added diff --git a/api/checkpoint/v1/types.go b/api/checkpoint/v1/types.go index 91d4d38..7e4cc2a 100644 --- a/api/checkpoint/v1/types.go +++ b/api/checkpoint/v1/types.go @@ -61,6 +61,9 @@ type PreparedClaim struct { Devices []PreparedDevice `json:"devices,omitempty"` // +optional NVIDIA *NVIDIAClaim `json:"nvidia,omitempty"` + // AbortedAt is when the claim entered PrepareStateAborted. + // +optional + AbortedAt *metav1.Time `json:"abortedAt,omitempty"` } type PrepareState string @@ -68,6 +71,10 @@ type PrepareState string const ( PrepareStateStarted PrepareState = "PrepareStarted" PrepareStateCompleted PrepareState = "PrepareCompleted" + // PrepareStateAborted marks a claim whose prepare was undone; the entry + // stays until AbortedAt plus a driver-defined TTL so that stale retries of + // the prepare can be recognised. + PrepareStateAborted PrepareState = "PrepareAborted" ) type PreparedDevice struct { @@ -92,6 +99,20 @@ type Device struct { // +optional // +listType=atomic CDIDeviceIDs []string `json:"cdiDeviceIDs,omitempty"` + // ShareID identifies the share of a device that several claims share. + // +optional + ShareID *types.UID `json:"shareID,omitempty"` + // Metadata is the device metadata the driver returned to the kubelet. + // +optional + Metadata *DeviceMetadata `json:"metadata,omitempty"` +} + +// DeviceMetadata mirrors the kubelet DRA API's device metadata. +type DeviceMetadata struct { + // +optional + Attributes map[string]resourcev1.DeviceAttribute `json:"attributes,omitempty"` + // +optional + NetworkData *resourcev1.NetworkDeviceData `json:"networkData,omitempty"` } // +k8s:deepcopy-gen=false @@ -202,6 +223,20 @@ type NVIDIAClaim struct { type NVIDIADeviceGroup struct { // +optional MPSControlDaemonID string `json:"mpsControlDaemonID,omitempty"` + // MPSApplied records whether MPS was applied to the group. Unset in + // checkpoints that predate it. + // +optional + MPSApplied *bool `json:"mpsApplied,omitempty"` + // TimeSliceApplied records whether time-slicing was applied to the group. + // Unset in checkpoints that predate it, which implied time-slicing. + // +optional + TimeSliceApplied *bool `json:"timeSliceApplied,omitempty"` + // Type is the device type of the group's ComputeDomain config. + // +optional + Type NVIDIADeviceType `json:"type,omitempty"` + // ComputeDomain is the ID of the ComputeDomain the group belongs to. + // +optional + ComputeDomain string `json:"computeDomain,omitempty"` } type NVIDIADeviceType string @@ -210,6 +245,9 @@ const ( NVIDIADeviceTypeGPU NVIDIADeviceType = "gpu" NVIDIADeviceTypeMIG NVIDIADeviceType = "mig" NVIDIADeviceTypeVFIO NVIDIADeviceType = "vfio" + // ComputeDomain devices: an IMEX channel or the daemon of a domain. + NVIDIADeviceTypeChannel NVIDIADeviceType = "channel" + NVIDIADeviceTypeDaemon NVIDIADeviceType = "daemon" ) type NVIDIADevice struct { @@ -219,6 +257,12 @@ type NVIDIADevice struct { // UUID is the GPU UUID of gpu and vfio devices. // +optional UUID string `json:"uuid,omitempty"` + // PCIBusID is the PCI bus ID of vfio devices. + // +optional + PCIBusID string `json:"pciBusID,omitempty"` + // ID is the IMEX channel or daemon ID of channel and daemon devices. + // +optional + ID *int32 `json:"id,omitempty"` } // NVIDIAPartition holds the MIG device identity of a hard partition. @@ -227,4 +271,7 @@ type NVIDIAPartition struct { GIID int32 `json:"giID"` CIID int32 `json:"ciID"` MIGUUID string `json:"migUUID"` + // ParentPCIBusID is the PCI bus ID of the parent GPU. + // +optional + ParentPCIBusID string `json:"parentPCIBusID,omitempty"` } diff --git a/api/checkpoint/v1/zz_generated.deepcopy.go b/api/checkpoint/v1/zz_generated.deepcopy.go index bee1a86..dcf91ef 100644 --- a/api/checkpoint/v1/zz_generated.deepcopy.go +++ b/api/checkpoint/v1/zz_generated.deepcopy.go @@ -23,6 +23,7 @@ package v1 import ( resourcev1 "k8s.io/api/resource/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. @@ -170,6 +171,16 @@ func (in *Device) DeepCopyInto(out *Device) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.ShareID != nil { + in, out := &in.ShareID, &out.ShareID + *out = new(types.UID) + **out = **in + } + if in.Metadata != nil { + in, out := &in.Metadata, &out.Metadata + *out = new(DeviceMetadata) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Device. @@ -182,13 +193,42 @@ func (in *Device) DeepCopy() *Device { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *DeviceMetadata) DeepCopyInto(out *DeviceMetadata) { + *out = *in + if in.Attributes != nil { + in, out := &in.Attributes, &out.Attributes + *out = make(map[string]resourcev1.DeviceAttribute, len(*in)) + for key, val := range *in { + (*out)[key] = *val.DeepCopy() + } + } + if in.NetworkData != nil { + in, out := &in.NetworkData, &out.NetworkData + *out = new(resourcev1.NetworkDeviceData) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DeviceMetadata. +func (in *DeviceMetadata) DeepCopy() *DeviceMetadata { + if in == nil { + return nil + } + out := new(DeviceMetadata) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NVIDIAClaim) DeepCopyInto(out *NVIDIAClaim) { *out = *in if in.Groups != nil { in, out := &in.Groups, &out.Groups *out = make([]NVIDIADeviceGroup, len(*in)) - copy(*out, *in) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } } } @@ -205,6 +245,11 @@ func (in *NVIDIAClaim) DeepCopy() *NVIDIAClaim { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NVIDIADevice) DeepCopyInto(out *NVIDIADevice) { *out = *in + if in.ID != nil { + in, out := &in.ID, &out.ID + *out = new(int32) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NVIDIADevice. @@ -220,6 +265,16 @@ func (in *NVIDIADevice) DeepCopy() *NVIDIADevice { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NVIDIADeviceGroup) DeepCopyInto(out *NVIDIADeviceGroup) { *out = *in + if in.MPSApplied != nil { + in, out := &in.MPSApplied, &out.MPSApplied + *out = new(bool) + **out = **in + } + if in.TimeSliceApplied != nil { + in, out := &in.TimeSliceApplied, &out.TimeSliceApplied + *out = new(bool) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NVIDIADeviceGroup. @@ -342,6 +397,10 @@ func (in *PreparedClaim) DeepCopyInto(out *PreparedClaim) { *out = new(NVIDIAClaim) (*in).DeepCopyInto(*out) } + if in.AbortedAt != nil { + in, out := &in.AbortedAt, &out.AbortedAt + *out = (*in).DeepCopy() + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PreparedClaim. @@ -375,7 +434,7 @@ func (in *PreparedDevice) DeepCopyInto(out *PreparedDevice) { if in.NVIDIA != nil { in, out := &in.NVIDIA, &out.NVIDIA *out = new(NVIDIADevice) - **out = **in + (*in).DeepCopyInto(*out) } } diff --git a/checkpoint/legacy.go b/checkpoint/legacy.go index c8eecb9..a4925e5 100644 --- a/checkpoint/legacy.go +++ b/checkpoint/legacy.go @@ -78,33 +78,55 @@ type legacyListDevice struct { type legacyClaim struct { CheckpointState string `json:"checkpointState"` Status resourcev1.ResourceClaimStatus `json:"status"` - PreparedDevices []*struct { - Devices []struct { - HAMiGPU *legacyUUIDDevice `json:"hami-gpu"` - GPU *legacyUUIDDevice `json:"gpu"` - VFIO *legacyUUIDDevice `json:"vfio"` - MIG *struct { - Concrete *struct { - ParentMinor int32 `json:"parentMinor"` - GIID int32 `json:"giId"` - CIID int32 `json:"ciId"` - MIGUUID string `json:"migUUID"` - ParentUUID string `json:"parentUUID"` - } `json:"concrete"` - Device legacyDevice `json:"device"` - } `json:"mig"` - } `json:"devices"` - ConfigState struct { - MPSControlDaemonID string `json:"mpsControlDaemonID"` - } `json:"configState"` - } `json:"preparedDevices"` - Name string `json:"name"` - Namespace string `json:"namespace"` + PreparedDevices []*legacyGroup `json:"preparedDevices"` + Name string `json:"name"` + Namespace string `json:"namespace"` + AbortedAt *metav1.Time `json:"abortedAt"` +} + +// legacyGroup is the devices of one config of a claim. +type legacyGroup struct { + Devices []struct { + HAMiGPU *legacyUUIDDevice `json:"hami-gpu"` + GPU *legacyUUIDDevice `json:"gpu"` + VFIO *legacyUUIDDevice `json:"vfio"` + Channel *legacyDomainDevice `json:"channel"` + Daemon *legacyDomainDevice `json:"daemon"` + MIG *struct { + Concrete *struct { + ParentMinor int32 `json:"parentMinor"` + GIID int32 `json:"giId"` + CIID int32 `json:"ciId"` + MIGUUID string `json:"migUUID"` + ParentUUID string `json:"parentUUID"` + // ParentPCIBusID postdates the first NVIDIA checkpoints. + ParentPCIBusID string `json:"parentPCIBusID"` + } `json:"concrete"` + Device legacyDevice `json:"device"` + } `json:"mig"` + } `json:"devices"` + ConfigState struct { + MPSControlDaemonID string `json:"mpsControlDaemonID"` + MPSApplied *bool `json:"mpsApplied"` + TimeSliceApplied *bool `json:"timeSliceApplied"` + // Type and ComputeDomain are the ComputeDomain plugin's. + Type string + ComputeDomain string + } `json:"configState"` } type legacyUUIDDevice struct { Info *struct { - UUID string `json:"uuid"` + UUID string `json:"uuid"` + PCIBusID string `json:"pciBusID"` + } `json:"info"` + Device legacyDevice `json:"device"` +} + +// legacyDomainDevice is a ComputeDomain channel or daemon device. +type legacyDomainDevice struct { + Info *struct { + ID int32 `json:"id"` } `json:"info"` Device legacyDevice `json:"device"` } @@ -116,6 +138,38 @@ type legacyDevice struct { PoolName string DeviceName string CDIDeviceIDs []string + ShareID *types.UID + Metadata *struct { + Attributes map[string]resourcev1.DeviceAttribute + NetworkData *resourcev1.NetworkDeviceData + } + + // 25.3.0-rc.2 wrote the kubelet DRA proto message instead. + RequestNames []string `json:"request_names"` + Pool string `json:"pool_name"` + Device string `json:"device_name"` + CDIIDs []string `json:"cdi_device_ids"` +} + +func orSlice(a, b []string) []string { + if len(a) > 0 { + return a + } + return b +} + +func (d legacyDevice) toAPI() api.Device { + device := api.Device{ + Requests: orSlice(d.Requests, d.RequestNames), + PoolName: cmp.Or(d.PoolName, d.Pool), + DeviceName: cmp.Or(d.DeviceName, d.Device), + CDIDeviceIDs: orSlice(d.CDIDeviceIDs, d.CDIIDs), + ShareID: d.ShareID, + } + if d.Metadata != nil { + device.Metadata = &api.DeviceMetadata{Attributes: d.Metadata.Attributes, NetworkData: d.Metadata.NetworkData} + } + return device } func isLegacy(data []byte) bool { @@ -204,6 +258,22 @@ func legacyV1ClaimToAPI(uid string, raw json.RawMessage) (api.PreparedClaim, err } return legacyClaimToAPI(uid, claim, true), nil } + // 25.3.0-rc.2 wrote the claims of the ComputeDomain plugin as lists of + // device groups. + var groupProbe []struct { + Devices json.RawMessage `json:"devices"` + } + if err := json.Unmarshal(raw, &groupProbe); err == nil && len(groupProbe) > 0 && groupProbe[0].Devices != nil { + var groups []*legacyGroup + if err := json.Unmarshal(raw, &groups); err != nil { + return api.PreparedClaim{}, err + } + claim := api.PreparedClaim{UID: types.UID(uid), State: api.PrepareStateCompleted} + for _, group := range groups { + appendLegacyGroup(&claim, group) + } + return claim, nil + } var devices []legacyListDevice if err := json.Unmarshal(raw, &devices); err != nil { return api.PreparedClaim{}, err @@ -245,46 +315,70 @@ func legacyClaimToAPI(uid string, c legacyClaim, v1 bool) api.PreparedClaim { if v1 { claim.State = api.PrepareStateCompleted } + claim.AbortedAt = c.AbortedAt for _, group := range c.PreparedDevices { - if group == nil { - continue - } - if claim.NVIDIA == nil { - claim.NVIDIA = &api.NVIDIAClaim{} + appendLegacyGroup(&claim, group) + } + return claim +} + +// appendLegacyGroup adds the devices of one NVIDIA config group to the claim. +func appendLegacyGroup(claim *api.PreparedClaim, group *legacyGroup) { + if group == nil { + return + } + if claim.NVIDIA == nil { + claim.NVIDIA = &api.NVIDIAClaim{} + } + index := int32(len(claim.NVIDIA.Groups)) + claim.NVIDIA.Groups = append(claim.NVIDIA.Groups, api.NVIDIADeviceGroup{ + MPSControlDaemonID: group.ConfigState.MPSControlDaemonID, + MPSApplied: group.ConfigState.MPSApplied, + TimeSliceApplied: group.ConfigState.TimeSliceApplied, + Type: api.NVIDIADeviceType(group.ConfigState.Type), + ComputeDomain: group.ConfigState.ComputeDomain, + }) + for _, d := range group.Devices { + nvidia := &api.NVIDIADevice{Group: index} + device := api.PreparedDevice{NVIDIA: nvidia} + uuidDevice := func(t api.NVIDIADeviceType, u *legacyUUIDDevice) { + nvidia.Type = t + if u.Info != nil { + nvidia.UUID = u.Info.UUID + nvidia.PCIBusID = u.Info.PCIBusID + } + device.Device = u.Device.toAPI() } - index := int32(len(claim.NVIDIA.Groups)) - claim.NVIDIA.Groups = append(claim.NVIDIA.Groups, api.NVIDIADeviceGroup{MPSControlDaemonID: group.ConfigState.MPSControlDaemonID}) - for _, d := range group.Devices { - nvidia := &api.NVIDIADevice{Group: index} - device := api.PreparedDevice{NVIDIA: nvidia} - uuidDevice := func(t api.NVIDIADeviceType, u *legacyUUIDDevice) { - nvidia.Type = t - if u.Info != nil { - nvidia.UUID = u.Info.UUID - } - device.Device = api.Device(u.Device) + domainDevice := func(t api.NVIDIADeviceType, c *legacyDomainDevice) { + nvidia.Type = t + if c.Info != nil { + nvidia.ID = &c.Info.ID } - switch { - case d.HAMiGPU != nil: - uuidDevice(api.NVIDIADeviceTypeGPU, d.HAMiGPU) - device.Partition = &api.Partition{Type: api.PartitionTypeSoft} - case d.GPU != nil: - uuidDevice(api.NVIDIADeviceTypeGPU, d.GPU) - case d.VFIO != nil: - uuidDevice(api.NVIDIADeviceTypeVFIO, d.VFIO) - case d.MIG != nil: - nvidia.Type = api.NVIDIADeviceTypeMIG - device.Device = api.Device(d.MIG.Device) - device.Partition = &api.Partition{Type: api.PartitionTypeHard} - if m := d.MIG.Concrete; m != nil { - device.Partition.ParentDevice = m.ParentUUID - device.Partition.NVIDIA = &api.NVIDIAPartition{ParentMinor: m.ParentMinor, GIID: m.GIID, CIID: m.CIID, MIGUUID: m.MIGUUID} - } + device.Device = c.Device.toAPI() + } + switch { + case d.HAMiGPU != nil: + uuidDevice(api.NVIDIADeviceTypeGPU, d.HAMiGPU) + device.Partition = &api.Partition{Type: api.PartitionTypeSoft} + case d.GPU != nil: + uuidDevice(api.NVIDIADeviceTypeGPU, d.GPU) + case d.VFIO != nil: + uuidDevice(api.NVIDIADeviceTypeVFIO, d.VFIO) + case d.MIG != nil: + nvidia.Type = api.NVIDIADeviceTypeMIG + device.Device = d.MIG.Device.toAPI() + device.Partition = &api.Partition{Type: api.PartitionTypeHard} + if m := d.MIG.Concrete; m != nil { + device.Partition.ParentDevice = m.ParentUUID + device.Partition.NVIDIA = &api.NVIDIAPartition{ParentMinor: m.ParentMinor, GIID: m.GIID, CIID: m.CIID, MIGUUID: m.MIGUUID, ParentPCIBusID: m.ParentPCIBusID} } - claim.Devices = append(claim.Devices, device) + case d.Channel != nil: + domainDevice(api.NVIDIADeviceTypeChannel, d.Channel) + case d.Daemon != nil: + domainDevice(api.NVIDIADeviceTypeDaemon, d.Daemon) } + claim.Devices = append(claim.Devices, device) } - return claim } // verifyLegacyChecksums checks the checksums the checkpointmanager stored. diff --git a/checkpoint/legacy_nvidia_cd_test.go b/checkpoint/legacy_nvidia_cd_test.go new file mode 100644 index 0000000..188686a --- /dev/null +++ b/checkpoint/legacy_nvidia_cd_test.go @@ -0,0 +1,96 @@ +/* +Copyright 2026 The HAMi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package checkpoint + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + resourcev1 "k8s.io/api/resource/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" + + api "github.com/Project-HAMi/dra-lib/api/checkpoint/v1" +) + +// The fixtures were written by the ComputeDomain kubelet plugin's +// checkpointmanager code: V1+V2 is the current layout, V1 only what releases +// before the v2 checkpoint wrote, and 2503rc2 the layout of 25.3.0-rc.2, whose +// v1 claims are lists of device groups with the kubelet proto device. + +func nvidiaCDClaim() api.PreparedClaim { + dev := func(req, name string) api.Device { + return api.Device{Requests: []string{req}, PoolName: "node-a", DeviceName: name, CDIDeviceIDs: []string{"k8s.compute-domain.nvidia.com/device=" + name}} + } + return api.PreparedClaim{ + UID: "claim-uid-1", + Name: "claim-1", + Namespace: "default", + State: api.PrepareStateCompleted, + Status: nvidiaCDStatus(), + Devices: []api.PreparedDevice{ + {Device: dev("channel", "channel-0"), NVIDIA: &api.NVIDIADevice{Group: 0, Type: api.NVIDIADeviceTypeChannel, ID: ptr.To[int32](5)}}, + {Device: dev("daemon", "daemon-0"), NVIDIA: &api.NVIDIADevice{Group: 1, Type: api.NVIDIADeviceTypeDaemon, ID: ptr.To[int32](0)}}, + }, + NVIDIA: &api.NVIDIAClaim{Groups: []api.NVIDIADeviceGroup{ + {Type: api.NVIDIADeviceTypeChannel, ComputeDomain: "domain-a"}, + {Type: api.NVIDIADeviceTypeDaemon, ComputeDomain: "domain-b"}, + }}, + } +} + +func nvidiaCDStatus() *resourcev1.ResourceClaimStatus { + return &resourcev1.ResourceClaimStatus{Allocation: &resourcev1.AllocationResult{Devices: resourcev1.DeviceAllocationResult{ + Results: []resourcev1.DeviceRequestAllocationResult{{Request: "channel", Driver: "compute-domain.nvidia.com", Pool: "node-a", Device: "channel-0"}}, + }}} +} + +func TestReadMigratesNVIDIAComputeDomain(t *testing.T) { + started := api.PreparedClaim{UID: "claim-uid-2", Name: "claim-2", Namespace: "team-a", State: api.PrepareStateStarted, Status: nvidiaCDStatus()} + aborted := api.PreparedClaim{UID: "claim-uid-3", Name: "claim-3", Namespace: "team-a", State: api.PrepareStateAborted, Status: nvidiaCDStatus(), + AbortedAt: &metav1.Time{Time: time.Unix(1764460800, 0)}} + oldest := nvidiaCDClaim() + oldest.Name, oldest.Namespace, oldest.Status = "", "", nil + // 25.3.0-rc.2 recorded neither status nor names. + rc2 := oldest + + v1 := nvidiaCDClaim() + v1.Name, v1.Namespace = "", "" + for fixture, want := range map[string]*api.Checkpoint{ + "testdata/legacy-nvidia-cd.json": {DriverName: testDriver, NodeBootID: "boot-id-1", PreparedClaims: []api.PreparedClaim{nvidiaCDClaim(), started, aborted}}, + "testdata/legacy-nvidia-cd-v1.json": {DriverName: testDriver, PreparedClaims: []api.PreparedClaim{v1}}, + "testdata/legacy-nvidia-cd-2503rc2.json": {DriverName: testDriver, PreparedClaims: []api.PreparedClaim{rc2}}, + } { + t.Run(fixture, func(t *testing.T) { + legacy, err := os.ReadFile(fixture) + require.NoError(t, err) + s, path := newTestStore(t) + require.NoError(t, os.WriteFile(path, legacy, 0o600)) + + got, err := s.Read() + require.NoError(t, err) + assert.Equal(t, want, got) + + again, err := s.Read() + require.NoError(t, err) + assert.Equal(t, want, again) + }) + } +} diff --git a/checkpoint/legacy_nvidia_gpu_test.go b/checkpoint/legacy_nvidia_gpu_test.go new file mode 100644 index 0000000..e4197c7 --- /dev/null +++ b/checkpoint/legacy_nvidia_gpu_test.go @@ -0,0 +1,94 @@ +/* +Copyright 2026 The HAMi Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package checkpoint + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + resourcev1 "k8s.io/api/resource/v1" + "k8s.io/utils/ptr" + + api "github.com/Project-HAMi/dra-lib/api/checkpoint/v1" +) + +// The fixtures were written by the NVIDIA GPU kubelet plugin's checkpointmanager +// code: V1+V2 is what releases with the v2 checkpoint wrote, V1 only what the +// earlier ones wrote. + +func nvidiaGPUClaim() api.PreparedClaim { + dev := func(req, name string) api.Device { + return api.Device{Requests: []string{req}, PoolName: "node-a", DeviceName: name, CDIDeviceIDs: []string{"k8s.gpu.nvidia.com/device=" + name}} + } + vfio := dev("vfio", "gpu-3-vfio") + vfio.Metadata = &api.DeviceMetadata{Attributes: map[string]resourcev1.DeviceAttribute{ + "resource.kubernetes.io/pciBusID": {StringValue: ptr.To("0000:5e:00.0")}, + "productName": {StringValue: ptr.To("NVIDIA A100")}, + }} + return api.PreparedClaim{ + UID: "claim-uid-1", + Name: "claim-1", + Namespace: "default", + State: api.PrepareStateCompleted, + Status: &resourcev1.ResourceClaimStatus{Allocation: &resourcev1.AllocationResult{Devices: resourcev1.DeviceAllocationResult{ + Results: []resourcev1.DeviceRequestAllocationResult{{Request: "gpu", Driver: "gpu.nvidia.com", Pool: "node-a", Device: "gpu-0"}}, + }}}, + Devices: []api.PreparedDevice{ + {Device: dev("gpu", "gpu-0"), NVIDIA: &api.NVIDIADevice{Group: 0, Type: api.NVIDIADeviceTypeGPU, UUID: "GPU-aaaa"}}, + { + Device: dev("mig", "gpu-1-mig-1g10gb-2"), + Partition: &api.Partition{Type: api.PartitionTypeHard, ParentDevice: "GPU-dddd", + NVIDIA: &api.NVIDIAPartition{ParentMinor: 1, GIID: 2, CIID: 0, MIGUUID: "MIG-cccc", ParentPCIBusID: "0000:3b:00.0"}}, + NVIDIA: &api.NVIDIADevice{Group: 1, Type: api.NVIDIADeviceTypeMIG}, + }, + {Device: vfio, NVIDIA: &api.NVIDIADevice{Group: 2, Type: api.NVIDIADeviceTypeVFIO, UUID: "GPU-eeee", PCIBusID: "0000:5e:00.0"}}, + }, + NVIDIA: &api.NVIDIAClaim{Groups: []api.NVIDIADeviceGroup{ + {TimeSliceApplied: ptr.To(true)}, + {MPSControlDaemonID: "mps-claim-uid-1"}, + {TimeSliceApplied: ptr.To(false)}, + }}, + } +} + +func TestReadMigratesNVIDIAGPU(t *testing.T) { + started := api.PreparedClaim{UID: "claim-uid-2", Name: "claim-2", Namespace: "team-a", State: api.PrepareStateStarted, Status: nvidiaGPUClaim().Status} + oldest := nvidiaGPUClaim() + oldest.Name, oldest.Namespace = "", "" + for fixture, want := range map[string]*api.Checkpoint{ + "testdata/legacy-nvidia-gpu.json": {DriverName: testDriver, NodeBootID: "boot-id-1", PreparedClaims: []api.PreparedClaim{nvidiaGPUClaim(), started}}, + "testdata/legacy-nvidia-gpu-v1.json": {DriverName: testDriver, PreparedClaims: []api.PreparedClaim{oldest}}, + } { + t.Run(fixture, func(t *testing.T) { + legacy, err := os.ReadFile(fixture) + require.NoError(t, err) + s, path := newTestStore(t) + require.NoError(t, os.WriteFile(path, legacy, 0o600)) + + got, err := s.Read() + require.NoError(t, err) + assert.Equal(t, want, got) + + // What was migrated is what the store reads back. + again, err := s.Read() + require.NoError(t, err) + assert.Equal(t, want, again) + }) + } +} diff --git a/checkpoint/testdata/legacy-nvidia-cd-2503rc2.json b/checkpoint/testdata/legacy-nvidia-cd-2503rc2.json new file mode 100644 index 0000000..bf771de --- /dev/null +++ b/checkpoint/testdata/legacy-nvidia-cd-2503rc2.json @@ -0,0 +1 @@ +{"checksum":2340767533,"v1":{"preparedClaims":{"claim-uid-1":[{"devices":[{"channel":{"info":{"id":5},"device":{"request_names":["channel"],"pool_name":"node-a","device_name":"channel-0","cdi_device_ids":["k8s.compute-domain.nvidia.com/device=channel-0"]}},"daemon":null}],"configState":{"Type":"channel","ComputeDomain":"domain-a"}},{"devices":[{"channel":null,"daemon":{"info":{"id":0},"device":{"request_names":["daemon"],"pool_name":"node-a","device_name":"daemon-0","cdi_device_ids":["k8s.compute-domain.nvidia.com/device=daemon-0"]}}}],"configState":{"Type":"daemon","ComputeDomain":"domain-b"}}]}}} diff --git a/checkpoint/testdata/legacy-nvidia-cd-v1.json b/checkpoint/testdata/legacy-nvidia-cd-v1.json new file mode 100644 index 0000000..c76e945 --- /dev/null +++ b/checkpoint/testdata/legacy-nvidia-cd-v1.json @@ -0,0 +1 @@ +{"checksum":4288992558,"v1":{"preparedClaims":{"claim-uid-1":{"status":{"allocation":{"devices":{"results":[{"request":"channel","driver":"compute-domain.nvidia.com","pool":"node-a","device":"channel-0"}]}}},"preparedDevices":[{"devices":[{"channel":{"info":{"id":5},"device":{"Requests":["channel"],"PoolName":"node-a","DeviceName":"channel-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=channel-0"]}},"daemon":null}],"configState":{"Type":"channel","ComputeDomain":"domain-a"}},{"devices":[{"channel":null,"daemon":{"info":{"id":0},"device":{"Requests":["daemon"],"PoolName":"node-a","DeviceName":"daemon-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=daemon-0"]}}}],"configState":{"Type":"daemon","ComputeDomain":"domain-b"}}]}}}} diff --git a/checkpoint/testdata/legacy-nvidia-cd.json b/checkpoint/testdata/legacy-nvidia-cd.json new file mode 100644 index 0000000..c98bf00 --- /dev/null +++ b/checkpoint/testdata/legacy-nvidia-cd.json @@ -0,0 +1 @@ +{"checksum":4288992558,"v1":{"preparedClaims":{"claim-uid-1":{"status":{"allocation":{"devices":{"results":[{"request":"channel","driver":"compute-domain.nvidia.com","pool":"node-a","device":"channel-0"}]}}},"preparedDevices":[{"devices":[{"channel":{"info":{"id":5},"device":{"Requests":["channel"],"PoolName":"node-a","DeviceName":"channel-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=channel-0"]}},"daemon":null}],"configState":{"Type":"channel","ComputeDomain":"domain-a"}},{"devices":[{"channel":null,"daemon":{"info":{"id":0},"device":{"Requests":["daemon"],"PoolName":"node-a","DeviceName":"daemon-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=daemon-0"]}}}],"configState":{"Type":"daemon","ComputeDomain":"domain-b"}}]}}},"v2":{"checksum":2809985884,"preparedClaims":{"claim-uid-1":{"checkpointState":"PrepareCompleted","status":{"allocation":{"devices":{"results":[{"request":"channel","driver":"compute-domain.nvidia.com","pool":"node-a","device":"channel-0"}]}}},"preparedDevices":[{"devices":[{"channel":{"info":{"id":5},"device":{"Requests":["channel"],"PoolName":"node-a","DeviceName":"channel-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=channel-0"]}},"daemon":null}],"configState":{"Type":"channel","ComputeDomain":"domain-a"}},{"devices":[{"channel":null,"daemon":{"info":{"id":0},"device":{"Requests":["daemon"],"PoolName":"node-a","DeviceName":"daemon-0","CDIDeviceIDs":["k8s.compute-domain.nvidia.com/device=daemon-0"]}}}],"configState":{"Type":"daemon","ComputeDomain":"domain-b"}}],"name":"claim-1","namespace":"default"},"claim-uid-2":{"checkpointState":"PrepareStarted","status":{"allocation":{"devices":{"results":[{"request":"channel","driver":"compute-domain.nvidia.com","pool":"node-a","device":"channel-0"}]}}},"name":"claim-2","namespace":"team-a"},"claim-uid-3":{"checkpointState":"PrepareAborted","status":{"allocation":{"devices":{"results":[{"request":"channel","driver":"compute-domain.nvidia.com","pool":"node-a","device":"channel-0"}]}}},"name":"claim-3","namespace":"team-a","abortedAt":"2025-11-30T00:00:00Z"}},"nodeBootID":"boot-id-1"}} diff --git a/checkpoint/testdata/legacy-nvidia-gpu-v1.json b/checkpoint/testdata/legacy-nvidia-gpu-v1.json new file mode 100644 index 0000000..8590dce --- /dev/null +++ b/checkpoint/testdata/legacy-nvidia-gpu-v1.json @@ -0,0 +1 @@ +{"checksum":346381570,"v1":{"preparedClaims":{"claim-uid-1":{"status":{"allocation":{"devices":{"results":[{"request":"gpu","driver":"gpu.nvidia.com","pool":"node-a","device":"gpu-0"}]}}},"preparedDevices":[{"devices":[{"gpu":{"info":{"uuid":"GPU-aaaa"},"device":{"Requests":["gpu"],"PoolName":"node-a","DeviceName":"gpu-0","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-0"]}},"mig":null}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":true}},{"devices":[{"gpu":null,"mig":{"concrete":{"parentMinor":1,"giId":2,"ciId":0,"migUUID":"MIG-cccc","parentUUID":"GPU-dddd","parentPCIBusID":"0000:3b:00.0"},"device":{"Requests":["mig"],"PoolName":"node-a","DeviceName":"gpu-1-mig-1g10gb-2","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-1-mig-1g10gb-2"]}}}],"configState":{"mpsControlDaemonID":"mps-claim-uid-1"}},{"devices":[{"gpu":null,"mig":null,"vfio":{"info":{"uuid":"GPU-eeee","pciBusID":"0000:5e:00.0"},"device":{"Requests":["vfio"],"PoolName":"node-a","DeviceName":"gpu-3-vfio","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-3-vfio"],"Metadata":{"Attributes":{"productName":{"string":"NVIDIA A100"},"resource.kubernetes.io/pciBusID":{"string":"0000:5e:00.0"}},"NetworkData":null}}}}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":false}}]}}}} diff --git a/checkpoint/testdata/legacy-nvidia-gpu.json b/checkpoint/testdata/legacy-nvidia-gpu.json new file mode 100644 index 0000000..760771a --- /dev/null +++ b/checkpoint/testdata/legacy-nvidia-gpu.json @@ -0,0 +1 @@ +{"checksum":346381570,"v1":{"preparedClaims":{"claim-uid-1":{"status":{"allocation":{"devices":{"results":[{"request":"gpu","driver":"gpu.nvidia.com","pool":"node-a","device":"gpu-0"}]}}},"preparedDevices":[{"devices":[{"gpu":{"info":{"uuid":"GPU-aaaa"},"device":{"Requests":["gpu"],"PoolName":"node-a","DeviceName":"gpu-0","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-0"]}},"mig":null}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":true}},{"devices":[{"gpu":null,"mig":{"concrete":{"parentMinor":1,"giId":2,"ciId":0,"migUUID":"MIG-cccc","parentUUID":"GPU-dddd","parentPCIBusID":"0000:3b:00.0"},"device":{"Requests":["mig"],"PoolName":"node-a","DeviceName":"gpu-1-mig-1g10gb-2","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-1-mig-1g10gb-2"]}}}],"configState":{"mpsControlDaemonID":"mps-claim-uid-1"}},{"devices":[{"gpu":null,"mig":null,"vfio":{"info":{"uuid":"GPU-eeee","pciBusID":"0000:5e:00.0"},"device":{"Requests":["vfio"],"PoolName":"node-a","DeviceName":"gpu-3-vfio","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-3-vfio"],"Metadata":{"Attributes":{"productName":{"string":"NVIDIA A100"},"resource.kubernetes.io/pciBusID":{"string":"0000:5e:00.0"}},"NetworkData":null}}}}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":false}}]}}},"v2":{"checksum":1104686740,"preparedClaims":{"claim-uid-1":{"checkpointState":"PrepareCompleted","status":{"allocation":{"devices":{"results":[{"request":"gpu","driver":"gpu.nvidia.com","pool":"node-a","device":"gpu-0"}]}}},"preparedDevices":[{"devices":[{"gpu":{"info":{"uuid":"GPU-aaaa"},"device":{"Requests":["gpu"],"PoolName":"node-a","DeviceName":"gpu-0","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-0"]}},"mig":null}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":true}},{"devices":[{"gpu":null,"mig":{"concrete":{"parentMinor":1,"giId":2,"ciId":0,"migUUID":"MIG-cccc","parentUUID":"GPU-dddd","parentPCIBusID":"0000:3b:00.0"},"device":{"Requests":["mig"],"PoolName":"node-a","DeviceName":"gpu-1-mig-1g10gb-2","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-1-mig-1g10gb-2"]}}}],"configState":{"mpsControlDaemonID":"mps-claim-uid-1"}},{"devices":[{"gpu":null,"mig":null,"vfio":{"info":{"uuid":"GPU-eeee","pciBusID":"0000:5e:00.0"},"device":{"Requests":["vfio"],"PoolName":"node-a","DeviceName":"gpu-3-vfio","CDIDeviceIDs":["k8s.gpu.nvidia.com/device=gpu-3-vfio"],"Metadata":{"Attributes":{"productName":{"string":"NVIDIA A100"},"resource.kubernetes.io/pciBusID":{"string":"0000:5e:00.0"}},"NetworkData":null}}}}],"configState":{"mpsControlDaemonID":"","timeSliceApplied":false}}],"name":"claim-1","namespace":"default"},"claim-uid-2":{"checkpointState":"PrepareStarted","status":{"allocation":{"devices":{"results":[{"request":"gpu","driver":"gpu.nvidia.com","pool":"node-a","device":"gpu-0"}]}}},"name":"claim-2","namespace":"team-a"}},"nodeBootID":"boot-id-1"}}