Skip to content

Commit

Permalink
Fix type assertion when an event is missed while connection to apiser…
Browse files Browse the repository at this point in the history
…ver was severed

Signed-off-by: Andrew Dye <[email protected]>
  • Loading branch information
EngHabu authored and andrewwdye committed Sep 23, 2024
1 parent cb75391 commit 51acfd0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion flytepropeller/pkg/controller/nodes/task/k8s/event_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ func (e *eventWatcher) OnUpdate(_, newObj interface{}) {
}

func (e *eventWatcher) OnDelete(obj interface{}) {
event := obj.(*eventsv1.Event)
event, casted := obj.(*eventsv1.Event)
if !casted {
unknown, casted := obj.(cache.DeletedFinalStateUnknown)
if !casted {
logger.Warnf(context.Background(), "Unknown object type [%T] in OnDelete", obj)
} else {
logger.Warnf(context.Background(), "Deleted object of unknown key [%v] type [%T] in OnDelete",
unknown.Key, unknown.Obj)
}

return
}

objectNsName := types.NamespacedName{Namespace: event.Regarding.Namespace, Name: event.Regarding.Name}
eventNsName := types.NamespacedName{Namespace: event.Namespace, Name: event.Name}
v, _ := e.objectCache.LoadOrStore(objectNsName, &objectEvents{})
Expand Down
20 changes: 20 additions & 0 deletions flytepropeller/pkg/controller/nodes/task/k8s/event_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
eventsv1 "k8s.io/api/events/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"
)

func TestEventWatcher_OnAdd(t *testing.T) {
Expand Down Expand Up @@ -143,6 +144,25 @@ func TestEventWatcher_OnDelete(t *testing.T) {
v, _ := ew.objectCache.Load(types.NamespacedName{Namespace: "ns3", Name: "name3"})
assert.Nil(t, v)
})

t.Run("bad object type", func(t *testing.T) {
ew.OnDelete(cache.DeletedFinalStateUnknown{
Key: "key",
Obj: &eventsv1.Event{
ObjectMeta: metav1.ObjectMeta{
Namespace: "eventns3",
Name: "eventname3",
},
Regarding: corev1.ObjectReference{
Namespace: "ns3",
Name: "name3",
},
},
})

v, _ := ew.objectCache.Load(types.NamespacedName{Namespace: "ns3", Name: "name3"})
assert.Nil(t, v)
})
}

func TestEventWatcher_List(t *testing.T) {
Expand Down

0 comments on commit 51acfd0

Please sign in to comment.