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

Commit e22cc0e

Browse files
authored
Merge pull request #764 from diegs/uid
checkpointer: two fixes (now that checkpointer tests work again)
2 parents fef22c1 + 346f38e commit e22cc0e

File tree

6 files changed

+67
-28
lines changed

6 files changed

+67
-28
lines changed

pkg/checkpoint/pod.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ var (
2828
)
2929
)
3030

31-
func sanitizeCheckpointPod(cp *v1.Pod) (*v1.Pod, error) {
31+
func sanitizeCheckpointPod(cp *v1.Pod) *v1.Pod {
3232
trueVar := true
3333

34+
// Check if this is already sanitized, i.e. it was read back from a checkpoint on disk.
35+
if _, ok := cp.Annotations[checkpointParentAnnotation]; ok {
36+
return cp
37+
}
38+
3439
// Keep same name, namespace, and labels as parent.
3540
cp.ObjectMeta = metav1.ObjectMeta{
3641
Name: cp.Name,
@@ -73,7 +78,7 @@ func sanitizeCheckpointPod(cp *v1.Pod) (*v1.Pod, error) {
7378
// Clear pod status
7479
cp.Status.Reset()
7580

76-
return cp, nil
81+
return cp
7782
}
7883

7984
// isPodCheckpointer returns true if the manifest is the pod checkpointer (has the same name as the parent).

pkg/checkpoint/pod_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,41 @@ func TestSanitizeCheckpointPod(t *testing.T) {
139139
},
140140
},
141141
},
142+
{
143+
desc: "Pod is already sanitized.",
144+
pod: &v1.Pod{
145+
TypeMeta: metav1.TypeMeta{
146+
APIVersion: "v1",
147+
Kind: "Pod",
148+
},
149+
ObjectMeta: metav1.ObjectMeta{
150+
Name: "podname",
151+
Namespace: "podnamespace",
152+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
153+
OwnerReferences: []metav1.OwnerReference{
154+
{APIVersion: "v1", Kind: "Pod", Name: "podname", UID: "pod-uid", Controller: &trueVar},
155+
},
156+
},
157+
},
158+
expected: &v1.Pod{
159+
TypeMeta: metav1.TypeMeta{
160+
APIVersion: "v1",
161+
Kind: "Pod",
162+
},
163+
ObjectMeta: metav1.ObjectMeta{
164+
Name: "podname",
165+
Namespace: "podnamespace",
166+
Annotations: map[string]string{checkpointParentAnnotation: "podname"},
167+
OwnerReferences: []metav1.OwnerReference{
168+
{APIVersion: "v1", Kind: "Pod", Name: "podname", UID: "pod-uid", Controller: &trueVar},
169+
},
170+
},
171+
},
172+
},
142173
}
143174

144175
for _, tc := range cases {
145-
got, err := sanitizeCheckpointPod(tc.pod)
146-
if err != nil {
147-
t.Errorf("\nUnexpected error: %v\n", err)
148-
}
176+
got := sanitizeCheckpointPod(tc.pod)
149177
if !apiequality.Semantic.DeepEqual(tc.expected, got) {
150178
t.Errorf("\nFor Test: %s\n\nExpected:\n%#v\nGot:\n%#v\n", tc.desc, tc.expected, got)
151179
}

pkg/checkpoint/process.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7+
"sort"
78
"time"
89

910
"github.com/golang/glog"
@@ -43,7 +44,8 @@ func (cs *checkpoints) update(localRunningPods, localParentPods, apiParentPods,
4344
cs.checkpoints[cs.selfCheckpoint.name] = cs.selfCheckpoint
4445
}
4546

46-
// Make sure all on-disk checkpoints are represented in memory, i.e. if we are restarting from a crash.
47+
// Make sure all on-disk checkpoints are represented in memory, i.e. if we are restarting from a
48+
// crash.
4749
for name, pod := range activeCheckpoints {
4850
if _, ok := cs.checkpoints[name]; !ok {
4951
cs.checkpoints[name] = &checkpoint{
@@ -142,6 +144,7 @@ func (cs *checkpoints) process(now time.Time, apiAvailable bool, localRunningPod
142144
stops = append(stops, name)
143145
}
144146
}
147+
sort.Strings(stops) // Ensure stable ordering.
145148
stops = append(stops, cs.selfCheckpoint.name)
146149
return starts, stops, removes
147150
case remove:
@@ -153,6 +156,7 @@ func (cs *checkpoints) process(now time.Time, apiAvailable bool, localRunningPod
153156
delete(cs.checkpoints, name)
154157
}
155158
}
159+
sort.Strings(removes) // Ensure stable ordering.
156160
removes = append(removes, cs.selfCheckpoint.name)
157161
delete(cs.checkpoints, cs.selfCheckpoint.name)
158162
cs.selfCheckpoint = nil
@@ -224,11 +228,7 @@ func (c *checkpointer) createCheckpointsForValidParents() {
224228
continue
225229
}
226230

227-
cp, err = sanitizeCheckpointPod(cp)
228-
if err != nil {
229-
glog.Errorf("Failed to sanitize manifest for %s: %v", id, err)
230-
continue
231-
}
231+
cp = sanitizeCheckpointPod(cp)
232232

233233
podChanged, err := writeCheckpointManifest(cp)
234234
if err != nil {

pkg/checkpoint/process_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package checkpoint
22

33
import (
4-
"flag"
54
"reflect"
65
"testing"
76
"time"
@@ -11,9 +10,6 @@ import (
1110
)
1211

1312
func TestProcess(t *testing.T) {
14-
flag.Set("logtostderr", "true")
15-
flag.Parse()
16-
1713
type testCase struct {
1814
desc string
1915
localRunning map[string]*v1.Pod
@@ -59,6 +55,13 @@ func TestProcess(t *testing.T) {
5955
apiParents: map[string]*v1.Pod{"AA": {}},
6056
expectStart: []string{"AA"},
6157
},
58+
{
59+
desc: "Inactive checkpoint and only api and kubelet parents: should start",
60+
inactiveCheckpoints: map[string]*v1.Pod{"AA": {}},
61+
apiParents: map[string]*v1.Pod{"AA": {}},
62+
localParents: map[string]*v1.Pod{"AA": {}},
63+
expectStart: []string{"AA"},
64+
},
6265
{
6366
desc: "Active checkpoint and no local running: no change",
6467
activeCheckpoints: map[string]*v1.Pod{"AA": {}},
@@ -170,8 +173,9 @@ func TestProcess(t *testing.T) {
170173
},
171174
"AA": {},
172175
},
173-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
174-
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
176+
expectStart: []string{"kube-system/pod-checkpointer"},
177+
expectStop: []string{"BB"},
178+
expectGraceRemove: []string{"AA", "BB", "kube-system/pod-checkpointer"},
175179
},
176180
{
177181
desc: "Inactive pod-checkpointer, no local parent, no api parent: should remove all",
@@ -187,7 +191,7 @@ func TestProcess(t *testing.T) {
187191
},
188192
"AA": {},
189193
},
190-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
194+
expectStart: []string{"kube-system/pod-checkpointer"},
191195
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
192196
},
193197
{
@@ -204,7 +208,8 @@ func TestProcess(t *testing.T) {
204208
},
205209
"AA": {},
206210
},
207-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
211+
expectStart: []string{"kube-system/pod-checkpointer"},
212+
expectStop: []string{"AA"},
208213
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
209214
},
210215
{
@@ -268,8 +273,8 @@ func TestProcess(t *testing.T) {
268273
},
269274
"AA": {},
270275
},
271-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
272-
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
276+
expectStart: []string{"kube-system/pod-checkpointer", "BB"},
277+
expectGraceRemove: []string{"AA", "BB", "kube-system/pod-checkpointer"},
273278
},
274279
{
275280
desc: "Running as an on-disk checkpointer: Inactive pod-checkpointer, no local parent, no api parent: should remove all",
@@ -286,7 +291,7 @@ func TestProcess(t *testing.T) {
286291
},
287292
"AA": {},
288293
},
289-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
294+
expectStart: []string{"kube-system/pod-checkpointer"},
290295
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
291296
},
292297
{
@@ -304,7 +309,8 @@ func TestProcess(t *testing.T) {
304309
},
305310
"AA": {},
306311
},
307-
expectStop: []string{"AA", "kube-system/pod-checkpointer"},
312+
expectStart: []string{"kube-system/pod-checkpointer"},
313+
expectStop: []string{"AA"},
308314
expectGraceRemove: []string{"AA", "kube-system/pod-checkpointer"},
309315
},
310316
}

pkg/checkpoint/state.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type checkpointState interface {
6565
// stateSelfCheckpointActive represents a checkpoint of the checkpointer itself, which has special
6666
// behavior.
6767
//
68-
// stateSelfCheckpointActive can transition to stateInactiveGracePeriod.
68+
// stateSelfCheckpointActive can transition to stateActiveGracePeriod.
6969
type stateSelfCheckpointActive struct{}
7070

7171
// transition implements state.transition()
@@ -80,9 +80,9 @@ func (s stateSelfCheckpointActive) transition(now time.Time, apis apiCondition)
8080
return s
8181
}
8282

83-
// The apiserver parent pod is deleted, transition to stateInactiveGracePeriod.
83+
// The apiserver parent pod is deleted, transition to stateActiveGracePeriod.
8484
// TODO(diegs): this is a little hacky, perhaps clean it up with a constructor.
85-
return stateInactiveGracePeriod{gracePeriodEnd: now.Add(checkpointGracePeriod)}.checkGracePeriod(now, apis)
85+
return stateActiveGracePeriod{gracePeriodEnd: now.Add(checkpointGracePeriod)}.checkGracePeriod(now, apis)
8686
}
8787

8888
// action implements state.action()

pkg/checkpoint/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestAllowedStateTransitions(t *testing.T) {
3131
want []checkpointState
3232
}{{
3333
state: stateSelfCheckpointActive{},
34-
want: []checkpointState{stateSelfCheckpointActive{}, stateInactiveGracePeriod{}},
34+
want: []checkpointState{stateSelfCheckpointActive{}, stateActiveGracePeriod{}},
3535
}, {
3636
state: stateNone{},
3737
want: []checkpointState{stateNone{}, stateInactive{}, stateInactiveGracePeriod{}, stateActive{}},

0 commit comments

Comments
 (0)