PKNLc{{eks_switcher/__init__.py""" A CLI utilitiy for for swapping your Kubernetes config amongst different EKS Clusters """ __version__ = '0.5' import boto3 import click from .aws_eks import list_clusters from .kube_config import generate_kube_config @click.command() @click.option('--profile', '-p', help='Name of AWS profile') @click.option('--name', '-n', help='Name of EKS Cluster') def main(profile, name): if profile is None: profile = 'default' session = boto3.Session(profile_name=profile) client = session.client('eks') if name is None: name = list_clusters(client) generate_kube_config(client, name, profile) PK[NLH쥅PPeks_switcher/__main__.pyimport sys from . import main if __name__ == "__main__": sys.exit(main()) PK[NLoueks_switcher/aws_config.pyfrom os.path import expanduser def get_config_contents(): home = expanduser('~') config_path = home + '/.aws/config' with open(config_path, 'r') as config_file: return config_file.read().split('\n') def get_role_arn(profile='default'): contents = get_config_contents() if profile == 'default': for line in contents: if line.strip() == '': return None if 'role_arn' in line: return line.split(' ')[2] return None else: correct_block = False for line in contents: if line == f'[profile {profile}]': correct_block = True continue if correct_block: if line.strip() == '': return if 'role_arn' in line: return line.split(' ')[2] return None PK[NLpweks_switcher/aws_eks.pyimport click def get_server_url(client, name): cluster = client.describe_cluster( name=name, ).get('cluster') return cluster.get('endpoint') def get_certificate_data(client, name): cluster = client.describe_cluster( name=name, ).get('cluster') return cluster.get('certificateAuthority').get('data') def list_clusters(client): clusters = client.list_clusters().get('clusters') print_formatted_cluster_list(clusters) choice = input('Enter the your choice: ') return clusters[int(choice) - 1] def print_formatted_cluster_list(clusters): click.echo('Select the cluster you wish to connect to') i = 0 for cluster in clusters: i += 1 click.echo(f'{i}) {cluster}') PKNLBeks_switcher/kube_config.pyfrom os.path import expanduser from jinja2 import Environment, PackageLoader, select_autoescape from .aws_eks import get_server_url, get_certificate_data from .aws_config import get_role_arn def generate_kube_config(client, name, profile): server_url = get_server_url(client, name) certificate_data = get_certificate_data(client, name) role_arn = get_role_arn(profile) env = Environment( loader=PackageLoader('eks_switcher', 'files'), autoescape=select_autoescape(['jinja2']) ) template = env.get_template('config.jinja2') home = expanduser('~') config_path = home + '/.kube/config' with open(config_path, 'w') as kube_config: kube_config.write(template.render( server_url=server_url, certificate_data=certificate_data, cluster_name=name, role_arn=role_arn, aws_profile=profile, )) PK[NLNII eks_switcher/files/config.jinja2apiVersion: v1 clusters: - cluster: server: {{ server_url }} certificate-authority-data: {{ certificate_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: heptio-authenticator-aws args: - "token" - "-i" - "{{ cluster_name }}" - "-r" - "{{ role_arn }}" env: - name: AWS_PROFILE value: {{ aws_profile }} PK!Hk,2+eks_switcher-0.5.dist-info/entry_points.txtN+I/N.,()J.-.,IH-ra<..PK[NL}?00"eks_switcher-0.5.dist-info/LICENSEMIT License Copyright (c) 2018 Joshua Armitage Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hp!Qa eks_switcher-0.5.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,zd&Y)r$[)T&UD"PK!H(#eks_switcher-0.5.dist-info/METADATA]N0E$P+DIJ$&{Lտ'A-.f1>6(fo$ӰHoUi,j>|#IݡW&WUDNаp4HGEܡw(fP Ʋk=:ѴSo'2e-Iwi6{RTT!A_]F&{YRQ:f.bjР0ԜGJ-5S9m|=~7 VcH) ]@u-BPK!H~!eks_switcher-0.5.dist-info/RECORD}IsP}~ Y Pؼ 3 {vJۻ9urÈ5OwQ0v&Sv:%$y$&g˞urXYN~&ST%?@u2cJX*0񥤰nm#w~Z]'>V*F&Q6I ~DXؐ6`7x2+07Q0f-Q LG{@:m\;1yffά{)yEwfo\G4ʦ-(5=yCQE<*$bڗ_Cvy]0Bg}V v:e1iX c5+6G[%?PKNLc{{eks_switcher/__init__.pyPK[NLH쥅PPeks_switcher/__main__.pyPK[NLou7eks_switcher/aws_config.pyPK[NLpweks_switcher/aws_eks.pyPKNLB eks_switcher/kube_config.pyPK[NLNII eks_switcher/files/config.jinja2PK!Hk,2+ceks_switcher-0.5.dist-info/entry_points.txtPK[NL}?00"eks_switcher-0.5.dist-info/LICENSEPK!Hp!Qa Heks_switcher-0.5.dist-info/WHEELPK!H(#eks_switcher-0.5.dist-info/METADATAPK!H~!eks_switcher-0.5.dist-info/RECORDPK Gt