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