apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "rtfs.fullname" . }}
  labels:
    {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.deploymentLabels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.deploymentLabels "context" $) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- else }}
  # When we enable rollout, we will want to remove all pods managed by deployment
  # So we will have to force it to scale to 0
  # Deployment will be there only for the template
  # First step will be to have pods managed by rollout and deployment side by side.
  # Then on second upgrade scale down deployment and keep only pods managed by rollout
  {{- if index .Values "jfrog-argo-rollouts" "rolloutStrategy" "enabled" }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- end }}
  strategy:
    type: "RollingUpdate"
  selector:
    matchLabels:
      {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      {{- if or .Values.commonAnnotations .Values.podAnnotations }}
      annotations:
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "rtfs.podAnnotations" . | nindent 8 }}
        {{- end }}
      {{- end }}
      labels:
        {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- if or .Values.imagePullSecrets .Values.global.imagePullSecrets }}
      {{- include "rtfs.imagePullSecrets" . | indent 6 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      initContainers:
        - name: 'copy-configs'
          image: {{ include "rtfs.initContainers.image" . }}
          imagePullPolicy: {{ .Values.initContainers.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.initContainers.resources }}
          resources: {{- toYaml .Values.initContainers.resources | nindent 12 }}
          {{- end }}
          command:
            - '/bin/bash'
            - '-c'
            - >
              echo "Copy system.yaml to {{ .Values.persistence.mountPath }}/etc";
              mkdir -p {{ .Values.persistence.mountPath }}/etc;
              cp -fv /tmp/etc/system.yaml {{ .Values.persistence.mountPath }}/etc/system.yaml;
              echo "Copying artifactory-federation properties to {{ .Values.persistence.mountPath }}/etc";
              mkdir -p {{ .Values.persistence.mountPath }}/etc;
              echo "Starting to copy files to artifactory-federation.properties";
              found=false;
              for file in /tmp/etc/properties/*; do
                  if [ -f "$file" ]; then
                      found=true;
                      key=$(basename "$file");
                      value=$(cat "$file");
                      echo "Processing file: $file";
                      echo "$key=$value" >> {{ .Values.persistence.mountPath }}/etc/artifactory-federation.properties;
                  fi;
              done;
              if [ "$found" = false ]; then
                  echo "No matching files found, creating an empty artifactory-federation.properties file";
                  touch {{ .Values.persistence.mountPath }}/etc/artifactory-federation.properties;
              fi
          volumeMounts:
            - name: data
              mountPath: {{ .Values.persistence.mountPath | quote }}
            - name: systemyaml
              mountPath: /tmp/etc
            {{- if or .Values.persistence.federationProperties .Values.persistence.customFederationPropertiesConfig }}
            - name: federationproperties
              mountPath: /tmp/etc/properties
            {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: {{ include "rtfs.image" . }}
          imagePullPolicy: {{ .Values.initContainers.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.persistence.mountPath | quote }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
          env:
            - name: JF_SHARED_NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.javaOpts }}
            - name: EXTRA_JAVA_OPTS
              value: {{ .Values.javaOpts }}
            {{- end }}
            {{- if .Values.database.url }}
            - name: JF_RTFS_DATABASE_URL
              value: {{ .Values.database.url }}
            {{- end }}
            {{- if .Values.database.username }}
            - name: JF_RTFS_DATABASE_USERNAME
              value: {{ .Values.database.username }}
            {{- end }}
            {{- if .Values.database.password }}
            - name:  JF_RTFS_DATABASE_PASSWORD
              value: {{ .Values.database.password }}
            {{- end }}
            {{- if or .Values.masterKey .Values.global.masterKey .Values.masterKeySecretName .Values.global.masterKeySecretName }}
            - name: JF_SHARED_SECURITY_MASTERKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.masterKeySecretName" . }}
                  key: master-key
            {{- end }}
            {{- if or .Values.joinKey .Values.global.joinKey .Values.joinKeySecretName .Values.global.joinKeySecretName }}
            - name: JF_SHARED_SECURITY_JOINKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.joinKeySecretName" . }}
                  key: join-key
            {{- end }}
{{- with .Values.extraEnvironmentVariables }}
{{ tpl (toYaml .) $ | indent 12 }}
{{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.startupProbe.config "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.livenessProbe.config "context" $) | nindent 12 }}
          {{- end }}
          {{- with .Values.lifecycle }}
          lifecycle:
{{ toYaml . | indent 12 }}
          {{- end }}
        - name: router
          image: {{ template "router.image" . }}
          imagePullPolicy: {{ .Values.router.image.imagePullPolicy }}
          resources: {{- toYaml .Values.router.resources | nindent 12 }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          command:
            - '/bin/bash'
            - '-c'
            - >
              exec /opt/jfrog/router/app/bin/entrypoint-router.sh
          env:
            - name: JF_ROUTER_TOPOLOGY_LOCAL_REQUIREDSERVICETYPES
              value: {{ include "rtfs.router.requiredServiceTypes" . }}
            - name: JF_SHARED_JFROGURL
              value: {{ include "rtfs.jfrogUrl" . }}
            {{- if or .Values.masterKey .Values.global.masterKey .Values.masterKeySecretName .Values.global.masterKeySecretName }}
            - name: JF_SHARED_SECURITY_MASTERKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.masterKeySecretName" . }}
                  key: master-key
            {{- end }}
            {{- if or .Values.joinKey .Values.global.joinKey .Values.joinKeySecretName .Values.global.joinKeySecretName }}
            - name: JF_SHARED_SECURITY_JOINKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.joinKeySecretName" . }}
                  key: join-key
            {{- end }}
{{- with .Values.router.extraEnvironmentVariables }}
{{ tpl (toYaml .) $ | indent 12 }}
{{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.router.externalPort }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.router.persistence.mountPath | quote }}
            {{- if .Values.router.extraVolumeMounts }}
            {{- toYaml .Values.router.extraVolumeMounts | nindent 12 }}
            {{- end }}
            {{- if .Values.router.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.router.readinessProbe.config "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.router.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.router.livenessProbe.config "context" $) | nindent 12 }}
            {{- end }}
            {{- with .Values.router.lifecycle }}
          lifecycle:
{{ toYaml . | indent 12 }}
            {{- end }}
        {{- if .Values.observability.enabled }}
        - name: observability
          image: {{ template "observability.image" . }}
          imagePullPolicy: {{ .Values.observability.image.pullPolicy | quote }}
          resources: {{- toYaml .Values.observability.resources | nindent 12 }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          env:
            - name: JF_SHARED_NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: JF_SHARED_PLATFORMSERVICES_ENABLED
              value: "true"
            {{- if or .Values.masterKey .Values.global.masterKey .Values.masterKeySecretName .Values.global.masterKeySecretName }}
            - name: JF_SHARED_SECURITY_MASTERKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.masterKeySecretName" . }}
                  key: master-key
            {{- end }}
            {{- if or .Values.joinKey .Values.global.joinKey .Values.joinKeySecretName .Values.global.joinKeySecretName }}
            - name: JF_SHARED_SECURITY_JOINKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "rtfs.joinKeySecretName" . }}
                  key: join-key
            {{- end }}
{{- with .Values.observability.extraEnvironmentVariables }}
{{ tpl (toYaml .) $ | indent 12 }}
{{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.observability.persistence.mountPath }}
              {{- if .Values.observability.persistence.subPath }}
              subPath: {{ .Values.observability.persistence.subPath }}
              {{- end }}
            {{- if .Values.observability.extraVolumeMounts }}
            {{- toYaml .Values.observability.extraVolumeMounts | nindent 12 }}
            {{- end }}
            {{- if .Values.observability.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.observability.startupProbe.config "context" $) | nindent 12 }}
            {{- end }}
            {{- if .Values.observability.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.observability.livenessProbe.config "context" $) | nindent 12 }}
            {{- end }}
            {{- with .Values.observability.lifecycle }}
          lifecycle:
{{ toYaml . | indent 12 }}
            {{- end }}
        {{- end }}
      volumes:
        - name: data
          emptyDir: {}
        - name: systemyaml
          secret:
            secretName: {{ printf "%s-%s" (include "rtfs.fullname" .) "systemyaml" }}
        {{- if and (.Values.persistence.federationProperties) (not .Values.persistence.customFederationPropertiesConfig) }}
        - name: federationproperties
          configMap:
            name: {{ include "rtfs.fullname" . }}-properties
        {{- end }}
        {{- if .Values.persistence.customFederationPropertiesConfig }}
        - name: federationproperties
          configMap:
            name: {{ .Values.persistence.customFederationPropertiesConfig }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
