common/templates/connect

79 lines
3.5 KiB
Plaintext

connect {
sidecar_service {
[[- if has .consul.connect "disable_default_tcp_check" ]]
disable_default_tcp_check = [[ .consul.connect.disable_default_tcp_check ]]
[[- end ]]
[[- $connect_proxy := "" ]]
[[- if and .proxy.enabled (has .proxy "service_name") (or (eq (urlParse .proxy.address).Hostname "localhost") (urlParse .proxy.address).Hostname "127.0.0.1") ]]
[[- $connect_proxy = .proxy.service_name ]]
[[- end ]]
[[- $connect_services := coll.Slice ]]
[[- if or (gt (len .consul.connect.upstreams) 0) (ne $connect_proxy "") ]]
proxy {
[[- range $idx, $upstream := .consul.connect.upstreams ]]
[[- $connect_services = append $upstream.destination_name $connect_services ]]
upstreams {
destination_name = "[[ $upstream.destination_name ]]"
[[- if has $upstream "local_bind_port" ]]
local_bind_port = [[ $upstream.local_bind_port ]]
[[- else if and (has $ "connect_services") (has $.connect_services $upstream.destination_name) (has (index $.connect_services $upstream.destination_name) "port") ]]
local_bind_port = [[ (index $.connect_services $upstream.destination_name).port ]]
[[- else ]]
[[- test.Fail (printf "Service %s has no local_bind_port defined" $upstream.destination_name) ]]
[[- end ]]
# Work arround, see https://github.com/hashicorp/nomad/issues/18538
destination_type = "service"
[[- $config := dict ]]
[[- if has $upstream "config" ]]
[[- $config = $upstream.config ]]
[[- else if and (has $ "connect_services") (has $.connect_services $upstream.destination_name) (has (index $.connect_services $upstream.destination_name) "config") ]]
[[- $config = (index $.connect_services $upstream.destination_name).config ]]
[[- end ]]
[[- if gt (len $config) 0 ]]
config {
[[- range $k, $v := $config ]]
[[- if or (isKind "number" $v) (isKind "bool" $v) ]]
[[ $k ]] = [[ $v ]]
[[- else if isKind "string" $v ]]
[[ $k ]] = "[[ $v ]]"
[[- else ]]
# Key [[ $k ]] has an unsupported value type ([[ kind $v ]]). Only bool, numbers of strings are allowed
[[- end ]]
[[- end ]]
}
[[- end ]]
}
[[- end ]]
[[- if and (ne $connect_proxy "") (not (has $connect_services $connect_proxy)) ]]
# Connect to the proxy service through the mesh
upstreams {
destination_name = [[ $connect_proxy | toJSON ]]
local_bind_port = [[ (urlParse .proxy.address).Port ]]
destination_type = "service"
}
[[- end ]]
}
[[- if has .consul.connect "tags" ]]
tags = [[ .consul.connect.tags | toJSON ]]
[[- end ]]
[[- end ]]
}
sidecar_task {
config {
[[- if has .consul.connect "image" ]]
image = "[[ .consul.connect.image ]]"
[[- end ]]
args = [
"-c",
"${NOMAD_SECRETS_DIR}/envoy_bootstrap.json",
"-l",
"[[ if has .consul.connect "log_level" ]][[ .consul.connect.log_level ]][[ else ]]${meta.connect.log_level}[[ end ]]",
"--concurrency",
"[[ if has .consul.connect "proxy_concurrency" ]][[ .consul.connect.proxy_concurrency ]][[ else ]]${meta.connect.proxy_concurrency}[[ end ]]",
"--disable-hot-restart"
]
}
[[ tmpl.Exec "common/resources.tpl" .consul.connect.resources | indent 4 ]]
}
}