k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/dra/state/checkpoint.go (about) 1 /* 2 Copyright 2023 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 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 // DRAManagerCheckpoint struct is used to store pod dynamic resources assignments in a checkpoint 36 type DRAManagerCheckpoint struct { 37 Version string `json:"version"` 38 Entries ClaimInfoStateList `json:"entries,omitempty"` 39 Checksum checksum.Checksum `json:"checksum"` 40 } 41 42 // DraManagerCheckpoint struct is an old implementation of the DraManagerCheckpoint 43 type DRAManagerCheckpointWithoutResourceHandles struct { 44 Version string `json:"version"` 45 Entries ClaimInfoStateListWithoutResourceHandles `json:"entries,omitempty"` 46 Checksum checksum.Checksum `json:"checksum"` 47 } 48 49 // List of claim info to store in checkpoint 50 type ClaimInfoStateList []ClaimInfoState 51 52 // List of claim info to store in checkpoint 53 // TODO: remove in Beta 54 type ClaimInfoStateListWithoutResourceHandles []ClaimInfoStateWithoutResourceHandles 55 56 // NewDRAManagerCheckpoint returns an instance of Checkpoint 57 func NewDRAManagerCheckpoint() *DRAManagerCheckpoint { 58 return &DRAManagerCheckpoint{ 59 Version: checkpointVersion, 60 Entries: ClaimInfoStateList{}, 61 } 62 } 63 64 // MarshalCheckpoint returns marshalled checkpoint 65 func (dc *DRAManagerCheckpoint) MarshalCheckpoint() ([]byte, error) { 66 // make sure checksum wasn't set before so it doesn't affect output checksum 67 dc.Checksum = 0 68 dc.Checksum = checksum.New(dc) 69 return json.Marshal(*dc) 70 } 71 72 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint 73 func (dc *DRAManagerCheckpoint) UnmarshalCheckpoint(blob []byte) error { 74 return json.Unmarshal(blob, dc) 75 } 76 77 // VerifyChecksum verifies that current checksum of checkpoint is valid 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 // Verify with old structs without ResourceHandles field 84 // TODO: remove in Beta 85 err = verifyChecksumWithoutResourceHandles(dc, ck) 86 } 87 dc.Checksum = ck 88 return err 89 } 90 91 // verifyChecksumWithoutResourceHandles is a helper function that verifies checksum of the 92 // checkpoint in the old format, without ResourceHandles field. 93 // TODO: remove in Beta. 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 // Calculate checksum for old checkpoint 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 }