Repository navigation
refactor(kubeletplugin): move the checkpoint to dra-lib, drop k8s.io/kubernetes #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| go.work | ||
| go.work.sum |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| /* | ||
| * 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 main | ||
|
|
||
| import ( | ||
| "maps" | ||
| "path/filepath" | ||
| "reflect" | ||
| "slices" | ||
|
|
||
| resourceapi "k8s.io/api/resource/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/dynamic-resource-allocation/kubeletplugin" | ||
|
|
||
| checkpointapi "github.com/Project-HAMi/dra-lib/api/checkpoint/v1" | ||
| "github.com/Project-HAMi/dra-lib/checkpoint" | ||
| ) | ||
|
|
||
| // checkpointGroup is the API group of the checkpoint shared by the HAMi DRA drivers. | ||
| const checkpointGroup = "checkpoint.internal.project-hami.io" | ||
|
|
||
| type checkpointStore interface { | ||
| Read() (*checkpointapi.Checkpoint, error) | ||
| Write(*checkpointapi.Checkpoint) error | ||
| Update(func(*checkpointapi.Checkpoint) error) error | ||
| } | ||
|
|
||
| // newCheckpointStore opens the driver checkpoint in dir. A legacy kubelet | ||
| // checkpointmanager file is migrated on first read; that is deprecated and will be removed in a future version. | ||
| func newCheckpointStore(dir string) (*checkpoint.Store, error) { | ||
| return checkpoint.NewStore(filepath.Join(dir, DriverPluginCheckpointFileBasename), | ||
| checkpoint.Options{Group: checkpointGroup, DriverName: DriverName}) | ||
| } | ||
|
|
||
| type ClaimCheckpointState string | ||
|
|
||
| const ( | ||
| ClaimCheckpointStateUnset ClaimCheckpointState = "" | ||
| ClaimCheckpointStatePrepareStarted ClaimCheckpointState = "PrepareStarted" | ||
| ClaimCheckpointStatePrepareCompleted ClaimCheckpointState = "PrepareCompleted" | ||
| ) | ||
|
|
||
| // CheckpointState is the driver state persisted in the checkpoint, in the | ||
| // form the driver works with. | ||
| type CheckpointState struct { | ||
| NodeBootID string | ||
| PreparedClaims PreparedClaimsByUID | ||
| } | ||
|
|
||
| type PreparedClaimsByUID map[string]PreparedClaim | ||
|
|
||
| type PreparedClaim struct { | ||
| CheckpointState ClaimCheckpointState | ||
| Status resourceapi.ResourceClaimStatus | ||
| PreparedDevices PreparedDevices | ||
| Name string | ||
| Namespace string | ||
| } | ||
|
|
||
| func newCheckpointState() *CheckpointState { | ||
| return &CheckpointState{PreparedClaims: make(PreparedClaimsByUID)} | ||
| } | ||
|
|
||
| func checkpointStateToAPI(state *CheckpointState) *checkpointapi.Checkpoint { | ||
| checkpoint := &checkpointapi.Checkpoint{NodeBootID: state.NodeBootID} | ||
| for _, uid := range slices.Sorted(maps.Keys(state.PreparedClaims)) { | ||
| pc := state.PreparedClaims[uid] | ||
| claim := checkpointapi.PreparedClaim{ | ||
| UID: types.UID(uid), | ||
| Name: pc.Name, | ||
| Namespace: pc.Namespace, | ||
| State: checkpointapi.PrepareState(pc.CheckpointState), | ||
| } | ||
| if status := pc.Status; !reflect.DeepEqual(status, resourceapi.ResourceClaimStatus{}) { | ||
| claim.Status = &status | ||
| } | ||
| if len(pc.PreparedDevices) > 0 { | ||
| claim.NVIDIA = &checkpointapi.NVIDIAClaim{} | ||
| } | ||
| for _, group := range pc.PreparedDevices { | ||
| if group == nil { | ||
| continue | ||
| } | ||
| index := int32(len(claim.NVIDIA.Groups)) | ||
| claim.NVIDIA.Groups = append(claim.NVIDIA.Groups, checkpointapi.NVIDIADeviceGroup{ | ||
| MPSControlDaemonID: group.ConfigState.MpsControlDaemonID, | ||
| }) | ||
| for i := range group.Devices { | ||
| claim.Devices = append(claim.Devices, preparedDeviceToAPI(&group.Devices[i], index)) | ||
| } | ||
| } | ||
| checkpoint.PreparedClaims = append(checkpoint.PreparedClaims, claim) | ||
| } | ||
| return checkpoint | ||
| } | ||
|
|
||
| func preparedDeviceToAPI(pd *PreparedDevice, group int32) checkpointapi.PreparedDevice { | ||
| nvidia := &checkpointapi.NVIDIADevice{Group: group} | ||
| device := checkpointapi.PreparedDevice{NVIDIA: nvidia} | ||
| switch { | ||
| case pd.HAMiGpu != nil: | ||
| nvidia.Type = checkpointapi.NVIDIADeviceTypeGPU | ||
| if pd.HAMiGpu.Info != nil { | ||
| nvidia.UUID = pd.HAMiGpu.Info.UUID | ||
| } | ||
| device.Device = deviceToAPI(pd.HAMiGpu.Device) | ||
| device.Partition = &checkpointapi.Partition{Type: checkpointapi.PartitionTypeSoft} | ||
| case pd.Gpu != nil: | ||
| nvidia.Type = checkpointapi.NVIDIADeviceTypeGPU | ||
| if pd.Gpu.Info != nil { | ||
| nvidia.UUID = pd.Gpu.Info.UUID | ||
| } | ||
| device.Device = deviceToAPI(pd.Gpu.Device) | ||
| case pd.Mig != nil: | ||
| nvidia.Type = checkpointapi.NVIDIADeviceTypeMIG | ||
| device.Device = deviceToAPI(pd.Mig.Device) | ||
| device.Partition = &checkpointapi.Partition{Type: checkpointapi.PartitionTypeHard} | ||
| if c := pd.Mig.Concrete; c != nil { | ||
| device.Partition.ParentDevice = c.ParentUUID | ||
| device.Partition.NVIDIA = &checkpointapi.NVIDIAPartition{ | ||
| ParentMinor: int32(c.ParentMinor), | ||
| GIID: int32(c.GIID), | ||
| CIID: int32(c.CIID), | ||
| MIGUUID: c.MigUUID, | ||
| } | ||
| } | ||
| case pd.Vfio != nil: | ||
| nvidia.Type = checkpointapi.NVIDIADeviceTypeVFIO | ||
| if pd.Vfio.Info != nil { | ||
| nvidia.UUID = pd.Vfio.Info.UUID | ||
| } | ||
| device.Device = deviceToAPI(pd.Vfio.Device) | ||
| } | ||
| return device | ||
| } | ||
|
|
||
| func deviceToAPI(d *kubeletplugin.Device) checkpointapi.Device { | ||
| if d == nil { | ||
| return checkpointapi.Device{} | ||
| } | ||
| return checkpointapi.Device{ | ||
| Requests: d.Requests, | ||
| PoolName: d.PoolName, | ||
| DeviceName: d.DeviceName, | ||
| CDIDeviceIDs: d.CDIDeviceIDs, | ||
| } | ||
| } | ||
|
|
||
| func checkpointStateFromAPI(checkpoint *checkpointapi.Checkpoint) *CheckpointState { | ||
| state := newCheckpointState() | ||
| state.NodeBootID = checkpoint.NodeBootID | ||
| for _, claim := range checkpoint.PreparedClaims { | ||
| pc := PreparedClaim{ | ||
| CheckpointState: ClaimCheckpointState(claim.State), | ||
| Name: claim.Name, | ||
| Namespace: claim.Namespace, | ||
| } | ||
| if claim.Status != nil { | ||
| pc.Status = *claim.Status | ||
| } | ||
| if claim.NVIDIA != nil { | ||
| for _, group := range claim.NVIDIA.Groups { | ||
| pc.PreparedDevices = append(pc.PreparedDevices, &PreparedDeviceGroup{ | ||
| ConfigState: DeviceConfigState{MpsControlDaemonID: group.MPSControlDaemonID}, | ||
| }) | ||
| } | ||
| } | ||
| for i := range claim.Devices { | ||
| device := &claim.Devices[i] | ||
| var group int | ||
| if device.NVIDIA != nil { | ||
| group = int(device.NVIDIA.Group) | ||
| } | ||
| for group >= len(pc.PreparedDevices) { | ||
| pc.PreparedDevices = append(pc.PreparedDevices, &PreparedDeviceGroup{}) | ||
| } | ||
| pc.PreparedDevices[group].Devices = append(pc.PreparedDevices[group].Devices, preparedDeviceFromAPI(device)) | ||
| } | ||
| state.PreparedClaims[string(claim.UID)] = pc | ||
| } | ||
| return state | ||
| } | ||
|
|
||
| func preparedDeviceFromAPI(device *checkpointapi.PreparedDevice) PreparedDevice { | ||
| if device.NVIDIA == nil { | ||
| return PreparedDevice{} | ||
| } | ||
| d := &kubeletplugin.Device{ | ||
| Requests: device.Device.Requests, | ||
| PoolName: device.Device.PoolName, | ||
| DeviceName: device.Device.DeviceName, | ||
| CDIDeviceIDs: device.Device.CDIDeviceIDs, | ||
| } | ||
| uuid := device.NVIDIA.UUID | ||
| switch device.NVIDIA.Type { | ||
| case checkpointapi.NVIDIADeviceTypeGPU: | ||
| // A HAMi-core GPU is a whole GPU shared through software limits. | ||
| if p := device.Partition; p != nil && p.Type == checkpointapi.PartitionTypeSoft { | ||
| return PreparedDevice{HAMiGpu: &PreparedHAMiGpu{Info: &HAMiGpuInfo{GpuInfo{UUID: uuid}}, Device: d}} | ||
| } | ||
| return PreparedDevice{Gpu: &PreparedGpu{Info: &GpuInfo{UUID: uuid}, Device: d}} | ||
| case checkpointapi.NVIDIADeviceTypeMIG: | ||
| mig := &PreparedMigDevice{Device: d} | ||
| if p := device.Partition; p != nil && p.NVIDIA != nil { | ||
| mig.Concrete = &MigLiveTuple{ | ||
| ParentMinor: GPUMinor(p.NVIDIA.ParentMinor), | ||
| GIID: int(p.NVIDIA.GIID), | ||
| CIID: int(p.NVIDIA.CIID), | ||
| MigUUID: p.NVIDIA.MIGUUID, | ||
| ParentUUID: p.ParentDevice, | ||
| } | ||
| } | ||
| return PreparedDevice{Mig: mig} | ||
| case checkpointapi.NVIDIADeviceTypeVFIO: | ||
| return PreparedDevice{Vfio: &PreparedVfioDevice{Info: &VfioDeviceInfo{UUID: uuid}, Device: d}} | ||
| } | ||
| return PreparedDevice{} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The golangci-lint step fails on Go 1.26.0.
go.modnow targetsgo 1.26.0.golangci-linttakes that target when no config overrides it, and this repository has no config file. The step installsversion: v2.6, which is built with Go 1.25. The config loader then rejects the target withthe Go language version (go1.25) used to build golangci-lint is lower than the targeted Go version (1.26.0). The lint step fails, and so does the build job.Set the linter to a release that supports Go 1.26. The
golangci-lintrelease notes state that support began in v2.9.0. The action's README example uses v2.13.Suggested fix
- name: golangci-lint uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9 with: - version: v2.6 + version: v2.13