OPA和Gatekeeper区别?

article/2025/9/24 1:31:16

Gaurav Chaware

Oct 27th, 2020

In the last couple of posts, I wrote about Open Policy Agent (OPA). People almost always ask one question  – what is the difference between OPA and Gatekeeper when it comes to Kubernetes admission control? And, generally, follow up with another question – so should I use Gatekeeper instead of OPA?

Admittedly, the OPA documentation does a nice job explaining OPA and its use cases. It also has a brief section about difference between Gatekeeper and OPA. But for those just getting started with OPA, this difference isn’t always clear.

So, here in this blog post, I will clarify the difference between OPA and Gatekeeper. Or, to be precise,  how Gatekeeper extends OPA.

Before we deep dive into differences, let’s make one thing clear. OPA is a general purpose policy engine. It has a number of use cases like API Authorization, SSH, Docker and more. Use of OPA is not tied to Kubernetes alone, neither Kubernetes is mandatory for using OPA. Kubernetes Admission Control is just one of OPA’s use cases. Please refer the OPA documentation for more use cases. And, note, this list is continuously expanding.

Gatekeeper on the other hand is specifically built for Kubernetes Admission Control use case of OPA. It uses OPA internally, but specifically for the Kubernetes admission control. So, we are going to focus only on this use case of OPA here.

Mandatory labels use case

Let’s consider you want to enforce a policy that pods running on your Kubernetes cluster must have appropriate labels. For e.g., a pod running in front-end namespace must have the app label with  one of  front-end or dashboard as value.

With OPA

The rego policy for this use case is as follows :

package k8s.labelsparameters.labels = {"front-end" : ["front-end", "dashboard"]}deny[msg] {namespace := input.request.namespacerequired := {label | label := parameters.labels[namespace][_]}provided := input.request.object.metadata.labels["app"]not required[provided]msg := sprintf("Missing required label %v",[required])
}

Now, this policy takes parameters.labels as the list of required labels per namespace. What happens when you want to add another namespace? Change the value for parameters.labels as {"front-end" : ["front-end", "dashboard"], "back-end" : ["reporting", "core"]}.

This requires the policy to be updated every time there is a change in labels or new namespace comes into picture. You can of course simplify the change management by using bundle APIs to centrally manage all the policies.

Now, let’s see how the same use case is solved by Gatekeeper.

With Gatekeeper

Gatekeeper uses OPA Constraint framework to define and enforce policies. To implement the labels use case, you will need to define a ConstraintTemplate and a Constraint using this template.

A ConstraintTemplate defines the policy that is used to enforce the constraint and also, the general schema of the constraint.  The ConstraintTemplate for our labels use case will be as follows :

apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:name: podrequiredlabels
spec:crd:spec:names:kind: PodRequiredLabelsvalidation:# Schema for the `parameters` fieldopenAPIV3Schema:properties:labels:type: arrayitems: stringtargets:- target: admission.k8s.gatekeeper.shrego: |package podrequiredlabelsviolation[{"msg": msg, "details": {"required_labels": required}}] {required := {label | label := input.parameters.labels[_]}provided := input.review.object.metadata.labels["app"]not required[provided]msg := sprintf("Missing required labels %v",[required])}

Now, using this ConstraintTemplate, create a Constraint as follows :

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: PodRequiredLabels
metadata:name: pod-required-labels
spec:match:namespaces: ["front-end"]kinds:- apiGroups: ["apps"]kinds: ["Deployment"]- apiGroupts: [""]kinds: ["Pod"]parameters:labels: ["front-end","dashboard"]

 

So, did you notice the difference?

Let’s see these in closer details now.

Firstly, notice how the parameters are defined. In OPA version, we added parameters to the policy. In Gatekeeper ConstraintTemplate, we defined the schema for parameters. And defined the actual parameter value in Constraint.  This allows one to use same template and define multiple constraints with different parameter values. So, for a new namespace, you simply need to define another Constraint as follows :

apiVersion: constraints.gatekeeper.sh/v1beta1
kind: PodRequiredLabels
metadata:name: pod-required-labels
spec:match:namespaces: ["back-end"]kinds:- apiGroups: ["apps"]kinds: ["Deployment"]- apiGroupts: [""]kinds: ["Pod"]parameters: labels: ["reporting","core"]

Notice also that you can define the scope of objects where  a Constraint will apply through match field.  The match field supports following matches : kindnamespacesexcludedNamespaceslabelSelectornamespaceSelector and scope.

So, you can define the template once and then, use multiple constraints with varying scopes to apply same policy with varying effect. With OPA version, you can write rego code to achieve the same.

Now that we have basic understanding of the different approaches, let’s take a look at other Gatekeeper and OPA differences.

Loading data and policies

Let’s see how policies and data is loaded in OPA and/or Gatekeeper.

OPA kube-mgmt integration

This is also Gatekeeper v1.0 – OPA as admission controller with kube-mgmt sidecar. Basically, kube-mgmt sidecar watches the various configured Kubernetes Objects. It loads any configmaps labeled with openpolicyagent.org/policy=rego into OPA as policies. It also loads any JSON from configmaps labeled with openpolicyagent.org/data=opa

OPA with kube-mgmt sidecar

In addition, kube-mgmt also loads various Kubernetes Objects in OPA as JSON. This is useful to make contextual decisions for scenarios where current state of objects in Kubernetes is necessary. An example being a policy to enforce unique ingress hostnames.

As an alternative to loading policies through configmaps, you can use bundle APIs. Here, you can manage the policies at a central place, say an S3 bucket, and configure OPA to periodically download the updated policies and data. This is a powerful feature that allows you to centrally manage the policies and also, use distributed OPA deployments to enforce policies uniformly.

Gatekeeper evolved from this model, into its current v3.0 and higher.

Gatekeeper v3.0

With Gatekeeper v3.x,  integrates the admission controller with OPA constraint framework. You write policies as ConstraintTemplates. And then declaratively configure the policies through Constraints. So, compared to the kube-mgmt approach –

  1. Policies are defined as constraint templates & constraints instead of configmaps/remote bundles
  2. To sync Kubernetes Objects into OPA, use the sync config resource.

OPA Gatekeeper

Logging and Auditing

Both Gatekeeper and OPA kube-mgmt print logs to stdout. These can be forwarded to external log aggregation systems like ELK, Datadog etc.

For decision logging, you can configure OPA to send the decisions to an external HTTP server. You can then process these decision logs to configure alerts,  generate insights and so on.

Auditing existing objects with OPA kube-mgmt requires some work with policies and replication. Basically, you need to configure kube-mgmt to replicate the needed Kubernetes objects into OPA. Then, run the policies against these objects to generate compliance audit.

Whereas Gatekeeper provides an audit feature in-built. All violations are stored in the status field of the failed Constraint.

Dry-run

Gatekeeper provides a dry-run functionality which enables testing a Constraint on a live cluster without actually enforcing it. Set the enforcementAction: dryrun on the Constraint to enable dry-run. Default value is deny. So, when it is set to dryrun, the Constraint will be evaluated against live objects. But no request will be denied even if the Constraint fails. This allows you to test the Constraints without adversely impacting the cluster operations.

Mutating Webhook

You can deploy OPA kube-mgmt as both validating webhook as well as mutating webhook configurations. Whereas, Gatekeeper currently does not support mutating admission control scenarios. This is work in progress, and I hope it will be available soon with Gatekeeper.

Shift-left in the CICD toolchain

Using OPA as an admission controller, we can prevent any non-standard workloads from running in our Kubernetes cluster. But, we all want to catch the non-conformance as early as possible.  You can use OPA as a CLI tool to validate manifests during PR checks. Or even simply run OPA tests as a validation step in your deployment pipeline. Most importantly, you can use the same policies for these validations that will be enforced by OPA admission controller.  Also checkout Conftest which allows you to write tests for your configuration files using Rego policies.

With Gatekeeper, you can maintain the constraint templates and constraints in source control system and enable continuous deployment. To enable validation of your deployment manifests in the deployment pipeline, you will need to employ other tools like Konstraint.

Finally..

So, like we saw, Gatekeeper is specifically built for Kubernetes admission control use case of OPA. It uses OPA and provides ConstraintTemplates to declaratively configure the policies. It does add some nice features like auditing, dry run. And, also has some missing pieces like the mutating webhook support. You can always use OPA and Gatekeeper simultaneously to support both validating and mutating webhook scenarios.

How has your experience been with Gatekeeper or OPA? Please let me know your feedback or reach out for general discussions on gaurav@infracloud.io / @Gaurav on OPA slack / @GGCTwts on twitter.

References:

  1. OPA Documentation – https://www.openpolicyagent.org/docs/latest/
  2. Gatekeeper Documentation – https://github.com/open-policy-agent/gatekeeper
  3. OPA Gatekeeper: Policy and Governance for Kubernetes

http://chatgpt.dhexx.cn/article/A6OyCR3t.shtml

相关文章

OPA策略引擎用法实战

简介 官网介绍 OPA简单的说就是一个功能强大的策略规则引擎,开发的时候多少会遇到一些多样的规则配置,这些配置不足以写到数据库,所以都写到了代码中、配置文件中。项目做大的时候,如果需要修改规则,那么只能重新修改…

OPA 鉴权

Gatekeeper 是基于 OPA的一个 Kubernetes 策略解决方案,可替代PSP或者部分RBAC功能。 当在集群中部署了Gatekeeper组件,APIServer所有的创建、更新或者删除操作都会触发 Gatekeeper来处理,如果不满足策略则拒绝 OPA(Open Policy…

SAP ui5 单元测试框架 - OPA

Sent: Monday, July 4, 2016 6:41 PM 本地eclipse复现错误的步骤如下: 修改cus.crm.lead项目的pom.xml的参数如下: 1.10.0-SNAPSHOT -> 2.0.0-SNAPSHOT1.6.14 -> 1.11.0 然后,maven install 一下,run on server. 访问: localhost:8080/cus.crm.l…

81. 采用 OPA5 进行 SAP UI5 集成测试(Integration Test)的一个例子

SAP UI5 应用开发教程之八十一 - 采用 OPA5 进行 SAP UI5 集成测试(Integration Test)的一个例子 本教程之前第 28 个步骤曾经介绍过使用 OPA5 进行 SAP UI5 集成测试的知识。 SAP UI5 应用开发教程之二十八 - SAP UI5 应用的集成测试工具 OPA 介绍 本文通过一个实际的例子来…

82. 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例

SAP UI5 应用开发教程之八十二 - 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例 本教程的前一步骤,我们介绍了如何使用 OPA5 对一个包含表格控件的 SAP UI5 视图进行集成测试,即通过代码的方式,点击表格控件底部的 More 按钮,测试其是否按照我们期望的那样,加载更多…

SAP UI5 应用开发教程之八十二 - 采用 OPA5 开发支持页面跳转的 SAP UI5 集成测试用例试读版

一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP UI5 的引导过程 Bootstrap SAP UI5 应用开发教程之三:开始接触第一个 SAP UI5 控…

SAP UI5 应用开发教程之八十一 - 采用 OPA5 进行 SAP UI5 集成测试(Integration Test)的一个例子试读版

一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP UI5 的引导过程 Bootstrap SAP UI5 应用开发教程之三:开始接触第一个 SAP UI5 控…

SAP UI5 应用开发教程之八十五 - 如何用 OPA5 编写测试用例来测试用户输入文本的功能试读版

一套适合 SAP UI5 初学者循序渐进的学习教程 作者简介 Jerry Wang,2007 年从电子科技大学计算机专业硕士毕业后加入 SAP 成都研究院工作至今。Jerry 是 SAP 社区导师,SAP 中国技术大使。在长达 15 年的 SAP 标准产品开发生涯里,Jerry 曾经先…

85. 如何用 OPA5 编写测试用例来测试用户输入文本的功能

SAP UI5 应用开发教程之八十五 - 如何用 OPA5 编写测试用例来测试用户输入文本的功能 本教程之前的系列文章,我们已经学习了如何在测试用例里,用代码的方式,来模拟用户点击 SAP UI5 表格控件的 More 按钮: SAP UI5 应用开发教程之八十一 - 采用 OPA5 进行 SAP UI5 集成测…

java实习生面试被问的较多的面试题(附参考答案)

1.集合有哪些子类?各自的数据结构?有什么区别? 注:HashSet底层采用HashMap实现,TreeSet底层采用TreeMap实现 2.Hashmap如何解决哈希冲突?与HashTable有何不同? hash : 翻译为“散列”&#xff…

10道经典java面试题_实习生必问(java基础)

10道经典java面试题_实习生必问(java基础) 第一,谈谈final, finally, finalize的区别。 final?修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因…

2020 java实习生面试题总结

2020java实习面试题总结: 本人是广州某高校大四的一名学生,下面是12月份的面试总结 一)hr的提问: 1.自我介绍(必须的) HR的关注点: 例子: 本人就读于xxx学校,xxx专业…

java实习生面试题_java实习生面试题大全(2019年整理)

java实习生面试题大全(2019年整理) 标准SQL语法及语句 一道关于group bySQL 语句面试题 表中有A B C 三列,用SQL 语句实现:当A 列大于B 列时选择A 列否则选择B 列,当B列大于C列时 选择B 列否则选择C 列 请用一条sql 语句查询出这三条记录并按以下条件显示…

java实习生面试题_java实习生面试题(含答案)

1.Java容器框架有哪些? Java容器框架中有两个名称分别为Collection和Set的接口 2.list,map,set,array,它们有什么区别 (推荐学习:java实习生面试题) List接口主要有三个实现类:LinkedList,ArrayList,Vector. LinkedList:底层基于链表实现,链表内存是散乱的,每一个元素存储本…

java实习生面试题

文章目录 一、数据结构与算法1)、数据结构2)、算法1. 排序2. 查找 二、java基础线程java基础 and equals() 1)、创建和启用多线程1.多线程安全的类 2)、死锁3)、避免(预防)和解决死锁1.死锁预防2.解决方法 三、Mysql优…

20道Java实习生笔试面试选择题(内附答案解析)

1、以下对继承的描述错误的是(A) A.Java中的继承允许一个子类继承多个父类 B.父类更具有通用性,子类更具体 C.Java中的继承存在的传递性 D.当实例化子类时会递归调用父类中的构造方法 解析:众所周知,JAVA类只支持…

Java实习生面试题汇总

Java实习生面试题汇总 简介 本人是二本大三学生,下半年大四。暑假在上海这边找实习工作,面了几家公司,所问到的问题记录在下面。 因为是在校生,没任何实习经历,一般找我面试的都是小公司,一般问的比较简…

java实习面试题整理

java实习面试题整理 1.栈(stack)和堆(heap)的区别1.和equals的区别1.throw和throws的区别1.cookie 和session 的区别1.final, finally, finalize的区别1.什么是多态1.接口的概念与特性1.内部类的概念与优点1.Static关键字1.This和…

【2022版】Dubbo面试题整理(含答案解析)

1、为什么要用 Dubbo? 随着服务化的进一步发展,服务越来越多,服务之间的调用和依赖关系也越来越复杂,诞生了面向服务的架构体系(SOA),也因此衍生出了一系列相应的技术,如对服务提供、服务调用、连接处理、…

Dubbo 面试题及答案

文章目录 1.Dubbo是什么?2.为什么要用Dubbo?3.Dubbo 和 Dubbox 有什么区别?4.Dubbo 停止维护了吗?5.你读过 Dubbo 的源码吗?6.在使用过程中都遇到了些什么问题?7.Dubbo和SpringCloud的区别?spri…