Skip to content

Instantly share code, notes, and snippets.

@epequeno
Last active August 2, 2018 19:23
Show Gist options
  • Save epequeno/7a0fd9ff1dcb829bc35bee46f2a97ee1 to your computer and use it in GitHub Desktop.
Save epequeno/7a0fd9ff1dcb829bc35bee46f2a97ee1 to your computer and use it in GitHub Desktop.
small app to get EKS cluster info from the API and print a filled out config file for ~/.kube/config from https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html
apiVersion: v1
clusters:
- cluster:
server: {{endpoint_url}}
certificate-authority-data: {{cert_data}}
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: aws
name: aws
current-context: aws
kind: Config
preferences: {}
users:
- name: aws
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
command: aws-iam-authenticator
args:
- "token"
- "-i"
- "{{cluster_name}}"
# - "-r"
# - "<role-arn>"
# env:
# - name: AWS_PROFILE
# value: "<aws-profile>"
"""get the relevant data from api and print config"""
# stdlib
# 3rd party
import click
from jinja2 import Template
import boto3
# local
@click.command()
@click.option('-r', '--region', default='us-east-1', help='aws region')
@click.argument('cluster_name')
def make_eks_config(region, cluster_name):
with open('/home/me/eks/eks_config.jinja') as fd:
template = Template(fd.read())
eks = boto3.client('eks', region_name=region)
try:
res = eks.describe_cluster(name=cluster_name).get('cluster')
except Exception as e:
click.echo(e)
exit(1)
url = res.get('endpoint')
cert_data = res.get('certificateAuthority', {}).get('data')
print(template.render(endpoint_url=url,
cert_data=cert_data,
cluster_name=cluster_name))
if __name__ == '__main__':
make_eks_config()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment