forked from lisenet/kubernetes-homelab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
break-kubernetes-cluster.sh
executable file
·103 lines (82 loc) · 2.61 KB
/
break-kubernetes-cluster.sh
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
#!/bin/bash
# AUTHOR: Tomas Nevar ([email protected])
# NAME: break-kubernetes-cluster.sh
# VERSION: 1.0
# DATE: 13/02/2022 (dd/mm/yy)
# LICENCE: Copyleft free software
set -eu
BINARY_ARRAY=(kubectl)
FUNCTION_ARRAY=(
break_controlplane_1
break_controlplane_2
break_controlplane_3
break_controlplane_4
)
function sanity_checks()
{
for package in "${BINARY_ARRAY[@]}"; do
# Check for binary installation
type "${package}" >/dev/null 2>&1 || { echo >&2 "I require ${package} but it's not installed. Aborting."; exit 1; };
done
}
# "There is more than one way to skin a cat"
function break_controlplane_1()
{
# Break kube-scheduler
local function_name="break-controlplane-1"
local config_file="/etc/kubernetes/manifests/kube-scheduler.yaml"
if [ -s "${config_file}" ]; then
# Break something
sed -i 's/image:/imaginaerum:/g' "${config_file}"
# Do something
kubectl run "busybox-${function_name}" --image=busybox -- sleep 3600
# Ask to fix something
printf "%s\\n%s\\n" "TasK: ${function_name}" "There is a pod 'busybox-${function_name}' scheduled but not running, fix the issue."
fi
}
function break_controlplane_2()
{
# Break kubelet
local function_name="break-controlplane-2"
local config_file="/etc/kubernetes/kubelet.conf"
if [ -s "${config_file}" ]; then
# Break something
sed -i 's/6443/31337/g' "${config_file}"
# Do something
sudo systemctl restart kubelet
# Ask to fix something
printf "%s\\n" "Task: ${function_name}" "Wait for 30s. There is a cluster node with a status of 'NotReady', fix the issue."
fi
}
function break_controlplane_3()
{
# Break etcd
local function_name="break-controlplane-3"
local config_file="/etc/kubernetes/pki/etcd/server.crt"
if [ -s "${config_file}" ]; then
# Break something
mv "${config_file}" "${config_file}.backup"
# Do something
kubectl -n kube-system delete "$(kubectl -n kube-system get po -l component=etcd -o name)"
# Ask to fix something
printf "%s\\n" "Task: ${function_name}" "There is a problem with etcd, fix the issue."
fi
}
function break_controlplane_4()
{
# Break coredns
local function_name="break-controlplane-4"
# Break something
kubectl -n kube-system get cm/coredns -o yaml|sed 's/forward.*/forward\ \. 0.0.0.0 \{/g'|kubectl apply -f - > /dev/null
# Do something
kubectl -n kube-system scale deploy/coredns --replicas=0 > /dev/null
# Ask to fix something
printf "%s\\n" "Task: ${function_name}" "There is a problem with coredns, fix the issue."
}
main()
{
sanity_checks
# Break something at random
eval "${FUNCTION_ARRAY[RANDOM%4]}"
}
main "$@"