Skip to content

Commit 25f3548

Browse files
authored
feat: Delay most of the initialization work to the start-up stage and always regenerate internal certificates (#133)
1 parent 90637c0 commit 25f3548

File tree

5 files changed

+307
-436
lines changed

5 files changed

+307
-436
lines changed

bin/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test*

compose/docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ services:
316316
command:
317317
- -config.file=/etc/loki/config/config.yaml
318318
- -target=all
319+
depends_on:
320+
prepare:
321+
condition: service_completed_successfully
319322
networks:
320323
higress-net:
321324
aliases:

compose/scripts/init.sh

+1-349
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,6 @@ initializeApiServer() {
129129
mkdir -p "$VOLUMES_ROOT/api" && cd "$_"
130130
checkExitCode "Creating volume for API server fails with $?"
131131

132-
if [ ! -f ca.key ] || [ ! -f ca.crt ]; then
133-
echo " Generating CA certificate...";
134-
openssl req -nodes -new -x509 -days 36500 -keyout ca.key -out ca.crt -subj "/CN=higress-root-ca/O=higress" > /dev/null 2>&1
135-
checkExitCode " Generating CA certificate for API server fails with $?";
136-
else
137-
echo " CA certificate already exists.";
138-
fi
139-
if [ ! -f server.key ] || [ ! -f server.crt ]; then
140-
echo " Generating server certificate..."
141-
openssl req -out server.csr -new -newkey rsa:$RSA_KEY_LENGTH -nodes -keyout server.key -subj "/CN=higress-api-server/O=higress" > /dev/null 2>&1 \
142-
&& openssl x509 -req -days 36500 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -sha256 -out server.crt > /dev/null 2>&1
143-
checkExitCode " Generating server certificate fails with $?";
144-
else
145-
echo " Server certificate already exists.";
146-
fi
147132
if [ ! -f nacos.key ]; then
148133
echo " Generating data encryption key..."
149134
if [ -z "$NACOS_DATA_ENC_KEY" ]; then
@@ -152,46 +137,7 @@ initializeApiServer() {
152137
echo -n "$NACOS_DATA_ENC_KEY" > nacos.key
153138
fi
154139
else
155-
echo " Client certificate already exists.";
156-
fi
157-
if [ ! -f client.key ] || [ ! -f client.crt ]; then
158-
echo " Generating client certificate..."
159-
openssl req -out client.csr -new -newkey rsa:$RSA_KEY_LENGTH -nodes -keyout client.key -subj "/CN=higress/O=system:masters" > /dev/null 2>&1 \
160-
&& openssl x509 -req -days 36500 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 02 -sha256 -out client.crt > /dev/null 2>&1
161-
checkExitCode " Generating client certificate fails with $?";
162-
else
163-
echo " Client certificate already exists.";
164-
fi
165-
166-
CLIENT_CERT=$(cat client.crt | base64 -w 0)
167-
CLIENT_KEY=$(cat client.key | base64 -w 0)
168-
169-
if [ ! -f $VOLUMES_ROOT/kube/config ]; then
170-
echo " Generating kubeconfig..."
171-
mkdir -p $VOLUMES_ROOT/kube
172-
cat <<EOF > $VOLUMES_ROOT/kube/config
173-
apiVersion: v1
174-
kind: Config
175-
clusters:
176-
- name: higress
177-
cluster:
178-
server: https://apiserver:8443
179-
insecure-skip-tls-verify: true
180-
users:
181-
- name: higress-admin
182-
user:
183-
client-certificate-data: ${CLIENT_CERT}
184-
client-key-data: ${CLIENT_KEY}
185-
contexts:
186-
- name: higress
187-
context:
188-
cluster: higress
189-
user: higress-admin
190-
preferences: {}
191-
current-context: higress
192-
EOF
193-
else
194-
echo " kubeconfig already exists."
140+
echo " Data encryption key already exists.";
195141
fi
196142
}
197143

@@ -206,301 +152,7 @@ initializeController() {
206152
fi
207153
}
208154

209-
initializePilot() {
210-
echo "Initializing pilot configurations..."
211-
212-
mkdir -p $VOLUMES_ROOT/pilot/cacerts && cd "$_"
213-
214-
if [ ! -f root-key.pem ] || [ ! -f root-cert.pem ]; then
215-
openssl req -newkey rsa:$RSA_KEY_LENGTH -nodes -keyout root-key.pem -x509 -days 36500 -out root-cert.pem > /dev/null 2>&1 <<EOF
216-
CN
217-
Shanghai
218-
Shanghai
219-
Higress
220-
Gateway
221-
Root CA
222-
223-
224-
225-
EOF
226-
checkExitCode " Generating Root CA certificate for pilot fails with $?"
227-
fi
228-
229-
if [ ! -f ca-key.pem ] || [ ! -f ca-cert.pem ]; then
230-
cat <<EOF > ca.cfg
231-
[req]
232-
distinguished_name = req_distinguished_name
233-
req_extensions = v3_req
234-
prompt = no
235-
236-
[req_distinguished_name]
237-
C = CN
238-
ST = Shanghai
239-
L = Shanghai
240-
O = Higress
241-
CN = Higress CA
242-
243-
[v3_req]
244-
keyUsage = keyCertSign
245-
basicConstraints = CA:TRUE
246-
subjectAltName = @alt_names
247-
248-
[alt_names]
249-
DNS.1 = ca.higress.io
250-
EOF
251-
openssl genrsa -out ca-key.pem $RSA_KEY_LENGTH > /dev/null \
252-
&& openssl req -new -key ca-key.pem -out ca-cert.csr -config ca.cfg -batch -sha256 > /dev/null 2>&1 \
253-
&& openssl x509 -req -days 36500 -in ca-cert.csr -sha256 -CA root-cert.pem -CAkey root-key.pem -CAcreateserial -out ca-cert.pem -extensions v3_req -extfile ca.cfg > /dev/null 2>&1
254-
checkExitCode "Generating intermedia CA certificate for pilot fails with $?"
255-
cp ca-cert.pem cert-chain.pem > /dev/null
256-
chmod a+r ca-key.pem
257-
rm ./*csr > /dev/null
258-
fi
259-
260-
if [ ! -f gateway-key.pem ] || [ ! -f gateway-cert.pem ]; then
261-
cat <<EOF > gateway.cfg
262-
[req]
263-
distinguished_name = req_distinguished_name
264-
req_extensions = v3_req
265-
prompt = no
266-
267-
[req_distinguished_name]
268-
C = CN
269-
ST = Shanghai
270-
L = Shanghai
271-
O = Higress
272-
CN = Higress Gateway
273-
274-
[v3_req]
275-
keyUsage = digitalSignature, keyEncipherment
276-
subjectAltName = URI:spiffe://cluster.local/ns/higress-system/sa/higress-gateway
277-
EOF
278-
openssl genrsa -out gateway-key.pem $RSA_KEY_LENGTH > /dev/null \
279-
&& openssl req -new -key gateway-key.pem -out gateway-cert.csr -config gateway.cfg -batch -sha256 > /dev/null 2>&1 \
280-
&& openssl x509 -req -days 36500 -in gateway-cert.csr -sha256 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out gateway-cert.pem -extensions v3_req -extfile gateway.cfg > /dev/null 2>&1
281-
checkExitCode "Generating certificate for gateway fails with $?"
282-
chmod a+r gateway-key.pem
283-
fi
284-
}
285-
286-
initializeGateway() {
287-
echo "Initializing gateway configurations..."
288-
289-
mkdir -p $VOLUMES_ROOT/gateway/certs && cd "$_"
290-
cp $VOLUMES_ROOT/pilot/cacerts/root-cert.pem ./root-cert.pem
291-
cp $VOLUMES_ROOT/pilot/cacerts/gateway-cert.pem ./cert-chain.pem
292-
cp $VOLUMES_ROOT/pilot/cacerts/gateway-key.pem ./key.pem
293-
cat $VOLUMES_ROOT/pilot/cacerts/ca-cert.pem >>./cert-chain.pem
294-
295-
mkdir -p $VOLUMES_ROOT/gateway/podinfo && cd "$_"
296-
cat <<EOF >./labels
297-
app="higress-gateway"
298-
higress="higress-system-higress-gateway"
299-
EOF
300-
301-
mkdir -p $VOLUMES_ROOT/gateway/istio/data
302-
303-
mkdir -p $VOLUMES_ROOT/gateway/log
304-
touch $VOLUMES_ROOT/gateway/log/access.log
305-
}
306-
307-
initializePrometheus() {
308-
echo "Initializing Prometheus configurations..."
309-
310-
mkdir -p $VOLUMES_ROOT/prometheus/config && cd "$_"
311-
cat <<EOF >./prometheus.yaml
312-
global:
313-
scrape_interval: 15s
314-
evaluation_interval: 15s
315-
scrape_configs:
316-
- job_name: 'prometheus'
317-
metrics_path: /prometheus/metrics
318-
static_configs:
319-
- targets: ['localhost:9090']
320-
- job_name: 'gateway'
321-
metrics_path: /stats/prometheus
322-
static_configs:
323-
- targets: ['gateway:15020']
324-
labels:
325-
container: 'higress-gateway'
326-
namespace: 'higress-system'
327-
higress: 'higress-system-higress-gateway'
328-
pod: 'higress'
329-
EOF
330-
331-
mkdir -p $VOLUMES_ROOT/prometheus/data
332-
chmod a+rwx $VOLUMES_ROOT/prometheus/data
333-
}
334-
335-
initializePromtail() {
336-
echo "Initializing Promtail configurations..."
337-
338-
mkdir -p $VOLUMES_ROOT/promtail/config && cd "$_"
339-
cat <<EOF >./promtail.yaml
340-
server:
341-
log_level: info
342-
http_listen_port: 3101
343-
344-
clients:
345-
- url: http://loki:3100/loki/api/v1/push
346-
347-
positions:
348-
filename: /var/promtail/promtail-positions.yaml
349-
target_config:
350-
sync_period: 10s
351-
scrape_configs:
352-
- job_name: access-logs
353-
static_configs:
354-
- targets:
355-
- localhost
356-
labels:
357-
__path__: /var/log/proxy/access.log
358-
pipeline_stages:
359-
- json:
360-
expressions:
361-
authority:
362-
method:
363-
path:
364-
protocol:
365-
request_id:
366-
response_code:
367-
response_flags:
368-
route_name:
369-
trace_id:
370-
upstream_cluster:
371-
upstream_host:
372-
upstream_transport_failure_reason:
373-
user_agent:
374-
x_forwarded_for:
375-
- labels:
376-
authority:
377-
method:
378-
path:
379-
protocol:
380-
request_id:
381-
response_code:
382-
response_flags:
383-
route_name:
384-
trace_id:
385-
upstream_cluster:
386-
upstream_host:
387-
upstream_transport_failure_reason:
388-
user_agent:
389-
x_forwarded_for:
390-
- timestamp:
391-
source: timestamp
392-
format: RFC3339Nano
393-
EOF
394-
395-
mkdir -p $VOLUMES_ROOT/promtail/data
396-
chmod a+rwx $VOLUMES_ROOT/promtail/data
397-
}
398-
399-
initializeLoki() {
400-
echo "Initializing Loki configurations..."
401-
402-
mkdir -p $VOLUMES_ROOT/loki/config && cd "$_"
403-
cat <<EOF >./config.yaml
404-
auth_enabled: false
405-
common:
406-
compactor_address: 'loki'
407-
path_prefix: /var/loki
408-
replication_factor: 1
409-
storage:
410-
filesystem:
411-
chunks_directory: /var/loki/chunks
412-
rules_directory: /var/loki/rules
413-
frontend:
414-
scheduler_address: ""
415-
frontend_worker:
416-
scheduler_address: ""
417-
index_gateway:
418-
mode: ring
419-
limits_config:
420-
max_cache_freshness_per_query: 10m
421-
reject_old_samples: true
422-
reject_old_samples_max_age: 168h
423-
split_queries_by_interval: 15m
424-
memberlist:
425-
join_members:
426-
- loki
427-
query_range:
428-
align_queries_with_step: true
429-
ruler:
430-
storage:
431-
type: local
432-
runtime_config:
433-
file: /etc/loki/config/runtime-config.yaml
434-
schema_config:
435-
configs:
436-
- from: "2022-01-11"
437-
index:
438-
period: 24h
439-
prefix: loki_index_
440-
object_store: filesystem
441-
schema: v12
442-
store: boltdb-shipper
443-
server:
444-
http_listen_port: 3100
445-
grpc_listen_port: 9095
446-
storage_config:
447-
hedging:
448-
at: 250ms
449-
max_per_second: 20
450-
up_to: 3
451-
tracing:
452-
enabled: false
453-
EOF
454-
cat <<EOF >./runtime-config.yaml
455-
{}
456-
EOF
457-
458-
mkdir -p $VOLUMES_ROOT/loki/data/
459-
chmod a+rwx $VOLUMES_ROOT/loki/data/
460-
}
461-
462-
initializeGrafana() {
463-
echo "Initializing Grafana configurations..."
464-
465-
mkdir -p $VOLUMES_ROOT/grafana/config && cd "$_"
466-
cat <<EOF >./grafana.ini
467-
[server]
468-
protocol=http
469-
domain=localhost
470-
root_url="%(protocol)s://%(domain)s/grafana"
471-
serve_from_sub_path=true
472-
473-
[auth]
474-
disable_login_form=true
475-
disable_signout_menu=true
476-
477-
[auth.anonymous]
478-
enabled=true
479-
org_name=Main Org.
480-
org_role=Viewer
481-
482-
[users]
483-
default_theme=light
484-
viewers_can_edit=true
485-
486-
[security]
487-
allow_embedding=true
488-
EOF
489-
490-
mkdir -p $VOLUMES_ROOT/grafana/lib
491-
chmod a+rwx $VOLUMES_ROOT/grafana/lib
492-
}
493-
494-
initializeO11y() {
495-
initializePrometheus
496-
initializePromtail
497-
initializeLoki
498-
initializeGrafana
499-
}
500155

501156
initializeConfigStorage
502157
initializeApiServer
503158
initializeController
504-
initializePilot
505-
initializeGateway
506-
initializeO11y

0 commit comments

Comments
 (0)