# Copyright 2019 Google LLC
#
# Licensed 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: v1
kind: ConfigMap
metadata:
  name: envoy-conf
data:
  envoy.yaml: |
    admin:
      access_log_path: "/dev/stdout"
      address:
        socket_address:
          address: 0.0.0.0
          port_value: 8090
    static_resources:
      listeners:
      - address:
          socket_address:
            address: 0.0.0.0
            port_value: 443
        filter_chains:
        - filters:
          - name: envoy.http_connection_manager
            typed_config:
              '@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
              stat_prefix: ingress_https
              common_http_protocol_options:
                idle_timeout: 3700s
              stream_idle_timeout: 3700s
              codec_type: AUTO
              access_log:
              - name: envoy.file_access_log
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
                  path: "/dev/stdout"
              route_config:
                name: local_route
                virtual_hosts:
                - name: https
                  domains:
                  - "*"
                  routes:
                  - match:
                      prefix: "/"
                      headers:
                        - name: x-pod-name
                          suffix_match: ":443"
                    route:
                      cluster: dynamic_forward_proxy_cluster_tls
                    typed_per_filter_config:
                      envoy.filters.http.dynamic_forward_proxy:
                        '@type': type.googleapis.com/envoy.extensions.filters.http.dynamic_forward_proxy.v3.PerRouteConfig
                        host_rewrite_header: X-Host-Port
                  - match:
                      prefix: "/"
                    route:
                      cluster: dynamic_forward_proxy_cluster
                    typed_per_filter_config:
                      envoy.filters.http.dynamic_forward_proxy:
                        '@type': type.googleapis.com/envoy.extensions.filters.http.dynamic_forward_proxy.v3.PerRouteConfig
                        host_rewrite_header: X-Host-Port
              http_filters:
              - name: envoy.filters.http.dynamic_forward_proxy
                typed_config:
                   '@type': type.googleapis.com/envoy.extensions.filters.http.dynamic_forward_proxy.v3.FilterConfig
                   dns_cache_config:
                    name: dynamic_forward_proxy_cache_config
                    dns_lookup_family: V4_ONLY
               - name: envoy.filters.http.router
                config: {}
          tls_context:
            common_tls_context:
              tls_certificates:
              - certificate_chain:
                  filename: "/etc/ssl/envoy/tls.crt"
                private_key:
                  filename: "/etc/ssl/envoy/tls.key"
      clusters:
      - name: dynamic_forward_proxy_cluster
        connect_timeout: 1s
        type: CLUSTER_PROVICED
        ignore_health_on_host_removal: true
        cluster_type:
          name: envoy.clusters.dynamic_forward_proxy
          typed_config:
            '@type': type.googleapis.com/envoy.extensions.clusters.dynamic_forward_proxy.v3.ClusterConfig
            dns_cache_config:
              name: dynamic_forward_proxy_cache_config
              dns_lookup_family: V4_ONLY
      - name: dynamic_forward_proxy_cluster_tls
        connect_timeout: 1s
        type: CLUSTER_PROVICED
        ignore_health_on_host_removal: true
        cluster_type:
          name: envoy.clusters.dynamic_forward_proxy
          typed_config:
            '@type': type.googleapis.com/envoy.extensions.clusters.dynamic_forward_proxy.v3.ClusterConfig
            dns_cache_config:
              name: dynamic_forward_proxy_cache_config
              dns_lookup_family: V4_ONLY
        transport_socket:
          name: envoy.transport_sockets.tls
          typed_config:
            '@type': type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext
            common_tls_context:
              validation_context:
                trusted_ca:
                  filename: /etc/ssl/certs/ca-certificates.crt
