checkpoint.go 4.4 KB

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