apiVersion: v1
kind: ConfigMap
metadata:
  name: envoy-conf
data:
  envoy.yaml: |
    admin:
      access_log_path: /tmp/admin_access.log
      address:
        socket_address:
          address: 127.0.0.1
          protocol: TCP
          port_value: 9901

    static_resources:
      listeners:
        - name: listener_0
          address:
            socket_address:
              address: 0.0.0.0
              protocol: TCP
              port_value: 10000
          filter_chains:
            transport_socket:
              name: envoy.transport_sockets.tls
              typed_config:
                "@type": type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.DownstreamTlsContext
                common_tls_context:
                  alpn_protocols: "h2,http/1.1"
                  tls_certificates:
                  - certificate_chain:
                      filename: "/etc/ssl/envoy/tls.crt"
                    private_key:
                      filename: "/etc/ssl/envoy/tls.key"
            filters:
              - name: envoy.filters.network.http_connection_manager
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
                  stat_prefix: ingress_http
                  access_log:
                    - name: 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: http
                        domains:
                          - "*"
                        routes:
                          - match:
                              prefix: /
                              grpc: {}
                            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
                            metadata:
                              filter_metadata:
                                envoy.filters.http.lua:
                                  svc: ".echo-grpc"
                                  port: 12345
                                  header: x-host-port
                                  default_route: "echo-grpc:12345"
                  http_filters:
                    - name: envoy.filters.http.lua
                      typed_config:
                        "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
                        inline_code: |
                          function envoy_on_request(request_handle)
                            local conf_svc = request_handle:metadata():get("svc")
                            local conf_port = request_handle:metadata():get("port")
                            local conf_header_key = request_handle:metadata():get("header")
                            local conf_default_route = request_handle:metadata():get("default_route")

                            -- utilities
                            function split(str, ts)
                              if ts == nil then return {} end
                              local t = {}
                              i=1
                              for s in string.gmatch(str, "([^"..ts.."]+)") do
                                t[i] = s
                                i = i + 1
                              end
                              return t
                            end

                            -- handle header
                            local xHostPort = conf_default_route
                            local host = ""
                            local port = conf_port
                            local original = ""
                            for key, value in pairs(request_handle:headers()) do
                              request_handle:logDebug(string.format("Header %s: %s",key, value))
                              if key == conf_header_key then
                                original = value
                                kv = split(value, ":")
                                host = kv[1]
                                if kv[2] == nil then
                                  port = conf_port
                                else
                                  port = kv[2]
                                end
                                xHostPort = string.format("%s%s:%s", host, conf_svc, port)
                              end
                            end
                            request_handle:headers():replace(conf_header_key, xHostPort)

                            -- debug log
                            request_handle:logInfo(string.format("%s, original=%s, after=%s", conf_header_key, original, xHostPort, changed))

                          end
                    - 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

      clusters:
        - name: dynamic_forward_proxy_cluster
          connect_timeout: 1s
          lb_policy: CLUSTER_PROVIDED
          http2_protocol_options: {}
          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
