Set KUBECONFIG environment variable dynamically

Viewed 16438

I am quite new to Bash and working on multiple kubernetes cluster. I wanted some kind of utility wherein I can set my KUBECONFIG variable dynamically as I work on multiple clusters. My kubeconfig file lies in multiple folders. What I want is to find all the kubeconfig files , get the paths and concatenate the paths with colon , set it to KUBECONFIG variable and export it in bashrc file.

I know pieces of command but not able to frame full code.

find /Users/anandabhishe/gitlab/ -name kubeconfig.yaml -exec echo {} \; 

I want to concatenate the output of find command and set my KUBECONFIG. like this "

export KUBECONFIG=/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc1-staging-hrwork-dev/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork-uat/kubeconfig.yaml:/Users/anandabhishe/gitlab/work2/kubeconfigs/scdc2-prod-hrwork/kubeconfig.yaml "

1 Answers

You can try something like that:

export KUBECONFIG=$(for i in $(find /Users/anandabhishe/gitlab/ -iname 'kubeconfig.yaml') ; do echo -n ":$i"; done | cut -c 2-)
Related