Skip to content

Commit

Permalink
plugin: Fix nil deref on failed ExtractVmInfo on VM update (#690)
Browse files Browse the repository at this point in the history
More info in #689. tl;dr: we need to avoid typed nils to avoid getting
nil derefs when trying to create the event.
  • Loading branch information
sharnoff authored Dec 11, 2023
1 parent 9234522 commit 8cf814b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/plugin/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

vmapi "github.com/neondatabase/autoscaling/neonvm/apis/neonvm/v1"

Expand Down Expand Up @@ -268,13 +269,18 @@ func (e *AutoscaleEnforcer) watchVMEvents(
if err != nil {
// Try to get the runner pod associated with the VM, if we can, but don't worry
// about it if we can't.
var runnerPod *corev1.Pod
var runnerPod runtime.Object
if podName := newVM.Status.PodName; podName != "" {
// NB: index.Get returns nil if not found, so we only have a non-nil
// runnerPod if it's currently known.
runnerPod, _ = podIndex.GetIndexed(func(index *watch.NameIndex[corev1.Pod]) (*corev1.Pod, bool) {
rp, _ := podIndex.GetIndexed(func(index *watch.NameIndex[corev1.Pod]) (*corev1.Pod, bool) {
return index.Get(newVM.Namespace, podName)
})
// avoid typed nils by only assigning if non-nil
// See <https://github.com/neondatabase/autoscaling/issues/689> for more.
if rp != nil {
runnerPod = rp
}
}

logger.Error("Failed to extract VM info in update for new VM", util.VMNameFields(newVM), zap.Error(err))
Expand Down

0 comments on commit 8cf814b

Please sign in to comment.