@@ -30,6 +30,8 @@ import (
3030 "k8s.io/dynamic-resource-allocation/resourceslice"
3131 drapbv1 "k8s.io/kubelet/pkg/apis/dra/v1beta1"
3232
33+ checkpointapi "sigs.k8s.io/dra-example-driver/internal/api/checkpoint"
34+ checkpointv1alpha1 "sigs.k8s.io/dra-example-driver/internal/api/checkpoint/v1alpha1"
3335 "sigs.k8s.io/dra-example-driver/internal/profiles"
3436)
3537
@@ -42,13 +44,14 @@ type OpaqueDeviceConfig struct {
4244
4345type DeviceState struct {
4446 sync.Mutex
45- driverName string
46- cdi * CDIHandler
47- driverResources resourceslice.DriverResources
48- allocatable AllocatableDevices
49- configDecoder runtime.Decoder
50- configHandler profiles.ConfigHandler
51- checkpointPath string
47+ driverName string
48+ cdi * CDIHandler
49+ driverResources resourceslice.DriverResources
50+ allocatable AllocatableDevices
51+ configDecoder runtime.Decoder
52+ configHandler profiles.ConfigHandler
53+ checkpointPath string
54+ checkpointSerializer * json.Serializer
5255}
5356
5457func NewDeviceState (config * Config ) (* DeviceState , error ) {
@@ -84,6 +87,23 @@ func NewDeviceState(config *Config) (*DeviceState, error) {
8487 },
8588 )
8689
90+ checkpointScheme := runtime .NewScheme ()
91+ sb = runtime .NewSchemeBuilder (
92+ checkpointapi .AddToScheme ,
93+ checkpointv1alpha1 .AddToScheme ,
94+ )
95+ if err := sb .AddToScheme (checkpointScheme ); err != nil {
96+ return nil , fmt .Errorf ("create config scheme: %w" , err )
97+ }
98+ checkpointSerializer := json .NewSerializerWithOptions (
99+ json .DefaultMetaFactory ,
100+ checkpointScheme ,
101+ checkpointScheme ,
102+ json.SerializerOptions {
103+ Pretty : true , Strict : true ,
104+ },
105+ )
106+
87107 allocatable := make (AllocatableDevices )
88108 for _ , slice := range driverResources .Pools [config .flags .nodeName ].Slices {
89109 for _ , device := range slice .Devices {
@@ -92,24 +112,25 @@ func NewDeviceState(config *Config) (*DeviceState, error) {
92112 }
93113
94114 state := & DeviceState {
95- driverName : config .flags .driverName ,
96- cdi : cdi ,
97- driverResources : driverResources ,
98- allocatable : allocatable ,
99- configDecoder : decoder ,
100- configHandler : configHandler ,
101- checkpointPath : filepath .Join (config .DriverPluginPath (), DriverPluginCheckpointFile ),
115+ driverName : config .flags .driverName ,
116+ cdi : cdi ,
117+ driverResources : driverResources ,
118+ allocatable : allocatable ,
119+ configDecoder : decoder ,
120+ configHandler : configHandler ,
121+ checkpointPath : filepath .Join (config .DriverPluginPath (), DriverPluginCheckpointFile ),
122+ checkpointSerializer : checkpointSerializer ,
102123 }
103124
104- _ , err = readCheckpoint (state .checkpointPath )
125+ _ , err = readCheckpoint (state .checkpointSerializer , state . checkpointPath )
105126 if err != nil && ! errors .Is (err , fs .ErrNotExist ) {
106127 return nil , fmt .Errorf ("failed to read checkpoint: %w" , err )
107128 }
108129 if err == nil {
109130 return state , nil
110131 }
111132
112- if err := writeCheckpoint (state .checkpointPath , newCheckpoint () ); err != nil {
133+ if err := writeCheckpoint (state .checkpointSerializer , state . checkpointPath , & checkpointapi. Checkpoint {} ); err != nil {
113134 return nil , fmt .Errorf ("unable to sync to checkpoint: %v" , err )
114135 }
115136
@@ -122,30 +143,30 @@ func (s *DeviceState) Prepare(claim *resourceapi.ResourceClaim) ([]*drapbv1.Devi
122143
123144 claimUID := string (claim .UID )
124145
125- checkpoint , err := readCheckpoint (s .checkpointPath )
146+ checkpoint , err := readCheckpoint (s .checkpointSerializer , s . checkpointPath )
126147 if err != nil {
127148 return nil , fmt .Errorf ("unable to sync from checkpoint: %v" , err )
128149 }
129150
130- preparedDevices := checkpoint .GetPreparedDevices ( claimUID )
131- if preparedDevices != nil {
132- return preparedDevices . GetDevices ( ), nil
151+ preparedClaim , ok := checkpoint .PreparedClaims [ claim . UID ]
152+ if ok {
153+ return getPreparedDevices ( preparedClaim ), nil
133154 }
134- preparedDevices , err = s .prepareDevices (claim )
155+ preparedClaim , err = s .prepareDevices (claim )
135156 if err != nil {
136157 return nil , fmt .Errorf ("prepare failed: %v" , err )
137158 }
138159
139- if err = s .cdi .CreateClaimSpecFile (claimUID , preparedDevices ); err != nil {
160+ if err = s .cdi .CreateClaimSpecFile (claimUID , preparedClaim ); err != nil {
140161 return nil , fmt .Errorf ("unable to create CDI spec file for claim: %v" , err )
141162 }
142163
143- checkpoint .AddPreparedDevices (claimUID , preparedDevices )
164+ checkpoint .AddPreparedDevices (claimUID , preparedClaim )
144165 if err := writeCheckpoint (s .checkpointPath , checkpoint ); err != nil {
145166 return nil , fmt .Errorf ("unable to sync to checkpoint: %v" , err )
146167 }
147168
148- return preparedDevices .GetDevices (), nil
169+ return preparedClaim .GetDevices (), nil
149170}
150171
151172func (s * DeviceState ) Unprepare (claimUID string ) error {
@@ -182,9 +203,9 @@ func (s *DeviceState) Unprepare(claimUID string) error {
182203 return nil
183204}
184205
185- func (s * DeviceState ) prepareDevices (claim * resourceapi.ResourceClaim ) (profiles. PreparedDevices , error ) {
206+ func (s * DeviceState ) prepareDevices (claim * resourceapi.ResourceClaim ) (checkpointapi. PreparedClaim , error ) {
186207 if claim .Status .Allocation == nil {
187- return nil , fmt .Errorf ("claim not yet allocated" )
208+ return checkpointapi. PreparedClaim {} , fmt .Errorf ("claim not yet allocated" )
188209 }
189210 // Check if any device request has admin access
190211 hasAdminAccess := s .checkAdminAccess (claim )
@@ -196,7 +217,7 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
196217 claim .Status .Allocation .Devices .Config ,
197218 )
198219 if err != nil {
199- return nil , fmt .Errorf ("error getting opaque device configs: %v" , err )
220+ return checkpointapi. PreparedClaim {} , fmt .Errorf ("error getting opaque device configs: %v" , err )
200221 }
201222
202223 // Add the default GPU Config to the front of the config list with the
@@ -213,7 +234,7 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
213234 continue
214235 }
215236 if _ , exists := s .allocatable [result .Device ]; ! exists {
216- return nil , fmt .Errorf ("requested device is not allocatable: %v" , result .Device )
237+ return checkpointapi. PreparedClaim {} , fmt .Errorf ("requested device is not allocatable: %v" , result .Device )
217238 }
218239 for _ , c := range slices .Backward (configs ) {
219240 if len (c .Requests ) == 0 || slices .Contains (c .Requests , result .Request ) {
@@ -231,7 +252,7 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
231252 // Apply the config to the list of results associated with it.
232253 containerEdits , err := s .configHandler .ApplyConfig (config , results )
233254 if err != nil {
234- return nil , fmt .Errorf ("error applying config: %w" , err )
255+ return checkpointapi. PreparedClaim {} , fmt .Errorf ("error applying config: %w" , err )
235256 }
236257
237258 // Merge any new container edits with the overall per device map.
@@ -242,11 +263,11 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
242263
243264 // Walk through each config and its associated device allocation results
244265 // and construct the list of prepared devices to return.
245- var preparedDevices profiles. PreparedDevices
266+ var preparedDevices []checkpointapi. PreparedDevice
246267 for _ , results := range configResultsMap {
247268 for _ , result := range results {
248- device := & profiles .PreparedDevice {
249- Device : drapbv1 .Device {
269+ device := checkpointapi .PreparedDevice {
270+ Device : checkpointapi .Device {
250271 RequestNames : []string {result .Request },
251272 PoolName : result .Pool ,
252273 DeviceName : result .Device ,
@@ -259,7 +280,7 @@ func (s *DeviceState) prepareDevices(claim *resourceapi.ResourceClaim) (profiles
259280 }
260281 }
261282
262- return preparedDevices , nil
283+ return checkpointapi. PreparedClaim { PreparedDevices : preparedDevices } , nil
263284}
264285
265286func (s * DeviceState ) unprepareDevices (claimUID string , devices profiles.PreparedDevices ) error {
0 commit comments