fix: guard against nil spec.replicas in deployment analyzer#1683
Open
anxkhn wants to merge 1 commit into
Open
Conversation
The Deployment analyzer dereferenced *deployment.Spec.Replicas without a nil check. Spec.Replicas is a *int32 and, although the API server usually defaults it to 1, a Deployment object whose replicas field is explicitly unset (nil) panics the analyze run with a nil pointer dereference. Guard the comparison with a nil check, mirroring the sibling StatefulSet analyzer which already checks Spec.Replicas != nil before dereferencing. Add a regression test that analyzes a Deployment with nil Spec.Replicas and asserts Analyze does not panic. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1683 +/- ##
===========================================
+ Coverage 34.76% 45.95% +11.18%
===========================================
Files 94 121 +27
Lines 6342 7533 +1191
===========================================
+ Hits 2205 3462 +1257
+ Misses 4046 3838 -208
- Partials 91 233 +142 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
AlexsJones
approved these changes
Jul 6, 2026
AlexsJones
left a comment
Member
There was a problem hiding this comment.
Reviewed the diff — this is a correct, minimal fix. ✅
pkg/analyzer/deployment.go:57did dereference*deployment.Spec.Replicasunconditionally, andSpec.Replicasis a*int32, so a Deployment with an explicitly-unsetspec.replicaswould panic the analyze run. Real bug.- The guard
deployment.Spec.Replicas != nil && …short-circuits before the deref, and every other*deployment.Spec.Replicasread (theStatus.Replicas > *Spec.Replicascomparison and both failure messages) lives inside that same block, so the single guard covers all of them. - It mirrors the sibling
StatefulSetAnalyzer, which already usessts.Spec.Replicas != nil && *(sts.Spec.Replicas) != …— so this keeps the two analyzers consistent. TestDeploymentAnalyzerNilReplicasis a proper regression test: it feeds a nil-replicas Deployment and asserts no panic / zero results. CI (build, golangci-lint, validate, codecov) is green.
No behavior change for the common case where spec.replicas is set. Recommending this for merge — over to @AlexsJones for the final call.
🤝 Handled by Alex's Repo Steward — replies reviewed by @AlexsJones.
Learn more or run your own: https://github.com/AlexsJones/repo-steward
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The Deployment analyzer dereferences
*deployment.Spec.Replicaswithout a nilcheck.
Spec.Replicasis a*int32; although the API server usually defaultsit to 1, a Deployment object whose
spec.replicasis explicitly unset (nil)panics the analyze run with a nil pointer dereference at
pkg/analyzer/deployment.go:57.This guards the comparison with a nil check, mirroring the sibling StatefulSet
analyzer (
pkg/analyzer/statefulset.go:97), which already checksSpec.Replicas != nilbefore dereferencing. The single guard also protects thesubsequent
*deployment.Spec.Replicasreads on lines 58, 62, and 79, since theyonly run inside the guarded block.
Added a regression test,
TestDeploymentAnalyzerNilReplicas, that analyzes aDeployment with nil
spec.replicasand assertsAnalyzereturns no results anddoes not panic.
Checks
Additional Information
No behavior change for Deployments that set
spec.replicas(the overwhelmingcommon case, since the API server defaults it). The only difference is that a
Deployment with an explicitly-unset replicas field is now skipped instead of
crashing the analyzer.