-
Notifications
You must be signed in to change notification settings - Fork 0
/
startK3sMultiDocker
executable file
·53 lines (47 loc) · 1.98 KB
/
startK3sMultiDocker
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
#!/bin/bash
cd "$( dirname "${BASH_SOURCE[0]}" )"
INSTANCE_NAME=${INSTANCE_NAME:-k3smulti}
YAML=${QEMU_YAML:-./docker-k3s-multiarch.yaml}
# start lima from desired yaml file
# check for disks and create them first with limactl disk create blah --size blah
DISK_NAME=docker
DISK_PRESENT=$(limactl disk ls 2>/dev/null| grep $DISK_NAME)
if [ -z "$DISK_PRESENT" ]; then
limactl disk create $DISK_NAME --size 100G
fi
EXISTS="$(limactl list -f '{{ .Name }}' $INSTANCE_NAME)"
if [ -z "$EXISTS" ]; then
echo "prune cache and start $INSTANCE_NAME using $YAML"
limactl prune
limactl start --name $INSTANCE_NAME --tty=false $YAML
else
INSTANCE_NAME=$INSTANCE_NAME ./restartInstance
fi
echo "Performing above steps ..."
# set up docker
docker context create lima-$INSTANCE_NAME --docker "host=unix://$HOME/.lima/$INSTANCE_NAME/sock/docker.sock"
docker context use lima-$INSTANCE_NAME
# copy kubeconfig
mkdir -p ~/.lima/$INSTANCE_NAME/copied-from-guest
HERE="$(pwd)"
cd
limactl shell $INSTANCE_NAME sudo cat /etc/rancher/k3s/k3s.yaml > ~/.lima/$INSTANCE_NAME/copied-from-guest/kubeconfig.yaml
# merge kube config with current one
MY_CONFIG=~/.kube/config
GUEST_CONFIG=/tmp/k3s$$
NEW_CONFIG=/tmp/new$$
limactl shell $INSTANCE_NAME sudo kubectl config view --flatten > $GUEST_CONFIG
sed -i "s/default/$INSTANCE_NAME/g" $GUEST_CONFIG
KUBECONFIG=$MY_CONFIG kubectl config set-context default
KUBECONFIG=$MY_CONFIG kubectl config use-context default
KUBECONFIG=$MY_CONFIG kubectl config delete-user $INSTANCE_NAME
KUBECONFIG=$MY_CONFIG kubectl config delete-context $INSTANCE_NAME
KUBECONFIG=$MY_CONFIG kubectl config delete-cluster $INSTANCE_NAME
KUBECONFIG=$MY_CONFIG:$GUEST_CONFIG kubectl config view --merge --flatten > $NEW_CONFIG
TS=$(stat --format "%y" $MY_CONFIG | sed 's/\..*//;s/ /_/g;s/://g')
mv $MY_CONFIG ${MY_CONFIG}_${TS}
mv $NEW_CONFIG $MY_CONFIG
rm -f $GUEST_CONFIG $NEW_CONFIG
cd "$HERE"
INSTANCE_NAME=$INSTANCE_NAME ./startRegistry
echo "see \"$HOME/.lima/$INSTANCE_NAME/serial.log\" for startup details"