{
  "application_log_config": {
    "log_format": {
{{- if .log_json }}
      "json_format": {
        "time": "%Y-%m-%dT%T.%fZ",
        "level": "%l",
        "scope": "envoy %n",
        "msg": "%j",
        "caller": "%g:%#",
        "thread": "%t"
      }
{{- else }}
      "text_format": "%Y-%m-%dT%T.%fZ\t%l\tenvoy %n %g:%#\t%v\tthread=%t"
{{- end }}
    }
  },
  "node": {
    "id": "{{ .nodeID }}",
    "cluster": "{{ .cluster }}",
    "locality": {
      {{- if .region }}
      "region": "{{ .region }}"
      {{- end }}
      {{- if .zone }}
      {{- if .region }}
      ,
      {{- end }}
      "zone": "{{ .zone }}"
      {{- end }}
      {{- if .sub_zone }}
      {{- if or .region .zone }}
      ,
      {{- end }}
      "sub_zone": "{{ .sub_zone }}"
      {{- end }}
    },
    "metadata": {{ .meta_json_str }}
  },
  "layered_runtime": {
    "layers": [
      {
        "name": "global config",
        "static_layer": {{ .runtime_flags }}
      },
      {
        "name": "admin",
        "admin_layer": {}
      }
    ]
  },
  "bootstrap_extensions": [
    {{- if .metadata_discovery }}
    {
      "name": "metadata_discovery",
      "typed_config": {
        "@type": "type.googleapis.com/udpa.type.v1.TypedStruct",
        "type_url": "type.googleapis.com/istio.workload.BootstrapExtension",
        "value": {
          "config_source": { "ads": {} }
        }
      }
    },
    {{- end }}
    {
      "name": "envoy.bootstrap.internal_listener",
      "typed_config": {
        "@type":"type.googleapis.com/udpa.type.v1.TypedStruct",
        "type_url": "type.googleapis.com/envoy.extensions.bootstrap.internal_listener.v3.InternalListener",
        "value": {
          "buffer_size_kb": 64
        }
      }
    }
  ],
  {{- if .stats_flush_interval }}
  "stats_flush_interval": "{{ .stats_flush_interval }}",
  {{- end }}
  {{- if .stats_eviction_interval }}
  "stats_eviction_interval": "{{ .stats_eviction_interval }}",
  {{- end }}
  "stats_config": {
    "use_all_default_tags": false,
    "stats_tags": [
      {
        "tag_name": "cluster_name",
        "regex": "^cluster(\\.(.+);)"
      },
      {
        "tag_name": "http_conn_manager_prefix",
        "regex": "^http\\.(((?:[_.[:digit:]\\w]*|[_\\[\\]aAbBcCdDeEfF[:digit:]\\w\\:]*));\\.)"
      },
      {
        "tag_name": "thread_name",
        "regex": "^server(\\.(.+))\\.watchdog"
      },
      {
        "tag_name": "tcp_prefix",
        "regex": "^tcp\\.((.*?)\\.)\\w+?$"
      },
      {
        "regex": "_rq(_(\\d{3}))$",
        "tag_name": "response_code"
      },
      {
        "tag_name": "response_code_class",
        "regex": "_rq(_(\\dxx))$"
      },
      {
        "tag_name": "http_conn_manager_listener_prefix",
        "regex": "^listener(?=\\.).*?\\.http\\.(((?:[_.[:digit:]]*|[_\\[\\]aAbBcCdDeEfF[:digit:]]*))\\.)"
      },
      {
        "tag_name": "listener_address",
        "regex": "^listener\\.(((?:[_.[:digit:]]*|[_\\[\\]aAbBcCdDeEfF[:digit:]]*))\\.)"
      },
      {
        "tag_name": "mongo_prefix",
        "regex": "^mongo\\.(.+?)\\.(collection|cmd|cx_|op_|delays_|decoding_)(.*?)$"
      },
      {{- range $a, $tag := .extraStatTags }}
      {
        "regex": "({{ $tag }}=\\.=(.*?);\\.;)",
        "tag_name": "{{ $tag }}"
      },
      {{- end }}
      {
        "regex": "(cache\\.(.+?)\\.)",
        "tag_name": "cache"
      },
      {
        "regex": "(component\\.(.+?)\\.)",
        "tag_name": "component"
      },
      {
        "regex": "(tag\\.(.+?);\\.)",
        "tag_name": "tag"
      },
      {
        "regex": "(wasm_filter\\.(.+?)\\.)",
        "tag_name": "wasm_filter"
      },
      {
        "tag_name": "authz_enforce_result",
        "regex": "rbac(\\.(allowed|denied))"
      },
      {
        "tag_name": "authz_dry_run_action",
        "regex": "(\\.istio_dry_run_(allow|deny)_)"
      },
      {
        "tag_name": "authz_dry_run_result",
        "regex": "(\\.shadow_(allowed|denied))"
      }
    ],
    {{- if .histogram_buckets }}
    "histogram_bucket_settings": {{ toJson .histogram_buckets }},
    {{- end }}
    "stats_matcher": {
      "inclusion_list": {
        "patterns": [
          {
          "prefix": "reporter="
          },
          {{- if .metadata_discovery }}
          {
          "prefix": "workload_discovery"
          },
          {{- end }}
          {{- range $a, $s := .inclusionPrefix }}
          {
          "prefix": "{{$s}}"
          },
          {{- end }}
          {{- range $a, $s := .inclusionSuffix }}
          {
          "suffix": "{{$s}}"
          },
          {{- end }}
          {{- range $a, $s := .inclusionRegexps }}
          {
          "safe_regex": {"regex":"{{js $s}}"}
          },
          {{- end }}
          {
          "prefix": "component"
          },
          {
          "prefix": "istio"
          }
        ]
      }
    }
  },
  "admin": {
    "access_log": [
      {
        "name": "envoy.access_loggers.file",
        "typed_config": {
          "@type": "type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog",
          "path": "/dev/null"
        }
      }
    ],
    "profile_path": "/var/lib/istio/data/envoy.prof",
    "address": {
      "socket_address": {
        "address": "{{ .localhost }}",
        "port_value": {{ .config.ProxyAdminPort }}
      }
    }
  },
  "dynamic_resources": {
    "lds_config": {
      "ads": {},
      "initial_fetch_timeout": "0s",
      "resource_api_version": "V3"
    },
    "cds_config": {
      "ads": {},
      "initial_fetch_timeout": "0s",
      "resource_api_version": "V3"
    },
    "ads_config": {
      "api_type": "{{ .xds_type }}",
      "set_node_on_first_message_only": true,
      "transport_api_version": "V3",
      "grpc_services": [
        {
          "envoy_grpc": {
            "cluster_name": "xds-grpc"
          }
        }
      ]
    }
  },
  "static_resources": {
    "clusters": [
      {
        "name": "prometheus_stats",
        "alt_stat_name": "prometheus_stats;",
        "type": "STATIC",
        "connect_timeout": "0.250s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "prometheus_stats",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {
                    "protocol": "TCP",
                    "address": "{{ .localhost }}",
                    "port_value": {{ .config.ProxyAdminPort }}
                  }
                }
              }
            }]
          }]
        }
      },
      {
        "name": "agent",
        "alt_stat_name": "agent;",
        "type": "STATIC",
        "connect_timeout": "0.250s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "agent",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {
                    "protocol": "TCP",
                    "address": "{{ .localhost }}",
                    "port_value": {{ .config.StatusPort }}
                  }
                }
              }
            }]
          }]
        }
      },
      {
        "name": "sds-grpc",
        "alt_stat_name": "sds-grpc;",
        "type": "STATIC",
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        },
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "sds-grpc",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "pipe": {
                    "path": "./var/run/secrets/workload-spiffe-uds/{{ .workload_identity_socket_file }}"
                  }
                }
              }
            }]
          }]
        }
      },
      {{- if .custom_file_path }}
      {
        "name": "sds-files-grpc",
        "alt_stat_name": "sds-files-grpc;",
        "type": "STATIC",
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        },
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "sds-files-grpc",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "pipe": {
                    "path": "{{ .custom_file_path }}"
                  }
                }
              }
            }]
          }]
        }
      },
      {{- end }}
      {
        "name": "xds-grpc",
        "alt_stat_name": "xds-grpc;",
        "type" : "STATIC",
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "xds-grpc",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "pipe": {
                    "path": "{{ .config.ConfigPath }}/XDS"
                  }
                }
              }
            }]
          }]
        },
        "circuit_breakers": {
          "thresholds": [
            {
              "priority": "DEFAULT",
              "max_connections": 100000,
              "max_pending_requests": 100000,
              "max_requests": 100000
            },
            {
              "priority": "HIGH",
              "max_connections": 100000,
              "max_pending_requests": 100000,
              "max_requests": 100000
            }
          ]
        },
        "upstream_connection_options": {
          "tcp_keepalive": {
            "keepalive_time": 300
          }
        },
        "max_requests_per_connection": 1,
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        }
      }
      {{ if .zipkin }}
      ,
      {
        "name": "zipkin",
        "alt_stat_name": "zipkin;",
        {{- if .tracing_tls }}
        "transport_socket": {{ .tracing_tls }},
        {{- end }}
        "type": "STRICT_DNS",
        "respect_dns_ttl": true,
        "dns_lookup_family": "{{ .dns_lookup_family }}",
        "dns_refresh_rate": "30s",
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "zipkin",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {{ .zipkin }}
                }
              }
            }]
          }]
        }
      }
      {{ else if .lightstep }}
      ,
      {
        "name": "lightstep",
        "alt_stat_name": "lightstep;",
        {{- if .tracing_tls }}
        "transport_socket": {{ .tracing_tls }},
        {{- end }}
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        },
        "type": "STRICT_DNS",
        "respect_dns_ttl": true,
        "dns_lookup_family": "{{ .dns_lookup_family }}",
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "lightstep",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {{ .lightstep }}
                }
              }
            }]
          }]
        }
      }
      {{ else if .datadog }}
      ,
      {
        "name": "datadog_agent",
        "alt_stat_name": "datadog_agent;",
        {{- if .tracing_tls }}
        "transport_socket": {{ .tracing_tls }},
        {{- end }}
        "connect_timeout": "1s",
        "type": "STRICT_DNS",
        "respect_dns_ttl": true,
        "dns_lookup_family": "{{ .dns_lookup_family }}",
        "lb_policy": "ROUND_ROBIN",
        "load_assignment": {
          "cluster_name": "datadog_agent",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {{ .datadog }}
                }
              }
            }]
          }]
        }
      }
      {{ end }}
      {{- if .envoy_metrics_service_address }}
      ,
      {
        "name": "envoy_metrics_service",
        "alt_stat_name": "envoy_metrics_service;",
        "type": "STRICT_DNS",
      {{- if .envoy_metrics_service_tls }}
        "transport_socket": {{ .envoy_metrics_service_tls }},
      {{- end }}
      {{- if .envoy_metrics_service_tcp_keepalive }}
        "upstream_connection_options": {{ .envoy_metrics_service_tcp_keepalive }},
      {{- end }}
        "respect_dns_ttl": true,
        "dns_lookup_family": "{{ .dns_lookup_family }}",
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        },
        "load_assignment": {
          "cluster_name": "envoy_metrics_service",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {{ .envoy_metrics_service_address }}
                }
              }
            }]
          }]
        }
      }
      {{ end }}
      {{ if .envoy_accesslog_service_address }}
      ,
      {
        "name": "envoy_accesslog_service",
        "alt_stat_name": "envoy_accesslog_service;",
        "type": "STRICT_DNS",
      {{- if .envoy_accesslog_service_tls }}
        "transport_socket": {{ .envoy_accesslog_service_tls }},
      {{- end }}
      {{- if .envoy_accesslog_service_tcp_keepalive }}
        "upstream_connection_options": {{ .envoy_accesslog_service_tcp_keepalive }},
      {{ end }}
        "respect_dns_ttl": true,
        "dns_lookup_family": "{{ .dns_lookup_family }}",
        "connect_timeout": "1s",
        "lb_policy": "ROUND_ROBIN",
        "typed_extension_protocol_options": {
          "envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
           "@type": "type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions",
           "explicit_http_config": {
            "http2_protocol_options": {}
           }
          }
        },
        "load_assignment": {
          "cluster_name": "envoy_accesslog_service",
          "endpoints": [{
            "lb_endpoints": [{
              "endpoint": {
                "address":{
                  "socket_address": {{ .envoy_accesslog_service_address }}
                }
              }
            }]
          }]
        }
      }
      {{ end }}
    ],
    "listeners":[
      {
        {{- if .metrics_localhost_access_only }}
        "name": "{{ .localhost }}_{{ .envoy_prometheus_port }}",
        {{ else }}
        "name": "{{ .wildcard }}_{{ .envoy_prometheus_port }}",
        {{ end }}
        "address": {
          "socket_address": {
            "protocol": "TCP",
            {{- if .metrics_localhost_access_only }}
            "address": "{{ .localhost }}",
            {{ else }}
            "address": "{{ .wildcard }}",
            {{ end }}
            "port_value": {{ .envoy_prometheus_port }}
          }
        },
        "ignore_global_conn_limit": true,
        "bypass_overload_manager": true,
        {{- if .dual_stack }}
        "additionalAddresses": [
          {
            "address": {
              "socket_address": {
                "protocol": "TCP",
                {{- if .metrics_localhost_access_only }}
                "address": "{{ .additional_localhost }}",
                {{ else }}
                "address": "{{ .additional_wildcard }}",
                {{ end }}
                "port_value": {{ .envoy_prometheus_port }}
              }
            }
          }
        ],
        {{- end}}
        "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",
                  "codec_type": "AUTO",
                  "stat_prefix": "stats",
                  "route_config": {
                    "virtual_hosts": [
                      {
                        "name": "backend",
                        "domains": [
                          "*"
                        ],
                        "routes": [
                          {
                            "match": {
                              "prefix": "/stats/prometheus"
                            },
                            "route": {
                              "cluster": "prometheus_stats"
                            }
                          }
                        ]
                      }
                    ]
                  },
                  "http_filters": [
                  {{- if .stats_compression }}
                  {
                    "name": "envoy.filters.http.compressor",
                    "typed_config": {
                      "@type": "type.googleapis.com/envoy.extensions.filters.http.compressor.v3.Compressor",
                      {{- if eq .stats_compression "gzip"}}
                      "compressor_library": {
                        "name": "text_optimized",
                        "typed_config": {
                          "@type": "type.googleapis.com/envoy.extensions.compression.gzip.compressor.v3.Gzip"
                        }
                      }
                      {{- else if eq .stats_compression "zstd"}}
                      "compressor_library": {
                        "name": "text_optimized",
                        "typed_config": {
                          "@type": "type.googleapis.com/envoy.extensions.compression.zstd.compressor.v3.Zstd"
                        }
                      }
                      {{- else if eq .stats_compression "brotli"}}
                      "compressor_library": {
                        "name": "text_optimized",
                        "typed_config": {
                          "@type": "type.googleapis.com/envoy.extensions.compression.brotli.compressor.v3.Brotli"
                        }
                      }
                      {{- end }}
                    }
                  },
                  {{- end }}
                  {
                    "name": "envoy.filters.http.router",
                    "typed_config": {
                      "@type": "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router"
                    }
                  }]
                }
              }
            ]
          }
        ]
      },
      {
        "name": "{{ .wildcard }}_{{ .envoy_status_port }}",
        "address": {
           "socket_address": {
             "protocol": "TCP",
             "address": "{{ .wildcard }}",
             "port_value": {{ .envoy_status_port }}
           }
        },
        "ignore_global_conn_limit": true,
        "bypass_overload_manager": true,
        {{- if .dual_stack }}
        "additionalAddresses": [
          {
            "address": {
              "socket_address": {
                "protocol": "TCP",
                "address": "{{ .additional_wildcard }}",
                "port_value": {{ .envoy_status_port }}
              }
            }
          }
        ],
        {{- end}}
        {{- if .envoy_status_port_enable_proxy_protocol }}
        "listener_filters": [
          {
            "name": "envoy.listener.proxy_protocol",
            "typed_config": {
              "@type": "type.googleapis.com/envoy.extensions.filters.listener.proxy_protocol.v3.ProxyProtocol",
              "allow_requests_without_proxy_protocol": true,
            }
          }
        ],
        {{- end }}
        "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",
                  "codec_type": "AUTO",
                  "stat_prefix": "agent",
                  "route_config": {
                    "virtual_hosts": [
                      {
                        "name": "backend",
                        "domains": [
                          "*"
                        ],
                        "routes": [
                          {
                            "match": {
                              "prefix": "/healthz/ready"
                            },
                            "route": {
                              "cluster": "agent"
                            }
                          }
                        ]
                      }
                    ]
                  },
                  "http_filters": [{
                    "name": "envoy.filters.http.router",
                    "typed_config": {
                      "@type": "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router"
                    }
                  }]
                }
              }
            ]
          }
        ]
      }
    ]
  },
  "overload_manager": {
    "resource_monitors": [
      {
        "name": "envoy.resource_monitors.global_downstream_max_connections",
        "typed_config": {
          "@type": "type.googleapis.com/envoy.extensions.resource_monitors.downstream_connections.v3.DownstreamConnectionsConfig",
          "max_active_downstream_connections": 2147483647
        }
      }
    ]
  }
  {{- if .zipkin }}
  ,
  "tracing": {
    "http": {
      "name": "envoy.tracers.zipkin",
      "typed_config": {
        "@type": "type.googleapis.com/envoy.config.trace.v3.ZipkinConfig",
        "collector_cluster": "zipkin",
        "collector_endpoint": "/api/v2/spans",
        "collector_endpoint_version": "HTTP_JSON",
        "trace_id_128bit": true,
        "shared_span_context": false
      }
    }
  }
  {{- else if .lightstep }}
  ,
  "tracing": {
    "http": {
      "name": "envoy.tracers.lightstep",
      "typed_config": {
        "@type": "type.googleapis.com/envoy.config.trace.v3.LightstepConfig",
        "collector_cluster": "lightstep",
        "access_token_file": "{{ .lightstepToken}}"
      }
    }
  }
  {{- else if .datadog }}
  ,
  "tracing": {
    "http": {
      "name": "envoy.tracers.datadog",
      "typed_config": {
        "@type": "type.googleapis.com/envoy.config.trace.v3.DatadogConfig",
        "collector_cluster": "datadog_agent",
        "service_name": "{{ .cluster }}"
      }
    }
  }
  {{ end }}
  {{ if or .envoy_metrics_service_address .statsd }}
  ,
  "stats_sinks": [
    {{ if .envoy_metrics_service_address }}
    {
      "name": "envoy.stat_sinks.metrics_service",
      "typed_config": {
        "@type": "type.googleapis.com/envoy.config.metrics.v3.MetricsServiceConfig",
        "transport_api_version": "V3",
        "grpc_service": {
          "envoy_grpc": {
            "cluster_name": "envoy_metrics_service"
          }
        }
      }
    }
    {{ end }}
    {{ if and .envoy_metrics_service_address .statsd }}
    ,
    {{ end }}
    {{ if .statsd }}
    {
      "name": "envoy.stat_sinks.statsd",
      "typed_config": {
        "@type": "type.googleapis.com/envoy.config.metrics.v3.StatsdSink",
        "address": {
          "socket_address": {{ .statsd }}
        }
      }
    }
    {{ end }}
  ]
  {{ end }}
  {{ if or .outlier_log_path .load_stats_config_json_str}}
  ,
  "cluster_manager": {
    "enable_deferred_cluster_creation": true,
    {{- if .outlier_log_path}}
    "outlier_detection": {
      "event_log_path": "{{ .outlier_log_path }}"
    }
    {{- end}}
    {{- if and .outlier_log_path .load_stats_config_json_str }}
    ,
    {{- end}}
    {{- if .load_stats_config_json_str }}
    "load_stats_config": {{- .load_stats_config_json_str }}
    {{- end }}
  }
  {{- end }}
  ,
  "deferred_stat_options": {
    "enable_deferred_creation_stats": true
  }
}
