In Kubernetes, Admission Controllers enforce policies on objects during create, update, and delete operations. Admission control is fundamental to policy enforcement in Kubernetes.

For example, by deploying OPA as an admission controller you can:

  • Require specific labels on all resources.
  • Require container images come from the corporate image registry.
  • Require all Pods specify resource requests and limits.
  • Prevent conflicting Ingress objects from being created.

Admission controllers can also mutate incoming objects. By deploying OPA as a mutating admission controller you can:

  • Inject sidecar containers into Pods.
  • Set specific annotations on all resources.
  • Rewrite container images to point at the corporate image registry.
  • Include node and pod (anti-)affinity selectors on Deployments.

These are just examples of policies you can enforce with admission controllers and OPA. There are dozens of other policies you will want to enforce in your Kubernetes clusters for security, cost, and availability reasons.

What is OPA Gatekeeper?

OPA Gatekeeper is a new project that provides first-class integration between OPA and Kubernetes. For background information see this blog post on kubernetes.io and check out this Katakoda tutorial.

OPA Gatekeeper adds the following on top of plain OPA:

  • An extensible, parameterized policy library.
  • Native Kubernetes CRDs for instantiating the policy library (aka “constraints”).
  • Native Kubernetes CRDs for extending the policy library (aka “constraint templates”).
  • Audit functionality.

If you want to kick the tires:

Recommendation: OPA Gatekeeper is currently in beta. If you are getting started with admission control, we recommend you try it out.

How Does It Work With Plain OPA and Kube-mgmt?

The Kubernetes API Server is configured to query OPA for admission control decisions when objects (e.g., Pods, Services, etc.) are created, updated, or deleted.

Overview & Architecture - 图1

Admission Control Flow

The API Server sends the entire Kubernetes object in the webhook request to OPA. OPA evaluates the policies it has loaded using the admission review as input. For example, the following policy denies objects that include container images referring to illegal registries:

  1. package kubernetes.admission
  2. deny[reason] {
  3. some container
  4. input_containers[container]
  5. not startswith(container.image, "hooli.com/")
  6. reason := "container image refers to illegal registry (must be hooli.com)"
  7. }
  8. input_containers[container] {
  9. container := input.request.object.spec.containers[_]
  10. }
  11. input_containers[container] {
  12. container := input.request.object.spec.template.spec.containers[_]
  13. }

When deny is evaluated with the input defined below the answer is:

  1. deny
  1. [
  2. "container image refers to illegal registry (must be hooli.com)"
  3. ]

The input document contains the following fields:

  • input.request.kind specifies the type of the object (e.g., Pod, Service, etc.)
  • input.request.operation specifies the type of the operation, i.e., CREATE, UPDATE, DELETE, CONNECT.
  • input.request.userInfo specifies the identity of the caller.
  • input.request.object contains the entire Kubernetes object.
  • input.request.oldObject specifies the previous version of the Kubernetes object on UPDATE and DELETE.

Here is an example of a Pod being created:

  1. {
  2. "kind": "AdmissionReview",
  3. "apiVersion": "admission.k8s.io/v1beta1",
  4. "request": {
  5. "kind": {
  6. "group": "",
  7. "version": "v1",
  8. "kind": "Pod"
  9. },
  10. "resource": {
  11. "group": "",
  12. "version": "v1",
  13. "resource": "pods"
  14. },
  15. "namespace": "opa-test",
  16. "operation": "CREATE",
  17. "userInfo": {
  18. "username": "system:serviceaccount:kube-system:replicaset-controller",
  19. "uid": "439dea65-3e4e-4fa8-b5f8-8fdc4bc7cf53",
  20. "groups": [
  21. "system:serviceaccounts",
  22. "system:serviceaccounts:kube-system",
  23. "system:authenticated"
  24. ]
  25. },
  26. "object": {
  27. "apiVersion": "v1",
  28. "kind": "Pod",
  29. "metadata": {
  30. "creationTimestamp": "2019-08-13T16:01:54Z",
  31. "generateName": "nginx-7bb7cd8db5-",
  32. "labels": {
  33. "pod-template-hash": "7bb7cd8db5",
  34. "run": "nginx"
  35. },
  36. "name": "nginx-7bb7cd8db5-dbplk",
  37. "namespace": "opa-test",
  38. "ownerReferences": [
  39. {
  40. "apiVersion": "apps/v1",
  41. "blockOwnerDeletion": true,
  42. "controller": true,
  43. "kind": "ReplicaSet",
  44. "name": "nginx-7bb7cd8db5",
  45. "uid": "7b6a307f-d9b4-4b65-a916-5d0b96305e87"
  46. }
  47. ],
  48. "uid": "266d2c8b-e43e-42d9-a19c-690bb6103900"
  49. },
  50. "spec": {
  51. "containers": [
  52. {
  53. "image": "nginx",
  54. "imagePullPolicy": "Always",
  55. "name": "nginx",
  56. "resources": {},
  57. "terminationMessagePath": "/dev/termination-log",
  58. "terminationMessagePolicy": "File",
  59. "volumeMounts": [
  60. {
  61. "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
  62. "name": "default-token-6h4dn",
  63. "readOnly": true
  64. }
  65. ]
  66. }
  67. ],
  68. "dnsPolicy": "ClusterFirst",
  69. "enableServiceLinks": true,
  70. "priority": 0,
  71. "restartPolicy": "Always",
  72. "schedulerName": "default-scheduler",
  73. "securityContext": {},
  74. "serviceAccount": "default",
  75. "serviceAccountName": "default",
  76. "terminationGracePeriodSeconds": 30,
  77. "tolerations": [
  78. {
  79. "effect": "NoExecute",
  80. "key": "node.kubernetes.io/not-ready",
  81. "operator": "Exists",
  82. "tolerationSeconds": 300
  83. },
  84. {
  85. "effect": "NoExecute",
  86. "key": "node.kubernetes.io/unreachable",
  87. "operator": "Exists",
  88. "tolerationSeconds": 300
  89. }
  90. ],
  91. "volumes": [
  92. {
  93. "name": "default-token-6h4dn",
  94. "secret": {
  95. "secretName": "default-token-6h4dn"
  96. }
  97. }
  98. ]
  99. },
  100. "status": {
  101. "phase": "Pending",
  102. "qosClass": "BestEffort"
  103. }
  104. },
  105. "oldObject": null
  106. }
  107. }

The policies you give to OPA ultimately generate an admission review response that is sent back to the API Server. Here is an example of the policy decision sent back to the API Server.

  1. {
  2. "kind": "AdmissionReview",
  3. "apiVersion": "admission.k8s.io/v1beta1",
  4. "response": {
  5. "allowed": false,
  6. "status": {
  7. "reason": "container image refers to illegal registry (must be hooli.com)"
  8. }
  9. }
  10. }

The API Server implements a “deny overrides” conflict resolution strategy. If any admission controller denies the request, the request is denied (even if one of the later admission controllers were to allow the request.)

Policies can be loaded into OPA dynamically via ConfigMap objects using the kube-mgmt sidecar container. The kube-mgmt sidecar container can also load any other Kubernetes object into OPA as JSON under data. This lets you enforce policies that rely on an eventually consistent snapshot of the Kubernetes cluster as context.

Overview & Architecture - 图2

Policy and Data Caching

See the Policy Authoring and Tutorial: Ingress Validation pages for more details.

Additional Resources

See the following pages on kubernetes.io for more information on admission control: