Update kube context using shell script

7/8/2021

I am trying to set the current context of the cluster using shell script.

#!/usr/bin/env bash

#Set Parameters
echo ${cluster_arn}

  sh "aws eks update-kubeconfig --name abc-eks-cluster --role-arn ${cluster_arn} --alias abc-eks-cluster"

export k8s_host="$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")"

However, the above command gives error:

sh: 0: Can't open aws eks update-kubeconfig --name abc-eks-cluster --role-arn arn:aws:iam::4399999999873:role/abc-eks-cluster-cluster-admin --alias abc-eks-cluster

Can someone suggest me how do what is the issue and how can I set the current context because the k8_host command yiedls another error that context is not set.

-- knowledge20
amazon-eks
amazon-web-services
kubernetes
shell

1 Answer

7/9/2021

I have no idea why you are even involving sh as a subshell, when you're already in a shell script, but

Can someone suggest me ... what is the issue

You have provided the entire command to sh but failed to use the -c that informs it that the first argument is an inline shell snippet; otherwise, sh expects that the first non-option argument is a file which is why it says it cannot open a file with that horrifically long name

There are two outcomes to get out of this mess: use -c or stop trying to use a subshell

  sh -c "aws eks update-kubeconfig --name abc-eks-cluster --role-arn ${cluster_arn} --alias abc-eks-cluster"

or:

aws eks update-kubeconfig --name abc-eks-cluster --role-arn ${cluster_arn} --alias abc-eks-cluster
-- mdaniel
Source: StackOverflow