# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  {{- if .Values.namespaceRestriction.enabled }}
  name: {{ include "dynamo-operator.fullname" . }}-validating-{{ .Release.Namespace }}
  {{- else }}
  name: {{ include "dynamo-operator.fullname" . }}-validating
  {{- end }}
  labels:
    app.kubernetes.io/component: webhook
    app.kubernetes.io/created-by: dynamo-operator
    app.kubernetes.io/part-of: dynamo-operator
    nvidia.com/dynamo-operator-namespace: {{ .Release.Namespace }}
  {{- include "dynamo-operator.labels" . | nindent 4 }}
  {{- if .Values.webhook.certManager.enabled }}
  annotations:
    cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ include "dynamo-operator.fullname" . }}-serving-cert
  {{- end }}
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /validate-nvidia-com-v1alpha1-dynamocomponentdeployment
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: vdynamocomponentdeployment.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1alpha1
    operations:
    - CREATE
    - UPDATE
    resources:
    - dynamocomponentdeployments
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /validate-nvidia-com-v1alpha1-dynamographdeployment
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: vdynamographdeployment.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1alpha1
    operations:
    - CREATE
    - UPDATE
    resources:
    - dynamographdeployments
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /validate-nvidia-com-v1alpha1-dynamomodel
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: vdynamomodel.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1alpha1
    operations:
    - CREATE
    - UPDATE
    resources:
    - dynamomodels
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /validate-nvidia-com-v1beta1-dynamographdeploymentrequest
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: vdynamographdeploymentrequest.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1beta1
    operations:
    - CREATE
    - UPDATE
    resources:
    - dynamographdeploymentrequests
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  {{- if .Values.namespaceRestriction.enabled }}
  name: {{ include "dynamo-operator.fullname" . }}-mutating-{{ .Release.Namespace }}
  {{- else }}
  name: {{ include "dynamo-operator.fullname" . }}-mutating
  {{- end }}
  labels:
    app.kubernetes.io/component: webhook
    app.kubernetes.io/created-by: dynamo-operator
    app.kubernetes.io/part-of: dynamo-operator
    nvidia.com/dynamo-operator-namespace: {{ .Release.Namespace }}
  {{- include "dynamo-operator.labels" . | nindent 4 }}
  {{- if .Values.webhook.certManager.enabled }}
  annotations:
    cert-manager.io/inject-ca-from: {{ .Release.Namespace }}/{{ include "dynamo-operator.fullname" . }}-serving-cert
  {{- end }}
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /mutate-nvidia-com-v1alpha1-dynamographdeployment
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: mdynamographdeployment.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1alpha1
    operations:
    - CREATE
    resources:
    - dynamographdeployments
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}
- admissionReviewVersions:
  - v1
  clientConfig:
    {{- if and (not .Values.webhook.certManager.enabled) .Values.webhook.certificateSecret.external }}
    {{- if .Values.webhook.caBundle }}
    caBundle: {{ .Values.webhook.caBundle }}
    {{- end }}
    {{- end }}
    service:
      name: {{ include "dynamo-operator.fullname" . }}-webhook-service
      namespace: {{ .Release.Namespace }}
      path: /mutate-nvidia-com-v1beta1-dynamographdeploymentrequest
  failurePolicy: {{ .Values.webhook.failurePolicy }}
  name: mdynamographdeploymentrequestv1beta1.kb.io
  {{- if .Values.webhook.namespaceSelector }}
  namespaceSelector:
    {{- toYaml .Values.webhook.namespaceSelector | nindent 4 }}
  {{- else if .Values.namespaceRestriction.enabled }}
  namespaceSelector:
    matchLabels:
      kubernetes.io/metadata.name: {{ .Release.Namespace }}
  {{- end }}
  rules:
  - apiGroups:
    - nvidia.com
    apiVersions:
    - v1beta1
    operations:
    - CREATE
    resources:
    - dynamographdeploymentrequests
  sideEffects: None
  timeoutSeconds: {{ .Values.webhook.timeoutSeconds }}

