Skip to content

fix: nil pointer dereference in CapacityReservationFromEC2 for Interruptible field#9080

Merged
ryan-mist merged 1 commit intoaws:mainfrom
KeitaW:fix/interruptible-nil-panic
Apr 13, 2026
Merged

fix: nil pointer dereference in CapacityReservationFromEC2 for Interruptible field#9080
ryan-mist merged 1 commit intoaws:mainfrom
KeitaW:fix/interruptible-nil-panic

Conversation

@KeitaW
Copy link
Copy Markdown
Contributor

@KeitaW KeitaW commented Apr 13, 2026

Description

Fixes #9079

lo.Ternary eagerly evaluates all arguments in Go, so:

Interruptible: lo.Ternary(cr.Interruptible == nil, false, *cr.Interruptible),

dereferences *cr.Interruptible unconditionally, panicking when the pointer is nil. The Interruptible field may be absent from the EC2 API response depending on partition/region — when absent, the Go SDK deserializes the *bool as nil, triggering the panic.

Any user configuring capacityReservationSelectorTerms on an EC2NodeClass will hit this panic if any matched reservation has a nil Interruptible field.

Changes

  • Replace lo.Ternary(cr.Interruptible == nil, false, *cr.Interruptible) with lo.FromPtrOr(cr.Interruptible, false) which safely returns the default when nil
  • Add test fixture with nil Interruptible and a regression test to capacityreservation_test.go

How was this change tested?

  • Added unit test with a capacity reservation fixture that omits the Interruptible field (nil pointer). The existing test fixtures all set Interruptible: lo.ToPtr(false), which is why this bug was not caught.
  • Validated on a live EKS cluster (Karpenter v1.10.0, us-west-2) with 6 capacity reservations (2 capacity blocks + 4 ODCRs), all with nil Interruptible. Before fix: continuous panic. After fix: all reservations discovered successfully.

@KeitaW KeitaW requested a review from a team as a code owner April 13, 2026 11:38
@KeitaW KeitaW requested a review from jigisha620 April 13, 2026 11:38
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 13, 2026

Preview deployment ready!

Preview URL: https://pr-9080.d18coufmbnnaag.amplifyapp.com

Built from commit e7bc7f364798c8b75f273d19e033f2a60cba956e

…uptible field

lo.Ternary eagerly evaluates all arguments in Go, so
lo.Ternary(cr.Interruptible == nil, false, *cr.Interruptible)
dereferences *cr.Interruptible unconditionally, panicking when
the pointer is nil. This is the default for all standard ODCRs
and capacity blocks returned by the EC2 API.

Replace with lo.FromPtrOr which safely returns the default value
when the pointer is nil.

Also adds a test case with nil Interruptible to prevent regression.

Fixes aws#9079
@KeitaW KeitaW force-pushed the fix/interruptible-nil-panic branch from c5a8ae7 to e7bc7f3 Compare April 13, 2026 12:03
Comment thread pkg/controllers/nodeclass/capacityreservation_test.go
Copy link
Copy Markdown
Contributor

@ryan-mist ryan-mist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@ryan-mist ryan-mist merged commit d1aa440 into aws:main Apr 13, 2026
18 checks passed
@KeitaW
Copy link
Copy Markdown
Contributor Author

KeitaW commented Apr 13, 2026

Thank you!

ryan-mist pushed a commit to ryan-mist/karpenter-provider-aws that referenced this pull request Apr 13, 2026
ryan-mist pushed a commit to ryan-mist/karpenter-provider-aws that referenced this pull request Apr 13, 2026
ryan-mist added a commit that referenced this pull request Apr 15, 2026
ryan-mist added a commit that referenced this pull request Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: nil pointer dereference in CapacityReservationFromEC2 — lo.Ternary eagerly evaluates *cr.Interruptible

2 participants