Skip to content

Commit 35a1897

Browse files
committed
refactor log-k8s and check-k8s to dynamically discover clusters from credhub
1 parent 24d02d7 commit 35a1897

File tree

2 files changed

+46
-111
lines changed

2 files changed

+46
-111
lines changed

tools/check-k8s.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,24 @@ checkClusterResources() {
5454
#===================================================================================
5555
unset https_proxy http_proxy no_proxy
5656
export KUBECONFIG="${HOME}/.kube/config"
57-
CLUSTER_CTX="$(kubectl config view -o json | jq -r ".contexts[].name")"
57+
contexts="$(kubectl config view -o json | jq -r ".contexts[].name" | sort | pr -3t -W 130)"
5858

5959
if [ "$1" = "" ] ; then
60-
printf "\n%bSelect a k8s context :%b\n${CLUSTER_CTX}" "${REVERSE}${GREEN}" "${STD}"
61-
printf "\n\n%bYour choice (<Enter> to select all) :%b " "${GREEN}${BOLD}" "${STD}" ; read choice
60+
printf "\n%bSelect a cluster :%b\n${contexts}" "${REVERSE}${GREEN}" "${STD}"
61+
printf "\n\n%bYour choice (<Enter> to select all) :%b " "${GREEN}${BOLD}" "${STD}" ; read context
6262
else
63-
flagCtx="$(echo "${CLUSTER_CTX}" | grep "$1")"
63+
flagCtx="$(echo "${contexts}" | grep "^$1$")"
6464
if [ "${flagCtx}" = "" ] ; then
6565
printf "\n%bCluster \"$1\" unknown...%b\n" "${RED}" "${STD}" ; exit 1
6666
else
67-
choice="$1"
67+
context="$1"
6868
fi
6969
fi
7070

71-
if [ "${choice}" = "" ] ; then
72-
for ctx in ${CLUSTER_CTX} ; do
71+
if [ "${context}" = "" ] ; then
72+
for ctx in ${contexts} ; do
7373
checkClusterResources "${ctx}"
7474
done
7575
else
76-
checkClusterResources "${choice}"
76+
checkClusterResources "${context}"
7777
fi

tools/log-k8s.sh

Lines changed: 38 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Log to kubernetes clusters for clis (kubectl, helm, k9s)
44
# Parameters :
55
# --context, -c : Select k8s context
6-
# --proxy, -p : Set proxy for
6+
# --proxy, -p : Set proxy
77
#===========================================================================
88

99
#--- Check scripts options
@@ -55,35 +55,6 @@ updateKubeConfig() {
5555
fi
5656
}
5757

58-
#--- Get k8s cluster configuration from credhub
59-
getClusterConfiguration() {
60-
credhub g -n /kubeconfigs/${K8S_CLUSTER} -j 2> /dev/null | jq -r '.value' > ${KUBECONFIG}
61-
if [ -s ${KUBECONFIG} ] ; then
62-
updateKubeConfig "clusters.name" "${K8S_CLUSTER}"
63-
updateKubeConfig "contexts.context.cluster" "${K8S_CLUSTER}"
64-
updateKubeConfig "contexts.name" "${K8S_CONTEXT}"
65-
updateKubeConfig "users.name" "${K8S_CONTEXT}"
66-
updateKubeConfig "contexts.context.user" "${K8S_CONTEXT}"
67-
updateKubeConfig "current-context" "${K8S_CONTEXT}"
68-
TARGET_KUBECONFIG="${TARGET_KUBECONFIG}:${KUBECONFIG}"
69-
else
70-
rm -f ${KUBECONFIG} > /dev/null 2>&1
71-
fi
72-
}
73-
74-
#--- Select k8s cluster
75-
selectCluster() {
76-
case "$1" in
77-
"1"|"core-connectivity") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-core-connectivity-k8s" ; K8S_CONTEXT="core-connectivity" ;;
78-
"2"|"supervision") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-supervision" ; K8S_CONTEXT="supervision" ;;
79-
"3"|"marketplace") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-marketplace" ; K8S_CONTEXT="marketplace" ;;
80-
"4"|"shared-services") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-shared-services" ; K8S_CONTEXT="shared-services" ;;
81-
"5"|"ha-datastore") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-k8s-ha-datastore" ; K8S_CONTEXT="ha-datastore" ;;
82-
"6"|"openshift-gcp") K8S_TYPE_CLUSTER="openshift" ; K8S_CLUSTER="openshift-gcp" ; K8S_CONTEXT="openshift-gcp" ; CREDHUB_ENDPOINT="/secrets/external/gcp_poc_openshift_cluster_api_url" ;;
83-
*) flag=0 ; flagError=1 ; clear ;;
84-
esac
85-
}
86-
8758
#--- Check scripts options
8859
while [ ${nbParameters} -gt 0 ] ; do
8960
case "$1" in
@@ -117,26 +88,38 @@ if [ ${flagError} = 0 ] ; then
11788
fi
11889
fi
11990

120-
#--- Log to k8s
91+
#--- Generate kubeconfig files for all clusters
12192
if [ ${flagError} = 0 ] ; then
122-
#--- Get all clusters configuration
123-
TARGET_KUBECONFIG=""
12493
clear
12594
printf "\n%bGet clusters properties...%b\n" "${YELLOW}${REVERSE}" "${STD}"
126-
if [ "${SITE_NAME}" = "fe-int" ] ; then
127-
MAX_ITEMS=8
128-
else
129-
MAX_ITEMS=6
130-
fi
13195

132-
for value in $(seq 1 ${MAX_ITEMS}) ; do
133-
selectCluster "${value}"
134-
if [ "${K8S_TYPE_CLUSTER}" = "k3s" ] ; then
135-
export KUBECONFIG="${HOME}/.kube/${K8S_CONTEXT}.yml"
136-
getClusterConfiguration
96+
#--- Get clusters list
97+
TARGET_KUBECONFIG="" ; K8S_CONTEXTS=""
98+
cluster_paths="$(credhub f | grep -E "name: /kubeconfigs/|name: /secrets/kubeconfigs/" | awk '{print $3}' | sort)"
99+
100+
for path in ${cluster_paths} ; do
101+
K8S_CLUSTER="$(echo "${path}" | sed -e "s+.*kubeconfigs\/++")"
102+
K8S_CONTEXT="$(echo "${path}" | sed -e "s+.*kubeconfigs\/++" -e "s+-k8s++" -e "s+00-++")"
103+
K8S_CONTEXTS="${K8S_CONTEXTS} ${K8S_CONTEXT}"
104+
export KUBECONFIG="${HOME}/.kube/${K8S_CONTEXT}.yml"
105+
106+
#--- Get k8s cluster configuration from credhub
107+
credhub g -n ${path} -j 2> /dev/null | jq -r '.value' > ${KUBECONFIG}
108+
if [ -s ${KUBECONFIG} ] ; then
109+
updateKubeConfig "clusters.name" "${K8S_CLUSTER}"
110+
updateKubeConfig "contexts.context.cluster" "${K8S_CLUSTER}"
111+
updateKubeConfig "contexts.name" "${K8S_CONTEXT}"
112+
updateKubeConfig "users.name" "${K8S_CONTEXT}"
113+
updateKubeConfig "contexts.context.user" "${K8S_CONTEXT}"
114+
updateKubeConfig "current-context" "${K8S_CONTEXT}"
115+
TARGET_KUBECONFIG="${TARGET_KUBECONFIG}:${KUBECONFIG}"
116+
else
117+
rm -f ${KUBECONFIG} > /dev/null 2>&1
137118
fi
138119
done
139120

121+
K8S_CONTEXTS="$(echo "${K8S_CONTEXTS}" | sed -e "s+^ ++" | tr " " "\n")"
122+
140123
#--- Concatenate all clusters config files
141124
export KUBECONFIG="$(echo "${TARGET_KUBECONFIG}" | sed -e "s+^:++")"
142125
kubectl config view --flatten > ${HOME}/.kube/config
@@ -149,70 +132,22 @@ if [ ${flagError} = 0 ] ; then
149132
fi
150133
fi
151134

135+
#--- Select a cluster
152136
if [ ${flagError} = 0 ] ; then
153-
#--- Select kubernetes cluster to work with
154-
flag=0
155-
if [ "${context}" = "" ] ; then
156-
while [ ${flag} = 0 ] ; do
157-
flag=1
158-
printf "\n%bKubernetes cluster :%b\n\n" "${REVERSE}${GREEN}" "${STD}"
159-
printf "%b1%b : core-connectivity\n" "${GREEN}${BOLD}" "${STD}"
160-
printf "%b2%b : supervision\n" "${GREEN}${BOLD}" "${STD}"
161-
printf "%b3%b : marketplace\n" "${GREEN}${BOLD}" "${STD}"
162-
printf "%b4%b : shared-services\n" "${GREEN}${BOLD}" "${STD}"
163-
printf "%b5%b : ha-datastore\n" "${GREEN}${BOLD}" "${STD}"
164-
if [ "${SITE_NAME}" = "fe-int" ] ; then
165-
printf "%b6%b : openshift gcp\n" "${GREEN}${BOLD}" "${STD}"
166-
fi
167-
printf "\n%bYour choice :%b " "${GREEN}${BOLD}" "${STD}" ; read choice
168-
selectCluster "${choice}"
169-
done
170-
else
171-
selectCluster "${context}"
172-
fi
137+
export KUBECONFIG="${HOME}/.kube/config"
173138

174-
#--- Concatenate clusters config files and set current context
175-
export KUBECONFIG="${HOME}/.kube/${K8S_CONTEXT}.yml"
176-
if [ "${K8S_TYPE_CLUSTER}" = "k3s" ] ; then
177-
if [ ! -f ${KUBECONFIG} ] ; then
178-
printf "\n%bERROR : No configuration file for \"${K8S_CLUSTER}\" cluster.%b\n" "${RED}" "${STD}" ; flagError=1
179-
fi
139+
if [ "${context}" = "" ] ; then
140+
contexts="$(kubectl config view -o json | jq -r ".contexts[].name" | sort | pr -3t -W 130)"
141+
printf "\n%bSelect a cluster :%b\n%s" "${REVERSE}${GREEN}" "${STD}" "${contexts}"
142+
printf "\n\n%bYour choice (<Enter> to select none) :%b " "${GREEN}${BOLD}" "${STD}" ; read context
180143
fi
181144

182-
export KUBECONFIG="${HOME}/.kube/config"
183-
if [ ${flagError} = 0 ] ; then
184-
#--- Connect to cluster
185-
if [ "${K8S_TYPE_CLUSTER}" = "openshift" ] ; then
186-
proxyStatus="$(env | grep "https_proxy" | grep "internet")"
187-
if [ "${proxyStatus}" = "" ] ; then
188-
printf "\n%bERROR : You need to set internet proxy to use \"${K8S_CLUSTER}\" cluster.%b\n" "${RED}" "${STD}" ; flagError=1
189-
else
190-
#--- Connect to openshift cluster
191-
OC_ENDPOINT="$(credhub g -n ${CREDHUB_ENDPOINT} -j 2> /dev/null | jq -r '.value')"
192-
message="$(oc login --server=${OC_ENDPOINT})"
193-
printf "\n%b${message}%b " "${YELLOW}${BOLD}" "${STD}"
194-
printf "\n\n%bOpenshift API token :%b " "${GREEN}${BOLD}" "${STD}" ; read -s API_TOKEN
195-
oc login --token=${API_TOKEN} --server=${OC_ENDPOINT} > /dev/null 2>&1
196-
flagError=$?
197-
if [ ${flagError} != 0 ] ; then
198-
printf "\n%bERROR : Invalid token \"${API_TOKEN}\".\n${message}\n%b" "${RED}" "${STD}"
199-
fi
200-
201-
#--- Rename cluster context
202-
current_context="$(kubectl ctx -c)"
203-
kubectl config rename-context ${current_context} ${K8S_CONTEXT} > /dev/null 2>&1
204-
fi
145+
if [ "${context}" != "" ] ; then
146+
check_selected="$(echo "${K8S_CONTEXTS}" | grep "^${context}$")"
147+
if [ "${check_selected}" = "" ] ; then
148+
printf "\n%bERROR : Cluster \"${context}\" unknown...%b\n" "${RED}" "${STD}"
205149
else
206-
#--- Set context to use for selected k3s cluster
207-
kubectl config use-context ${K8S_CONTEXT} > /dev/null 2>&1
208-
flagError=$?
209-
if [ ${flagError} != 0 ] ; then
210-
printf "\n%bERROR : Unable to set context for \"${K8S_CLUSTER}\" cluster.%b\n" "${RED}" "${STD}"
211-
fi
212-
fi
213-
214-
if [ ${flagError} = 0 ] ; then
215-
printf "\n%bCluster \"${K8S_CONTEXT}\" available.%b\n" "${YELLOW}${REVERSE}" "${STD}"
150+
kctx ${context}
216151
fi
217152
fi
218-
fi
153+
fi

0 commit comments

Comments
 (0)