{{- if .Values.webhook.enabled }}
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  name: {{ .Release.Name }}-webhook
  annotations:
    cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/jenkins-{{ .Values.webhook.certificate.name }}
webhooks:
- admissionReviewVersions:
  - v1
  - v1beta1
  clientConfig:
    service:
      name: jenkins-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /validate-jenkins-io-v1alpha2-jenkins
  failurePolicy: Fail
  name: vjenkins.kb.io
  timeoutSeconds: 30
  rules:
  - apiGroups:
    - jenkins.io
    apiVersions:
    - v1alpha2
    operations:
    - CREATE
    - UPDATE
    resources:
    - jenkins
    scope: "Namespaced"
  sideEffects: None

---
apiVersion: v1
kind: Service
metadata:
  name: jenkins-webhook-service
  namespace: {{ .Release.Namespace }}
spec:
  ports:
    - port: 443
      targetPort: 9443
  selector:
    app.kubernetes.io/name: {{ include "jenkins-operator.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
---
{{- end }}