UPSTREAM: <carry>: Fix nil pointer dereference in VirtualMachine methods#183
UPSTREAM: <carry>: Fix nil pointer dereference in VirtualMachine methods#183RadekManak wants to merge 2 commits into
Conversation
Add nil receiver guards to IsVirtualMachine() and IsVirtualMachineScaleSetVM() methods to prevent panic when the receiver is nil. This addresses the issue where EnsureHostInPool calls GetInstanceViewStatus which calls IsVirtualMachine without checking for nil receiver first. Fixes OCPBUGS-62712
WalkthroughAdded nil-receiver guards to two VirtualMachine methods to avoid nil dereference and adjusted VMSS error handling to explicitly handle ErrorNotVmssInstance. A unit test verifying nil VirtualMachine behavior was added. ChangesNil Guard Defensive Improvements
VMSS Error Handling Adjustment
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 11 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (11 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
When getVmssVM returns ErrorNotVmssInstance, EnsureHostInPool fell through to vm.GetInstanceViewStatus() with vm == nil, causing a nil pointer dereference panic. Fix the error handling to skip the node (matching the pattern used in GetInstanceIDByNodeName), and add a nil receiver guard to ManagedByVMSS for defensive consistency. Fixes OCPBUGS-62712
|
/hold clanker |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/provider/azure_vmss.go (1)
1038-1043: ⚡ Quick winAvoid error-level logging for the expected
ErrorNotVmssInstancepath.At Line 1038,
logger.Error(...)runs before theErrorNotVmssInstancecheck, so skipped non-VMSS nodes are logged as errors. Reordering avoids false error noise.Proposed change
if err != nil { if errors.Is(err, cloudprovider.InstanceNotFound) { klog.Infof("EnsureHostInPool: skipping node %s because it is not found", vmName) return "", "", "", nil, nil } - - logger.Error(err, "failed to get vmss vm", "vmName", vmName) if errors.Is(err, ErrorNotVmssInstance) { klog.Infof("EnsureHostInPool: skipping node %s because it is not a VMSS instance", vmName) return "", "", "", nil, nil } + logger.Error(err, "failed to get vmss vm", "vmName", vmName) return "", "", "", nil, err }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/provider/azure_vmss.go` around lines 1038 - 1043, The current code logs every error with logger.Error before checking for ErrorNotVmssInstance, causing expected non-VMSS nodes to be emitted as errors; update the EnsureHostInPool error handling around the vmss get call so that you first check if errors.Is(err, ErrorNotVmssInstance) and handle that path (klog.Infof and return nils) without calling logger.Error, and only call logger.Error(err, "failed to get vmss vm", "vmName", vmName) for unexpected errors; locate the block referencing ErrorNotVmssInstance and logger.Error in azure_vmss.go and reorder or gate the log call accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/provider/azure_vmss.go`:
- Around line 1038-1043: The current code logs every error with logger.Error
before checking for ErrorNotVmssInstance, causing expected non-VMSS nodes to be
emitted as errors; update the EnsureHostInPool error handling around the vmss
get call so that you first check if errors.Is(err, ErrorNotVmssInstance) and
handle that path (klog.Infof and return nils) without calling logger.Error, and
only call logger.Error(err, "failed to get vmss vm", "vmName", vmName) for
unexpected errors; locate the block referencing ErrorNotVmssInstance and
logger.Error in azure_vmss.go and reorder or gate the log call accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: fb21d883-c5c8-4c49-a87c-54491b2c3882
📒 Files selected for processing (3)
pkg/provider/azure_vmss.gopkg/provider/virtualmachine/virtualmachine.gopkg/provider/virtualmachine/virtualmachine_test.go
|
@RadekManak: The following tests failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
Summary
EnsureHostInPoolwhengetVmssVMreturnsErrorNotVmssInstance: the code fell through tovm.GetInstanceViewStatus()withvm == nil, causing a nil pointer dereferenceIsVirtualMachine(),IsVirtualMachineScaleSetVM(), andManagedByVMSS()as defense in depthRoot Cause
In
EnsureHostInPool(azure_vmss.go:1038-1042), whengetVmssVMreturnedErrorNotVmssInstance, the negated condition!errors.Is(err, ErrorNotVmssInstance)evaluated tofalse, so the function did not return — it fell through tovm.GetInstanceViewStatus()withvm == nil, panicking atIsVirtualMachine()on the nil receiver.Fix
EnsureHostInPoolto skip the node (return nil error) whenErrorNotVmssInstanceis encountered, matching the pattern already used inGetInstanceIDByNodeNamevm != nilguards toIsVirtualMachine(),IsVirtualMachineScaleSetVM(), andManagedByVMSS()so a nil receiver returnsfalseinstead of panickingTest plan
TestNilVirtualMachineunit test covering all nil receiver methodsTestEnsureHostInPoolpassespkg/provider/...test suite passes (no failures)Fixes: https://issues.redhat.com/browse/OCPBUGS-62712
Summary by CodeRabbit
Bug Fixes
Tests