Services select eligible Pods by way of label matches. Having multiple Service apply based on same labels can cause conflicts and have unintended consequences. This policy ensures that within the same Namespace a Service has a unique set of labels as a selector.
apiVersion: kyverno.io/v1kind: ClusterPolicymetadata:name: require-unique-service-selectorannotations:policies.kyverno.io/title: Require Unique Service Selectorpolicies.kyverno.io/category: Otherkyverno.io/kyverno-version: 1.9.0kyverno.io/kubernetes-version: "1.24"policies.kyverno.io/subject: Servicepolicies.kyverno.io/description: Services select eligible Pods by way of label matches. Having multiple Service apply based on same labels can cause conflicts and have unintended consequences. This policy ensures that within the same Namespace a Service has a unique set of labels as a selector.spec:validationFailureAction: Auditbackground: falserules:- name: check-service-selectormatch:any:- resources:kinds:- Servicepreconditions:all:- key: "{{request.operation || 'BACKGROUND'}}"operator: NotEqualsvalue: DELETEcontext:- name: servicesapiCall:urlPath: /api/v1/namespaces/{{request.namespace}}/servicesjmesPath: items[?spec.selector]- name: service_countvariable:jmesPath: services[?label_match(spec.selector, `{{request.object.spec.selector}}`)] | length(@)validate:message: There is already a matching selector for this Service.deny:conditions:any:- key: "{{service_count}}"operator: GreaterThanvalue: 0
This policy performs some best practices validation on Application fields. Path or chart must be specified but never both. And destination.name or destination.server must be specified but never both.
This policy prevents the use of the default project in an Application.
This policy prevents updates to the project field after an Application is created.