apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "metrics-server.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "metrics-server.labels" . | nindent 4 }}
  {{- with .Values.deploymentAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicas }}
  {{- with .Values.updateStrategy }}
  strategy:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "metrics-server.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "metrics-server.selectorLabels" . | nindent 8 }}
      {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      schedulerName: {{ .Values.schedulerName }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "metrics-server.serviceAccountName" . }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.priorityClassName }}
      priorityClassName: {{ . | quote }}
      {{- end }}
      {{- if .Values.hostNetwork.enabled }}
      hostNetwork: true
      {{- end }}
      containers:
        - name: metrics-server
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          image: {{ include "metrics-server.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - {{ printf "--secure-port=%d" (int .Values.containerPort) }}
          {{- range .Values.defaultArgs }}
            - {{ . }}
          {{- end }}
          {{- if .Values.metrics.enabled }}
            - --authorization-always-allow-paths=/metrics
          {{- end }}
          {{- range .Values.args }}
            - {{ . }}
          {{- end }}
          ports:
          - name: https
            protocol: TCP
            containerPort: {{ .Values.containerPort }}
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
          {{- with .Values.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- if .Values.addonResizer.enabled }}
        - name: metrics-server-nanny
          image: {{ include "metrics-server.addonResizer.image" . }}
          env:
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MY_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          command:
            - /pod_nanny
            - --config-dir=/etc/config
            - --deployment={{ include "metrics-server.fullname" . }}
            - --container=metrics-server
            - --threshold={{ .Values.addonResizer.nanny.threshold }}
            - --poll-period={{ .Values.addonResizer.nanny.pollPeriod }}
            - --estimator=exponential
            - --minClusterSize={{ .Values.addonResizer.nanny.minClusterSize }}
            - --use-metrics=true
          volumeMounts:
            - name: nanny-config-volume
              mountPath: /etc/config
          {{- with .Values.addonResizer.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
        {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
      {{- if .Values.addonResizer.enabled }}
        - name: nanny-config-volume
          configMap:
            name: {{ include "metrics-server.addonResizer.configMap" . }}
      {{- end }}
      {{- with .Values.extraVolumes }}
        {{- toYaml . | 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 }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
