k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/memorymanager/state/checkpoint.go (about) 1 /* 2 Copyright 2020 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 22 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" 23 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" 24 ) 25 26 var _ checkpointmanager.Checkpoint = &MemoryManagerCheckpoint{} 27 28 // MemoryManagerCheckpoint struct is used to store memory/pod assignments in a checkpoint 29 type MemoryManagerCheckpoint struct { 30 PolicyName string `json:"policyName"` 31 MachineState NUMANodeMap `json:"machineState"` 32 Entries ContainerMemoryAssignments `json:"entries,omitempty"` 33 Checksum checksum.Checksum `json:"checksum"` 34 } 35 36 // NewMemoryManagerCheckpoint returns an instance of Checkpoint 37 func NewMemoryManagerCheckpoint() *MemoryManagerCheckpoint { 38 //nolint:staticcheck // unexported-type-in-api user-facing error message 39 return &MemoryManagerCheckpoint{ 40 Entries: ContainerMemoryAssignments{}, 41 MachineState: NUMANodeMap{}, 42 } 43 } 44 45 // MarshalCheckpoint returns marshalled checkpoint 46 func (mp *MemoryManagerCheckpoint) MarshalCheckpoint() ([]byte, error) { 47 // make sure checksum wasn't set before so it doesn't affect output checksum 48 mp.Checksum = 0 49 mp.Checksum = checksum.New(mp) 50 return json.Marshal(*mp) 51 } 52 53 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint 54 func (mp *MemoryManagerCheckpoint) UnmarshalCheckpoint(blob []byte) error { 55 return json.Unmarshal(blob, mp) 56 } 57 58 // VerifyChecksum verifies that current checksum of checkpoint is valid 59 func (mp *MemoryManagerCheckpoint) VerifyChecksum() error { 60 ck := mp.Checksum 61 mp.Checksum = 0 62 err := ck.Verify(mp) 63 mp.Checksum = ck 64 return err 65 }