github.com/kubewharf/katalyst-core@v0.5.3/pkg/agent/sysadvisor/metacache/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 metacache 18 19 import ( 20 "encoding/json" 21 22 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" 23 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" 24 25 "github.com/kubewharf/katalyst-core/pkg/agent/sysadvisor/types" 26 ) 27 28 var _ checkpointmanager.Checkpoint = &MetaCacheCheckpoint{} 29 30 type MetaCacheCheckpoint struct { 31 PodEntries types.PodEntries `json:"pod_entries"` 32 PoolEntries types.PoolEntries `json:"pool_entries"` 33 RegionEntries types.RegionEntries `json:"region_entries"` 34 Checksum checksum.Checksum `json:"checksum"` 35 } 36 37 func NewMetaCacheCheckpoint() *MetaCacheCheckpoint { 38 return &MetaCacheCheckpoint{ 39 PodEntries: make(types.PodEntries), 40 PoolEntries: make(types.PoolEntries), 41 RegionEntries: make(types.RegionEntries), 42 } 43 } 44 45 // MarshalCheckpoint returns marshaled checkpoint 46 func (cp *MetaCacheCheckpoint) MarshalCheckpoint() ([]byte, error) { 47 // make sure checksum wasn't set before so it doesn't affect output checksum 48 cp.Checksum = 0 49 cp.Checksum = checksum.New(cp) 50 return json.Marshal(*cp) 51 } 52 53 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint 54 func (cp *MetaCacheCheckpoint) UnmarshalCheckpoint(blob []byte) error { 55 return json.Unmarshal(blob, cp) 56 } 57 58 // VerifyChecksum verifies that current checksum of checkpoint is valid 59 func (cp *MetaCacheCheckpoint) VerifyChecksum() error { 60 ck := cp.Checksum 61 cp.Checksum = 0 62 err := ck.Verify(cp) 63 cp.Checksum = ck 64 return err 65 }