# grafana_charts_prepare-downscale-webhook.yaml
{{- if .Values.webhooks.enabled -}}
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  name: prepare-downscale-{{ .Release.Namespace }}
  labels:
    grafana.com/inject-rollout-operator-ca: "true"
    grafana.com/namespace: {{ .Release.Namespace | quote }}
    {{- include "rollout-operator.labels" . | nindent 4 }}
webhooks:
  - name: prepare-downscale-{{ .Release.Namespace }}.grafana.com
    clientConfig:
      service:
        namespace: {{ .Release.Namespace | quote }}
        name: {{ include "rollout-operator.fullname" . }}
        path: /admission/prepare-downscale
        port: 443
    rules:
      - operations:
          - UPDATE
        apiGroups:
          - apps
        apiVersions:
          - v1
        resources:
          - statefulsets
          - statefulsets/scale
        scope: Namespaced
    admissionReviewVersions:
      - v1
    {{- if not (kindIs "invalid" .Values.namespaceSelector) }}
    namespaceSelector:
      {{- if .Values.namespaceSelector.matchLabels }}
      matchLabels:
        {{- toYaml .Values.namespaceSelector.matchLabels | nindent 8 }}
      {{- else }}
      matchLabels:
        kubernetes.io/metadata.name: {{ .Release.Namespace | quote }}
      {{- end }}
      {{- with .Values.namespaceSelector.matchExpressions }}
      matchExpressions:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- end }}
    {{- with .Values.webhooks.objectSelector }}
    objectSelector:
      {{- toYaml . | nindent 6 }}
    {{- end }}
    sideEffects: NoneOnDryRun
    matchPolicy: Equivalent
    timeoutSeconds: {{.Values.webhooks.timeoutSeconds}}
    failurePolicy: {{.Values.webhooks.failurePolicy}}
{{- end -}}