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:
            - 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: 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: http
                          domains:
                            - "*"
                          routes:
                            - 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
                              metadata:
                                filter_metadata:
                                  # expected header format 'x-host-port: 10-0-0-10'. otherwise forward to default_route.
                                  envoy.filters.http.lua:
                                    svc: ".echo-grpc"
                                    port: 8081
                                    header: x-host-port
                                    default_route: "echo-grpc:8081"
                    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")

                              -- handle header
                              local xHostPort = conf_default_route
                              local original = ""
                              value = request_handle:headers():get(conf_header_key)
                              if value ~= nil then
                                request_handle:logDebug(string.format("Header %s: %s", conf_header_key, value))
                                original = value
                                xHostPort = string.format("%s%s:%s", value, conf_svc, conf_port)
                              end
                              request_handle:headers():replace(conf_header_key, xHostPort)

                              -- debug log
                              request_handle:logDebug(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
          ignore_health_on_host_removal: true
          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
