Skip to content

Commit

Permalink
move networking checks over to json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rbren committed Dec 23, 2019
1 parent 30b49c4 commit 3fa627a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
24 changes: 24 additions & 0 deletions checks/hostPortSet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: HostPortSet
id: hostPortSet
successMessage: Host port is not configured
failureMessage: Host port should not be configured
category: Networking
controllers:
exclude:
- Job
- CronJob
containers:
exclude:
- initContainer
target: Container
schema:
'$schema': http://json-schema.org/draft-07/schema
type: object
required:
properties:
ports:
type: array
items:
properties:
hostPort:
const: 0
23 changes: 0 additions & 23 deletions pkg/validator/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func ValidateContainer(container *corev1.Container, parentPodResult *PodResult,
panic(err)
}

cv.validateNetworking(conf, controllerName)
cv.validateSecurity(conf, controllerName)

cRes := ContainerResult{
Expand Down Expand Up @@ -155,28 +154,6 @@ func (cv *ContainerValidation) validateResourceRange(id, resourceName string, ra
}
}

func (cv *ContainerValidation) validateNetworking(conf *config.Configuration, controllerName string) {
category := messages.CategoryNetworking

name := "HostPortSet"
if conf.IsActionable(conf.Networking, name, controllerName) {
hostPortSet := false
for _, port := range cv.Container.Ports {
if port.HostPort != 0 {
hostPortSet = true
break
}
}

id := config.GetIDFromField(conf.Networking, name)
if hostPortSet {
cv.addFailure(messages.HostPortFailure, conf.Networking.HostPortSet, category, id)
} else {
cv.addSuccess(messages.HostPortSuccess, category, id)
}
}
}

func (cv *ContainerValidation) validateSecurity(conf *config.Configuration, controllerName string) {
category := messages.CategorySecurity
securityContext := cv.Container.SecurityContext
Expand Down
5 changes: 4 additions & 1 deletion pkg/validator/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,10 @@ func TestValidateNetworking(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tt.cv = resetCV(tt.cv)
tt.cv.validateNetworking(&conf.Configuration{Networking: tt.networkConf}, "")
err := applyContainerSchemaChecks(&conf.Configuration{Networking: tt.networkConf}, tt.cv.Container, "", conf.Deployments, tt.cv.IsInitContainer, &tt.cv)
if err != nil {
panic(err)
}
assert.Len(t, tt.cv.messages(), len(tt.expectedMessages))
assert.ElementsMatch(t, tt.cv.messages(), tt.expectedMessages)
})
Expand Down
1 change: 1 addition & 0 deletions pkg/validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
"livenessProbe",
"pullPolicyNotAlways",
"tagNotSpecified",
"hostPortSet",
}
)

Expand Down

0 comments on commit 3fa627a

Please sign in to comment.