@@ -18,51 +18,90 @@ package state
1818
1919import  (
2020	"encoding/json" 
21+ 	"hash/crc32" 
2122
22- 	"k8s.io/kubernetes /pkg/kubelet/checkpointmanager " 
23- 	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum " 
23+ 	metav1  "k8s.io/apimachinery /pkg/apis/meta/v1 " 
24+ 	"k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors " 
2425)
2526
26- var  _  checkpointmanager.Checkpoint  =  & DRAManagerCheckpoint {}
27+ const  (
28+ 	CheckpointAPIGroup    =  "checkpoint.dra.kubelet.k8s.io" 
29+ 	CheckpointKind        =  "DRACheckpoint" 
30+ 	CheckpointAPIVersion  =  CheckpointAPIGroup  +  "/v1" 
31+ )
2732
28- const  checkpointVersion  =  "v1" 
33+ // Checkpoint represents a structure to store DRA checkpoint data 
34+ type  Checkpoint  struct  {
35+ 	// Data is a JSON serialized checkpoint data 
36+ 	Data  string 
37+ 	// Checksum is a checksum of Data 
38+ 	Checksum  uint32 
39+ }
2940
30- // DRAManagerCheckpoint struct is used to store pod dynamic resources assignments in a checkpoint 
31- type  DRAManagerCheckpoint  struct  {
32- 	Version   string              `json:"version"` 
33- 	Entries   ClaimInfoStateList  `json:"entries,omitempty"` 
34- 	Checksum  checksum.Checksum   `json:"checksum"` 
41+ type  CheckpointData  struct  {
42+ 	metav1.TypeMeta 
43+ 	ClaimInfoStateList  ClaimInfoStateList 
3544}
3645
37- // List of claim info to store in checkpoint 
38- type  ClaimInfoStateList  []ClaimInfoState 
46+ // NewCheckpoint creates a new checkpoint from a list of claim info states 
47+ func  NewCheckpoint (data  ClaimInfoStateList ) (* Checkpoint , error ) {
48+ 	cpData  :=  & CheckpointData {
49+ 		TypeMeta : metav1.TypeMeta {
50+ 			Kind :       CheckpointKind ,
51+ 			APIVersion : CheckpointAPIVersion ,
52+ 		},
53+ 		ClaimInfoStateList : data ,
54+ 	}
55+ 
56+ 	cpDataBytes , err  :=  json .Marshal (cpData )
57+ 	if  err  !=  nil  {
58+ 		return  nil , err 
59+ 	}
3960
40- // NewDRAManagerCheckpoint returns an instance of Checkpoint 
41- func  NewDRAManagerCheckpoint () * DRAManagerCheckpoint  {
42- 	return  & DRAManagerCheckpoint {
43- 		Version : checkpointVersion ,
44- 		Entries : ClaimInfoStateList {},
61+ 	cp  :=  & Checkpoint {
62+ 		Data :     string (cpDataBytes ),
63+ 		Checksum : crc32 .ChecksumIEEE (cpDataBytes ),
4564	}
65+ 
66+ 	return  cp , nil 
4667}
4768
48- // MarshalCheckpoint returns marshalled checkpoint 
49- func  (dc  * DRAManagerCheckpoint ) MarshalCheckpoint () ([]byte , error ) {
50- 	// make sure checksum wasn't set before so it doesn't affect output checksum 
51- 	dc .Checksum  =  0 
52- 	dc .Checksum  =  checksum .New (dc )
53- 	return  json .Marshal (* dc )
69+ // MarshalCheckpoint marshals checkpoint to JSON 
70+ func  (cp  * Checkpoint ) MarshalCheckpoint () ([]byte , error ) {
71+ 	return  json .Marshal (cp )
5472}
5573
56- // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint 
57- func  (dc  * DRAManagerCheckpoint ) UnmarshalCheckpoint (blob  []byte ) error  {
58- 	return  json .Unmarshal (blob , dc )
74+ // UnmarshalCheckpoint unmarshals checkpoint from JSON 
75+ // and verifies its data checksum 
76+ func  (cp  * Checkpoint ) UnmarshalCheckpoint (blob  []byte ) error  {
77+ 	if  err  :=  json .Unmarshal (blob , cp ); err  !=  nil  {
78+ 		return  err 
79+ 	}
80+ 
81+ 	// verify checksum 
82+ 	if  err  :=  cp .VerifyChecksum (); err  !=  nil  {
83+ 		return  err 
84+ 	}
85+ 
86+ 	return  nil 
5987}
6088
61- // VerifyChecksum verifies that current checksum of checkpoint is valid 
62- func  (dc  * DRAManagerCheckpoint ) VerifyChecksum () error  {
63- 	ck  :=  dc .Checksum 
64- 	dc .Checksum  =  0 
65- 	err  :=  ck .Verify (dc )
66- 	dc .Checksum  =  ck 
67- 	return  err 
89+ // VerifyChecksum verifies that current checksum 
90+ // of checkpointed Data is valid 
91+ func  (cp  * Checkpoint ) VerifyChecksum () error  {
92+ 	expectedCS  :=  crc32 .ChecksumIEEE ([]byte (cp .Data ))
93+ 	if  expectedCS  !=  cp .Checksum  {
94+ 		return  & errors.CorruptCheckpointError {ActualCS : uint64 (cp .Checksum ), ExpectedCS : uint64 (expectedCS )}
95+ 	}
96+ 	return  nil 
97+ }
98+ 
99+ // GetClaimInfoStateList returns list of claim info states from checkpoint 
100+ func  (cp  * Checkpoint ) GetClaimInfoStateList () (ClaimInfoStateList , error ) {
101+ 	var  data  CheckpointData 
102+ 	if  err  :=  json .Unmarshal ([]byte (cp .Data ), & data ); err  !=  nil  {
103+ 		return  nil , err 
104+ 	}
105+ 
106+ 	return  data .ClaimInfoStateList , nil 
68107}
0 commit comments