0

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
  • 353
  • 5
  • 16
  • check out if this post answer your question https://stackoverflow.com/a/62542265/15964308 – rriovall Nov 24 '21 at 18:07
  • I dont think i can use `kubectl config view --minify -o jsonpath='{.clusters[].name}'` ... for e.g., on eks I get `demo2-kubearmor-ub20.us-east-2.eksctl.io` and on gke i get `gke_mimetic-kit-294408_us-central1-c_accuknox-saas-dev` .... I am not sure if grepping "eksctl or gke" in the name will be a wise thing. Do you think it is ok? – takladev Nov 26 '21 at 13:06

1 Answers1

0

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
  • 406
  • 3
  • 8
  • Thanks. But I am looking for a solution that will consistently work with EKS, GKE, minikube, k3s, microk8s, AKS, etc ... Turns out there is no single stop solution for this! In the above example, you are using COS from Google. What if the image used is not COS, it is ubuntu! – takladev Dec 02 '21 at 18:12