k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/cpumanager/state/checkpoint.go (about) 1 /* 2 Copyright 2018 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 "fmt" 22 "hash/fnv" 23 "strings" 24 25 "k8s.io/apimachinery/pkg/util/dump" 26 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" 27 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" 28 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors" 29 ) 30 31 var _ checkpointmanager.Checkpoint = &CPUManagerCheckpointV1{} 32 var _ checkpointmanager.Checkpoint = &CPUManagerCheckpointV2{} 33 var _ checkpointmanager.Checkpoint = &CPUManagerCheckpoint{} 34 35 // CPUManagerCheckpoint struct is used to store cpu/pod assignments in a checkpoint in v2 format 36 type CPUManagerCheckpoint struct { 37 PolicyName string `json:"policyName"` 38 DefaultCPUSet string `json:"defaultCpuSet"` 39 Entries map[string]map[string]string `json:"entries,omitempty"` 40 Checksum checksum.Checksum `json:"checksum"` 41 } 42 43 // CPUManagerCheckpointV1 struct is used to store cpu/pod assignments in a checkpoint in v1 format 44 type CPUManagerCheckpointV1 struct { 45 PolicyName string `json:"policyName"` 46 DefaultCPUSet string `json:"defaultCpuSet"` 47 Entries map[string]string `json:"entries,omitempty"` 48 Checksum checksum.Checksum `json:"checksum"` 49 } 50 51 // CPUManagerCheckpointV2 struct is used to store cpu/pod assignments in a checkpoint in v2 format 52 type CPUManagerCheckpointV2 = CPUManagerCheckpoint 53 54 // NewCPUManagerCheckpoint returns an instance of Checkpoint 55 func NewCPUManagerCheckpoint() *CPUManagerCheckpoint { 56 //nolint:staticcheck // unexported-type-in-api user-facing error message 57 return newCPUManagerCheckpointV2() 58 } 59 60 func newCPUManagerCheckpointV1() *CPUManagerCheckpointV1 { 61 return &CPUManagerCheckpointV1{ 62 Entries: make(map[string]string), 63 } 64 } 65 66 func newCPUManagerCheckpointV2() *CPUManagerCheckpointV2 { 67 return &CPUManagerCheckpointV2{ 68 Entries: make(map[string]map[string]string), 69 } 70 } 71 72 // MarshalCheckpoint returns marshalled checkpoint in v1 format 73 func (cp *CPUManagerCheckpointV1) MarshalCheckpoint() ([]byte, error) { 74 // make sure checksum wasn't set before so it doesn't affect output checksum 75 cp.Checksum = 0 76 cp.Checksum = checksum.New(cp) 77 return json.Marshal(*cp) 78 } 79 80 // MarshalCheckpoint returns marshalled checkpoint in v2 format 81 func (cp *CPUManagerCheckpointV2) MarshalCheckpoint() ([]byte, error) { 82 // make sure checksum wasn't set before so it doesn't affect output checksum 83 cp.Checksum = 0 84 cp.Checksum = checksum.New(cp) 85 return json.Marshal(*cp) 86 } 87 88 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint in v1 format 89 func (cp *CPUManagerCheckpointV1) UnmarshalCheckpoint(blob []byte) error { 90 return json.Unmarshal(blob, cp) 91 } 92 93 // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint in v2 format 94 func (cp *CPUManagerCheckpointV2) UnmarshalCheckpoint(blob []byte) error { 95 return json.Unmarshal(blob, cp) 96 } 97 98 // VerifyChecksum verifies that current checksum of checkpoint is valid in v1 format 99 func (cp *CPUManagerCheckpointV1) VerifyChecksum() error { 100 if cp.Checksum == 0 { 101 // accept empty checksum for compatibility with old file backend 102 return nil 103 } 104 105 ck := cp.Checksum 106 cp.Checksum = 0 107 object := dump.ForHash(cp) 108 object = strings.Replace(object, "CPUManagerCheckpointV1", "CPUManagerCheckpoint", 1) 109 cp.Checksum = ck 110 111 hash := fnv.New32a() 112 fmt.Fprintf(hash, "%v", object) 113 if cp.Checksum != checksum.Checksum(hash.Sum32()) { 114 return errors.ErrCorruptCheckpoint 115 } 116 117 return nil 118 } 119 120 // VerifyChecksum verifies that current checksum of checkpoint is valid in v2 format 121 func (cp *CPUManagerCheckpointV2) VerifyChecksum() error { 122 if cp.Checksum == 0 { 123 // accept empty checksum for compatibility with old file backend 124 return nil 125 } 126 ck := cp.Checksum 127 cp.Checksum = 0 128 err := ck.Verify(cp) 129 cp.Checksum = ck 130 return err 131 }