Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions karpenter/src/common/EventList.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest';
import { sortEventsByAge } from './EventList';

function fakeEvent(lastOccurrence: string) {
return { lastOccurrence } as any;
}

describe('sortEventsByAge', () => {
it('sorts newer events before older ones', () => {
const newer = fakeEvent('2024-06-01T00:00:00Z');
const older = fakeEvent('2024-01-01T00:00:00Z');
expect(sortEventsByAge(newer, older)).toBeLessThan(0);
expect(sortEventsByAge(older, newer)).toBeGreaterThan(0);
});

it('treats equal timestamps as equal', () => {
const a = fakeEvent('2024-01-01T00:00:00Z');
const b = fakeEvent('2024-01-01T00:00:00Z');
expect(sortEventsByAge(a, b)).toBe(0);
});
});
11 changes: 9 additions & 2 deletions karpenter/src/common/EventList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export interface ObjectEventListProps {
title: string;
}

// Event only exposes lastTimestamp through getValue() internally, not as a public
// property, so reading n1.lastTimestamp directly is always undefined -- this sorted
// nothing. lastOccurrence is the getter that actually resolves to a real value,
// including for EventSeries-based repeated events where lastTimestamp is empty.
export function sortEventsByAge(n1: Event, n2: Event): number {
return new Date(n2.lastOccurrence).getTime() - new Date(n1.lastOccurrence).getTime();
}

export default function CustomObjectEventList(props: ObjectEventListProps) {
const { t } = useTranslation();
let fieldSelector = `source=${props.source},involvedObject.kind=${props.kind}`;
Expand Down Expand Up @@ -98,8 +106,7 @@ export default function CustomObjectEventList(props: ObjectEventListProps) {
/>
);
},
sort: (n1: KubeEvent, n2: KubeEvent) =>
new Date(n2.lastTimestamp).getTime() - new Date(n1.lastTimestamp).getTime(),
sort: sortEventsByAge,
},
]}
data={eventList}
Expand Down