...
1
16
17 package checkpoint
18
19 import (
20 "encoding/json"
21 "fmt"
22 "hash/fnv"
23 "strings"
24
25 "k8s.io/apimachinery/pkg/util/dump"
26 "k8s.io/klog/v2"
27 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
28 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors"
29 )
30
31
32 type PodDevicesEntryV1 struct {
33 PodUID string
34 ContainerName string
35 ResourceName string
36 DeviceIDs []string
37 AllocResp []byte
38 }
39
40
41
42 type checkpointDataV1 struct {
43 PodDeviceEntries []PodDevicesEntryV1
44 RegisteredDevices map[string][]string
45 }
46
47
48
49
50 func (cp checkpointDataV1) checksum() checksum.Checksum {
51 object := dump.ForHash(cp)
52 object = strings.Replace(object, "checkpointDataV1", "checkpointData", 1)
53 object = strings.Replace(object, "PodDevicesEntryV1", "PodDevicesEntry", -1)
54 hash := fnv.New32a()
55 fmt.Fprintf(hash, "%v", object)
56 return checksum.Checksum(hash.Sum32())
57 }
58
59
60 type DataV1 struct {
61 Data checkpointDataV1
62 Checksum checksum.Checksum
63 }
64
65
66
67
68
69 func NewV1(devEntries []PodDevicesEntryV1,
70 devices map[string][]string) DeviceManagerCheckpoint {
71 return &DataV1{
72 Data: checkpointDataV1{
73 PodDeviceEntries: devEntries,
74 RegisteredDevices: devices,
75 },
76 }
77 }
78
79
80 func (cp *DataV1) MarshalCheckpoint() ([]byte, error) {
81 klog.InfoS("Marshalling a device manager V1 checkpoint")
82 cp.Checksum = cp.Data.checksum()
83 return json.Marshal(*cp)
84 }
85
86
87 func (cp *DataV1) UnmarshalCheckpoint(blob []byte) error {
88 return json.Unmarshal(blob, cp)
89 }
90
91
92 func (cp *DataV1) VerifyChecksum() error {
93 if cp.Checksum != cp.Data.checksum() {
94 return errors.ErrCorruptCheckpoint
95 }
96 return nil
97 }
98
99
100
101 func (cp *DataV1) GetDataInLatestFormat() ([]PodDevicesEntry, map[string][]string) {
102 var podDevs []PodDevicesEntry
103 for _, entryV1 := range cp.Data.PodDeviceEntries {
104 devsPerNuma := NewDevicesPerNUMA()
105
106
107 devsPerNuma[0] = entryV1.DeviceIDs
108 podDevs = append(podDevs, PodDevicesEntry{
109 PodUID: entryV1.PodUID,
110 ContainerName: entryV1.ContainerName,
111 ResourceName: entryV1.ResourceName,
112 DeviceIDs: devsPerNuma,
113 AllocResp: entryV1.AllocResp,
114 })
115 }
116 return podDevs, cp.Data.RegisteredDevices
117 }
118
View as plain text