1+ /*
2+ * Copyright The Kubernetes Authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117package main
218
319import (
420 "encoding/json"
5-
6- "k8s.io/kubernetes/pkg/kubelet/checkpointmanager "
7- "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum "
21+ "fmt"
22+ "os "
23+ "path/filepath "
824
925 "sigs.k8s.io/dra-example-driver/internal/profiles"
1026)
1127
1228type PreparedClaims map [string ]profiles.PreparedDevices
1329
1430type Checkpoint struct {
15- Checksum checksum.Checksum `json:"checksum"`
16- V1 * CheckpointV1 `json:"v1,omitempty"`
31+ V1 * CheckpointV1 `json:"v1,omitempty"`
1732}
1833
19- var _ checkpointmanager.Checkpoint = & Checkpoint {}
20-
2134type CheckpointV1 struct {
2235 PreparedClaims PreparedClaims `json:"preparedClaims,omitempty"`
2336}
2437
2538func newCheckpoint () * Checkpoint {
2639 pc := & Checkpoint {
27- Checksum : 0 ,
28- V1 : & CheckpointV1 {
29- PreparedClaims : make (PreparedClaims ),
30- },
40+ V1 : & CheckpointV1 {},
3141 }
3242 return pc
3343}
3444
45+ func readCheckpoint (path string ) (* Checkpoint , error ) {
46+ data , err := os .ReadFile (path )
47+ if err != nil {
48+ return nil , err
49+ }
50+ checkpoint := new (Checkpoint )
51+ err = json .Unmarshal (data , checkpoint )
52+ if err != nil {
53+ return nil , fmt .Errorf ("unmarshal json from %s: %w" , path , err )
54+ }
55+ return checkpoint , nil
56+ }
57+
58+ func writeCheckpoint (path string , checkpoint * Checkpoint ) (err error ) {
59+ data , err := json .Marshal (checkpoint )
60+ if err != nil {
61+ return fmt .Errorf ("marshal json: %w" , err )
62+ }
63+ dir := filepath .Dir (path )
64+ tmp , err := os .CreateTemp (dir , "tmp-checkpoint-*" )
65+ if err != nil {
66+ return fmt .Errorf ("create temp file in %s: %w" , dir , err )
67+ }
68+ defer func () {
69+ if err1 := tmp .Close (); err1 != nil && err == nil {
70+ err = fmt .Errorf ("close temp file: %w" , err1 )
71+ }
72+ }()
73+ if _ , err := tmp .Write (data ); err != nil {
74+ return fmt .Errorf ("write to temp file %s: %w" , tmp .Name (), err )
75+ }
76+ if err := tmp .Sync (); err != nil {
77+ return fmt .Errorf ("sync temp file: %w" , err )
78+ }
79+ if err := os .Rename (tmp .Name (), path ); err != nil {
80+ return fmt .Errorf ("rename %s to %s: %w" , tmp .Name (), path , err )
81+ }
82+ return nil
83+ }
84+
3585func (cp * Checkpoint ) GetPreparedDevices (claimUID string ) profiles.PreparedDevices {
3686 if cp .V1 == nil {
3787 return nil
@@ -47,6 +97,10 @@ func (cp *Checkpoint) AddPreparedDevices(claimUID string, pds profiles.PreparedD
4797 return
4898 }
4999
100+ if cp .V1 .PreparedClaims == nil {
101+ cp .V1 .PreparedClaims = make (PreparedClaims )
102+ }
103+
50104 cp .V1 .PreparedClaims [claimUID ] = pds
51105}
52106
@@ -57,30 +111,3 @@ func (cp *Checkpoint) RemovePreparedDevices(claimUID string) {
57111
58112 delete (cp .V1 .PreparedClaims , claimUID )
59113}
60-
61- func (cp * Checkpoint ) MarshalCheckpoint () ([]byte , error ) {
62- cp .Checksum = 0
63- out , err := json .Marshal (* cp )
64- if err != nil {
65- return nil , err
66- }
67- cp .Checksum = checksum .New (out )
68- return json .Marshal (* cp )
69- }
70-
71- func (cp * Checkpoint ) UnmarshalCheckpoint (data []byte ) error {
72- return json .Unmarshal (data , cp )
73- }
74-
75- func (cp * Checkpoint ) VerifyChecksum () error {
76- ck := cp .Checksum
77- cp .Checksum = 0
78- defer func () {
79- cp .Checksum = ck
80- }()
81- out , err := json .Marshal (* cp )
82- if err != nil {
83- return err
84- }
85- return ck .Verify (out )
86- }
0 commit comments