Skip to content

Commit

Permalink
add context option to log-k8s for automatic cluster selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ogrand committed Sep 6, 2023
1 parent d5293ad commit 60190b0
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions tools/log-k8s.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#!/bin/bash
#===========================================================================
# Log to kubernetes clusters for clis (kubectl, helm, k9s)
# Parameters :
# --context, -c : Select k8s context
# --proxy, -p : Set proxy for
#===========================================================================

#--- Check scripts options
flagError=0
flagError=0 ; PROXY_MODE=0 ; context="" ; nbParameters=$#
usage() {
printf "\n%bUSAGE:" "${RED}"
printf "\n log-k8s [OPTIONS]\n\nOPTIONS:"
printf "\n %-20s %s" "--proxy, -p " "Set proxy in kubeconfig"
printf "\n %-40s %s" "--context, -c \"cluster context name\"" "Set k8s cluster context"
printf "\n %-40s %s" "--proxy, -p " "Set proxy in kubeconfig"
printf "%b\n\n" "${STD}" ; flagError=1
}

case "$1" in
"-p"|"--proxy") PROXY_MODE=1 ;;
"") PROXY_MODE=0 ;;
*) usage ;;
esac

#--- Update propertie value from KUBECONFIG file
updateKubeConfig() {
key_path="$1"
Expand Down Expand Up @@ -76,18 +74,32 @@ getClusterConfiguration() {
#--- Select k8s cluster
selectCluster() {
case "$1" in
"1") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-core-connectivity-k8s" ; K8S_CONTEXT="core-connectivity" ;;
"2") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="01-ci-k8s" ; K8S_CONTEXT="ci-k8s" ;;
"3") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-supervision" ; K8S_CONTEXT="supervision" ;;
"4") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-marketplace" ; K8S_CONTEXT="marketplace" ;;
"5") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-shared-services" ; K8S_CONTEXT="shared-services" ;;
"6") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-k8s-ha-datastore" ; K8S_CONTEXT="ha-datastore" ;;
"7") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="k3s-sandbox" ; K8S_CONTEXT="sandbox" ;;
"8") K8S_TYPE_CLUSTER="openshift" ; K8S_CLUSTER="openshift-gcp" ; K8S_CONTEXT="openshift-gcp" ; CREDHUB_ENDPOINT="/secrets/external/gcp_poc_openshift_cluster_api_url" ;;
*) flag=0 ; clear ;;
"1"|"core-connectivity") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-core-connectivity-k8s" ; K8S_CONTEXT="core-connectivity" ;;
"2"|"ci-k8s") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="01-ci-k8s" ; K8S_CONTEXT="ci-k8s" ;;
"3"|"supervision") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-supervision" ; K8S_CONTEXT="supervision" ;;
"4"|"marketplace") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-marketplace" ; K8S_CONTEXT="marketplace" ;;
"5"|"shared-services") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-shared-services" ; K8S_CONTEXT="shared-services" ;;
"6"|"ha-datastore") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="00-k8s-ha-datastore" ; K8S_CONTEXT="ha-datastore" ;;
"7"|"sandbox") K8S_TYPE_CLUSTER="k3s" ; K8S_CLUSTER="k3s-sandbox" ; K8S_CONTEXT="sandbox" ;;
"8"|"openshift-gcp") K8S_TYPE_CLUSTER="openshift" ; K8S_CLUSTER="openshift-gcp" ; K8S_CONTEXT="openshift-gcp" ; CREDHUB_ENDPOINT="/secrets/external/gcp_poc_openshift_cluster_api_url" ;;
*) flag=0 ; flagError=1 ; clear ;;
esac
}

#--- Check scripts options
while [ ${nbParameters} -gt 0 ] ; do
case "$1" in
"-c"|"--context")
if [ "$2" = "" ] ; then
usage ; nbParameters=0
else
context="$2" ; shift ; shift ; nbParameters=$#
fi ;;
"-p"|"--proxy") PROXY_MODE=1 ; shift ; nbParameters=$# ;;
*) usage ; nbParameters=0 ;;
esac
done

#--- Log to credhub
if [ ${flagError} = 0 ] ; then
#--- Create k8s configration directory
Expand Down Expand Up @@ -142,22 +154,26 @@ fi
if [ ${flagError} = 0 ] ; then
#--- Select kubernetes cluster to work with
flag=0
while [ ${flag} = 0 ] ; do
flag=1
printf "\n%bKubernetes cluster :%b\n\n" "${REVERSE}${GREEN}" "${STD}"
printf "%b1%b : core connectivity\n" "${GREEN}${BOLD}" "${STD}"
printf "%b2%b : ci\n" "${GREEN}${BOLD}" "${STD}"
printf "%b3%b : supervision\n" "${GREEN}${BOLD}" "${STD}"
printf "%b4%b : marketplace\n" "${GREEN}${BOLD}" "${STD}"
printf "%b5%b : shared services\n" "${GREEN}${BOLD}" "${STD}"
printf "%b6%b : ha datastore\n" "${GREEN}${BOLD}" "${STD}"
if [ "${SITE_NAME}" = "fe-int" ] ; then
printf "%b7%b : sandbox\n" "${GREEN}${BOLD}" "${STD}"
printf "%b8%b : openshift gcp\n" "${GREEN}${BOLD}" "${STD}"
fi
printf "\n%bYour choice :%b " "${GREEN}${BOLD}" "${STD}" ; read choice
selectCluster "${choice}"
done
if [ "${context}" = "" ] ; then
while [ ${flag} = 0 ] ; do
flag=1
printf "\n%bKubernetes cluster :%b\n\n" "${REVERSE}${GREEN}" "${STD}"
printf "%b1%b : core connectivity\n" "${GREEN}${BOLD}" "${STD}"
printf "%b2%b : ci\n" "${GREEN}${BOLD}" "${STD}"
printf "%b3%b : supervision\n" "${GREEN}${BOLD}" "${STD}"
printf "%b4%b : marketplace\n" "${GREEN}${BOLD}" "${STD}"
printf "%b5%b : shared services\n" "${GREEN}${BOLD}" "${STD}"
printf "%b6%b : ha datastore\n" "${GREEN}${BOLD}" "${STD}"
if [ "${SITE_NAME}" = "fe-int" ] ; then
printf "%b7%b : sandbox\n" "${GREEN}${BOLD}" "${STD}"
printf "%b8%b : openshift gcp\n" "${GREEN}${BOLD}" "${STD}"
fi
printf "\n%bYour choice :%b " "${GREEN}${BOLD}" "${STD}" ; read choice
selectCluster "${choice}"
done
else
selectCluster "${context}"
fi

#--- Concatenate clusters config files and set current context
export KUBECONFIG="${HOME}/.kube/${K8S_CONTEXT}.yml"
Expand Down

0 comments on commit 60190b0

Please sign in to comment.