I want to forbid the creation of Pods in Namespaces which do not have a ResourceQuota. If possible, I want Gatekeeper to ensure that there is a ResourceQuota which sets limits.cpu
and limits.memory
before allowing the creation of Pods.
I have created below configurations, but they have not solved my problem:
template
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: k8sresoucequota
spec:
crd:
spec:
names:
kind: k8sResouceQuota
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8sresoucequota
violation[{"msg": msg}] {
input.request.kind.kind == "Pod"
requestns := input.request.object.metadata.namespace
existingrqs := {e | e := data.inventory.namespace[requestns]["v1beta1"]["ResourceQuota"].metadata.name}
not ns_exists(requestns,existingrqs)
msg := sprintf("container <%v> could not be created because the <%v> namespace does not have ResourceQuotas defined", [input.request.object.metadata.name,input.request.object.metadata.namespace])
}
ns_exists(ns,arr) {
arr[_] = ns
}
Constraint
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: k8sResouceQuota
metadata:
name: namespace-must-have-resourcequota
spec:
match:
kinds:
- apiGroups: [""]
kinds: ["Pod"]
excludedNamespaces:
- kube-system
- kube-public
- kube-node-lease
- default
- gatekeeper-system
- kubernetes-dashboard
sync.yaml
apiVersion: config.gatekeeper.sh/v1alpha1
kind: Config
metadata:
name: config
namespace: "gatekeeper-system"
spec:
sync:
syncOnly:
- group: ""
version: "v1beta1"
kind: "Pod"
- group: ""
version: "v1beta1"
kind: "Namespace"
- group: ""
version: "v1beta1"
kind: "ResourceQuota"