blob: d9f420b6acef5bc04db60d57f6e936e2cf1a03d3 (
plain)
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
|
#!/usr/bin/env bash
set -eu
set -o pipefail
if [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
cat <<EOF
Usage: multi-capo-shell [command...]
Interactively selects a CAPO cluster from all management clusters using
fzf, then opens a capo-shell for the selected cluster.
Arguments:
command Command to pass through to capo-shell (default: \$SHELL or bash)
Environment variables:
MANAGEMENT_KUBECONFIGS Semicolon-separated list of management kubeconfig paths
KUBECONFIG Semicolon-separated management cluster kubeconfig paths (additional)
MULTI_CAPO_SHELL_CUSTOM_COLUMN_<name> yq expression for an extra fzf preview column
MULTI_CAPO_SHELL_CUSTOM_NAME_EXPRESSION yq expression for a custom cluster display name
EOF
exit 0
fi
if ! command -v fzf &>/dev/null; then
echo "fzf is required but not installed. Please install fzf to use this script." >&2
exit 1
fi
declare -a management_kubeconfigs=()
mapfile -t management_kubeconfigs < <(
[[ -v MANAGEMENT_KUBECONFIGS ]] && echo -e "${MANAGEMENT_KUBECONFIGS//;/\\n}"
[[ -v KUBECONFIG ]] && echo -e "${KUBECONFIG//;/\\n}"
)
declare -a custom_columns=()
for custom_column_var in ${!MULTI_CAPO_SHELL_CUSTOM_COLUMN_*}; do
custom_columns+=("${!custom_column_var}")
done
if [[ "${#custom_columns[@]}" -gt 0 ]]; then
printf -v custom_columns_string "\\(%s):" "${custom_columns[@]}"
else
custom_columns_string=""
fi
MULTI_CAPO_SHELL_CUSTOM_NAME_EXPRESSION="${MULTI_CAPO_SHELL_CUSTOM_NAME_EXPRESSION:-\"\"}"
if [[ "${#management_kubeconfigs[@]}" -gt 0 ]]; then
shopt -s lastpipe
for mgmt_kubeconfig in "${management_kubeconfigs[@]}"; do
export KUBECONFIG="$mgmt_kubeconfig"
for context in $(kubectl config get-contexts -o name); do
(kubectl --context "$context" get cluster -A -o yaml || true) | yq -r --arg kubeconfig "$KUBECONFIG" --arg context "$context" '.items | map("\($kubeconfig):\($context):\(.metadata.namespace):\(.metadata.name):'"$custom_columns_string"'\(('"${MULTI_CAPO_SHELL_CUSTOM_NAME_EXPRESSION}"') as $custom_name | if $custom_name == "" then .metadata.name else "\($custom_name) (\(.metadata.name))" end)")[]'
done
done | fzf --sync --preview="awk -v name={} 'BEGIN{split(name,splitted,\":\"); print splitted[2] \"@\" splitted[1]}'" -d : --with-nth=$((4 + ${#custom_columns[@]})),$((5 + ${#custom_columns[@]})) | IFS=: read -r mgmt_kubeconfig mgmt_context namespace name _
exec env KUBECONFIG="$mgmt_kubeconfig" KUBECONFIG_CONTEXT="$mgmt_context" capo-shell "$namespace" "$name" "${@}"
fi
|