...
1
16
17 package state
18
19 import (
20 "encoding/json"
21 "fmt"
22 "hash/fnv"
23 "strings"
24
25 "k8s.io/apimachinery/pkg/util/dump"
26 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
27 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
28 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
29 )
30
31 var _ checkpointmanager.Checkpoint = &DRAManagerCheckpoint{}
32
33 const checkpointVersion = "v1"
34
35
36 type DRAManagerCheckpoint struct {
37 Version string `json:"version"`
38 Entries ClaimInfoStateList `json:"entries,omitempty"`
39 Checksum checksum.Checksum `json:"checksum"`
40 }
41
42
43 type DRAManagerCheckpointWithoutResourceHandles struct {
44 Version string `json:"version"`
45 Entries ClaimInfoStateListWithoutResourceHandles `json:"entries,omitempty"`
46 Checksum checksum.Checksum `json:"checksum"`
47 }
48
49
50 type ClaimInfoStateList []ClaimInfoState
51
52
53
54 type ClaimInfoStateListWithoutResourceHandles []ClaimInfoStateWithoutResourceHandles
55
56
57 func NewDRAManagerCheckpoint() *DRAManagerCheckpoint {
58 return &DRAManagerCheckpoint{
59 Version: checkpointVersion,
60 Entries: ClaimInfoStateList{},
61 }
62 }
63
64
65 func (dc *DRAManagerCheckpoint) MarshalCheckpoint() ([]byte, error) {
66
67 dc.Checksum = 0
68 dc.Checksum = checksum.New(dc)
69 return json.Marshal(*dc)
70 }
71
72
73 func (dc *DRAManagerCheckpoint) UnmarshalCheckpoint(blob []byte) error {
74 return json.Unmarshal(blob, dc)
75 }
76
77
78 func (dc *DRAManagerCheckpoint) VerifyChecksum() error {
79 ck := dc.Checksum
80 dc.Checksum = 0
81 err := ck.Verify(dc)
82 if err == errors.ErrCorruptCheckpoint {
83
84
85 err = verifyChecksumWithoutResourceHandles(dc, ck)
86 }
87 dc.Checksum = ck
88 return err
89 }
90
91
92
93
94 func verifyChecksumWithoutResourceHandles(dc *DRAManagerCheckpoint, checkSum checksum.Checksum) error {
95 entries := ClaimInfoStateListWithoutResourceHandles{}
96 for _, entry := range dc.Entries {
97 entries = append(entries, ClaimInfoStateWithoutResourceHandles{
98 DriverName: entry.DriverName,
99 ClassName: entry.ClassName,
100 ClaimUID: entry.ClaimUID,
101 ClaimName: entry.ClaimName,
102 Namespace: entry.Namespace,
103 PodUIDs: entry.PodUIDs,
104 CDIDevices: entry.CDIDevices,
105 })
106 }
107 oldcheckpoint := &DRAManagerCheckpointWithoutResourceHandles{
108 Version: checkpointVersion,
109 Entries: entries,
110 Checksum: 0,
111 }
112
113 object := dump.ForHash(oldcheckpoint)
114 object = strings.Replace(object, "DRAManagerCheckpointWithoutResourceHandles", "DRAManagerCheckpoint", 1)
115 object = strings.Replace(object, "ClaimInfoStateListWithoutResourceHandles", "ClaimInfoStateList", 1)
116 hash := fnv.New32a()
117 fmt.Fprintf(hash, "%v", object)
118 if checkSum != checksum.Checksum(hash.Sum32()) {
119 return errors.ErrCorruptCheckpoint
120 }
121 return nil
122 }
123
View as plain text