Understanding the Risk
Consider the following pod definition file. In the image field, a user can reference an image from any registry:Using Admission Controllers to Restrict Registries
One effective method is to use Kubernetes admission controllers. When a pod creation request is made, it is processed through several stages: authentication, authorization, and admission control. By deploying a validating admission webhook server, you can inspect each incoming request and verify that the container image originates from an approved registry. If not, the webhook will reject the request with a clear error message. For example, consider the following Python code snippet that demonstrates an admission webhook allowing only images from “internal-registry.io”:Ensure that your validating webhook server is highly available. This prevents disruptions in pod creation if the webhook becomes unreachable.
Implementing Policies with OPA and Rego
An alternative approach is to deploy Open Policy Agent (OPA) with a validating webhook. By leveraging OPA’s Rego language, you can write custom policies that allow container images only from trusted registries. The example below denies any image that does not begin with “internal-registry.io/“:Using the Built-In ImagePolicyWebhook Admission Controller
The Kubernetes API server includes a built-in admission controller called ImagePolicyWebhook. This controller works with an external webhook server to enforce image policy rules using an admission configuration file. The diagram below illustrates the Kubernetes admission control process. It covers the steps from executingkubectl commands through authentication, authorization, and admission controller validations, culminating in pod creation:

Admission Configuration File
An admission configuration file provides the necessary details for connecting to the webhook server. It includes a reference to a KubeConfig file for authentication credentials along with parameters such as TTLs, retry backoff intervals, and default behaviors. For example:If the admission webhook server is unreachable, setting
defaultAllow: true permits pod creation unless the webhook explicitly denies it. Adjust this setting based on your security requirements.