Skip to content
Merged
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
34 changes: 34 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,40 @@ func TestBoundAndSortEvents_TruncatesAndNotifies(t *testing.T) {
}
}

func TestBoundAndSortEvents_TruncationKeepsUnavailableNotice(t *testing.T) {
now := time.Now()
build := func(rv string) *corev1.EventList {
list := &corev1.EventList{Items: make([]corev1.Event, 0, 150), ListMeta: metav1.ListMeta{ResourceVersion: rv}}
for i := 0; i < 150; i++ {
list.Items = append(list.Items, corev1.Event{
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("ev%d", i), Namespace: "games"},
LastTimestamp: metav1.NewTime(now.Add(time.Duration(-i) * time.Second)),
Reason: "Test",
})
}
return list
}

t.Run("unavailable notice kept", func(t *testing.T) {
bounded := boundAndSortEvents(build("unavailable clusters: east, west"))
if len(bounded.Items) != 100 {
t.Fatalf("want 100 items after bounding, got %d", len(bounded.Items))
}
want := "truncated (showing newest 100 of 150 events); unavailable clusters: east, west"
if bounded.ResourceVersion != want {
t.Errorf("want resourceVersion %q, got %q", want, bounded.ResourceVersion)
}
})

t.Run("plain resource version replaced", func(t *testing.T) {
bounded := boundAndSortEvents(build("12345"))
want := "truncated (showing newest 100 of 150 events)"
if bounded.ResourceVersion != want {
t.Errorf("want resourceVersion %q, got %q", want, bounded.ResourceVersion)
}
})
}

func TestBoundAndSortEvents_NoTruncationWhenUnderLimit(t *testing.T) {
now := time.Now()
list := &corev1.EventList{
Expand Down
13 changes: 12 additions & 1 deletion tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
Expand All @@ -26,6 +27,11 @@ import (
// maxListEvents is the maximum number of events to include in list responses.
const maxListEvents = 100

// unavailableClustersPrefix starts the resource version the panel client sets
// when some clusters could not be read (internal/panel/fanout.go). Truncation
// keeps that notice so the warning is not lost.
const unavailableClustersPrefix = "unavailable clusters: "

// registeredToolNames lists every tool this server installs, in
// registration order. main_test.go uses it to assert the read-only
// invariant (no name/description implying a mutating verb) without needing
Expand Down Expand Up @@ -280,6 +286,7 @@ func filterEventsByLabel(list *corev1.EventList, selector string) (*corev1.Event
// bounds the list to maxListEvents. If the list is truncated, it mutates
// the list metadata to include a truncation notice in the resourceVersion
// field, since MCP text responses don't have a structured place for metadata.
// An existing unavailable-clusters notice is kept after the truncation notice.
// Returns a DeepCopy to avoid mutating the input.
func boundAndSortEvents(list *corev1.EventList) *corev1.EventList {
out := list.DeepCopy()
Expand All @@ -293,7 +300,11 @@ func boundAndSortEvents(list *corev1.EventList) *corev1.EventList {
// Bound to maxListEvents and set truncation notice if needed
if len(out.Items) > maxListEvents {
out.Items = out.Items[:maxListEvents]
out.ResourceVersion = fmt.Sprintf("truncated (showing newest %d of %d events)", maxListEvents, len(list.Items))
notice := fmt.Sprintf("truncated (showing newest %d of %d events)", maxListEvents, len(list.Items))
if strings.HasPrefix(list.ResourceVersion, unavailableClustersPrefix) {
notice += "; " + list.ResourceVersion
}
out.ResourceVersion = notice
}

return out
Expand Down