Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 5f23e44

Browse files
authored
Merge pull request #443 from aaronlevy/pr-387
Ensure labels are preserved
2 parents 616e523 + 067a220 commit 5f23e44

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

cmd/checkpoint/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,26 @@ func isPodCheckpointer(pod *v1.Pod) bool {
404404
}
405405

406406
func sanitizeCheckpointPod(cp *v1.Pod) (*v1.Pod, error) {
407-
// Clear ObjectMeta except for name/namespace
408-
// NOTE(aaron): If we want to keep labels, we need to add a new label so the static pod
409-
// will not be adopted by higher-level parent (e.g. daemonset/deployment).
410-
// Otherwise you end up in situations where parent tries deleting mirror pods.
407+
trueVar := true
408+
409+
// Keep same name, namespace, and labels as parent.
411410
cp.ObjectMeta = metav1.ObjectMeta{
412411
Name: cp.Name,
413412
Namespace: cp.Namespace,
414413
Annotations: make(map[string]string),
414+
Labels: cp.Labels,
415+
// Set the ownerRef to the parent pod. We do this because:
416+
// If the ownerRef stays the same (e.g. the original deployment), then the deployment controller will try to manage the static/mirror pod.
417+
// If we clear the ownerRef, then a higher-level object will adopt this pod based on the label selector (e.g. the original deployment).
418+
OwnerReferences: []metav1.OwnerReference{
419+
{
420+
APIVersion: cp.APIVersion,
421+
Kind: cp.Kind,
422+
Name: cp.Name,
423+
UID: cp.UID,
424+
Controller: &trueVar,
425+
},
426+
},
415427
}
416428

417429
// Track this checkpoint's parent pod

cmd/checkpoint/main_test.go

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,11 @@ func TestSanitizeCheckpointPod(t *testing.T) {
286286
pod *v1.Pod
287287
expected *v1.Pod
288288
}
289+
trueVar := true
289290

290291
cases := []testCase{
291292
{
292-
desc: "Pod name and namespace are preserved & checkpoint annotation added",
293+
desc: "Pod name and namespace are preserved, checkpoint annotation added, owner points to parent",
293294
pod: &v1.Pod{
294295
ObjectMeta: metav1.ObjectMeta{
295296
Name: "podname",
@@ -298,14 +299,15 @@ func TestSanitizeCheckpointPod(t *testing.T) {
298299
},
299300
expected: &v1.Pod{
300301
ObjectMeta: metav1.ObjectMeta{
301-
Name: "podname",
302-
Namespace: "podnamespace",
303-
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
302+
Name: "podname",
303+
Namespace: "podnamespace",
304+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
305+
OwnerReferences: []metav1.OwnerReference{{Name: "podname", Controller: &trueVar}},
304306
},
305307
},
306308
},
307309
{
308-
desc: "Existing annotations are removed, checkpoint annotation added",
310+
desc: "Existing annotations are removed, checkpoint annotation added, owner points to parent",
309311
pod: &v1.Pod{
310312
ObjectMeta: metav1.ObjectMeta{
311313
Name: "podname",
@@ -315,9 +317,10 @@ func TestSanitizeCheckpointPod(t *testing.T) {
315317
},
316318
expected: &v1.Pod{
317319
ObjectMeta: metav1.ObjectMeta{
318-
Name: "podname",
319-
Namespace: "podnamespace",
320-
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
320+
Name: "podname",
321+
Namespace: "podnamespace",
322+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
323+
OwnerReferences: []metav1.OwnerReference{{Name: "podname", Controller: &trueVar}},
321324
},
322325
},
323326
},
@@ -332,9 +335,10 @@ func TestSanitizeCheckpointPod(t *testing.T) {
332335
},
333336
expected: &v1.Pod{
334337
ObjectMeta: metav1.ObjectMeta{
335-
Name: "podname",
336-
Namespace: "podnamespace",
337-
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
338+
Name: "podname",
339+
Namespace: "podnamespace",
340+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
341+
OwnerReferences: []metav1.OwnerReference{{Name: "podname", Controller: &trueVar}},
338342
},
339343
},
340344
},
@@ -348,10 +352,61 @@ func TestSanitizeCheckpointPod(t *testing.T) {
348352
Spec: v1.PodSpec{ServiceAccountName: "foo", DeprecatedServiceAccount: "bar"},
349353
},
350354
expected: &v1.Pod{
355+
ObjectMeta: metav1.ObjectMeta{
356+
Name: "podname",
357+
Namespace: "podnamespace",
358+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
359+
OwnerReferences: []metav1.OwnerReference{{Name: "podname", Controller: &trueVar}},
360+
},
361+
},
362+
},
363+
{
364+
desc: "Labels are preserved",
365+
pod: &v1.Pod{
366+
ObjectMeta: metav1.ObjectMeta{
367+
Name: "podname",
368+
Namespace: "podnamespace",
369+
Labels: map[string]string{"foo": "bar"},
370+
},
371+
},
372+
expected: &v1.Pod{
373+
ObjectMeta: metav1.ObjectMeta{
374+
Name: "podname",
375+
Namespace: "podnamespace",
376+
Labels: map[string]string{"foo": "bar"},
377+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
378+
OwnerReferences: []metav1.OwnerReference{{Name: "podname", Controller: &trueVar}},
379+
},
380+
},
381+
},
382+
{
383+
desc: "OwnerReference of checkpoint points to parent pod",
384+
pod: &v1.Pod{
385+
TypeMeta: metav1.TypeMeta{
386+
APIVersion: "v1",
387+
Kind: "Pod",
388+
},
389+
ObjectMeta: metav1.ObjectMeta{
390+
Name: "podname",
391+
Namespace: "podnamespace",
392+
UID: "pod-uid",
393+
OwnerReferences: []metav1.OwnerReference{
394+
{APIVersion: "v1", Kind: "Daemonset", Name: "daemonname", UID: "daemon-uid"},
395+
},
396+
},
397+
},
398+
expected: &v1.Pod{
399+
TypeMeta: metav1.TypeMeta{
400+
APIVersion: "v1",
401+
Kind: "Pod",
402+
},
351403
ObjectMeta: metav1.ObjectMeta{
352404
Name: "podname",
353405
Namespace: "podnamespace",
354406
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
407+
OwnerReferences: []metav1.OwnerReference{
408+
{APIVersion: "v1", Kind: "Pod", Name: "podname", UID: "pod-uid", Controller: &trueVar},
409+
},
355410
},
356411
},
357412
},
@@ -360,12 +415,13 @@ func TestSanitizeCheckpointPod(t *testing.T) {
360415
for _, tc := range cases {
361416
got, err := sanitizeCheckpointPod(tc.pod)
362417
if err != nil {
363-
t.Errorf("Unexpected error: %v", err)
418+
t.Errorf("\nUnexpected error: %v\n", err)
364419
}
365420
if !api.Semantic.DeepEqual(tc.expected, got) {
366-
t.Errorf("For Test: %s\n\nExpected:\n%+v\nGot:\n%+v\n", tc.desc, tc.expected, got)
421+
t.Errorf("\nFor Test: %s\n\nExpected:\n%#v\nGot:\n%#v\n", tc.desc, tc.expected, got)
367422
}
368423
}
424+
369425
}
370426

371427
func TestPodListToParentPods(t *testing.T) {
@@ -603,7 +659,7 @@ func TestCopyPod(t *testing.T) {
603659
t.Errorf("Unexpected error: %v", err)
604660
}
605661
if !api.Semantic.DeepEqual(pod, *got) {
606-
t.Errorf("Expected:\n%+v\nGot:\n%+v", pod, got)
662+
t.Errorf("Expected:\n%#v\nGot:\n%#v", pod, got)
607663
}
608664
}
609665

0 commit comments

Comments
 (0)