forked from rboyer/devconsul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidecar-boot.sh
executable file
·122 lines (103 loc) · 3.15 KB
/
sidecar-boot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
set -euo pipefail
readonly proxy_type="${SBOOT_PROXY_TYPE:-}"
readonly mode="${SBOOT_MODE:-}"
readonly agent_tls="${SBOOT_AGENT_TLS:-}"
readonly agent_grpc_tls="${SBOOT_AGENT_GRPC_TLS:-}"
readonly partition="${SBOOT_PARTITION:-}"
echo "launching a '${proxy_type}' sidecar proxy"
readonly service_register_file="${SBOOT_REGISTER_FILE:-}"
if [[ -z "${service_register_file}" ]]; then
echo "missing required env var SBOOT_REGISTER_FILE" >&2
exit 1
fi
api_args=()
acl_api_args=()
case "${mode}" in
insecure)
;;
direct)
readonly token_file="${SBOOT_TOKEN_FILE:-}"
if [[ -z "${token_file}" ]]; then
echo "missing required env var SBOOT_TOKEN_FILE" >&2
exit 1
fi
api_args+=( -token-file "${token_file}" )
acl_api_args+=( -token-file "${token_file}" )
;;
login)
readonly bearer_token_file="${SBOOT_BEARER_TOKEN_FILE:-}"
if [[ -z "${bearer_token_file}" ]]; then
echo "missing required env var SBOOT_BEARER_TOKEN_FILE" >&2
exit 1
fi
readonly token_sink_file="${SBOOT_TOKEN_SINK_FILE:-}"
if [[ -z "${token_sink_file}" ]]; then
echo "missing required env var SBOOT_TOKEN_SINK_FILE" >&2
exit 1
fi
#TODO: handle api_args[@] here somehow
consul login \
-method=minikube \
-bearer-token-file="${bearer_token_file}" \
-token-sink-file="${token_sink_file}" \
-meta "host=$(hostname)"
echo "Wrote new token to ${token_sink_file}"
api_args+=( -token-file "${token_sink_file}" )
acl_api_args+=( -token-file "${token_sink_file}" )
;;
*)
echo "unknown mode: $mode" >&2
exit 1
;;
esac
if [[ -n "${partition}" ]]; then
api_args+=( -partition "${partition}" )
acl_api_args+=( -partition "${partition}" )
fi
if [[ -n "$agent_tls" ]]; then
api_args+=(
-ca-file /tls/consul-agent-ca.pem
-http-addr https://127.0.0.1:8501
)
else
api_args+=( -http-addr http://127.0.0.1:8500 )
fi
acl_api_args+=( -http-addr http://127.0.0.1:8500 )
grpc_args=()
if [[ -n "$agent_grpc_tls" ]]; then
grpc_args+=( -grpc-addr https://127.0.0.1:8503 )
else
grpc_args+=( -grpc-addr http://127.0.0.1:8502 )
fi
if [[ "${mode}" != "insecure" ]]; then
while : ; do
if consul acl token read "${acl_api_args[@]}" -self ; then
break
fi
echo "waiting for ACLs to work..."
sleep 0.1
done
fi
while : ; do
echo "Registering service..."
if consul services register "${api_args[@]}" "${service_register_file}"; then
break
fi
echo "waiting for registration to work..."
sleep 0.1
done
echo "Launching proxy..."
case "${proxy_type}" in
envoy)
consul connect envoy -bootstrap "${grpc_args[@]}" "${api_args[@]}" "$@" > /tmp/envoy.config
exec consul connect envoy "${grpc_args[@]}" "${api_args[@]}" "$@"
;;
builtin)
# TODO: handle agent tls?
exec consul connect proxy "${api_args[@]}" "$@"
;;
*)
echo "unknown proxy type: ${proxy_type}" >&2
exit 1
esac