mirror of
https://github.com/FairwindsOps/polaris.git
synced 2026-05-15 13:47:41 +00:00
implement image checks using json schema
This commit is contained in:
13
checks/pullPolicyNotAlways.yaml
Normal file
13
checks/pullPolicyNotAlways.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
name: PullPolicyNotAlways
|
||||
id: pullPolicyNotAlways
|
||||
successMessage: Image pull policy is "Always"
|
||||
failureMessage: Image pull policy should be "Always"
|
||||
category: Images
|
||||
target: Container
|
||||
schema:
|
||||
'$schema': http://json-schema.org/draft-07/schema
|
||||
required:
|
||||
- imagePullPolicy
|
||||
properties:
|
||||
imagePullPolicy:
|
||||
const: Always
|
||||
18
checks/tagNotSpecified.yaml
Normal file
18
checks/tagNotSpecified.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
name: TagNotSpecified
|
||||
id: tagNotSpecified
|
||||
successMessage: Image tag is specified
|
||||
failureMessage: Image tag should be specified
|
||||
category: Images
|
||||
target: Container
|
||||
schema:
|
||||
'$schema': http://json-schema.org/draft-07/schema
|
||||
required:
|
||||
- image
|
||||
allOf:
|
||||
- properties:
|
||||
image:
|
||||
pattern: ^.+:.+$
|
||||
- properties:
|
||||
image:
|
||||
not:
|
||||
pattern: ^.+:latest$
|
||||
@@ -16,7 +16,6 @@ package validator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fairwindsops/polaris/pkg/config"
|
||||
"github.com/fairwindsops/polaris/pkg/validator/messages"
|
||||
@@ -65,7 +64,6 @@ func ValidateContainer(container *corev1.Container, parentPodResult *PodResult,
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cv.validateImage(conf, controllerName)
|
||||
cv.validateNetworking(conf, controllerName)
|
||||
cv.validateSecurity(conf, controllerName)
|
||||
|
||||
@@ -157,31 +155,6 @@ func (cv *ContainerValidation) validateResourceRange(id, resourceName string, ra
|
||||
}
|
||||
}
|
||||
|
||||
func (cv *ContainerValidation) validateImage(conf *config.Configuration, controllerName string) {
|
||||
category := messages.CategoryImages
|
||||
|
||||
name := "PullPolicyNotAlways"
|
||||
if conf.IsActionable(conf.Images, name, controllerName) {
|
||||
id := config.GetIDFromField(conf.Images, name)
|
||||
if cv.Container.ImagePullPolicy != corev1.PullAlways {
|
||||
cv.addFailure(messages.ImagePullPolicyFailure, conf.Images.PullPolicyNotAlways, category, id)
|
||||
} else {
|
||||
cv.addSuccess(messages.ImagePullPolicySuccess, category, id)
|
||||
}
|
||||
}
|
||||
|
||||
name = "TagNotSpecified"
|
||||
if conf.IsActionable(conf.Images, name, controllerName) {
|
||||
id := config.GetIDFromField(conf.Images, name)
|
||||
img := strings.Split(cv.Container.Image, ":")
|
||||
if len(img) == 1 || img[1] == "latest" {
|
||||
cv.addFailure(messages.ImageTagFailure, conf.Images.TagNotSpecified, category, id)
|
||||
} else {
|
||||
cv.addSuccess(messages.ImageTagSuccess, category, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (cv *ContainerValidation) validateNetworking(conf *config.Configuration, controllerName string) {
|
||||
category := messages.CategoryNetworking
|
||||
|
||||
|
||||
@@ -528,7 +528,10 @@ func TestValidateImage(t *testing.T) {
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
tt.cv = resetCV(tt.cv)
|
||||
tt.cv.validateImage(&conf.Configuration{Images: tt.image}, "")
|
||||
err := applyContainerSchemaChecks(&conf.Configuration{Images: tt.image}, tt.cv.Container, "", conf.Deployments, tt.cv.IsInitContainer, &tt.cv)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
assert.Len(t, tt.cv.Errors, len(tt.expected))
|
||||
assert.ElementsMatch(t, tt.cv.Errors, tt.expected)
|
||||
})
|
||||
|
||||
@@ -55,6 +55,8 @@ var (
|
||||
// Container checks
|
||||
"readinessProbe",
|
||||
"livenessProbe",
|
||||
"pullPolicyNotAlways",
|
||||
"tagNotSpecified",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user