apiVersion: v1
# ===============LICENSE_START=======================================================
# Acumos Apache-2.0
# ===================================================================================
# Copyright (C) 2019 Nordix Foundation
# Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================================
# This Acumos software file is distributed by Nordix Foundation and AT&T
# 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
#
# This file 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.
# ===============LICENSE_END=========================================================
#
# What this is: Kubernetes configmap template for nginx as deployed with
# Acumos models in kubernetes environments, for logging of model events.
#
# Usage: intended to be customized and created for the specific deployment by
# deploy.sh
#

kind: ConfigMap
metadata:
  namespace: <NAMESPACE>
  name: nginx-mc-<TRACKING_ID>
data:
  proxy.conf: |
    # HTTP 1.1 support
    proxy_http_version  1.1;
    proxy_cache_bypass  $http_upgrade;

    proxy_set_header Upgrade            $http_upgrade;
    proxy_set_header Connection         "upgrade";
    proxy_set_header Host               $host;
    proxy_set_header X-Real-IP          $remote_addr;
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    proxy_set_header X-Forwarded-Host   $host;
    proxy_set_header X-Forwarded-Port   $server_port;
    # Used to correlate logs from model runner
    proxy_set_header X-Request-ID       $request_id;
  cors.conf: |
    proxy_hide_header Access-Control-Allow-Origin;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow_Credentials' 'true';
    add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
    add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

    if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow_Credentials' 'true';
      add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
      add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';
      add_header 'Content-Length' 0;
      return 204;
    }
  nginx.conf: |
    user www-data;
    pid /run/nginx.pid;
    worker_processes auto;
    worker_rlimit_nofile 65535;

    events {
      multi_accept on;
      worker_connections 65535;
    }

    http {
      charset utf-8;
      sendfile on;
      tcp_nopush on;
      tcp_nodelay on;
      server_tokens off;
      log_not_found off;
      types_hash_max_size 2048;
      client_max_body_size 16M;

      # MIME
      include mime.types;
      default_type application/octet-stream;

      # logging custom format for model methods
      log_format  acumos  '{ "model": { "solutionId": "<COMP_SOLUTION_ID>", "revisionId": "<COMP_REVISION_ID>", "name": "<MODEL_NAME>", "operationName": "$request_uri", "userId": "<USER_ID>" }, '
                        ' "remoteInfo": { "remoteAddr": "$remote_addr", "remoteUser": "$remote_user", "timeLocal": "$time_local", "reqStr": "$request", "httpReferer": "$http_referer", "userAgent": "$http_user_agent", "http_x_forwarded_for": "$http_x_forwarded_for" },  '
                        ' "request": { "scheme": "$scheme", "length": $request_length }, '
                        ' "response": { "status": "$status", "bytesSent": $body_bytes_sent, "time": $request_time } '
                        '}';
      access_log /var/log/nginx/model-usage-<COMP_SOLUTION_ID>.log acumos;
      error_log /var/log/nginx/error-<COMP_SOLUTION_ID>.log warn;

      server {
        server_name nginx-proxy-mc;

        # reverse proxy
        location / {
          proxy_pass http://modelconnector-<TRACKING_ID>:8555/;
          include /etc/nginx/proxy.conf;
        }
      }
    }
