-
Notifications
You must be signed in to change notification settings - Fork 245
/
checkpoint-util.go
243 lines (212 loc) · 9.4 KB
/
checkpoint-util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Copyright 2023 The CDI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
)
const (
// ImportPaused provides a const to indicate that a multistage import is waiting for the next stage
ImportPaused = "ImportPaused"
// MessageImportPaused provides a const for a "multistage import paused" message
MessageImportPaused = "Multistage import into PVC %s is paused"
)
// CheckpointRecord is set after comparing the list of checkpoints in the DataVolume/VolumeImportSource
// spec with the annotations on the PVC indicating which checkpoints have already been copied.
// Return the first checkpoint that does not have this annotation, meaning the first checkpoint that has not yet been copied.
type CheckpointRecord struct {
cdiv1.DataVolumeCheckpoint
IsFinal bool
}
// CheckpointArgs is a struct used to store all checkpoint-related arguments to simplify passing them.
type CheckpointArgs struct {
Client client.Client
Log logr.Logger
// Checkpoints is a list of DataVolumeCheckpoints, representing stages in a multistage import.
Checkpoints []cdiv1.DataVolumeCheckpoint
// IsFinal indicates whether the current DataVolumeCheckpoint is the final checkpoint.
IsFinal bool
}
// UpdatesMultistageImportSucceeded handles multi-stage annotations when the importer pod is succeeded
func UpdatesMultistageImportSucceeded(pvc *corev1.PersistentVolumeClaim, args *CheckpointArgs) error {
if multiStageImport := metav1.HasAnnotation(pvc.ObjectMeta, AnnCurrentCheckpoint); !multiStageImport {
return nil
}
// The presence of the current checkpoint annotation indicates it is a stage in a multistage import.
// If all the checkpoints have been copied, then we need to remove the annotations from the PVC.
// Otherwise, we need to change the annotations to advance to the next checkpoint.
currentCheckpoint := pvc.Annotations[AnnCurrentCheckpoint]
alreadyCopied := checkpointAlreadyCopied(pvc, currentCheckpoint)
finalCheckpoint, _ := strconv.ParseBool(pvc.Annotations[AnnFinalCheckpoint])
if finalCheckpoint && alreadyCopied {
// Last checkpoint done, so clean up
if err := deleteMultistageImportAnnotations(pvc, args); err != nil {
return err
}
} else {
// Advances annotations to next checkpoint
if err := setPvcMultistageImportAnnotations(pvc, args); err != nil {
return err
}
}
return nil
}
// MaybeSetPvcMultiStageAnnotation sets the annotation if pvc needs it, and does not have it yet
func MaybeSetPvcMultiStageAnnotation(pvc *corev1.PersistentVolumeClaim, args *CheckpointArgs) error {
if pvc.Status.Phase == corev1.ClaimBound {
// If a PVC already exists with no multi-stage annotations, check if it
// needs them set (if not already finished with an import).
multiStageImport := (len(args.Checkpoints) > 0)
multiStageAnnotationsSet := metav1.HasAnnotation(pvc.ObjectMeta, AnnCurrentCheckpoint)
multiStageAlreadyDone := metav1.HasAnnotation(pvc.ObjectMeta, AnnMultiStageImportDone)
if multiStageImport && !multiStageAnnotationsSet && !multiStageAlreadyDone {
err := setPvcMultistageImportAnnotations(pvc, args)
if err != nil {
return err
}
}
}
return nil
}
// Set the PVC annotations related to multi-stage imports so that they point to the next checkpoint to copy.
func setPvcMultistageImportAnnotations(pvc *corev1.PersistentVolumeClaim, args *CheckpointArgs) error {
pvcCopy := pvc.DeepCopy()
// Only mark this checkpoint complete if it was completed by the current pod.
// This keeps us from skipping over checkpoints when a reconcile fails at a bad time.
uuidAlreadyUsed := false
for key, value := range pvcCopy.Annotations {
if strings.HasPrefix(key, getCheckpointCopiedKey("")) { // Blank checkpoint name to get just the prefix
if value == pvcCopy.Annotations[AnnCurrentPodID] {
uuidAlreadyUsed = true
break
}
}
}
if !uuidAlreadyUsed {
// Mark checkpoint complete by saving UID of current pod to a
// PVC annotation specific to this checkpoint.
currentCheckpoint := pvcCopy.Annotations[AnnCurrentCheckpoint]
if currentCheckpoint != "" {
currentPodID := pvcCopy.Annotations[AnnCurrentPodID]
annotation := getCheckpointCopiedKey(currentCheckpoint)
pvcCopy.ObjectMeta.Annotations[annotation] = currentPodID
args.Log.V(1).Info("UUID not already used, marking checkpoint completed by current pod ID.", "checkpoint", currentCheckpoint, "podId", currentPodID)
} else {
args.Log.Info("Cannot mark empty checkpoint complete. Check spec for empty checkpoints.")
}
}
// else: If the UID was already used for another transfer, then we are
// just waiting for a new pod to start up to transfer the next checkpoint.
// Set multi-stage PVC annotations so further reconcile loops will create new pods as needed.
checkpoint := GetNextCheckpoint(pvcCopy, args)
if checkpoint != nil { // Only move to the next checkpoint if there is a next checkpoint to move to
pvcCopy.ObjectMeta.Annotations[AnnCurrentCheckpoint] = checkpoint.Current
pvcCopy.ObjectMeta.Annotations[AnnPreviousCheckpoint] = checkpoint.Previous
pvcCopy.ObjectMeta.Annotations[AnnFinalCheckpoint] = strconv.FormatBool(checkpoint.IsFinal)
// Check to see if there is a running pod for this PVC. If there are
// more checkpoints to copy but the PVC is stopped in Succeeded,
// reset the phase to get another pod started for the next checkpoint.
podNamespace := pvc.Namespace
phase := pvcCopy.ObjectMeta.Annotations[AnnPodPhase]
pod, _ := GetPodFromPvc(args.Client, podNamespace, pvcCopy)
if pod == nil && phase == string(corev1.PodSucceeded) {
// Reset PVC phase so importer will create a new pod
pvcCopy.ObjectMeta.Annotations[AnnPodPhase] = string(corev1.PodUnknown)
delete(pvcCopy.ObjectMeta.Annotations, AnnImportPod)
}
// else: There's a pod already running, no need to try to start a new one.
}
// else: There aren't any checkpoints ready to be copied over.
// only update if something has changed
if !reflect.DeepEqual(pvc, pvcCopy) {
return args.Client.Update(context.TODO(), pvcCopy)
}
return nil
}
// Clean up PVC annotations after a multi-stage import.
func deleteMultistageImportAnnotations(pvc *corev1.PersistentVolumeClaim, args *CheckpointArgs) error {
pvcCopy := pvc.DeepCopy()
delete(pvcCopy.Annotations, AnnCurrentCheckpoint)
delete(pvcCopy.Annotations, AnnPreviousCheckpoint)
delete(pvcCopy.Annotations, AnnFinalCheckpoint)
delete(pvcCopy.Annotations, AnnCurrentPodID)
prefix := getCheckpointCopiedKey("")
for key := range pvcCopy.Annotations {
if strings.HasPrefix(key, prefix) {
delete(pvcCopy.Annotations, key)
}
}
pvcCopy.ObjectMeta.Annotations[AnnMultiStageImportDone] = "true"
// only update if something has changed
if !reflect.DeepEqual(pvc, pvcCopy) {
return args.Client.Update(context.TODO(), pvcCopy)
}
return nil
}
// Single place to hold the scheme for annotations that indicate a checkpoint
// has already been copied. Currently storage.checkpoint.copied.[checkpoint] = ID,
// where ID is the UID of the pod that successfully transferred that checkpoint.
func getCheckpointCopiedKey(checkpoint string) string {
return AnnCheckpointsCopied + "." + checkpoint
}
// Find out if this checkpoint has already been copied by looking for an annotation
// like storage.checkpoint.copied.[checkpoint]. If it exists, then this checkpoint
// was already copied.
func checkpointAlreadyCopied(pvc *corev1.PersistentVolumeClaim, checkpoint string) bool {
annotation := getCheckpointCopiedKey(checkpoint)
return metav1.HasAnnotation(pvc.ObjectMeta, annotation)
}
// GetNextCheckpoint returns the appropriate checkpoint according to multistage annotations
func GetNextCheckpoint(pvc *corev1.PersistentVolumeClaim, args *CheckpointArgs) *CheckpointRecord {
numCheckpoints := len(args.Checkpoints)
if numCheckpoints < 1 {
return nil
}
// If there are no annotations, get the first checkpoint from the spec
if pvc.ObjectMeta.Annotations[AnnCurrentCheckpoint] == "" {
checkpoint := &CheckpointRecord{
cdiv1.DataVolumeCheckpoint{
Current: args.Checkpoints[0].Current,
Previous: args.Checkpoints[0].Previous,
},
(numCheckpoints == 1) && args.IsFinal,
}
return checkpoint
}
// If there are annotations, keep checking the spec checkpoint list for an existing "copied.X" annotation until the first one not found
for count, specCheckpoint := range args.Checkpoints {
if specCheckpoint.Current == "" {
args.Log.Info(fmt.Sprintf("DataVolume spec has a blank 'current' entry in checkpoint %d", count))
continue
}
if !checkpointAlreadyCopied(pvc, specCheckpoint.Current) {
checkpoint := &CheckpointRecord{
cdiv1.DataVolumeCheckpoint{
Current: specCheckpoint.Current,
Previous: specCheckpoint.Previous,
},
(numCheckpoints == (count + 1)) && args.IsFinal,
}
return checkpoint
}
}
return nil
}