From dc33fea56e170c49a733751786c2303df56fea3d Mon Sep 17 00:00:00 2001 From: Kristoffer Ahl Date: Wed, 7 Dec 2022 22:54:30 +0100 Subject: [PATCH] Fixing issue with out of range indices when removing items from resource lists. --- controllers/core/orphaned_resource_reconciler.go | 10 +++++----- controllers/core/resourceset_reconciler.go | 3 +++ internal/pkg/tenant/types.go | 3 +++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/controllers/core/orphaned_resource_reconciler.go b/controllers/core/orphaned_resource_reconciler.go index 104caf1..665fd50 100644 --- a/controllers/core/orphaned_resource_reconciler.go +++ b/controllers/core/orphaned_resource_reconciler.go @@ -59,13 +59,13 @@ func (h *OrphanedResourceEventHandler) On(e eventsource.Event) { case *tenant.ResourceAdded: h.state.Active = append(h.state.Active, event.Resource) index, _ := h.state.Deleted.Find(event.Resource.Id) - if index >= 0 { - h.state.Deleted = h.state.Deleted.Remove(index) - } + h.state.Deleted = h.state.Deleted.Remove(index) case *tenant.ResourceRemoved: index, r := h.state.Active.Find(event.ResourceId) - h.state.Active = h.state.Active.Remove(index) - h.state.Deleted = append(h.state.Deleted, *r) + if index >= 0 && r != nil { + h.state.Active = h.state.Active.Remove(index) + h.state.Deleted = append(h.state.Deleted, *r) + } case *tenant.ResourceGenererationFailed: h.state.DeleteAllowed = false case *tenant.ResourceGenererationSuccessful: diff --git a/controllers/core/resourceset_reconciler.go b/controllers/core/resourceset_reconciler.go index ed0f813..23e1659 100644 --- a/controllers/core/resourceset_reconciler.go +++ b/controllers/core/resourceset_reconciler.go @@ -217,6 +217,9 @@ func NewResourceSet() *corev1alpha1.ResourceSet { } func remove(slice corev1alpha1.ResourceSetResourceList, index int) corev1alpha1.ResourceSetResourceList { + if index < 0 { + return slice + } return append(slice[:index], slice[index+1:]...) } diff --git a/internal/pkg/tenant/types.go b/internal/pkg/tenant/types.go index c011226..9e488d2 100644 --- a/internal/pkg/tenant/types.go +++ b/internal/pkg/tenant/types.go @@ -70,6 +70,9 @@ func (rl ResourceList) Find(id string) (index int, existing *Resource) { } func (rl ResourceList) Remove(index int) ResourceList { + if index < 0 { + return rl + } return append(rl[:index], rl[index+1:]...) }