# 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.

{{/* Validate installation to prevent conflicts */}}
{{- include "dynamo-operator.validateClusterWideInstallation" . -}}
{{- include "dynamo-operator.validateConfiguration" . -}}
{{- include "dynamo-operator.validateDiscoveryBackend" . -}}

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "dynamo-operator.fullname" . }}-controller-manager
  labels:
    app.kubernetes.io/component: manager
    app.kubernetes.io/created-by: dynamo-operator
    app.kubernetes.io/part-of: dynamo-operator
    control-plane: controller-manager
  {{- include "dynamo-operator.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.controllerManager.replicas }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      control-plane: controller-manager
    {{- include "dynamo-operator.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        control-plane: controller-manager
      {{- include "dynamo-operator.selectorLabels" . | nindent 8 }}
      annotations:
        kubectl.kubernetes.io/default-container: manager
        checksum/config: {{ include (print $.Template.BasePath "/operator-config.yaml") . | sha256sum }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.controllerManager.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.controllerManager.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.upgradeCRD }}
      initContainers:
      - name: crd-apply
        image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag | default .Chart.AppVersion }}
        imagePullPolicy: {{ .Values.controllerManager.manager.image.pullPolicy | default "IfNotPresent" }}
        securityContext: {{- toYaml .Values.controllerManager.manager.containerSecurityContext | nindent 10 }}
        command: ["/crd-apply"]
        args:
          - "--crds-dir=/opt/dynamo-operator/crds/"
          - "--version={{ .Chart.AppVersion }}"
      {{- end }}
      containers:
      - args: {{- toYaml .Values.controllerManager.kubeRbacProxy.args | nindent
          8 }}
        env:
        - name: KUBERNETES_CLUSTER_DOMAIN
          value: {{ quote .Values.kubernetesClusterDomain }}
        imagePullPolicy: {{ .Values.controllerManager.kubeRbacProxy.image.pullPolicy | quote }}
        image: {{ .Values.controllerManager.kubeRbacProxy.image.repository }}:{{
          .Values.controllerManager.kubeRbacProxy.image.tag | default .Chart.AppVersion
          }}
        name: kube-rbac-proxy
        ports:
        - containerPort: 8443
          name: https
          protocol: TCP
        resources: {{- toYaml .Values.controllerManager.kubeRbacProxy.resources
          | nindent 10 }}
        securityContext: {{- toYaml .Values.controllerManager.kubeRbacProxy.containerSecurityContext
          | nindent 10 }}
      - args:
        - --config=/etc/dynamo-operator/config.yaml
        - --operator-version={{ .Chart.AppVersion }}
        command:
        - /manager
        env:
        - name: KUBERNETES_CLUSTER_DOMAIN
          value: {{ quote .Values.kubernetesClusterDomain }}
        imagePullPolicy: {{ .Values.controllerManager.manager.image.pullPolicy | quote }}
        image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag
| default .Chart.AppVersion }}
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8081
          initialDelaySeconds: 15
          periodSeconds: 20
        name: manager
        ports:
        - containerPort: 9443
          name: webhook-server
          protocol: TCP
        readinessProbe:
          httpGet:
            path: /readyz
            port: 8081
          initialDelaySeconds: 5
          periodSeconds: 10
        resources: {{- toYaml .Values.controllerManager.manager.resources | nindent
          10 }}
        securityContext: {{- toYaml .Values.controllerManager.manager.containerSecurityContext
          | nindent 10 }}
        volumeMounts:
        - mountPath: /etc/dynamo-operator
          name: operator-config
          readOnly: true
        - mountPath: /tmp/k8s-webhook-server/serving-certs
          name: cert
          readOnly: true
      securityContext:
        runAsNonRoot: true
      serviceAccountName: {{ include "dynamo-operator.fullname" . }}-controller-manager
      terminationGracePeriodSeconds: 30
      volumes:
      - name: operator-config
        configMap:
          name: {{ include "dynamo-operator.fullname" . }}-config
      - name: cert
        secret:
          defaultMode: 420
          secretName: {{ .Values.webhook.certificateSecret.name }}
          # Allow the pod to start before the secret exists so the
          # in-operator cert-controller can bootstrap it at runtime.
          optional: true
