From 7b434b1338872c6265e399d46e2c34e01360b389 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 25 Mar 2020 18:15:21 -0700 Subject: [PATCH] Fix panic in counts --- pkg/server/resources/counts/counts.go | 33 +++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/server/resources/counts/counts.go b/pkg/server/resources/counts/counts.go index 811e345..50e274e 100644 --- a/pkg/server/resources/counts/counts.go +++ b/pkg/server/resources/counts/counts.go @@ -54,12 +54,35 @@ type Summary struct { Transitioning int `json:"transitioning,omitempty"` } +func (s *Summary) DeepCopy() *Summary { + r := *s + if r.States != nil { + r.States = map[string]int{} + for k := range s.States { + r.States[k] = s.States[k] + } + } + return &r +} + type ItemCount struct { Summary Summary `json:"summary,omitempty"` Namespaces map[string]Summary `json:"namespaces,omitempty"` Revision int `json:"revision,omitempty"` } +func (i *ItemCount) DeepCopy() *ItemCount { + r := *i + r.Summary = *r.Summary.DeepCopy() + if r.Namespaces != nil { + r.Namespaces = map[string]Summary{} + for k, v := range i.Namespaces { + r.Namespaces[k] = *v.DeepCopy() + } + } + return &r +} + type Store struct { empty.Store ccache clustercache.ClusterCache @@ -157,15 +180,7 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types. counts[schema.ID] = itemCount countsCopy := map[string]ItemCount{} for k, v := range counts { - ns := map[string]Summary{} - for i, j := range v.Namespaces { - ns[i] = j - } - countsCopy[k] = ItemCount{ - Summary: v.Summary, - Revision: v.Revision, - Namespaces: ns, - } + countsCopy[k] = *v.DeepCopy() } result <- types.APIEvent{