|
| 1 | +/* |
| 2 | +Copyright 2017 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 checkpoint |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io/ioutil" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/dchest/safefile" |
| 28 | + "github.com/ghodss/yaml" |
| 29 | + "github.com/golang/glog" |
| 30 | + |
| 31 | + "k8s.io/api/core/v1" |
| 32 | + "k8s.io/kubernetes/pkg/apis/core" |
| 33 | + "k8s.io/kubernetes/pkg/volume/util" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + // Delimiter used on checkpoints written to disk |
| 38 | + delimiter = "_" |
| 39 | + podPrefix = "Pod" |
| 40 | +) |
| 41 | + |
| 42 | +// Manager is the interface used to manage checkpoints |
| 43 | +// which involves writing resources to disk to recover |
| 44 | +// during restart or failure scenarios. |
| 45 | +// https://github.com/kubernetes/community/pull/1241/files |
| 46 | +type Manager interface { |
| 47 | + // LoadPods will load checkpointed Pods from disk |
| 48 | + LoadPods() ([]*v1.Pod, error) |
| 49 | + |
| 50 | + // WritePod will serialize a Pod to disk |
| 51 | + WritePod(pod *v1.Pod) error |
| 52 | + |
| 53 | + // Deletes the checkpoint of the given pod from disk |
| 54 | + DeletePod(pod *v1.Pod) error |
| 55 | +} |
| 56 | + |
| 57 | +var instance Manager |
| 58 | +var mutex = &sync.Mutex{} |
| 59 | + |
| 60 | +// fileCheckPointManager - is a checkpointer that writes contents to disk |
| 61 | +// The type information of the resource objects are encoded in the name |
| 62 | +type fileCheckPointManager struct { |
| 63 | + path string |
| 64 | +} |
| 65 | + |
| 66 | +// NewCheckpointManager will create a Manager that points to the following path |
| 67 | +func NewCheckpointManager(path string) Manager { |
| 68 | + // NOTE: This is a precaution; current implementation should not run |
| 69 | + // multiple checkpoint managers. |
| 70 | + mutex.Lock() |
| 71 | + defer mutex.Unlock() |
| 72 | + instance = &fileCheckPointManager{path: path} |
| 73 | + return instance |
| 74 | +} |
| 75 | + |
| 76 | +// GetInstance will return the current Manager, there should be only one. |
| 77 | +func GetInstance() Manager { |
| 78 | + mutex.Lock() |
| 79 | + defer mutex.Unlock() |
| 80 | + return instance |
| 81 | +} |
| 82 | + |
| 83 | +// loadPod will load Pod Checkpoint yaml file. |
| 84 | +func (fcp *fileCheckPointManager) loadPod(file string) (*v1.Pod, error) { |
| 85 | + return util.LoadPodFromFile(file) |
| 86 | +} |
| 87 | + |
| 88 | +// checkAnnotations will validate the checkpoint annotations exist on the Pod |
| 89 | +func (fcp *fileCheckPointManager) checkAnnotations(pod *v1.Pod) bool { |
| 90 | + if podAnnotations := pod.GetAnnotations(); podAnnotations != nil { |
| 91 | + if podAnnotations[core.BootstrapCheckpointAnnotationKey] == "true" { |
| 92 | + return true |
| 93 | + } |
| 94 | + } |
| 95 | + return false |
| 96 | +} |
| 97 | + |
| 98 | +// getPodPath returns the full qualified path for the pod checkpoint |
| 99 | +func (fcp *fileCheckPointManager) getPodPath(pod *v1.Pod) string { |
| 100 | + return fmt.Sprintf("%v/Pod%v%v.yaml", fcp.path, delimiter, pod.GetUID()) |
| 101 | +} |
| 102 | + |
| 103 | +// LoadPods Loads All Checkpoints from disk |
| 104 | +func (fcp *fileCheckPointManager) LoadPods() ([]*v1.Pod, error) { |
| 105 | + checkpoints := make([]*v1.Pod, 0) |
| 106 | + files, err := ioutil.ReadDir(fcp.path) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + for _, f := range files { |
| 111 | + // get just the filename |
| 112 | + _, fname := filepath.Split(f.Name()) |
| 113 | + // Get just the Resource from "Resource_Name" |
| 114 | + fnfields := strings.Split(fname, delimiter) |
| 115 | + switch fnfields[0] { |
| 116 | + case podPrefix: |
| 117 | + pod, err := fcp.loadPod(fmt.Sprintf("%s/%s", fcp.path, f.Name())) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + checkpoints = append(checkpoints, pod) |
| 122 | + default: |
| 123 | + glog.Warningf("Unsupported checkpoint file detected %v", f) |
| 124 | + } |
| 125 | + } |
| 126 | + return checkpoints, nil |
| 127 | +} |
| 128 | + |
| 129 | +// Writes a checkpoint to a file on disk if annotation is present |
| 130 | +func (fcp *fileCheckPointManager) WritePod(pod *v1.Pod) error { |
| 131 | + var err error |
| 132 | + if fcp.checkAnnotations(pod) { |
| 133 | + if blob, err := yaml.Marshal(pod); err == nil { |
| 134 | + err = safefile.WriteFile(fcp.getPodPath(pod), blob, 0644) |
| 135 | + } |
| 136 | + } else { |
| 137 | + // This is to handle an edge where a pod update could remove |
| 138 | + // an annotation and the checkpoint should then be removed. |
| 139 | + err = fcp.DeletePod(pod) |
| 140 | + } |
| 141 | + return err |
| 142 | +} |
| 143 | + |
| 144 | +// Deletes a checkpoint from disk if present |
| 145 | +func (fcp *fileCheckPointManager) DeletePod(pod *v1.Pod) error { |
| 146 | + podPath := fcp.getPodPath(pod) |
| 147 | + if err := os.Remove(podPath); !os.IsNotExist(err) { |
| 148 | + return err |
| 149 | + } |
| 150 | + return nil |
| 151 | +} |
0 commit comments