How to identify kubernetes cluster provider using kubectl

11/24/2021

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 e.g., for EKS I could not figure out what regex to use.

-- takladev
kubectl
kubernetes

1 Answer

12/2/2021

yes grepping gke is working for me $ 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
-- rriovall
Source: StackOverflow