apiVersion: v1
kind: ConfigMap
metadata:
  name: es-console
data:
  default.conf: |-
    server {
      listen 8080;
      server_name localhost;
      set $base /usr/share/nginx/html;

      # use external resolver to lookup backends, cache for 30 seconds

      resolver 127.0.0.1:5353 ipv6=off valid=30s;

      set $prometheus "prometheus-server";
      set $monitorapi "es-monitor-api";
      set $grafana "grafana-server";
      set $alertmanager {{ splitList "," .Values.alertManagers | first | quote }};

      # nginx config primer:
      # location ~ (regex.*)(matchers.*) { regex matchers become $1 and $2 in the block }
      #   nginx uses longest path match to decide which location handler to use
      #   the result of a location handler can then match and enter another location handler
      # proxy_pass http://$variable triggers re-resolve mode, otherwise would have fixed IP
      # proxy_redirect 'match' 'replace'
      #   magic 'replace' value ' $1/' with a space prefix tells nginx to not add server and port
      # sub_filter 'match' 'replace'
      #   rewrites backend content, which we use for inserting the base path
      # proxy_cookie_path 'match' 'replace'
      #   fixes session cookie Path attribute, for Grafana

      # proxy_pass backends with dynamic base url

      sub_filter_once off;

      set $nocache 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';

      location ~ (.*/service/prometheus)(/.*) {
        proxy_pass http://$prometheus$2$is_args$args;
        sub_filter '="/' '="$1/';
        sub_filter 'PATH_PREFIX = ""' 'PATH_PREFIX = window.location.pathname.substr(0, window.location.pathname.lastIndexOf("/graph"))';
        proxy_redirect '/' ' $1/';
      }

      location ~ (.*/service/es-monitor-api)(/.*) {
        proxy_pass http://$monitorapi$2$is_args$args;
        proxy_redirect '/' ' $1/';
      }

      # our injected plugin should never cache...
      location ~ (.*/service/grafana)(/dashboard/script/exporter-async.js) {
        proxy_pass http://$grafana:3000$2$is_args$args;
        sub_filter_types *;
        sub_filter '/service/grafana' '$1';
        proxy_cookie_path '/service/grafana' '$1';
        proxy_redirect '/' ' $1/';
        # no-cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control $nocache;
        if_modified_since off;
        expires off;
        etag off;
      }

      location ~ (.*/service/grafana)(/.*) {
        proxy_pass http://$grafana:3000$2$is_args$args;
        sub_filter_types *;
        sub_filter '/service/grafana' '$1';
        proxy_cookie_path '/service/grafana' '$1';
        proxy_redirect '/' ' $1/';
      }

      location ~ (.*/service/alertmanager)(/.*) {
        proxy_pass http://$alertmanager$2$is_args$args;
        proxy_redirect '/' ' $1/';
      }

      # redirect /service/prometheus -> /service/prometheus/
      # redirect /service/grafana -> /service/grafana/
      # redirect /service/alertmanager -> /service/alertmanager/

      location ~ (.*/service/(prometheus|grafana|alertmanager))$ {
        return 301 ' $1/';
      }

      # version

      location ~ (.*)/version$ {
        return 200 '{
          "description": {{ .Chart.Description | quote }},
          "version": {{ .Chart.Version | quote }},
          {{- range $k, $v := .Values }}
            {{- if eq $k "imageCredentials" }}
              {{- range $k2, $v2 := . }}
                {{- if and (eq $k2 "registry") ($v2) }}
          "imageCredentials": { {{ $k2 |quote }}: {{ $v2 | quote}} },
                {{- end}}
              {{- end}}
            {{- else if ($v) }}
          {{ $k | quote }}: {{ $v | quote }},
            {{- end}}
          {{- end}}
          "name": {{ .Chart.Name | quote }}
        }';
        default_type application/json;
      }

      # console entry point

      location ~ (.*/)index.html {
        alias $base/index.html;
        sub_filter 'base href="/"' 'base href="$1"';
        # no-cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control $nocache;
        if_modified_since off;
        expires off;
        etag off;
      }

      # bookmarks / reloads in the /workloads/ path
      # (for our single page app, these should reload index.html but keep the path)

      location ~ (.*/)(namespaces/[^/]+/workloads/.*) {
        alias $base/index.html;
        default_type text/html;
        sub_filter 'base href="/"' 'base href="$1"';
        # kill cache
        add_header Last-Modified $date_gmt;
        add_header Cache-Control $nocache;
        if_modified_since off;
        expires off;
        etag off;
      }

      # static files

      location ~ (.*)(/assets/.*) {
        alias $base$2;
      }

      location ~ (.*)(/.*) {
        root $base;
        index index.html;
        try_files $2 $1/index.html;
      }

    }
