github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/orm/checkpoint/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 checkpoint 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 // ResourceManagerCheckpoint defines the operations to retrieve pod resources 27 type ResourceManagerCheckpoint interface { 28 checkpointmanager.Checkpoint 29 GetData() []PodResourcesEntry 30 } 31 32 // PodResourcesEntry connects pod information to resources 33 type PodResourcesEntry struct { 34 PodUID string 35 ContainerName string 36 ResourceName string 37 AllocationInfo string 38 } 39 40 // checkpointData struct is used to store pod to resource allocation information 41 // in a checkpoint file. 42 // TODO: add version control when we need to change checkpoint format. 43 type checkpointData struct { 44 PodResourceEntries []PodResourcesEntry 45 } 46 47 // Data holds checkpoint data and its checksum 48 type Data struct { 49 Data checkpointData 50 Checksum checksum.Checksum 51 } 52 53 // New returns an instance of Checkpoint 54 func New(resEntries []PodResourcesEntry) ResourceManagerCheckpoint { 55 return &Data{ 56 Data: checkpointData{ 57 PodResourceEntries: resEntries, 58 }, 59 } 60 } 61 62 // MarshalCheckpoint returns marshaled data 63 func (cp *Data) MarshalCheckpoint() ([]byte, error) { 64 cp.Checksum = checksum.New(cp.Data) 65 return json.Marshal(*cp) 66 } 67 68 // UnmarshalCheckpoint returns unmarshalled data 69 func (cp *Data) UnmarshalCheckpoint(blob []byte) error { 70 return json.Unmarshal(blob, cp) 71 } 72 73 // VerifyChecksum verifies that passed checksum is same as calculated checksum 74 func (cp *Data) VerifyChecksum() error { 75 return cp.Checksum.Verify(cp.Data) 76 } 77 78 // GetData returns resource entries and registered resources 79 func (cp *Data) GetData() []PodResourcesEntry { 80 return cp.Data.PodResourceEntries 81 }