...
1
16
17 package checkpoint
18
19 import (
20 "encoding/json"
21
22 "k8s.io/apimachinery/pkg/util/sets"
23 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
24 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
25 )
26
27
28 type DeviceManagerCheckpoint interface {
29 checkpointmanager.Checkpoint
30 GetDataInLatestFormat() ([]PodDevicesEntry, map[string][]string)
31 }
32
33
34 type DevicesPerNUMA map[int64][]string
35
36
37 type PodDevicesEntry struct {
38 PodUID string
39 ContainerName string
40 ResourceName string
41 DeviceIDs DevicesPerNUMA
42 AllocResp []byte
43 }
44
45
46
47
48 type checkpointData struct {
49 PodDeviceEntries []PodDevicesEntry
50 RegisteredDevices map[string][]string
51 }
52
53
54 type Data struct {
55 Data checkpointData
56 Checksum checksum.Checksum
57 }
58
59
60 func NewDevicesPerNUMA() DevicesPerNUMA {
61 return make(DevicesPerNUMA)
62 }
63
64
65
66 func (dev DevicesPerNUMA) Devices() sets.Set[string] {
67 result := sets.New[string]()
68
69 for _, devs := range dev {
70 result.Insert(devs...)
71 }
72 return result
73 }
74
75
76 func New(devEntries []PodDevicesEntry, devices map[string][]string) DeviceManagerCheckpoint {
77 return newV2(devEntries, devices)
78 }
79
80 func newV2(devEntries []PodDevicesEntry, devices map[string][]string) DeviceManagerCheckpoint {
81 return &Data{
82 Data: checkpointData{
83 PodDeviceEntries: devEntries,
84 RegisteredDevices: devices,
85 },
86 }
87 }
88
89
90 func (cp *Data) MarshalCheckpoint() ([]byte, error) {
91 cp.Checksum = checksum.New(cp.Data)
92 return json.Marshal(*cp)
93 }
94
95
96 func (cp *Data) UnmarshalCheckpoint(blob []byte) error {
97 return json.Unmarshal(blob, cp)
98 }
99
100
101 func (cp *Data) VerifyChecksum() error {
102 return cp.Checksum.Verify(cp.Data)
103 }
104
105
106
107 func (cp *Data) GetDataInLatestFormat() ([]PodDevicesEntry, map[string][]string) {
108 return cp.Data.PodDeviceEntries, cp.Data.RegisteredDevices
109 }
110
View as plain text