checkpoint.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2017 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 checkpoint
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "k8s.io/klog"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/kubernetes/pkg/apis/core"
  20. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  21. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
  22. )
  23. const (
  24. // Delimiter used on checkpoints written to disk
  25. delimiter = "_"
  26. podPrefix = "Pod"
  27. )
  28. // PodCheckpoint defines the operations to retrieve pod
  29. type PodCheckpoint interface {
  30. checkpointmanager.Checkpoint
  31. GetPod() *v1.Pod
  32. }
  33. // Data to be stored as checkpoint
  34. type Data struct {
  35. Pod *v1.Pod
  36. Checksum checksum.Checksum
  37. }
  38. // NewPodCheckpoint returns new pod checkpoint
  39. func NewPodCheckpoint(pod *v1.Pod) PodCheckpoint {
  40. return &Data{Pod: pod}
  41. }
  42. // MarshalCheckpoint returns marshalled data
  43. func (cp *Data) MarshalCheckpoint() ([]byte, error) {
  44. cp.Checksum = checksum.New(*cp.Pod)
  45. return json.Marshal(*cp)
  46. }
  47. // UnmarshalCheckpoint returns unmarshalled data
  48. func (cp *Data) UnmarshalCheckpoint(blob []byte) error {
  49. return json.Unmarshal(blob, cp)
  50. }
  51. // VerifyChecksum verifies that passed checksum is same as calculated checksum
  52. func (cp *Data) VerifyChecksum() error {
  53. return cp.Checksum.Verify(*cp.Pod)
  54. }
  55. // GetPod retrieves the pod from the checkpoint
  56. func (cp *Data) GetPod() *v1.Pod {
  57. return cp.Pod
  58. }
  59. // checkAnnotations will validate the checkpoint annotations exist on the Pod
  60. func checkAnnotations(pod *v1.Pod) bool {
  61. if podAnnotations := pod.GetAnnotations(); podAnnotations != nil {
  62. if podAnnotations[core.BootstrapCheckpointAnnotationKey] == "true" {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. //getPodKey returns the full qualified path for the pod checkpoint
  69. func getPodKey(pod *v1.Pod) string {
  70. return fmt.Sprintf("%s%s%v.yaml", podPrefix, delimiter, pod.GetUID())
  71. }
  72. // LoadPods Loads All Checkpoints from disk
  73. func LoadPods(cpm checkpointmanager.CheckpointManager) ([]*v1.Pod, error) {
  74. pods := make([]*v1.Pod, 0)
  75. checkpointKeys, err := cpm.ListCheckpoints()
  76. if err != nil {
  77. klog.Errorf("Failed to list checkpoints: %v", err)
  78. }
  79. for _, key := range checkpointKeys {
  80. checkpoint := NewPodCheckpoint(nil)
  81. err := cpm.GetCheckpoint(key, checkpoint)
  82. if err != nil {
  83. klog.Errorf("Failed to retrieve checkpoint for pod %q: %v", key, err)
  84. continue
  85. }
  86. pods = append(pods, checkpoint.GetPod())
  87. }
  88. return pods, nil
  89. }
  90. // WritePod a checkpoint to a file on disk if annotation is present
  91. func WritePod(cpm checkpointmanager.CheckpointManager, pod *v1.Pod) error {
  92. var err error
  93. if checkAnnotations(pod) {
  94. data := NewPodCheckpoint(pod)
  95. err = cpm.CreateCheckpoint(getPodKey(pod), data)
  96. } else {
  97. // This is to handle an edge where a pod update could remove
  98. // an annotation and the checkpoint should then be removed.
  99. err = cpm.RemoveCheckpoint(getPodKey(pod))
  100. }
  101. return err
  102. }
  103. // DeletePod deletes a checkpoint from disk if present
  104. func DeletePod(cpm checkpointmanager.CheckpointManager, pod *v1.Pod) error {
  105. return cpm.RemoveCheckpoint(getPodKey(pod))
  106. }