Skip to content

Commit f8f1795

Browse files
janiszclaude
andauthored
Fix nil pointer dereference in pdbminavailable check (#1041) (#1043)
The pdbminavailable check crashed with a panic when it encountered a PodDisruptionBudget that has spec.minAvailable defined but is missing the required spec.selector field. The code attempted to access pdb.Spec.Selector.MatchLabels and pdb.Spec.Selector.MatchExpressions without checking if Selector was nil, causing a nil pointer dereference. This fix adds a nil check for pdb.Spec.Selector before accessing its fields. If the selector is missing, the check now returns a clear diagnostic message with a link to Kubernetes documentation instead of panicking. Also added a unit test TestPDBWithMinAvailableButNoSelector to verify the fix handles this edge case correctly. Fixes #1041 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6767e13 commit f8f1795

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/templates/pdbminavailable/template.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ func minAvailableCheck(lintCtx lintcontext.LintContext, object lintcontext.Objec
7676
}
7777
}
7878

79+
// Check if selector is present - it's a required field for PDB
80+
if pdb.Spec.Selector == nil {
81+
return []diagnostic.Diagnostic{
82+
{
83+
Message: "PDB is missing required selector field: https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget",
84+
},
85+
}
86+
}
87+
7988
// Build the label selector for the PDB to use for comparison
8089
labelSelector, err := metaV1.LabelSelectorAsSelector(&metaV1.LabelSelector{
8190
MatchLabels: pdb.Spec.Selector.MatchLabels,

pkg/templates/pdbminavailable/template_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,25 @@ func (p *PDBTestSuite) TestPDBWithMinAvailableAndKedaScaledObjectHasMinReplicas(
296296
},
297297
})
298298
}
299+
300+
// test that the check handles PDB with minAvailable but no selector (should not panic)
301+
func (p *PDBTestSuite) TestPDBWithMinAvailableButNoSelector() {
302+
p.ctx.AddMockPodDisruptionBudget(p.T(), "test-pdb")
303+
p.ctx.ModifyPodDisruptionBudget(p.T(), "test-pdb", func(pdb *v1.PodDisruptionBudget) {
304+
pdb.Namespace = "test"
305+
pdb.Spec.Selector = nil // Missing required selector field
306+
pdb.Spec.MinAvailable = &intstr.IntOrString{StrVal: "40%", Type: intstr.String}
307+
})
308+
309+
p.Validate(p.ctx, []templates.TestCase{
310+
{
311+
Param: params.Params{},
312+
Diagnostics: map[string][]diagnostic.Diagnostic{
313+
"test-pdb": {
314+
{Message: "PDB is missing required selector field: https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget"},
315+
},
316+
},
317+
ExpectInstantiationError: false,
318+
},
319+
})
320+
}

0 commit comments

Comments
 (0)