# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License 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.
#
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ingress_name}}
  namespace: {{namespace}}
  annotations:
      nginx.ingress.kubernetes.io/proxy-body-size: "1024m"
      {% if tls %}
      cert-manager.io/cluster-issuer: "letsencrypt-issuer"
      {% endif %}
      {% if ingress_class == 'traefik' %}
      traefik.ingress.kubernetes.io/transport.respondingTimeouts.readTimeout: "{{route_timeout_seconds}}"
      traefik.ingress.kubernetes.io/transport.respondingTimeouts.writeTimeout: "{{route_timeout_seconds}}"
      traefik.ingress.kubernetes.io/transport.respondingTimeouts.idleTimeout: "{{route_timeout_seconds}}"
      {% else %}      
      nginx.ingress.kubernetes.io/proxy-connect-timeout: "{{route_timeout_seconds}}"
      nginx.ingress.kubernetes.io/proxy-send-timeout: "{{route_timeout_seconds}}"
      nginx.ingress.kubernetes.io/proxy-read-timeout: "{{route_timeout_seconds}}"      
      {% endif %}
      {% if needs_prefix and apply_nginx_rewrite_rule %}
      nginx.ingress.kubernetes.io/use-regex: "true"
      nginx.ingress.kubernetes.io/rewrite-target: {{rewrite_target}}/$1
      {% endif %}       
      {% if needs_rewrite and apply_nginx_rewrite_rule %}
      nginx.ingress.kubernetes.io/use-regex: "true"
      nginx.ingress.kubernetes.io/rewrite-target: {{rewrite_target}}
      {% endif %}     
      {% if (needs_rewrite or needs_prefix) and apply_traefik_prefix_middleware %}
      traefik.ingress.kubernetes.io/router.middlewares: nuvolaris-{{middleware_ingress_name}}@kubernetescrd
      {% endif %}
spec:
  ingressClassName: "{{ingress_class}}"
  {% if tls %}
  tls:
    - hosts:
      - {{hostname}}
      secretName: {{secret_name}}
  {% endif %}      
  rules:
    - host: "{{hostname}}"
      http:
        paths:
          - {% if ingress_class == 'traefik' %}
            path: {{context_path}}
            {% else %}
            path: {{context_path}}{{context_regexp}}
            {% endif %}
            pathType: {{path_type}}
            backend:
              service:
                name: {{service_name}}
                port: 
                  number: {{service_port}}
