forked from schu/kubedee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubedee
executable file
·192 lines (171 loc) · 7.58 KB
/
kubedee
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
#!/bin/bash
cmd_create() {
local cluster_name
# [[ -d "${kubedee_dir}/clusters/${cluster_name}" ]] && kubedee::exit_error "Found existing cluster with name: ${cluster_name}"
# kubedee::create_network "${cluster_name}"
# kubedee::create_storage_pool
# kubedee::prune_old_caches "${cluster_name}"
# NOTE: commenting these out, assuming we've taken care of these
# with our packer build
# kubedee::prepare_container_image "${cluster_name}"
# if [[ -n "${kubernetes_version}" ]]; then
# kubedee::fetch_k8s "${kubernetes_version}"
# kubedee::copy_k8s_binaries "${cluster_name}" "${kubedee_cache_dir}/kubernetes/${kubernetes_version}"
# else
# kubedee::copy_k8s_binaries "${cluster_name}" "${bin_dir}"
# fi
# kubedee::copy_etcd_binaries "${cluster_name}"
# kubedee::copy_crio_files "${cluster_name}"
# kubedee::copy_runc_binaries "${cluster_name}"
# kubedee::copy_cni_plugins "${cluster_name}"
# kubedee::create_certificate_authority_etcd "${cluster_name}"
# kubedee::create_certificate_authority_k8s "${cluster_name}"
# kubedee::create_certificate_authority_aggregation "${cluster_name}"
# kubedee::create_certificate_aggregation_client "${cluster_name}"
# kubedee::create_certificate_admin "${cluster_name}"
}
cmd_delete() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
[[ ! -d "${kubedee_dir}/clusters/${cluster_name}" ]] && kubedee::exit_error "Found no existing cluster with name: ${cluster_name}"
kubedee::log_info "Deleting cluster ${cluster_name} ..."
for c in $(lxc list --format json | jq -r '.[].name'); do
[[ "${c}" == "kubedee-${cluster_name}-"* ]] && lxc delete -f "${c}"
done
kubedee::delete_network "${cluster_name}"
[[ "${no_set_context}" == "false" ]] && kubectl config unset current-context >/dev/null
kubectl config delete-context "kubedee-${cluster_name}" &>/dev/null || true
rm -rf "${kubedee_dir}/clusters/${cluster_name}"
}
cmd_start() {
# local cluster_name
# cluster_name="$(kubedee::validate_name "${1}")"
# [[ -d "${kubedee_dir}/clusters/${cluster_name}" ]] || kubedee::exit_error "Found no cluster with name: ${cluster_name} - did you create it?"
local -r admission_plugins="NamespaceLifecycle,NodeRestriction,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota${install_psps:+",PodSecurityPolicy"}"
# declare -A worker_suffixes
# for ((i = 0; i < num_worker; i++)); do
# worker_suffixes[${i}]="$(tr -cd 'a-z0-9' </dev/urandom | head -c 6 || true)"
# done
# kubedee::launch_etcd "${cluster_name}"
# kubedee::launch_container "${cluster_name}" "kubedee-${cluster_name}-controller"
# for ((i = 0; i < num_worker; i++)); do
# kubedee::launch_container "${cluster_name}" "kubedee-${cluster_name}-worker-${worker_suffixes[${i}]}"
# done
kubedee::configure_etcd "${cluster_name}"
kubedee::create_certificate_kubernetes "${cluster_name}" "${apiserver_extra_hostnames}"
kubedee::configure_controller "${cluster_name}" "kubedee-${cluster_name}-controller" "${admission_plugins}"
for ((i = 0; i < num_worker; i++)); do
kubedee::configure_worker "${cluster_name}" "kubedee-${cluster_name}-worker-${worker_suffixes[${i}]}"
done
kubedee::create_kubeconfig_admin "${cluster_name}"
kubedee::configure_rbac "${cluster_name}"
if [[ "${install_psps}" == "true" ]]; then
kubedee::deploy_pod_security_policies "${cluster_name}"
fi
kubedee::deploy_flannel "${cluster_name}"
kubedee::deploy_core_dns "${cluster_name}"
kubedee::wait_for_node "${cluster_name}" "kubedee-${cluster_name}-controller"
kubedee::label_and_taint_controller "${cluster_name}" "kubedee-${cluster_name}-controller"
for ((i = 0; i < num_worker; i++)); do
kubedee::wait_for_node "${cluster_name}" "kubedee-${cluster_name}-worker-${worker_suffixes[${i}]}"
done
for ((i = 0; i < num_worker; i++)); do
kubedee::label_worker "${cluster_name}" "kubedee-${cluster_name}-worker-${worker_suffixes[${i}]}"
done
[[ "${no_set_context}" == "false" ]] && kubedee::configure_kubeconfig "${cluster_name}"
local admin_sa=""
if [[ "${create_admin_sa}" == "true" ]]; then
kubedee::create_admin_service_account "${cluster_name}"
admin_sa="$(kubedee::get_service_account_token "${cluster_name}" kubedee-admin kube-system)"
fi
if [[ "${create_user_sa}" == "true" ]]; then
kubedee::create_user_service_account "${cluster_name}"
fi
echo
kubedee::log_success "Cluster ${cluster_name} started"
if [[ "${no_set_context}" == "false" ]]; then
kubedee::log_success "kubectl config current-context set to kubedee-${cluster_name}\\n"
else
echo
fi
if [[ -n "${admin_sa}" ]]; then
kubedee::log_info "Admin service account token can be obtained with 'kubedee admin-sa-token ${cluster_name}'"
fi
kubedee::log_info "Cluster nodes can be accessed with 'lxc exec <name> bash'"
kubedee::log_info "Cluster files can be found in '${kubedee_dir}/clusters/${cluster_name}'\\n"
kubedee::log_info "Current node status is (should be ready soon):"
kubectl --kubeconfig "${kubedee_dir}/clusters/${cluster_name}/kubeconfig/admin.kubeconfig" get nodes
echo
}
cmd_start-worker() {
local cluster_name
[[ -d "${kubedee_dir}/clusters/${cluster_name}" ]] || kubedee::exit_error "Found no cluster with name: ${cluster_name} - did you create it?"
local name_suffix
name_suffix="$(tr -cd 'a-z0-9' </dev/urandom | head -c 6)" || true
kubedee::prepare_container_image "${cluster_name}"
kubedee::launch_container "${cluster_name}" "kubedee-${cluster_name}-worker-${name_suffix}"
kubedee::configure_worker "${cluster_name}" "kubedee-${cluster_name}-worker-${name_suffix}"
kubedee::log_success "Node ${cluster_name}-${name_suffix} started"
}
cmd_list() {
mkdir -p "${kubedee_dir}/clusters/"
find "${kubedee_dir}/clusters/" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;
}
cmd_kubectl-env() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
cat <<EOF
export KUBECONFIG=${kubedee_dir}/clusters/${cluster_name}/kubeconfig/admin.kubeconfig
EOF
}
cmd_etcd-env() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
local -r container_name="kubedee-${cluster_name}-etcd"
local etcd_ip
etcd_ip="$(kubedee::container_ipv4_address "${container_name}")"
cat <<EOF
export ETCDCTL_CACERT=${kubedee_dir}/clusters/${cluster_name}/certificates/ca.pem
export ETCDCTL_CERT=${kubedee_dir}/clusters/${cluster_name}/certificates/etcd.pem
export ETCDCTL_KEY=${kubedee_dir}/clusters/${cluster_name}/certificates/etcd-key.pem
export ETCDCTL_INSECURE_TRANSPORT=false
export ETCDCTL_ENDPOINTS=https://${etcd_ip}:2379
export ETCDCTL_API=3
EOF
}
cmd_smoke-test() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
kubedee::smoke_test "${cluster_name}"
}
cmd_controller-ip() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
local ip
ip="$(kubedee::container_ipv4_address "kubedee-${cluster_name}-controller")"
echo "${ip}"
}
cmd_create-admin-sa() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
kubedee::create_admin_service_account "${cluster_name}"
}
cmd_admin-sa-token() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
local sa_token
sa_token="$(kubedee::get_service_account_token "${cluster_name}" kubedee-admin kube-system)"
echo "${sa_token}"
}
cmd_create-user-sa() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
kubedee::create_user_service_account "${cluster_name}"
}
cmd_user-sa-token() {
local cluster_name
cluster_name="$(kubedee::validate_name "${1}")"
local sa_token
sa_token="$(kubedee::get_service_account_token "${cluster_name}" "kubedee-user")"
echo "${sa_token}"
}