From c16d593a71b92a701d8f3ef136a405b9742746e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serta=C3=A7=20=C3=96zercan?= <852750+sozercan@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:40:00 -0700 Subject: [PATCH] fix(client): guard nil handler cache in RemoveData Signed-off-by: Sertac Ozercan --- constraint/pkg/client/client.go | 5 +++-- constraint/pkg/client/client_test.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/constraint/pkg/client/client.go b/constraint/pkg/client/client.go index 522333ca6..414df8f4c 100644 --- a/constraint/pkg/client/client.go +++ b/constraint/pkg/client/client.go @@ -617,8 +617,9 @@ func (c *Client) RemoveData(ctx context.Context, data interface{}) (*types.Respo if cacher, ok := h.(handler.Cacher); ok { cache := cacher.GetCache() - - cache.Remove(relPath) + if cache != nil { + cache.Remove(relPath) + } } } diff --git a/constraint/pkg/client/client_test.go b/constraint/pkg/client/client_test.go index 0f410bd74..af8dcb9c2 100644 --- a/constraint/pkg/client/client_test.go +++ b/constraint/pkg/client/client_test.go @@ -28,6 +28,14 @@ import ( "k8s.io/utils/ptr" ) +type nilCacheHandler struct { + *handlertest.Handler +} + +func (h *nilCacheHandler) GetCache() handler.Cache { + return nil +} + func TestBackend_NewClient_InvalidTargetName(t *testing.T) { tcs := []struct { name string @@ -2134,3 +2142,16 @@ func TestClient_RemoveData_Cache(t *testing.T) { }) } } + +func TestClient_RemoveData_NilCache(t *testing.T) { + h := &nilCacheHandler{ + Handler: &handlertest.Handler{}, + } + + c := clienttest.New(t, client.Targets(h)) + + _, err := c.RemoveData(context.Background(), &handlertest.Object{Namespace: "foo"}) + if err != nil { + t.Fatalf("got RemoveData() error = %v, want nil", err) + } +}