-
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): Adds a new template linter to check the restart policy…
… for all deployments like objects (#915) Co-authored-by: Jonathan Henrique Medeiros <[email protected]>
- Loading branch information
1 parent
37f508e
commit 99380d7
Showing
10 changed files
with
465 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 deployment-like object does not use a restart policy" | ||
remediation: >- | ||
Set up the restart policy for your object to 'Always' or 'OnFailure' 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,50 @@ | ||
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" | ||
) | ||
|
||
var acceptedRestartPolicies = []coreV1.RestartPolicy{coreV1.RestartPolicyAlways, coreV1.RestartPolicyOnFailure} | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Restart policy", | ||
Key: templateKey, | ||
Description: "Flag applications running without the restart policy.", | ||
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 | ||
} | ||
for _, policy := range acceptedRestartPolicies { | ||
if spec.RestartPolicy == policy { | ||
return nil | ||
} | ||
} | ||
return []diagnostic.Diagnostic{ | ||
{Message: fmt.Sprintf("object has a restart policy defined with '%s' but the only accepted restart policies are '%s'", spec.RestartPolicy, acceptedRestartPolicies)}, | ||
} | ||
}, 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,118 @@ | ||
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) addObjectWithoutPodSpec(name string) { | ||
s.ctx.AddMockService(s.T(), name) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) TestInvalidRestartPolicies() { | ||
const ( | ||
withoutRestartPolicy = "without-restart-policy" | ||
emptyRestartPolicy = "empty-restart-policy" | ||
restartPolicyNever = "restart-policy-never" | ||
) | ||
|
||
s.addDeploymentWithoutRestartPolicy(withoutRestartPolicy) | ||
s.addDeploymentWithEmptyRestartPolicy(emptyRestartPolicy) | ||
s.addDeploymentWithRestartPolicy(restartPolicyNever, coreV1.RestartPolicyNever) | ||
|
||
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 policies are '[Always OnFailure]'"}, | ||
}, | ||
emptyRestartPolicy: { | ||
{Message: "object has a restart policy defined with '' but the only accepted restart policies are '[Always OnFailure]'"}, | ||
}, | ||
restartPolicyNever: { | ||
{Message: "object has a restart policy defined with 'Never' but the only accepted restart policies are '[Always OnFailure]'"}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) TestAcceptableRestartPolicy() { | ||
const ( | ||
alwaysRestartPolicy = "restart-policy-always" | ||
onFailureRestartPolicy = "restart-policy-on-failure" | ||
) | ||
s.addDeploymentWithRestartPolicy(alwaysRestartPolicy, coreV1.RestartPolicyAlways) | ||
s.addDeploymentWithRestartPolicy(onFailureRestartPolicy, coreV1.RestartPolicyOnFailure) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
alwaysRestartPolicy: nil, | ||
onFailureRestartPolicy: nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *RestartPolicyTestSuite) TestObjectWithoutPodSpec() { | ||
const ( | ||
objectWithoutPodSpec = "object-without-pod-spec" | ||
) | ||
|
||
s.addObjectWithoutPodSpec(objectWithoutPodSpec) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
objectWithoutPodSpec: nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.