-
Notifications
You must be signed in to change notification settings - Fork 2
/
lkr
executable file
·213 lines (186 loc) · 6.25 KB
/
lkr
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
set -e
trap exit SIGINT;
echo "𝖫𝖪𝖱 - 𝖫𝖮𝖢𝖠𝖫 𝖪𝖴𝖡𝖤𝖱𝖭𝖤𝖳𝖤𝖲 𝖱𝖴𝖭𝖭𝖤𝖱"
usage="Run Local Kubernetes cluster.\n\n\tFind more information at https://github.com/nicoloboschi/lkr\n\nCommands:\n"
usage+="\tstart\tStart the cluster and set as current context in your kubectl configuration.\n"
usage+="\tstop\tStop the cluster.\n"
usage+="\trestart\tStop and restart the cluster and set as current context in your kubectl configuration.\n"
usage+="\tstatus\tCheck if the cluster is running or not.\n"
usage+="\tlogs\tShow cluster logs.\n"
usage+="\tset\tConfigure the cluster in your kubectl configuration.\n"
usage+="\tuse\tConfigure the cluster in your kubectl configuration as current context.\n"
usage+="\tload\tLoad a local docker image into the cluster's node.\n"
usage+="\tlist\tList all docker images available into the cluster's node.\n"
usage+="\nShortcuts:\n"
usage+="\t9\tStarts k9s for the cluster.\n"
usage+="\tk\tKubectl command referred to the local cluster.\n"
trap "exit" INT TERM
if [ "$#" -eq 0 ]; then
printf "$usage"
exit 1
fi
detect_k3s_image() {
if [ -z "$LKR_IMAGE" ]; then
if [ "$(uname -m)" == "arm64" ]; then
echo "rancher/k3s:v1.30.1-k3s1-arm64"
else
echo "rancher/k3s:v1.30.1-k3s1"
fi
else
echo "$LKR_IMAGE"
fi
}
#container_engine=podman
container_engine=docker
container_name=lkr
docker_image=$(detect_k3s_image)
workdir=/tmp/lkr
LKR_VERBOSE=${LKR_VERBOSE:-"false"}
lkr_log() {
echo "$@"
}
lkr_logv() {
if [ "$LKR_VERBOSE" == "true" ]; then
echo "$@"
fi
}
with_progress_bar() {
("$@") &
pid=$!
i=1
sp="/-\|"
echo -n ' '
jobs="NaN"
while [ $jobs != "0" ]; do
jobs=$(jobs | grep Running | wc -l)
printf "\b${sp:i++%${#sp}:1}"
printf "\033[0K\r"
sleep 0.1
done
}
with_retries() {
for i in {1..5}; do "$@" 2>/dev/null && break || sleep 1; done
}
cleanup_docker() {
lkr_logv "Cleaning up docker."
docker volume prune -f >/dev/null 2>&1 || true
docker network prune -f >/dev/null 2>&1 || true
}
start() {
cleanup_docker
mkdir -p $workdir
mkdir -p $workdir/containers
lkr_log "Starting the cluster. (k3s image: $docker_image)"
($container_engine run --rm -d \
--name $container_name \
-v /sys/fs/cgroup:/sys/fs/cgroup:rw \
-v $workdir/containers:/containers \
--cgroupns=host \
--privileged \
-p 6443:6443 \
$docker_image server --tls-san=localhost --disable=traefik --service-node-port-range=30000-32767) > /dev/null
with_retries $container_engine cp $container_name:/etc/rancher/k3s/k3s.yaml $workdir/kubeconfig
with_retries KUBECONFIG=$workdir/kubeconfig kubectl wait --for=condition=Ready nodes --all --timeout=600s
lkr_log "Cluster ready, configuring 'ns' namespace."
in_context kubectl create namespace ns
in_context kubectl config set-context --current --namespace=ns > /dev/null
lkr_log "Cluster configured."
with_progress_bar set_in_kube_config
with_progress_bar use_in_kube_config
}
stop() {
$container_engine rm -f $container_name >/dev/null
rm -rf $workdir
}
in_context() {
KUBECONFIG=$workdir/kubeconfig "$@"
}
load() {
lkr_log "Loading image $1"
id=$(docker inspect $1 | jq -r '.[0].Id')
if [ "$id" == "null" ]; then
lkr_log "Image not found, please pull it first."
exit 1
fi
filename=$(echo $id | base64)
host_filename_done=$workdir/containers/${filename}_done
host_filename=$workdir/containers/$filename
if [ ! -f "$host_filename_done" ]; then
lkr_log "Generating image tarball."
docker save $1 > $host_filename
touch $host_filename_done
lkr_log "Generated."
fi
lkr_log "Importing image."
$container_engine exec $container_name ctr image import --all-platforms /containers/$filename
lkr_log "Imported."
}
list() {
$container_engine exec $container_name ctr image list | awk '{print $1}' | grep -v "sha256"
}
shell() {
$container_engine exec -it $container_name sh
}
set_in_kube_config() {
certificate_auth_data=$(cat $workdir/kubeconfig | grep certificate-authority-data | awk -F: '{print $2}' | tr -d ' ')
client_ca_data=$(cat $workdir/kubeconfig | grep client-certificate-data | awk -F: '{print $2}' | tr -d ' ')
client_key_data=$(cat $workdir/kubeconfig | grep client-key-data | awk -F: '{print $2}' | tr -d ' ')
kubectl config set-cluster lkr --server="https://127.0.0.1:6443" > /dev/null
kubectl config set clusters.lkr.certificate-authority-data $certificate_auth_data > /dev/null
kubectl config set-credentials lkr > /dev/null
kubectl config set users.lkr.client-certificate-data $client_ca_data > /dev/null
kubectl config set users.lkr.client-key-data $client_key_data > /dev/null
kubectl config set-context lkr --cluster=lkr --namespace=ns --user=lkr > /dev/null
}
use_in_kube_config() {
kubectl config use-context lkr
}
get_status() {
output=$(($container_engine ps | grep $container_name) || echo "")
if [ "$output" == "" ]; then
echo "Stopped"
else
echo "Running"
fi
}
if [ "$1" == "start" ]; then
if [ "$(get_status)" == "Running" ]; then
lkr_log "Cluster already running."
with_progress_bar set_in_kube_config
with_progress_bar use_in_kube_config
else
with_progress_bar start
fi
elif [ "$1" == "status" ]; then
get_status
elif [ "$1" == "restart" ]; then
with_progress_bar stop
with_progress_bar start
with_progress_bar set_in_kube_config
with_progress_bar use_in_kube_config
elif [ "$1" == "stop" ]; then
with_progress_bar stop
elif [ "$1" == "logs" ]; then
$container_engine logs $container_name
elif [ "$1" == "shell" ]; then
shell
elif [ "$1" == "set" ]; then
with_progress_bar set_in_kube_config
with_progress_bar use_in_kube_config
elif [ "$1" == "use" ]; then
with_progress_bar use_in_kube_config
elif [ "$1" == "load" ]; then
shift
with_progress_bar load "$@"
elif [ "$1" == "list" ]; then
with_progress_bar list
elif [ "$1" == "k" ]; then
shift
with_progress_bar in_context kubectl "$@"
elif [ "$1" == "9" ]; then
shift
in_context k9s "$@"
else
in_context "$@"
fi