diff --git a/pkg/kubelet/cm/cpumanager/state/BUILD b/pkg/kubelet/cm/cpumanager/state/BUILD index e3d2e77a966..ae3d7681c0e 100644 --- a/pkg/kubelet/cm/cpumanager/state/BUILD +++ b/pkg/kubelet/cm/cpumanager/state/BUILD @@ -13,10 +13,9 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/kubelet/checkpointmanager:go_default_library", + "//pkg/kubelet/checkpointmanager/checksum:go_default_library", "//pkg/kubelet/checkpointmanager/errors:go_default_library", "//pkg/kubelet/cm/cpuset:go_default_library", - "//pkg/kubelet/util/store:go_default_library", - "//pkg/util/hash:go_default_library", "//vendor/github.com/golang/glog:go_default_library", ], ) diff --git a/pkg/kubelet/cm/cpumanager/state/checkpoint.go b/pkg/kubelet/cm/cpumanager/state/checkpoint.go index 11cfec257ab..40e0fc81ad3 100644 --- a/pkg/kubelet/cm/cpumanager/state/checkpoint.go +++ b/pkg/kubelet/cm/cpumanager/state/checkpoint.go @@ -18,55 +18,50 @@ package state import ( "encoding/json" - "hash/fnv" "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" - "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors" - hashutil "k8s.io/kubernetes/pkg/util/hash" + "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" ) var _ checkpointmanager.Checkpoint = &CPUManagerCheckpoint{} // CPUManagerCheckpoint struct is used to store cpu/pod assignments in a checkpoint type CPUManagerCheckpoint struct { - PolicyName string - DefaultCPUSet string - Entries map[string]string - Checksum uint64 + PolicyName string `json:"policyName"` + DefaultCPUSet string `json:"defaultCpuSet"` + Entries map[string]string `json:"entries,omitempty"` + Checksum checksum.Checksum `json:"checksum"` } // NewCPUManagerCheckpoint returns an instance of Checkpoint func NewCPUManagerCheckpoint() *CPUManagerCheckpoint { - return &CPUManagerCheckpoint{Entries: make(map[string]string)} + return &CPUManagerCheckpoint{ + Entries: make(map[string]string), + } } // MarshalCheckpoint returns marshalled checkpoint func (cp *CPUManagerCheckpoint) MarshalCheckpoint() ([]byte, error) { + // make sure checksum wasn't set before so it doesn't affect output checksum + cp.Checksum = 0 + cp.Checksum = checksum.New(cp) return json.Marshal(*cp) } // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint func (cp *CPUManagerCheckpoint) UnmarshalCheckpoint(blob []byte) error { - if err := json.Unmarshal(blob, cp); err != nil { - return err - } - if cp.Checksum != cp.GetChecksum() { - return errors.ErrCorruptCheckpoint - } - return nil + return json.Unmarshal(blob, cp) } -// GetChecksum returns calculated checksum of checkpoint -func (cp *CPUManagerCheckpoint) GetChecksum() uint64 { - orig := cp.Checksum +// VerifyChecksum verifies that current checksum of checkpoint is valid +func (cp *CPUManagerCheckpoint) VerifyChecksum() error { + if cp.Checksum == 0 { + // accept empty checksum for compatibility with old file backend + return nil + } + ck := cp.Checksum cp.Checksum = 0 - hash := fnv.New32a() - hashutil.DeepHashObject(hash, *cp) - cp.Checksum = orig - return uint64(hash.Sum32()) -} - -// UpdateChecksum calculates and updates checksum of the checkpoint -func (cp *CPUManagerCheckpoint) UpdateChecksum() { - cp.Checksum = cp.GetChecksum() + err := ck.Verify(cp) + cp.Checksum = ck + return err } diff --git a/pkg/kubelet/cm/cpumanager/state/state_checkpoint.go b/pkg/kubelet/cm/cpumanager/state/state_checkpoint.go index d660a1ecb05..8f3e5112cae 100644 --- a/pkg/kubelet/cm/cpumanager/state/state_checkpoint.go +++ b/pkg/kubelet/cm/cpumanager/state/state_checkpoint.go @@ -25,7 +25,6 @@ import ( "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/errors" "k8s.io/kubernetes/pkg/kubelet/cm/cpuset" - utilstore "k8s.io/kubernetes/pkg/kubelet/util/store" ) // cpuManagerCheckpointName is the name of checkpoint file diff --git a/pkg/kubelet/cm/cpumanager/state/state_checkpoint_test.go b/pkg/kubelet/cm/cpumanager/state/state_checkpoint_test.go index 9d30d5dca05..199d035c6eb 100644 --- a/pkg/kubelet/cm/cpumanager/state/state_checkpoint_test.go +++ b/pkg/kubelet/cm/cpumanager/state/state_checkpoint_test.go @@ -48,7 +48,7 @@ func TestCheckpointStateRestore(t *testing.T) { "PolicyName": "none", "DefaultCPUSet": "4-6", "Entries": {}, - "Checksum": 861251554 + "Checksum": 2912033808 }`, "none", "", @@ -65,7 +65,7 @@ func TestCheckpointStateRestore(t *testing.T) { "container1": "4-6", "container2": "1-3" }, - "Checksum": 2604807655 + "Checksum": 1535905563 }`, "none", "", @@ -102,7 +102,7 @@ func TestCheckpointStateRestore(t *testing.T) { "PolicyName": "other", "DefaultCPUSet": "1-3", "Entries": {}, - "Checksum": 4266067046 + "Checksum": 4195836012 }`, "none", `configured policy "none" differs from state checkpoint policy "other"`, @@ -114,7 +114,7 @@ func TestCheckpointStateRestore(t *testing.T) { "PolicyName": "none", "DefaultCPUSet": "1.3", "Entries": {}, - "Checksum": 4073769779 + "Checksum": 1025273327 }`, "none", `could not parse default cpu set "1.3": strconv.Atoi: parsing "1.3": invalid syntax`, @@ -129,7 +129,7 @@ func TestCheckpointStateRestore(t *testing.T) { "container1": "4-6", "container2": "asd" }, - "Checksum": 3835486974 + "Checksum": 2764213924 }`, "none", `could not parse cpuset "asd" for container id "container2": strconv.Atoi: parsing "asd": invalid syntax`, diff --git a/pkg/kubelet/cm/cpumanager/state/testing/util.go b/pkg/kubelet/cm/cpumanager/state/testing/util.go index c18c64cc10f..d39e3321a08 100644 --- a/pkg/kubelet/cm/cpumanager/state/testing/util.go +++ b/pkg/kubelet/cm/cpumanager/state/testing/util.go @@ -35,10 +35,7 @@ func (mc *MockCheckpoint) UnmarshalCheckpoint(blob []byte) error { return nil } -// GetChecksum fakes getting checksum -func (mc *MockCheckpoint) GetChecksum() uint64 { - return 0 +// VerifyChecksum fakes verifying checksum +func (mc *MockCheckpoint) VerifyChecksum() error { + return nil } - -// UpdateChecksum fakes updating cheksum -func (mc *MockCheckpoint) UpdateChecksum() {}