github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/qrm-plugins/memory/dynamicpolicy/state/checkpoint.go (about) 1 /* 2 Copyright 2022 The Katalyst 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 = &MemoryPluginCheckpoint{} 27 28 type MemoryPluginCheckpoint struct { 29 PolicyName string `json:"policyName"` 30 MachineState NUMANodeResourcesMap `json:"machineState"` 31 PodResourceEntries PodResourceEntries `json:"pod_resource_entries"` 32 SocketTopology map[int]string `json:"socket_topology,omitempty"` 33 Checksum checksum.Checksum `json:"checksum"` 34 } 35 36 func NewMemoryPluginCheckpoint() *MemoryPluginCheckpoint { 37 return &MemoryPluginCheckpoint{ 38 PodResourceEntries: make(PodResourceEntries), 39 MachineState: make(NUMANodeResourcesMap), 40 SocketTopology: make(map[int]string), 41 } 42 } 43 44 // MarshalCheckpoint returns marshaled checkpoint 45 func (cp *MemoryPluginCheckpoint) MarshalCheckpoint() ([]byte, error) { 46 // make sure checksum wasn't set before, so it doesn't affect output checksum 47 cp.Checksum = 0 48 cp.Checksum = checksum.New(cp) 49 return json.Marshal(*cp) 50 } 51 52 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint 53 func (cp *MemoryPluginCheckpoint) UnmarshalCheckpoint(blob []byte) error { 54 return json.Unmarshal(blob, cp) 55 } 56 57 // VerifyChecksum verifies that current checksum of checkpoint is valid 58 func (cp *MemoryPluginCheckpoint) VerifyChecksum() error { 59 ck := cp.Checksum 60 cp.Checksum = 0 61 err := ck.Verify(cp) 62 cp.Checksum = ck 63 return err 64 }