我正在考虑创建一个自定义 Azure policy用于 AKS 部署。有很多可用的内置策略:https://learn.microsoft.com/en-us/azure/aks/policy-reference但我还没有找到适合这种特殊情况的。
我们想要检查每个 Pod 是否正在运行 2 个副本。为此,我刚刚使用现有的内置策略作为模板。当我有以下策略规则时,使用 rego 脚本将其部署为自定义策略,该脚本只会拒绝一切正常:
"policyRule": {
"if": {
"field": "type",
"in": [
"Microsoft.Kubernetes/connectedClusters",
"Microsoft.ContainerService/managedClusters"
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"templateInfo": {
"sourceType": "PublicURL",
"url": "<regoscript that denies everything>"
},
"apiGroups": [
""
],
"kinds": [
"Pod"
],
"excludedNamespaces": "[parameters('excludedNamespaces')]",
"namespaces": "[parameters('namespaces')]",
"labelSelector": "[parameters('labelSelector')]",
"values": {
"excludedContainers": "[parameters('excludedContainers')]",
"excludedImages": "[parameters('excludedImages')]"
}
}
}
}
但是,一旦我改变了
"kinds": [
"Pod"
],
至
"kinds": [
"Deployment"
],
它停止执行任何操作。 rego 脚本打印它收到的原始对象,因此 Deployment 似乎没有匹配任何逻辑。为什么会这样?部署应该是 k8s 中的有效对象:https://kubernetes.io/docs/concepts/overview/working-with-objects/#required-fields
最佳答案
我们在 OPA Slack 中解决了这个问题,但为了后代:当 kinds
包含 时,
。apiGroups
属性必须包含 apps
>部署
关于azure - 如何为 k8s 部署创建自定义 Azure 策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76515857/