Skip to content

Commit

Permalink
Fixing issue with out of range indices when removing items from resou…
Browse files Browse the repository at this point in the history
…rce lists.
  • Loading branch information
kristofferahl committed Dec 7, 2022
1 parent 00c4070 commit dc33fea
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions controllers/core/orphaned_resource_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions controllers/core/resourceset_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]...)
}

Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/tenant/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]...)
}

Expand Down

0 comments on commit dc33fea

Please sign in to comment.