Skip to content

Commit 642dbb0

Browse files
committed
add systemd services for configuration after start
the services does the various needed tasks to setup the ocp or microshift cluster, these systemd units runs small shell scripts which are based on: https://github.com/crc-org/crc-cloud/blob/main/pkg/bundle/setup/clustersetup.sh and does the following tasks: - creates crc specific configurations for dnsmasq - sets a new uuid as cluster id - creates the pod for routes-controller - tries to grow the disk and filesystem - checks if the cluster operators are ready - adds the pull secret to the cluster - sets kubeadmin and developer user passwords - sets a custom ca for authentication - sets custom nip.io cluster domain
1 parent 90d221c commit 642dbb0

29 files changed

+488
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ podman-remote/
1111
.sw[a-p]
1212
crc-cluster-kube-apiserver-operator
1313
crc-cluster-kube-controller-manager-operator
14+
systemd/crc-dnsmasq.sh

createdisk-library.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ function prepare_hyperV() {
216216
echo 'CONST{virt}=="microsoft", RUN{builtin}+="kmod load hv_sock"' > /etc/udev/rules.d/90-crc-vsock.rules
217217
EOF
218218
}
219+
219220
function prepare_qemu_guest_agent() {
220221
local vm_ip=$1
221222

@@ -385,3 +386,36 @@ function remove_pull_secret_from_disk() {
385386
esac
386387
}
387388

389+
function copy_systemd_units() {
390+
case "${BUNDLE_TYPE}" in
391+
"snc"|"okd")
392+
export APPS_DOMAIN="apps-crc.testing"
393+
envsubst '${APPS_DOMAIN}' < systemd/dnsmasq.sh.template > systemd/crc-dnsmasq.sh
394+
unset APPS_DOMAIN
395+
;;
396+
"microshift")
397+
export APPS_DOMAIN="apps.crc.testing"
398+
envsubst '${APPS_DOMAIN}' < systemd/dnsmasq.sh.template > systemd/crc-dnsmasq.sh
399+
unset APPS_DOMAIN
400+
;;
401+
esac
402+
403+
${SSH} core@${VM_IP} -- 'mkdir -p /home/core/systemd-units && mkdir -p /home/core/systemd-scripts'
404+
${SCP} systemd/crc-*.service core@${VM_IP}:/home/core/systemd-units/
405+
${SCP} systemd/crc-*.path core@${VM_IP}:/home/core/systemd-units/
406+
${SCP} systemd/crc-*.sh core@${VM_IP}:/home/core/systemd-scripts/
407+
408+
case "${BUNDLE_TYPE}" in
409+
"snc"|"okd")
410+
${SCP} systemd/ocp-*.service core@${VM_IP}:/home/core/systemd-units/
411+
${SCP} systemd/ocp-*.path core@${VM_IP}:/home/core/systemd-units/
412+
${SCP} systemd/ocp-*.sh core@${VM_IP}:/home/core/systemd-scripts/
413+
;;
414+
esac
415+
416+
${SSH} core@${VM_IP} -- 'sudo cp /home/core/systemd-units/* /etc/systemd/system/ && sudo cp /home/core/systemd-scripts/* /usr/local/bin/'
417+
${SSH} core@${VM_IP} -- 'ls /home/core/systemd-scripts/ | xargs -t -I % sudo chmod +x /usr/local/bin/%'
418+
${SSH} core@${VM_IP} -- 'sudo restorecon -rv /usr/local/bin'
419+
${SSH} core@${VM_IP} -- 'ls /home/core/systemd-units/ | xargs sudo systemctl enable'
420+
${SSH} core@${VM_IP} -- 'rm -rf /home/core/systemd-units /home/core/systemd-scripts'
421+
}

createdisk.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ EOF
137137
${SSH} core@${VM_IP} -- "sudo rpm-ostree install qemu-user-static-x86"
138138
fi
139139

140+
copy_systemd_units
141+
140142
cleanup_vm_image ${VM_NAME} ${VM_IP}
141143

142144
# Delete all the pods except openshift-multus (which have file for crio cni config)

docs/self-sufficient-bundle.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Self sufficient bundles
2+
3+
Since release 4.19.0 of OpenShift Local, the bundles generated by `snc` contain additional systemd services to provision the cluster and remove the need for
4+
an outside entity to provision the cluster, although an outside process needs to create some files on pre-defined locations inside the VM for the systemd
5+
services to do their work.
6+
7+
## The following table lists the systemd services and the location of files they need to provision the cluster, users of SNC need to create those files
8+
9+
| Systemd unit | Runs for (ocp, MicroShift, both) | Input files location | Marker env variables |
10+
| :----------------------------: | :------------------------------: | :----------------------------------: | :------------------: |
11+
| `crc-cluster-status.service` | both | none | none |
12+
| `crc-pullsecret.service` | both | /opt/crc/pull-secret | none |
13+
| `crc-dnsmasq.service` | both | none | none |
14+
| `crc-routes-controller.service`| both | none | none |
15+
| `ocp-cluster-ca.service` | ocp | /opt/crc/custom-ca.crt | CRC_CLOUD=1 |
16+
| `ocp-clusterid.service` | ocp | none | none |
17+
| `ocp-custom-domain.service` | ocp | none | CRC_CLOUD=1 |
18+
| `ocp-growfs.service` | ocp | none | none |
19+
| `ocp-userpasswords.service` | ocp | /opt/crc/pass_{kubeadmin, developer} | none |
20+
21+
In addition to the above services we have `ocp-cluster-ca.path`, `crc-pullsecret.path` and `ocp-userpasswords.path` that monitors the filesystem paths
22+
related to their `*.service` counterparts and starts the service when the paths become available.
23+
24+
> [!NOTE]
25+
> "Marker env variable" is set using an env file, if the required env variable is not set then unit is skipped
26+
> some units are run only when CRC_CLOUD=1 is set, these are only needed when using the bundles with crc-cloud
27+
28+
The systemd services are heavily based on the [`clustersetup.sh`](https://github.com/crc-org/crc-cloud/blob/main/pkg/bundle/setup/clustersetup.sh) script found in the `crc-cloud` project.
29+
30+
## Naming convention for the systemd unit files
31+
32+
Systemd units that are needed for both 'OpenShift' and 'MicroShift' are named as `crc-*.service`, units that are needed only for 'OpenShift' are named
33+
as `ocp-*.service` and when we add units that are only needed for 'MicroShift' they should be named as `ucp-*.service`
34+

systemd/crc-cluster-status.service

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=CRC Unit checking if cluster is ready
3+
After=kubelet.service
4+
5+
[Service]
6+
Type=oneshot
7+
ExecStart=/usr/local/bin/crc-cluster-status.sh
8+
RemainAfterExit=true
9+
10+
[Install]
11+
WantedBy=multi-user.target

systemd/crc-cluster-status.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
export KUBECONFIG=/opt/kubeconfig
6+
7+
function check_cluster_healthy() {
8+
WAIT="authentication|console|etcd|ingress|openshift-apiserver"
9+
10+
until `oc get co > /dev/null 2>&1`
11+
do
12+
sleep 2
13+
done
14+
15+
for i in $(oc get co | grep -P "$WAIT" | awk '{ print $3 }')
16+
do
17+
if [[ $i == "False" ]]
18+
then
19+
return 1
20+
fi
21+
done
22+
return 0
23+
}
24+
25+
rm -rf /tmp/.crc-cluster-ready
26+
27+
COUNTER=0
28+
CLUSTER_HEALTH_SLEEP=8
29+
CLUSTER_HEALTH_RETRIES=500
30+
31+
while ! check_cluster_healthy
32+
do
33+
sleep $CLUSTER_HEALTH_SLEEP
34+
if [[ $COUNTER == $CLUSTER_HEALTH_RETRIES ]]
35+
then
36+
return 1
37+
fi
38+
((COUNTER++))
39+
done
40+
41+
# need to set a marker to let `crc` know the cluster is ready
42+
touch /tmp/.crc-cluster-ready
43+

systemd/crc-dnsmasq.service

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=CRC Unit for configuring dnsmasq
3+
Wants=network-online.target
4+
After=network-online.target ssh-access.target ovs-configuration.service
5+
StartLimitIntervalSec=0
6+
7+
[Service]
8+
Type=oneshot
9+
Restart=on-failure
10+
EnvironmentFile=/etc/systemd/system/crc-env
11+
ExecStart=/usr/local/bin/crc-dnsmasq.sh
12+
ExecStartPost=/usr/bin/systemctl start dnsmasq.service
13+
14+
[Install]
15+
WantedBy=multi-user.target

systemd/crc-pullsecret.path

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=CRC Unit for monitoring the pull secret path
3+
After=kubelet.service
4+
5+
[Path]
6+
PathExists=/opt/crc/pull-secret
7+
TriggerLimitIntervalSec=1min
8+
TriggerLimitBurst=0
9+
10+
[Install]
11+
WantedBy=multi-user.target

systemd/crc-pullsecret.service

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=CRC Unit for adding pull secret to cluster
3+
After=kubelet.service
4+
5+
[Service]
6+
Type=oneshot
7+
ExecStart=/usr/local/bin/crc-pullsecret.sh
8+
9+
[Install]
10+
WantedBy=multi-user.target

systemd/crc-pullsecret.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
source /usr/local/bin/crc-systemd-common.sh
6+
export KUBECONFIG="/opt/kubeconfig"
7+
8+
wait_for_resource secret
9+
10+
# check if existing pull-secret is valid if not add the one from /opt/crc/pull-secret
11+
existingPsB64=$(oc get secret pull-secret -n openshift-config -o jsonpath="{['data']['\.dockerconfigjson']}")
12+
existingPs=$(echo "${existingPsB64}" | base64 -d)
13+
14+
echo "${existingPs}" | jq -e '.auths'
15+
16+
if [[ $? != 0 ]]; then
17+
pullSecretB64=$(cat /opt/crc/pull-secret | base64 -w0)
18+
oc patch secret pull-secret -n openshift-config --type merge -p "{\"data\":{\".dockerconfigjson\":\"${pullSecretB64}\"}}"
19+
rm -f /opt/crc/pull-secret
20+
fi
21+

systemd/crc-routes-controller.service

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[Unit]
2+
Description=CRC Unit starting routes controller
3+
Wants=network-online.target gvisor-tap-vsock.service sys-class-net-tap0.device
4+
After=sys-class-net-tap0.device network-online.target kubelet.service gvisor-tap-vsock.service
5+
6+
[Service]
7+
Type=oneshot
8+
EnvironmentFile=/etc/systemd/system/crc-env
9+
ExecStart=/usr/local/bin/crc-routes-controller.sh
10+
11+
[Install]
12+
WantedBy=multi-user.target

systemd/crc-routes-controller.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -x
4+
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+
10+
source /usr/local/bin/crc-systemd-common.sh
11+
export KUBECONFIG=/opt/kubeconfig
12+
13+
wait_for_resource pods
14+
15+
oc apply -f /opt/crc/routes-controller.yaml
16+

systemd/crc-systemd-common.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# $1 is the resource to check
2+
# $2 is an optional maximum retry count; default 20
3+
function wait_for_resource() {
4+
local retry=0
5+
local max_retry=${2:-20}
6+
until `oc get "$1" > /dev/null 2>&1`
7+
do
8+
[ $retry == $max_retry ] && exit 1
9+
sleep 5
10+
((retry++))
11+
done
12+
}

systemd/dnsmasq.sh.template

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
if [[ ${CRC_NETWORK_MODE_USER} -eq 1 ]]; then
6+
echo -n "network-mode 'user' detected: skipping dnsmasq configuration"
7+
exit 0
8+
fi
9+
10+
hostName=$(hostname)
11+
hostIp=$(hostname --all-ip-addresses | awk '{print $1}')
12+
13+
cat << EOF > /etc/dnsmasq.d/crc-dnsmasq.conf
14+
listen-address=192.168.130.11
15+
expand-hosts
16+
log-queries
17+
local=/crc.testing/
18+
domain=crc.testing
19+
address=/${APPS_DOMAIN}/192.168.130.11
20+
address=/api.crc.testing/192.168.130.11
21+
address=/api-int.crc.testing/192.168.130.11
22+
address=/$hostName/$hostIp
23+
EOF
24+

systemd/ocp-cluster-ca.path

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=CRC Unit monitoring custom-ca.crt file path
3+
After=kubelet.service
4+
5+
[Path]
6+
PathExists=/opt/crc/custom-ca.crt
7+
TriggerLimitIntervalSec=1min
8+
TriggerLimitBurst=0
9+
10+
[Install]
11+
WantedBy=multi-user.target

systemd/ocp-cluster-ca.service

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=CRC Unit setting custom cluster ca
3+
After=kubelet.service ocp-clusterid.service
4+
5+
[Service]
6+
Type=oneshot
7+
ExecStart=/usr/local/bin/ocp-cluster-ca.sh
8+
9+
[Install]
10+
WantedBy=multi-user.target

systemd/ocp-cluster-ca.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
source /usr/local/bin/crc-systemd-common.sh
6+
export KUBECONFIG="/opt/kubeconfig"
7+
PASS_KUBEADMIN="$(cat /opt/crc/password_kubeadmin)"
8+
9+
wait_for_resource configmap
10+
11+
custom_ca_path=/opt/crc/custom-ca.crt
12+
13+
if [[ ! -f ${custom_ca_path} ]]; then
14+
echo "Cert bundle /opt/crc/custom-ca.crt not found"
15+
exit 0
16+
fi
17+
18+
oc create configmap client-ca-custom -n openshift-config --from-file=ca-bundle.crt=${custom_ca_path}
19+
oc patch apiserver cluster --type=merge -p '{"spec": {"clientCA": {"name": "client-ca-custom"}}}'
20+
oc create configmap admin-kubeconfig-client-ca -n openshift-config --from-file=ca-bundle.crt=${custom_ca_path} \
21+
--dry-run -o yaml | oc replace -f -
22+
23+
rm -f /opt/crc/custom-ca.crt
24+
25+
echo "Logging in again to update $KUBECONFIG"
26+
COUNTER=0
27+
MAXIMUM_LOGIN_RETRY=500
28+
until `oc login --insecure-skip-tls-verify=true -u kubeadmin -p "$PASS_KUBEADMIN" https://api.crc.testing:6443 > /dev/null 2>&1`
29+
do
30+
[ $COUNTER == $MAXIMUM_LOGIN_RETRY ] && echo "Unable to login to the cluster..., installation failed."
31+
echo "Logging into OpenShift with updated credentials try $COUNTER, hang on...."
32+
sleep 5
33+
((COUNTER++))
34+
done

systemd/ocp-clusterid.service

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=CRC Unit setting random cluster ID
3+
After=kubelet.service
4+
5+
[Service]
6+
Type=oneshot
7+
ExecStart=/usr/local/bin/ocp-clusterid.sh
8+
Restart=on-failure
9+
10+
[Install]
11+
WantedBy=multi-user.target

systemd/ocp-clusterid.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
source /usr/local/bin/crc-systemd-common.sh
6+
export KUBECONFIG="/opt/kubeconfig"
7+
uuid=$(uuidgen)
8+
9+
wait_for_resource clusterversion
10+
11+
oc patch clusterversion version -p "{\"spec\":{\"clusterID\":\"${uuid}\"}}" --type merge

systemd/ocp-custom-domain.service

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=CRC Unit setting nip.io domain for cluster
3+
After=kubelet.service ocp-clusterid.service ocp-cluster-ca.service
4+
5+
[Service]
6+
Type=oneshot
7+
EnvironmentFile=/etc/systemd/system/crc-env
8+
ExecStart=/usr/local/bin/ocp-custom-domain.sh
9+
10+
[Install]
11+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)