Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/templates/pdbminavailable/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func minAvailableCheck(lintCtx lintcontext.LintContext, object lintcontext.Objec
}
}

// Check if selector is present - it's a required field for PDB
if pdb.Spec.Selector == nil {
return []diagnostic.Diagnostic{
{
Message: "PDB is missing required selector field: https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget",
},
}
}

// Build the label selector for the PDB to use for comparison
labelSelector, err := metaV1.LabelSelectorAsSelector(&metaV1.LabelSelector{
MatchLabels: pdb.Spec.Selector.MatchLabels,
Expand Down
22 changes: 22 additions & 0 deletions pkg/templates/pdbminavailable/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,25 @@ func (p *PDBTestSuite) TestPDBWithMinAvailableAndKedaScaledObjectHasMinReplicas(
},
})
}

// test that the check handles PDB with minAvailable but no selector (should not panic)
func (p *PDBTestSuite) TestPDBWithMinAvailableButNoSelector() {
p.ctx.AddMockPodDisruptionBudget(p.T(), "test-pdb")
p.ctx.ModifyPodDisruptionBudget(p.T(), "test-pdb", func(pdb *v1.PodDisruptionBudget) {
pdb.Namespace = "test"
pdb.Spec.Selector = nil // Missing required selector field
pdb.Spec.MinAvailable = &intstr.IntOrString{StrVal: "40%", Type: intstr.String}
})

p.Validate(p.ctx, []templates.TestCase{
{
Param: params.Params{},
Diagnostics: map[string][]diagnostic.Diagnostic{
"test-pdb": {
{Message: "PDB is missing required selector field: https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget"},
},
},
ExpectInstantiationError: false,
},
})
}
Loading