checkpoint.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package state
  14. import (
  15. "encoding/json"
  16. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  17. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
  18. )
  19. var _ checkpointmanager.Checkpoint = &CPUManagerCheckpoint{}
  20. // CPUManagerCheckpoint struct is used to store cpu/pod assignments in a checkpoint
  21. type CPUManagerCheckpoint struct {
  22. PolicyName string `json:"policyName"`
  23. DefaultCPUSet string `json:"defaultCpuSet"`
  24. Entries map[string]string `json:"entries,omitempty"`
  25. Checksum checksum.Checksum `json:"checksum"`
  26. }
  27. // NewCPUManagerCheckpoint returns an instance of Checkpoint
  28. func NewCPUManagerCheckpoint() *CPUManagerCheckpoint {
  29. return &CPUManagerCheckpoint{
  30. Entries: make(map[string]string),
  31. }
  32. }
  33. // MarshalCheckpoint returns marshalled checkpoint
  34. func (cp *CPUManagerCheckpoint) MarshalCheckpoint() ([]byte, error) {
  35. // make sure checksum wasn't set before so it doesn't affect output checksum
  36. cp.Checksum = 0
  37. cp.Checksum = checksum.New(cp)
  38. return json.Marshal(*cp)
  39. }
  40. // UnmarshalCheckpoint tries to unmarshal passed bytes to checkpoint
  41. func (cp *CPUManagerCheckpoint) UnmarshalCheckpoint(blob []byte) error {
  42. return json.Unmarshal(blob, cp)
  43. }
  44. // VerifyChecksum verifies that current checksum of checkpoint is valid
  45. func (cp *CPUManagerCheckpoint) VerifyChecksum() error {
  46. if cp.Checksum == 0 {
  47. // accept empty checksum for compatibility with old file backend
  48. return nil
  49. }
  50. ck := cp.Checksum
  51. cp.Checksum = 0
  52. err := ck.Verify(cp)
  53. cp.Checksum = ck
  54. return err
  55. }