-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add template for imagePullPolicy checks (#202)
- Loading branch information
Showing
7 changed files
with
246 additions
and
1 deletion.
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
89 changes: 89 additions & 0 deletions
89
pkg/templates/imagepullpolicy/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.
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,12 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
// list of forbidden image pull policy | ||
// +noregex | ||
// +notnegatable | ||
// +enum=Always | ||
// +enum=IfNotPresent | ||
// +enum=Never | ||
ForbiddenPolicies []string | ||
} |
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,41 @@ | ||
package imagepullpolicy | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.stackrox.io/kube-linter/internal/set" | ||
"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/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/imagepullpolicy/internal/params" | ||
"golang.stackrox.io/kube-linter/pkg/templates/util" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
templateKey = "image-pull-policy" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Image Pull Policy", | ||
Key: templateKey, | ||
Description: "Flag containers with forbidden image pull policy", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
forbiddenPolicies := set.NewStringSet(p.ForbiddenPolicies...) | ||
return util.PerContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic { | ||
if forbiddenPolicies.Contains(string(container.ImagePullPolicy)) { | ||
return []diagnostic.Diagnostic{{Message: fmt.Sprintf("container %q has imagePullPolicy set to %s", container.Name, container.ImagePullPolicy)}} | ||
} | ||
return nil | ||
}), 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,59 @@ | ||
package imagepullpolicy | ||
|
||
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/imagepullpolicy/internal/params" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestContainerImage(t *testing.T) { | ||
suite.Run(t, new(ContainerImageTestSuite)) | ||
} | ||
|
||
type ContainerImageTestSuite struct { | ||
templates.TemplateTestSuite | ||
ctx *mocks.MockLintContext | ||
} | ||
|
||
func (s *ContainerImageTestSuite) SetupTest() { | ||
s.Init(templateKey) | ||
s.ctx = mocks.NewMockContext() | ||
} | ||
|
||
func (s *ContainerImageTestSuite) addDeploymentWithContainerImage(name string, pullPolicy v1.PullPolicy) { | ||
s.ctx.AddMockDeployment(s.T(), name) | ||
s.ctx.AddContainerToDeployment(s.T(), name, v1.Container{Name: "test-container", ImagePullPolicy: pullPolicy}) | ||
} | ||
|
||
func (s *ContainerImageTestSuite) TestImaPolicy() { | ||
const ( | ||
alwaysDep = "deployment-with-always-pull-policy" | ||
ifNotPresentDep = "deployment-with-if-not-present-pull-policy" | ||
neverDep = "deployment-with-never-pull-policy" | ||
) | ||
|
||
s.addDeploymentWithContainerImage(alwaysDep, v1.PullAlways) | ||
s.addDeploymentWithContainerImage(ifNotPresentDep, v1.PullIfNotPresent) | ||
s.addDeploymentWithContainerImage(neverDep, v1.PullNever) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
ForbiddenPolicies: []string{"Always"}, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
alwaysDep: { | ||
{Message: "container \"test-container\" has imagePullPolicy set to Always"}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |