{
  "node": {
    {{ if (index .metadata.Labels "td.networking.gke.io/mesh-name") }}
      "id": "projects/{{ .gcp_project_number }}/networks/mesh:{{ (index .metadata.Labels "td.networking.gke.io/mesh-name") }}/nodes/{{ .nodeID}}",
    {{ else if (index .metadata.Labels "td.networking.gke.io/gateway-name") }}
      "id": "projects/{{ .gcp_project_number }}/networks/scope:{{ (index .metadata.Labels "td.networking.gke.io/gateway-name") }}/nodes/{{ .nodeID}}",
    {{ else }}
      "id": "projects/{{ .gcp_project_number }}/networks/default/nodes/{{ .nodeID}}",
    {{ end }}
    "cluster": "{{ .cluster }}",
    "locality": {
      {{ if .region }}
      "region": "{{ .region }}",
      {{ end }}
      {{ if .zone }}
      "zone": "{{ .zone }}",
      {{ end }}
      {{ if .sub_zone }}
      "sub_zone": "{{ .sub_zone }}",
      {{ end }}
    },
    "metadata": {{ .meta_json_str }}
  },
  "dynamic_resources": {
    "lds_config": {
      "resource_api_version": "V3",
      "ads": {}
    },
    "cds_config": {
      "resource_api_version": "V3",
      "ads": {}
    },
    "ads_config": {
      "api_type": "GRPC",
      "transport_api_version": "V3",
      "grpc_services": [
        {
          "google_grpc": {
            "target_uri": "{{ .discovery_address }}",
            "stat_prefix": "googlegrpcxds",
            "channel_credentials": {
              "ssl_credentials": {
                {{ with .xds_root_cert}}"root_certs": {"filename": "{{.}}"}{{ end }}
              }
            },
            "call_credentials": [{
            {{ if .sts }}
              "sts_service": {
                "token_exchange_service_uri": "http://localhost:{{ .sts_port }}/token",
                "subject_token_path": "./var/run/secrets/tokens/istio-token",
                "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
                "scope": "https://www.googleapis.com/auth/cloud-platform"
              }
            {{ else }}
              "google_compute_engine": {}
            {{ end }}
            }],
            "channel_args": {
              "args": {
                "grpc.http2.max_pings_without_data": {
                  "int_value": 0
                },
                "grpc.keepalive_time_ms": {
                  "int_value": 10000
                },
                "grpc.keepalive_timeout_ms": {
                  "int_value": 20000
                }
              }
            }
          },
          "initial_metadata": [
          {{ if .sts }}
          {{ if .gcp_project_id }}
            {
              "key": "x-goog-user-project",
              "value": "{{ .gcp_project_id }}"
            }
          {{ end }}
          {{ end }}
          ]
        }
      ]
    }
  },
  "cluster_manager": {
    "load_stats_config": {
      "api_type": "GRPC",
      "transport_api_version": "V3",
      "grpc_services": [
        {
          "google_grpc": {
            "target_uri": "{{ .discovery_address }}",
            "stat_prefix": "googlegrpcxds",
            "channel_credentials": {
              "ssl_credentials": {}
            },
            "call_credentials": [{
            {{ if .sts }}
              "sts_service": {
                "token_exchange_service_uri": "http://localhost:{{ .sts_port }}/token",
                "subject_token_path": "./var/run/secrets/tokens/istio-token",
                "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
                "scope": "https://www.googleapis.com/auth/cloud-platform"
              }
            {{ else }}
              "google_compute_engine": {}
            {{ end }}
            }],
            "channel_args": {
              "args": {
                "grpc.http2.max_pings_without_data": {
                  "int_value": 0
                },
                "grpc.keepalive_time_ms": {
                  "int_value": 10000
                },
                "grpc.keepalive_timeout_ms": {
                  "int_value": 20000
                }
              }
            }
          },
          "initial_metadata": [
          {{ if .sts }}
          {{ if .gcp_project_id }}
            {
              "key": "x-goog-user-project",
              "value": "{{ .gcp_project_id }}"
            }
          {{ end }}
          {{ end }}
          ]
        }
      ]
    }
  },
  "admin": {
    "access_log_path": "/dev/null",
    "address": {
      "socket_address": {
        "address": "127.0.0.1",
        "port_value": {{ .config.ProxyAdminPort }}
      }
    }
  }
  {{ if .stackdriver }}
  ,
  "tracing": {
    "http": {
      "name": "envoy.tracers.opencensus",
      "typed_config": {
      "@type": "type.googleapis.com/envoy.config.trace.v3.OpenCensusConfig",
      "stackdriver_exporter_enabled": true,
      "stackdriver_project_id": "{{ .stackdriverProjectID }}",
      {{ if .sts_port }}
      "stackdriver_grpc_service": {
        "google_grpc": {
          "target_uri": "cloudtrace.googleapis.com",
          "stat_prefix": "oc_stackdriver_tracer",
          "channel_credentials": {
            "ssl_credentials": {}
          },
          "call_credentials": [{
            "sts_service": {
              "token_exchange_service_uri": "http://localhost:{{ .sts_port }}/token",
              "subject_token_path": "./var/run/secrets/tokens/istio-token",
              "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
              "scope": "https://www.googleapis.com/auth/cloud-platform"
            }
          }]
        },
        "initial_metadata": [
        {{ if .gcp_project_id }}
          {
            "key": "x-goog-user-project",
            "value": "{{ .gcp_project_id }}"
          }
        {{ end }}
        ]
      },
      {{ end }}
      "stdout_exporter_enabled": {{ .stackdriverDebug }},
      "incoming_trace_context": ["CLOUD_TRACE_CONTEXT", "TRACE_CONTEXT", "GRPC_TRACE_BIN", "B3"],
      "outgoing_trace_context": ["CLOUD_TRACE_CONTEXT", "TRACE_CONTEXT", "GRPC_TRACE_BIN", "B3"],
      "trace_config":{
        "constant_sampler":{
          "decision": "ALWAYS_PARENT"
        },
        "max_number_of_annotations": {{ .stackdriverMaxAnnotations }},
        "max_number_of_attributes": {{ .stackdriverMaxAttributes }},
        "max_number_of_message_events": {{ .stackdriverMaxEvents }},
        "max_number_of_links": 200
      }
    }
  }}
  {{ end }}
  ,
  "layered_runtime": {
    "layers": [
      {
        "name": "rtds_layer",
        "rtds_layer": {
          "name": "traffic_director_runtime",
          "rtds_config": {
	    "resource_api_version": "V3",
            "ads": {}
           }
       }
      }
     ]
  },
  "static_resources": {
    "clusters": [
      {
        "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 }}
                  }
                }
              }
            }]
          }]
        }
      }
    ],
    "listeners":[
      {
        "address": {
           "socket_address": {
             "protocol": "TCP",
             "address": "{{ .wildcard }}",
             "port_value": {{ .envoy_status_port }}
           }
        },
        "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"
                    }
                  }]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}
