Back to Policies

Restrict Ingress Classes in ValidatingPolicy

Ingress classes should only be allowed which match up to deployed Ingress controllers in the cluster. Allowing users to define classes which cannot be satisfied by a deployed Ingress controller can result in either no or undesired functionality. This policy checks Ingress resources and only allows those which define `HAProxy` or `nginx` in the respective annotation. This annotation has largely been replaced as of Kubernetes 1.18 with the IngressClass resource.

View on GitHub

Policy Definition

apiVersion: policies.kyverno.io/v1alpha1
kind: ValidatingPolicy
metadata:
name: restrict-ingress-classes
annotations:
policies.kyverno.io/title: Restrict Ingress Classes in ValidatingPolicy
policies.kyverno.io/category: Sample in Vpol
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Ingress
policies.kyverno.io/minversion: 1.14.0
kyverno.io/kubernetes-version: "1.30"
policies.kyverno.io/description: Ingress classes should only be allowed which match up to deployed Ingress controllers in the cluster. Allowing users to define classes which cannot be satisfied by a deployed Ingress controller can result in either no or undesired functionality. This policy checks Ingress resources and only allows those which define `HAProxy` or `nginx` in the respective annotation. This annotation has largely been replaced as of Kubernetes 1.18 with the IngressClass resource.
spec:
validationActions:
- Audit
evaluation:
background:
enabled: true
matchConstraints:
resourceRules:
- apiGroups:
- networking.k8s.io
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- ingresses
validations:
- expression: object.metadata.?annotations[?'kubernetes.io/ingress.class'].orValue('') in ['HAProxy', 'nginx']
message: Unknown ingress class.

Related Policies