k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/devicemanager/checkpoint/checkpoint.go (about) 1 /* 2 Copyright 2017 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 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 // DeviceManagerCheckpoint defines the operations to retrieve pod devices 28 type DeviceManagerCheckpoint interface { 29 checkpointmanager.Checkpoint 30 GetDataInLatestFormat() ([]PodDevicesEntry, map[string][]string) 31 } 32 33 // DevicesPerNUMA represents device ids obtained from device plugin per NUMA node id 34 type DevicesPerNUMA map[int64][]string 35 36 // PodDevicesEntry connects pod information to devices 37 type PodDevicesEntry struct { 38 PodUID string 39 ContainerName string 40 ResourceName string 41 DeviceIDs DevicesPerNUMA 42 AllocResp []byte 43 } 44 45 // checkpointData struct is used to store pod to device allocation information 46 // in a checkpoint file. 47 // TODO: add version control when we need to change checkpoint format. 48 type checkpointData struct { 49 PodDeviceEntries []PodDevicesEntry 50 RegisteredDevices map[string][]string 51 } 52 53 // Data holds checkpoint data and its checksum 54 type Data struct { 55 Data checkpointData 56 Checksum checksum.Checksum 57 } 58 59 // NewDevicesPerNUMA is a function that creates DevicesPerNUMA map 60 func NewDevicesPerNUMA() DevicesPerNUMA { 61 return make(DevicesPerNUMA) 62 } 63 64 // Devices is a function that returns all device ids for all NUMA nodes 65 // and represent it as sets.Set[string] 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 // New returns an instance of Checkpoint - must be an alias for the most recent version 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 // MarshalCheckpoint returns marshalled data 90 func (cp *Data) MarshalCheckpoint() ([]byte, error) { 91 cp.Checksum = checksum.New(cp.Data) 92 return json.Marshal(*cp) 93 } 94 95 // UnmarshalCheckpoint returns unmarshalled data 96 func (cp *Data) UnmarshalCheckpoint(blob []byte) error { 97 return json.Unmarshal(blob, cp) 98 } 99 100 // VerifyChecksum verifies that passed checksum is same as calculated checksum 101 func (cp *Data) VerifyChecksum() error { 102 return cp.Checksum.Verify(cp.Data) 103 } 104 105 // GetDataInLatestFormat returns device entries and registered devices in the *most recent* 106 // checkpoint format, *not* in the original format stored on disk. 107 func (cp *Data) GetDataInLatestFormat() ([]PodDevicesEntry, map[string][]string) { 108 return cp.Data.PodDeviceEntries, cp.Data.RegisteredDevices 109 }