Back to Policies

Block Large Images

Pods which run containers of very large image size take longer to pull and require more space to store. A user may either inadvertently or purposefully name an image which is unusually large to disrupt operations. This policy checks the size of every container image and blocks if it is over 2 Gibibytes.

View on GitHub

Policy Definition

apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: block-large-images
annotations:
policies.kyverno.io/title: Block Large Images
policies.kyverno.io/category: Other
policies.kyverno.io/severity: medium
policies.kyverno.io/minversion: 1.15.0
policies.kyverno.io/subject: Pod
policies.kyverno.io/description: Pods which run containers of very large image size take longer to pull and require more space to store. A user may either inadvertently or purposefully name an image which is unusually large to disrupt operations. This policy checks the size of every container image and blocks if it is over 2 Gibibytes.
spec:
evaluation:
background:
enabled: true
validationActions:
- Audit
variables:
- name: allContainers
expression: object.spec.containers + object.spec.?initContainers.orValue([]) + object.spec.?ephemeralContainers.orValue([])
- name: maxSizeBytes
expression: "2147483648"
matchConstraints:
resourceRules:
- resources:
- pods
operations:
- CREATE
- UPDATE
apiGroups:
- ""
apiVersions:
- v1
validations:
- message: images with size greater than 2Gi not allowed
expression: variables.allContainers.all(container, image.GetMetadata(container.image).manifest.layers.map(layer, layer.size).sum() <= variables.maxSizeBytes)

Related Policies