-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
7 changes: 7 additions & 0 deletions
7
pkg/builtinchecks/yamls/pdb-unhealthy-pod-eviction-policy.yaml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: "pdb-unhealthy-pod-eviction-policy" | ||
description: "Indicates when a PodDisruptionBudget does not explicitly set the unhealthyPodEvictionPolicy field." | ||
remediation: "Set unhealthyPodEvictionPolicy to AlwaysAllow. Refer to https://kubernetes.io/docs/tasks/run-application/configure-pdb/#unhealthy-pod-eviction-policy for more information." | ||
scope: | ||
objectKinds: | ||
- PodDisruptionBudget | ||
template: "pdb-unhealthy-pod-eviction-policy" |
This file contains 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
52 changes: 52 additions & 0 deletions
52
pkg/templates/pdbunhealthypodevictionpolicy/internal/params/gen-params.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
pkg/templates/pdbunhealthypodevictionpolicy/internal/params/params.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pdbunhealthypodevictionpolicy | ||
|
||
import ( | ||
"golang.stackrox.io/kube-linter/pkg/check" | ||
"golang.stackrox.io/kube-linter/pkg/config" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/pdbunhealthypodevictionpolicy/internal/params" | ||
pdbV1 "k8s.io/api/policy/v1" | ||
) | ||
|
||
const ( | ||
templateKey = "pdb-unhealthy-pod-eviction-policy" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: ".spec.unhealthyPodEvictionPolicy in PDB is set to default", | ||
Key: templateKey, | ||
Description: "Flag PodDisruptionBudget objects that do not explicitly set unhealthyPodEvictionPolicy.", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{ | ||
objectkinds.PodDisruptionBudget}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) { | ||
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
pdb, ok := object.K8sObject.(*pdbV1.PodDisruptionBudget) | ||
if !ok { | ||
return nil | ||
} | ||
if pdb.Spec.UnhealthyPodEvictionPolicy == nil { | ||
return []diagnostic.Diagnostic{{Message: "unhealthyPodEvictionPolicy is not explicitly set"}} | ||
} | ||
return nil | ||
}, nil | ||
}), | ||
}) | ||
} |
60 changes: 60 additions & 0 deletions
60
pkg/templates/pdbunhealthypodevictionpolicy/template_test.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pdbunhealthypodevictionpolicy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/pdbunhealthypodevictionpolicy/internal/params" | ||
pdbv1 "k8s.io/api/policy/v1" | ||
) | ||
|
||
func TestUnhealthyPodEvictionPolicyPDB(t *testing.T) { | ||
suite.Run(t, new(UnhealthyPodEvictionPolicyPDBTestSuite)) | ||
} | ||
|
||
type UnhealthyPodEvictionPolicyPDBTestSuite struct { | ||
templates.TemplateTestSuite | ||
|
||
ctx *mocks.MockLintContext | ||
} | ||
|
||
func (s *UnhealthyPodEvictionPolicyPDBTestSuite) SetupTest() { | ||
s.Init(templateKey) | ||
s.ctx = mocks.NewMockContext() | ||
} | ||
|
||
func (s *UnhealthyPodEvictionPolicyPDBTestSuite) TestNoUnhealthyPodEvictionPolicy() { | ||
s.ctx.AddMockPodDisruptionBudget(s.T(), "test-pdb-no-unhealthy-pod-eviction-policy") | ||
s.ctx.ModifyPodDisruptionBudget(s.T(), "test-pdb-no-unhealthy-pod-eviction-policy", func(pdb *pdbv1.PodDisruptionBudget) { | ||
pdb.Spec.UnhealthyPodEvictionPolicy = nil | ||
}) | ||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
"test-pdb-no-unhealthy-pod-eviction-policy": {{Message: "unhealthyPodEvictionPolicy is not explicitly set"}}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *UnhealthyPodEvictionPolicyPDBTestSuite) TestUnhealthyPodEvictionPolicyIsSet() { | ||
s.ctx.AddMockPodDisruptionBudget(s.T(), "test-pdb-unhealthy-pod-eviction-policy-is-set") | ||
s.ctx.ModifyPodDisruptionBudget(s.T(), "test-pdb-unhealthy-pod-eviction-policy-is-set", func(pdb *pdbv1.PodDisruptionBudget) { | ||
var policy pdbv1.UnhealthyPodEvictionPolicyType = "AlwaysAllow" | ||
pdb.Spec.UnhealthyPodEvictionPolicy = &policy | ||
}) | ||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
"test-pdb-unhealthy-pod-eviction-policy-is-set": nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
apiVersion: policy/v1 | ||
kind: PodDisruptionBudget | ||
metadata: | ||
name: fire | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: app1 | ||
maxUnavailable: 1 | ||
--- | ||
apiVersion: policy/v1 | ||
kind: PodDisruptionBudget | ||
metadata: | ||
name: dont-fire-1 | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: app2 | ||
maxUnavailable: 1 | ||
unhealthyPodEvictionPolicy: AlwaysAllow | ||
--- | ||
apiVersion: policy/v1 | ||
kind: PodDisruptionBudget | ||
metadata: | ||
name: dont-fire-2 | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: app2 | ||
maxUnavailable: 1 | ||
unhealthyPodEvictionPolicy: IfHealthyBudget |