Back to Policies

Restrict Ingress defaultBackend in ValidatingPolicy

An Ingress with no rules sends all traffic to a single default backend. The defaultBackend is conventionally a configuration option of the Ingress controller and is not specified in your Ingress resources. If none of the hosts or paths match the HTTP request in the Ingress objects, the traffic is routed to your default backend. In a multi-tenant environment, you want users to use explicit hosts, they should not be able to overwrite the global default backend service. This policy prohibits the use of the defaultBackend field.

View on GitHub

Policy Definition

apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: restrict-ingress-defaultbackend
annotations:
policies.kyverno.io/title: Restrict Ingress defaultBackend in ValidatingPolicy
policies.kyverno.io/category: Best Practices in vpol
policies.kyverno.io/severity: high
kyverno.io/kyverno-version: 1.14.0
policies.kyverno.io/minversion: 1.14.0
kyverno.io/kubernetes-version: "1.30"
policies.kyverno.io/subject: Ingress
policies.kyverno.io/description: An Ingress with no rules sends all traffic to a single default backend. The defaultBackend is conventionally a configuration option of the Ingress controller and is not specified in your Ingress resources. If none of the hosts or paths match the HTTP request in the Ingress objects, the traffic is routed to your default backend. In a multi-tenant environment, you want users to use explicit hosts, they should not be able to overwrite the global default backend service. This policy prohibits the use of the defaultBackend field.
spec:
validationActions:
- Audit
evaluation:
background:
enabled: true
matchConstraints:
resourceRules:
- apiGroups:
- networking.k8s.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- ingresses
validations:
- expression: "!has(object.spec.defaultBackend)"
message: Setting the defaultBackend field is prohibited.

Related Policies