{{- include "common.values.setup" . }}

{{/* Append the hardcoded settings */}}
{{- define "searx.harcodedValues" -}}
envFrom:
- secretRef:
    {{ if not .Values.searx.existingSecret }}
    name: {{ include "common.names.fullname" . }}-config
    {{ else }}
    name: {{ .Values.searx.existingSecret }}
    {{ end }}

persistence:
  searx-config:
    enabled: true
    type: "custom"
    mountPath: "-"
    volumeSpec:
      configMap:
        name: {{ printf "%v-config" (include "common.names.fullname" .) }}

additionalContainers:
  caddy:
    name: caddy
    image: "{{ .Values.caddy.repository }}:{{ .Values.caddy.tag }}"
    imagePullPolicy: {{ .Values.caddy.pullPolicy }}
    ports:
    - containerPort: {{ .Values.service.main.ports.http.port }}
      name: http
    volumeMounts:
    - name: searx-config
      mountPath: /etc/caddy/Caddyfile
      subPath: Caddyfile
    {{- with .Values.caddy.resources }}
    resources:
      {{- toYaml . | nindent 6 }}
    {{- end }}

  filtron:
    name: filtron
    image: "{{ .Values.filtron.repository }}:{{ .Values.filtron.tag }}"
    imagePullPolicy: {{ .Values.filtron.pullPolicy }}
    args:
      [
        "-listen",
        "0.0.0.0:4040",
        "-api",
        "0.0.0.0:4041",
        "-target",
        "localhost:8080",
      ]
    ports:
    - containerPort: 4040
      name: filtron
    - containerPort: 4041
      name: api
    volumeMounts:
    - name: searx-config
      mountPath: /etc/filtron/rules.json
      subPath: rules.json
    {{- with .Values.filtron.resources }}
    resources:
      {{- toYaml . | nindent 6 }}
    {{- end }}

  morty:
    name: morty
    image: "{{ .Values.morty.repository }}:{{ .Values.morty.tag }}"
    imagePullPolicy: {{ .Values.morty.pullPolicy }}
    envFrom:
    - secretRef:
        {{ if not .Values.searx.existingSecret }}
        name: {{ include "common.names.fullname" . }}-config
        {{ else }}
        name: {{ .Values.searx.existingSecret }}
        {{ end }}
    args: ["-listen", "localhost:3000", "-timeout", "6", "ipv6"]
    ports:
    - containerPort: 3000
      name: morty
    {{- with .Values.morty.resources }}
    resources:
      {{- toYaml . | nindent 6 }}
    {{- end }}
{{- end -}}
{{- $_ := mergeOverwrite .Values (include "searx.harcodedValues" . | fromYaml) -}}

{{/* Render the templates */}}
{{ include "common.all" . }}
