-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_cluster.sh
executable file
·159 lines (140 loc) · 5.43 KB
/
setup_cluster.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
set -euo pipefail
dj_kubelet_repo_root="/var/lib/dj-kubelet"
console_base_url_hostname="console.dj-kubelet.com"
apiserver_hostname="k8s.dj-kubelet.com"
certbot_flags="--register-unsafely-without-email"
CLIENT_ID="${CLIENT_ID:-}"
echo "CLIENT_ID: $CLIENT_ID"
if [ "$CLIENT_ID" == "" ]; then
echo "CLIENT_ID is empty"
fi
CLIENT_SECRET="${CLIENT_SECRET:-}"
echo "CLIENT_SECRET: $CLIENT_SECRET"
if [ "$CLIENT_SECRET" == "" ]; then
echo "CLIENT_SECRET is empty"
fi
main() {
if [[ "$1" == "kubeadm" ]]; then
kubeadm init --apiserver-cert-extra-sans "k8s.dj-kubelet.com,local-k8s.dj-kubelet.com" || true
mkdir -p "$HOME/.kube"
cp -f "/etc/kubernetes/admin.conf" "$HOME/.kube/config"
chown "$(id -u):$(id -g)" "$HOME/.kube/config"
kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.8/install/kubernetes/quick-install.yaml
else
kind create cluster --name dj-kubelet --config "$dj_kubelet_repo_root/cloud/kind-config.yaml" || true
fi
# TODO Error if DNS record is not pointing to ext ip
#metadata_token=$(curl -X PUT \
# -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" \
# "http://169.254.169.254/latest/api/token")
#curl -H "X-aws-ec2-metadata-token: $metadata_token" \
# "http://169.254.169.254/latest/meta-data/public-ipv4"
#dig +short console.dj-kubelet.com
#dig +short k8s.dj-kubelet.com
# Add template dj-controller and CRDs
kubectl create namespace dj-controller || true
kubectl apply -k "$dj_kubelet_repo_root/dj-controller/prod"
# Deploy dj-scheduler
kubectl create namespace dj-scheduler || true
kubectl apply -k "$dj_kubelet_repo_root/dj-scheduler/prod"
# Deploy console
create_console_prod_overlay "$dj_kubelet_repo_root/console/prod"
kubectl create namespace console || true
kubectl apply -k "$dj_kubelet_repo_root/console/prod"
kubectl -n console get pods
# Forward apiserver
# socat TCP-LISTEN:6443,fork,bind=10.0.0.x TCP:127.0.0.1:6443 &
# Deploy oauth-refresher
cd "$dj_kubelet_repo_root/oauth-refresher"
create_oauth_refresher_prod_overlay "$dj_kubelet_repo_root/oauth-refresher/prod"
kubectl create namespace oauth-refresher || true
kubectl apply -k "$dj_kubelet_repo_root/oauth-refresher/prod"
}
rand_32() {
head -c100 /dev/urandom | base64 | head -c32
}
create_server_cert_letsencrypt() {
local overlay_dir="$1"
certbot certonly --standalone --keep-until-expiring --no-eff-email --agree-tos $certbot_flags -d "$console_base_url_hostname"
certbot certificates
local cert_dir="/etc/letsencrypt/live/$console_base_url_hostname/"
ls -l "$cert_dir"
ln -sf "$cert_dir/fullchain.pem" "$overlay_dir/server.pem"
ln -sf "$cert_dir/privkey.pem" "$overlay_dir/server-key.pem"
}
create_server_cert_selfsigned() {
local overlay_dir="$1"
cfssl selfsign localhost <(cfssl print-defaults csr) | cfssljson -bare "$overlay_dir/server"
}
create_console_prod_overlay() {
local overlay_dir="$1"
mkdir -p "$overlay_dir"
create_server_cert_selfsigned "$overlay_dir"
#create_server_cert_letsencrypt "$overlay_dir"
cat >"$overlay_dir/deployment-patch.yaml" <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: console
spec:
template:
spec:
containers:
- name: console
args:
- --port=:8443
- --base-url=https://${console_base_url_hostname}:30443
- --apiserver-endpoint=https://${apiserver_hostname}:6443
- --cert-file=/etc/tls/tls.crt
- --key-file=/etc/tls/tls.key
EOF
if ! grep "^COOKIE_STORE_AUTH_KEY=" "$overlay_dir/envfile" >/dev/null; then
echo "COOKIE_STORE_AUTH_KEY=$(rand_32)" >>"$overlay_dir/envfile"
fi
if ! grep "^COOKIE_STORE_ENCRYPTION_KEY=" "$overlay_dir/envfile" >/dev/null; then
echo "COOKIE_STORE_ENCRYPTION_KEY=$(rand_32)" >>"$overlay_dir/envfile"
fi
# Create CLIENT_ID and CLIENT_SECRET in envfile before applying
if ! grep "^CLIENT_ID=" "$overlay_dir/envfile" >/dev/null; then
echo "CLIENT_ID missing"
if [ "$CLIENT_ID" != "" ]; then
echo "CLIENT_ID=$CLIENT_ID" >>"$overlay_dir/envfile"
fi
fi
if ! grep "^CLIENT_SECRET=" "$overlay_dir/envfile" >/dev/null; then
echo "CLIENT_SECRET missing"
if [ "$CLIENT_SECRET" != "" ]; then
echo "CLIENT_SECRET=$CLIENT_SECRET" >>"$overlay_dir/envfile"
fi
fi
}
create_oauth_refresher_prod_overlay() {
local overlay_dir="$1"
mkdir -p "$overlay_dir"
if ! grep "^AUTH_URL=" "$overlay_dir/envfile" >/dev/null; then
cat >>"$overlay_dir/envfile" <<EOF
AUTH_URL=https://accounts.spotify.com/authorize
EOF
fi
if ! grep "^TOKEN_URL=" "$overlay_dir/envfile" >/dev/null; then
cat >>"$overlay_dir/envfile" <<EOF
TOKEN_URL=https://accounts.spotify.com/api/token
EOF
fi
# Create CLIENT_ID and CLIENT_SECRET in envfile before applying
if ! grep "^CLIENT_ID=" "$overlay_dir/envfile" >/dev/null; then
echo "CLIENT_ID missing"
if [ "$CLIENT_ID" != "" ]; then
echo "CLIENT_ID=$CLIENT_ID" >>"$overlay_dir/envfile"
fi
fi
if ! grep "^CLIENT_SECRET=" "$overlay_dir/envfile" >/dev/null; then
echo "CLIENT_SECRET missing"
if [ "$CLIENT_SECRET" != "" ]; then
echo "CLIENT_SECRET=$CLIENT_SECRET" >>"$overlay_dir/envfile"
fi
fi
}
main "$@"