-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(lint): Add new linter to restart policy for all deployment li…
…ke objects
- Loading branch information
Jonathan Henrique Medeiros
committed
Feb 21, 2025
1 parent
9d11bc6
commit 762b670
Showing
10 changed files
with
338 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name: "restart-policy" | ||
description: "Indicates when a object deployment's like doesn't use a restart policy" | ||
remediation: >- | ||
Set up the restart policy for your object to 'Always' to increase the fault tolerance. | ||
scope: | ||
objectKinds: | ||
- DeploymentLike | ||
template: "restart-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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
package restartpolicy | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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/extract" | ||
"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/restartpolicy/internal/params" | ||
coreV1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
templateKey = "restart-policy" | ||
acceptedRestartPolicy = coreV1.RestartPolicyAlways | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Restart policy", | ||
Key: templateKey, | ||
Description: "Flag applications running without the restart policy set to Always.", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
spec, found := extract.PodSpec(object.K8sObject) | ||
if !found { | ||
return nil | ||
} | ||
if spec.RestartPolicy == acceptedRestartPolicy { | ||
return nil | ||
} | ||
return []diagnostic.Diagnostic{ | ||
{Message: fmt.Sprintf("object has a restart policy defined with '%s' but the only accepted restart policy is '%s'", spec.RestartPolicy, acceptedRestartPolicy)}, | ||
} | ||
}, nil | ||
}), | ||
}) | ||
} |
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,98 @@ | ||
package restartpolicy | ||
|
||
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/restartpolicy/internal/params" | ||
appsV1 "k8s.io/api/apps/v1" | ||
coreV1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestRestartPolicy(t *testing.T) { | ||
suite.Run(t, new(RestartPolicyTestSuite)) | ||
} | ||
|
||
type RestartPolicyTestSuite struct { | ||
templates.TemplateTestSuite | ||
|
||
ctx *mocks.MockLintContext | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) SetupTest() { | ||
s.Init(templateKey) | ||
s.ctx = mocks.NewMockContext() | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) addDeploymentWithRestartPolicy(name string, policy coreV1.RestartPolicy) { | ||
s.ctx.AddMockDeployment(s.T(), name) | ||
s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { | ||
deployment.Spec.Template.Spec.RestartPolicy = policy | ||
}) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) addDeploymentWithEmptyRestartPolicy(name string) { | ||
s.ctx.AddMockDeployment(s.T(), name) | ||
s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) { | ||
deployment.Spec.Template.Spec.RestartPolicy = "" | ||
}) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) addDeploymentWithoutRestartPolicy(name string) { | ||
s.ctx.AddMockDeployment(s.T(), name) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) TestInvalidRestartPolicies() { | ||
const ( | ||
withoutRestartPolicy = "without-restart-policy" | ||
emptyRestartPolicy = "empty-restart-policy" | ||
restartPolicyNever = "restart-policy-never" | ||
restartPolicyOnFailure = "restart-policy-on-failure" | ||
) | ||
|
||
s.addDeploymentWithoutRestartPolicy(withoutRestartPolicy) | ||
s.addDeploymentWithEmptyRestartPolicy(emptyRestartPolicy) | ||
s.addDeploymentWithRestartPolicy(restartPolicyNever, coreV1.RestartPolicyNever) | ||
s.addDeploymentWithRestartPolicy(restartPolicyOnFailure, coreV1.RestartPolicyOnFailure) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
withoutRestartPolicy: { | ||
{Message: "object has a restart policy defined with '' but the only accepted restart policy is 'Always'"}, | ||
}, | ||
emptyRestartPolicy: { | ||
{Message: "object has a restart policy defined with '' but the only accepted restart policy is 'Always'"}, | ||
}, | ||
restartPolicyNever: { | ||
{Message: "object has a restart policy defined with 'Never' but the only accepted restart policy is 'Always'"}, | ||
}, | ||
restartPolicyOnFailure: { | ||
{Message: "object has a restart policy defined with 'OnFailure' but the only accepted restart policy is 'Always'"}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) TestAcceptableRestartPolicy() { | ||
const ( | ||
acceptableRestartPolicy = "acceptable-restart-policy" | ||
) | ||
s.addDeploymentWithRestartPolicy(acceptableRestartPolicy, coreV1.RestartPolicyAlways) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
acceptableRestartPolicy: 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,86 @@ | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: dont-fire-deployment | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Always | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: dont-fire-pod | ||
spec: | ||
restartPolicy: Always | ||
--- | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
name: dont-fire-daemonset | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Always | ||
--- | ||
apiVersion: apps/v1 | ||
kind: ReplicaSet | ||
metadata: | ||
name: dont-fire-replicaset | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Always | ||
--- | ||
apiVersion: v1 | ||
kind: ReplicationController | ||
metadata: | ||
name: dont-fire-replicationcontroller | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Always | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: fire-deployment | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Never | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: fire-pod | ||
spec: | ||
restartPolicy: OnFailure | ||
--- | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
name: fire-daemonset | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Never | ||
--- | ||
apiVersion: apps/v1 | ||
kind: ReplicaSet | ||
metadata: | ||
name: fire-replicaset | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: OnFailure | ||
--- | ||
apiVersion: v1 | ||
kind: ReplicationController | ||
metadata: | ||
name: fire-replicationcontroller | ||
spec: | ||
template: | ||
spec: | ||
restartPolicy: Never |