Skip to content

Commit 379d0b4

Browse files
committed
Refactor to make more resilient
1 parent afefac8 commit 379d0b4

29 files changed

+273
-119
lines changed

systemd/crc-cluster-status.service

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ StartLimitBurst=10
1010
[Service]
1111
Type=oneshot
1212
Restart=on-failure
13-
RestartSec=40
14-
EnvironmentFile=-/etc/sysconfig/crc-env
13+
RestartSec=10
1514
ExecCondition=/usr/local/bin/crc-self-sufficient-env.sh
1615
ExecStart=/usr/local/bin/crc-cluster-status.sh
1716
RemainAfterExit=true

systemd/crc-cluster-status.sh

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set -o errtrace
77
set -x
88

99
export KUBECONFIG=/opt/kubeconfig
10+
MAXIMUM_LOGIN_RETRY=10
11+
RETRY_DELAY=5
1012

1113
if [ ! -f /opt/crc/pass_kubeadmin ]; then
1214
echo "kubeadmin password file not found"
@@ -21,19 +23,40 @@ fi
2123

2224

2325
echo "Logging into OpenShift with kubeadmin user to update $KUBECONFIG"
24-
COUNTER=1
25-
MAXIMUM_LOGIN_RETRY=10
2626

27-
# use a `(set +x)` subshell to avoid leaking the password
28-
until (set +x ; oc login --insecure-skip-tls-verify=true -u kubeadmin -p "$(cat /opt/crc/pass_kubeadmin)" https://api.crc.testing:6443 > /dev/null 2>&1); do
29-
if [ "$COUNTER" -ge "$MAXIMUM_LOGIN_RETRY" ]; then
30-
echo "Unable to login to the cluster..., authentication failed."
27+
try_login() {
28+
( # use a `(set +x)` subshell to avoid leaking the password
29+
set +x
30+
set +e # don't abort on error in this subshell
31+
oc login --insecure-skip-tls-verify=true \
32+
-u kubeadmin \
33+
-p "$(cat /opt/crc/pass_kubeadmin)" \
34+
https://api.crc.testing:6443 > /dev/null 2>&1
35+
)
36+
success="$?"
37+
if [[ "$success" == 0 ]]; then
38+
echo "Login successed"
39+
else
40+
echo "Login didn't complete ..."
41+
fi
42+
43+
return "$success"
44+
}
45+
46+
counter=1
47+
until try_login; do
48+
((counter++))
49+
if [ "$counter" -ge "$MAXIMUM_LOGIN_RETRY" ]; then
50+
echo "Unable to login to the cluster after $counter tries..., authentication failed."
3151
exit 1
3252
fi
33-
echo "Logging into OpenShift with updated credentials try $COUNTER, hang on...."
34-
sleep 5
35-
((COUNTER++))
53+
sleep "$RETRY_DELAY"
54+
echo "Logging into OpenShift with updated credentials try $counter, hang on...."
3655
done
3756

3857
# need to set a marker to let `crc` know the cluster is ready
3958
touch /tmp/.crc-cluster-ready
59+
60+
echo "All done"
61+
62+
exit 0

systemd/crc-dnsmasq.service

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ StartLimitIntervalSec=30
88
[Service]
99
Type=oneshot
1010
Restart=on-failure
11-
EnvironmentFile=-/etc/sysconfig/crc-env
12-
ExecStartPre=/bin/systemctl start ovs-configuration.service
1311
ExecCondition=/usr/local/bin/crc-self-sufficient-env.sh
12+
ExecStartPre=/bin/systemctl start ovs-configuration.service
1413
ExecStart=/usr/local/bin/crc-dnsmasq.sh
1514
ExecStartPost=/usr/bin/systemctl restart NetworkManager.service
1615
ExecStartPost=/usr/bin/systemctl restart dnsmasq.service

systemd/crc-needs-tap.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
set -o pipefail
4+
set -o errexit
5+
set -o nounset
6+
set -o errtrace
7+
set -x
8+
9+
source /etc/sysconfig/crc-env || echo "WARNING: crc-env not found"
10+
11+
NEED_TAP=0
12+
DONT_NEED_TAP=1
13+
14+
if systemd-detect-virt | grep -q '^apple$' ; then
15+
echo "Running with Apple virtualization. Need tap0."
16+
exit $NEED_TAP
17+
fi
18+
19+
if /usr/local/bin/crc-self-sufficient-env.sh; then
20+
echo "Running with a self-sufficient bundle. Don't keep tap0"
21+
exit $DONT_NEED_TAP
22+
fi
23+
24+
echo "No particular environment detected. Don't keep tap0"
25+
26+
exit DONT_NEED_TAP

systemd/crc-no-tap.service

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[Unit]
2-
Description=Ensure that tap0 network configuration is absent on Apple Virtualization
2+
Description=Ensure that tap0 network configuration is disabled when not necessary
33
Before=NetworkManager.service
4+
45
After=local-fs.target
56
RequiresMountsFor=/etc/NetworkManager/system-connections
67

78
[Service]
89
Type=oneshot
9-
EnvironmentFile=-/etc/sysconfig/crc-env
10+
ExecCondition=!/usr/local/bin/crc-needs-tap.sh
1011
ExecStart=/usr/local/bin/crc-no-tap.sh
1112

1213
[Install]

systemd/crc-no-tap.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#!/bin/bash
22

3-
# Return true if running under Apple Virtualization or CRC_SELF_SUFFICIENT is set, otherwise false
3+
set -o pipefail
4+
set -o errexit
5+
set -o nounset
6+
set -o errtrace
7+
set -x
48

5-
if systemd-detect-virt | grep -q '^apple$' || [ -n "$CRC_SELF_SUFFICIENT" ]; then
6-
rm -f /etc/NetworkManager/system-connections/tap0.nmconnection
7-
systemctl disable --now gv-user-network@tap0.service
8-
fi
9+
echo "Disabling the tap0 network configuration ..."
10+
11+
rm -f /etc/NetworkManager/system-connections/tap0.nmconnection
12+
systemctl disable --now [email protected]
913

1014
exit 0

systemd/crc-pullsecret.service

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ ConditionPathExists=!/opt/crc/%n.done
88
[Service]
99
Type=oneshot
1010
Restart=on-failure
11-
RestartSec=40
12-
EnvironmentFile=-/etc/sysconfig/crc-env
11+
RestartSec=10
1312
ExecCondition=/usr/local/bin/crc-self-sufficient-env.sh
1413
ExecStart=/usr/local/bin/crc-pullsecret.sh
1514
ExecStartPost=-touch /opt/crc/%n.done

systemd/crc-pullsecret.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,32 @@ set -x
99
source /usr/local/bin/crc-systemd-common.sh
1010
export KUBECONFIG="/opt/kubeconfig"
1111

12-
wait_for_resource secret
12+
wait_for_resource_or_die secret
13+
14+
set +x # disable the logging to avoid leaking the pull secrets
1315

1416
set +x # disable the logging to avoid leaking the pull secrets
1517

1618
# check if existing pull-secret is valid if not add the one from /opt/crc/pull-secret
17-
existingPsB64=$(oc get secret pull-secret -n openshift-config -o jsonpath="{['data']['\.dockerconfigjson']}")
18-
existingPs=$(echo "${existingPsB64}" | base64 -d)
19+
existingPs=$(oc get secret pull-secret -n openshift-config \
20+
-o jsonpath="{['data']['\.dockerconfigjson']}" \
21+
| base64 -d)
1922

2023
# check if the .auths field is there
21-
if echo "${existingPs}" | jq -e 'has("auths")' >/dev/null 2>&1; then
24+
echo "${existingPs}" | jq -e '.auths' > /dev/null
25+
has_auths_field=$?
26+
27+
if [[ "$has_auths_field" == 0 ]]; then
2228
echo "Cluster already has the pull secrets, nothing to do"
2329
exit 0
2430
fi
2531

2632
echo "Cluster doesn't have the pull secrets. Setting them from /opt/crc/pull-secret ..."
27-
pullSecretB64=$(base64 -w0 < /opt/crc/pull-secret)
28-
oc patch secret pull-secret -n openshift-config --type merge -p "{\"data\":{\".dockerconfigjson\":\"${pullSecretB64}\"}}"
33+
pullSecretB64=$(cat /opt/crc/pull-secret | base64 -w0)
34+
oc patch secret pull-secret -n openshift-config --type merge \
35+
-p '{"data":{".dockerconfigjson": "'${pullSecretB64}'"}}'
36+
37+
38+
echo "All done"
2939

3040
exit 0

systemd/crc-routes-controller.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ StartLimitBurst=10
88
Type=oneshot
99
Restart=on-failure
1010
RestartSec=40
11-
EnvironmentFile=-/etc/sysconfig/crc-env
11+
ExecCondition=/usr/local/bin/crc-user-mode-networking.sh
1212
ExecCondition=/usr/local/bin/crc-self-sufficient-env.sh
1313
ExecStart=/usr/local/bin/crc-routes-controller.sh
1414

systemd/crc-routes-controller.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#!/bin/bash
22

3+
set -o pipefail
4+
set -o errexit
5+
set -o nounset
6+
set -o errtrace
37
set -x
48

5-
if [[ ${CRC_NETWORK_MODE_USER} -eq 0 ]]; then
6-
echo -n "network-mode 'system' detected: skipping routes-controller pod deployment"
7-
exit 0
8-
fi
9+
ROUTE_CONTROLLER=/opt/crc/routes-controller.yaml
910

1011
source /usr/local/bin/crc-systemd-common.sh
12+
1113
export KUBECONFIG=/opt/kubeconfig
1214

13-
wait_for_resource pods
15+
wait_for_resource_or_die pods
16+
wait_for_resource_or_die deployments
1417

15-
oc apply -f /opt/crc/routes-controller.yaml
18+
oc apply -f "$ROUTE_CONTROLLER"
1619

20+
exit 0

0 commit comments

Comments
 (0)