简体   繁体   中英

How to identify kubernetes cluster provider using kubectl

I work on a k8s daemonset whose deployment requires that certain values are changed depending on the k8s cluster provider type (gke, eks, aks, minikube, k3s, kind, self-managed k8s installed using kubeadm on a vm, ... etc).

What is the right way to identify what is the k8s cluster provider type given that kubectl/kubeconfig is already configured?

One option is to use kubectl config current-context view :

CURRENT_CONTEXT_NAME="$(kubectl config current-context view)"
PLATFORM="self-managed"

autoDetectEnvironment(){
    if [[ -z "$CURRENT_CONTEXT_NAME" ]]; then
        echo "no configuration has been provided"
        return
    fi  

    echo "Autodetecting environment"
    if [[ $CURRENT_CONTEXT_NAME =~ ^minikube.* ]]; then
        PLATFORM="minikube"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^gke_.* ]]; then
        PLATFORM="gke"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^kind-.* ]]; then
        PLATFORM="kind"
    elif [[ $CURRENT_CONTEXT_NAME =~ ^k3d-.* ]]; then
        PLATFORM="k3d"
        elif [[ $CURRENT_CONTEXT_NAME =~ ^kubernetes-.* ]]; then
                PLATFORM="self-managed"
        else
                echo "No k8s cluster configured or unknown env!"
                exit 2
        fi  
}

However, this seems hacky and am sure it will not work under all cases. For eg, for EKS I could not figure out what regex to use.

yes grepping gke is working for me $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" $ kubectl get nodes -o wide | awk '{print $1}' | grep -e "gke" however there should be a fancier way to get the Cloud provider, I also tried kubectl get nodes -o wide that in output of OS-IMAGE column says the provider

~$ kubectl get nodes -o wide | awk '{print $8,$9,$10,$11}'
OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME 
Container-Optimized OS from Google
Container-Optimized OS from Google
Container-Optimized OS from Google

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM