From 0e744953955a8f55cc723fe63f1258fa8de378e3 Mon Sep 17 00:00:00 2001
From: Jonathan Crowther <jonathan.crowther@suse.com>
Date: Fri, 1 Mar 2024 09:48:20 -0500
Subject: [PATCH] DeepCopy the count before sending it as APIEvent

---
 go.mod                         | 1 +
 go.sum                         | 2 ++
 pkg/resources/counts/buffer.go | 3 ++-
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/go.mod b/go.mod
index 23e34957..3db23d79 100644
--- a/go.mod
+++ b/go.mod
@@ -28,6 +28,7 @@ require (
 	github.com/stretchr/testify v1.8.4
 	github.com/urfave/cli v1.22.14
 	github.com/urfave/cli/v2 v2.25.7
+	golang.design/x/reflect v0.0.0-20220504060917-02c43be63f3b
 	golang.org/x/sync v0.5.0
 	gopkg.in/yaml.v3 v3.0.1
 	k8s.io/api v0.28.6
diff --git a/go.sum b/go.sum
index 11cb33ad..65284e65 100644
--- a/go.sum
+++ b/go.sum
@@ -396,6 +396,8 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
 go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJPI1Nnw=
 go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
 go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
+golang.design/x/reflect v0.0.0-20220504060917-02c43be63f3b h1:lkOPTy76R9NZ6FeDQWkDj3NsLtD8Csc9AAFYEl3kiME=
+golang.design/x/reflect v0.0.0-20220504060917-02c43be63f3b/go.mod h1:QXG482h3unP32W/YwIPOc+09bvY447B7T+iLjC/JPcA=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
diff --git a/pkg/resources/counts/buffer.go b/pkg/resources/counts/buffer.go
index 0f97d14a..be2ed851 100644
--- a/pkg/resources/counts/buffer.go
+++ b/pkg/resources/counts/buffer.go
@@ -4,6 +4,7 @@ import (
 	"time"
 
 	"github.com/rancher/apiserver/pkg/types"
+	"golang.design/x/reflect"
 )
 
 // debounceDuration determines how long events will be held before they are sent to the consumer
@@ -49,7 +50,7 @@ func debounceCounts(result chan types.APIEvent, input chan Count) {
 			}
 		case <-t.C:
 			if currentCount != nil {
-				result <- toAPIEvent(*currentCount)
+				result <- toAPIEvent(reflect.DeepCopy(*currentCount))
 				currentCount = nil
 			}
 		}