Skip to content

Commit

Permalink
fix(CosmosFullNode): Cached pods need to look for synced status (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNix authored Aug 3, 2023
1 parent 43cb785 commit b92c5ca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/cosmos/cache_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *CacheController) Collect(ctx context.Context, controller client.ObjectK

// SyncedPods returns only the pods that are ready and in sync (i.e. caught up with chain tip).
func (c *CacheController) SyncedPods(ctx context.Context, controller client.ObjectKey) []*corev1.Pod {
return kube.AvailablePods(c.Collect(ctx, controller).Pods(), 5*time.Second, time.Now())
return kube.AvailablePods(c.Collect(ctx, controller).SyncedPods(), 5*time.Second, time.Now())
}

func (c *CacheController) listPods(ctx context.Context, controller client.ObjectKey) ([]corev1.Pod, error) {
Expand Down
15 changes: 11 additions & 4 deletions internal/cosmos/cache_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestCacheController_SyncedPods(t *testing.T) {
collector.StubCollection = StatusCollection{
{Pod: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "1"}}},
{Pod: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "2"}}, Status: catchingUp},
{Pod: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "3"}}},
{Pod: &corev1.Pod{ObjectMeta: metav1.ObjectMeta{UID: "should not see me"}}, Status: catchingUp},
}

Expand All @@ -211,16 +212,22 @@ func TestCacheController_SyncedPods(t *testing.T) {
require.NoError(t, err)

key := client.ObjectKey{Name: name, Namespace: namespace}
// Wait until we've fetched comet status in the background and cached it.
require.Eventually(t, func() bool {
return len(controller.Collect(ctx, key)) > 0
p := controller.Collect(ctx, key)
l := len(p)
_, e := p[0].GetStatus()
return l == 1 && e == nil
}, time.Second, time.Millisecond)

readyStatus := corev1.PodStatus{Conditions: []corev1.PodCondition{
{Type: corev1.PodReady, Status: corev1.ConditionTrue, LastTransitionTime: metav1.NewTime(time.Now().Add(-5 * time.Second))}},
}

pods = []corev1.Pod{
{ObjectMeta: metav1.ObjectMeta{UID: "1"}, Status: readyStatus},
{ObjectMeta: metav1.ObjectMeta{UID: "2"}},
{ObjectMeta: metav1.ObjectMeta{UID: "2"}, Status: readyStatus},
{ObjectMeta: metav1.ObjectMeta{UID: "3"}}, // not ready
{ObjectMeta: metav1.ObjectMeta{UID: "new"}},
}

Expand All @@ -230,9 +237,9 @@ func TestCacheController_SyncedPods(t *testing.T) {

gotColl := controller.Collect(ctx, key)
uids := lo.Map(gotColl, func(item StatusItem, _ int) string { return string(item.Pod.UID) })
require.Equal(t, []string{"1", "2", "new"}, uids)
require.Equal(t, []string{"1", "2", "3", "new"}, uids)

_, err = gotColl[2].GetStatus()
_, err = gotColl[3].GetStatus()
require.Error(t, err)
require.EqualError(t, err, "missing status")

Expand Down

0 comments on commit b92c5ca

Please sign in to comment.