1- import click
2- import yaml
1+ import base64
2+ import os
33from pathlib import Path
4+ import shutil
5+ import yaml
46from .config import ConfigManager
57from ..britive_cli import BritiveCli
6- import os
7- import base64
88
99
1010def sanitize (name : str ):
@@ -19,7 +19,7 @@ def check_env_var(filename, cli: BritiveCli):
1919 # no env var present
2020 if not kubeconfig :
2121 command = f'export KUBECONFIG=~/.kube/config:{ filename } '
22- cli .print (f 'Please ensure your KUBECONFIG environment variable includes the Britive managed kube config file.' )
22+ cli .print ('Please ensure your KUBECONFIG environment variable includes the Britive managed kube config file.' )
2323 cli .print (command )
2424 else :
2525 for configfile in kubeconfig .split (':' ):
@@ -28,8 +28,7 @@ def check_env_var(filename, cli: BritiveCli):
2828 return # we found what we came for - silently continue
2929
3030 # if we get here we need to instruct the user to add the britive managed kube config file
31- cli .print (f'Please modify your KUBECONFIG environment variable to include the '
32- f'Britive managed kube config file.' )
31+ cli .print ('Please modify your KUBECONFIG environment variable to include the Britive managed kube config file.' )
3332 command = f'export KUBECONFIG="${{KUBECONFIG}}:{ filename } "'
3433 cli .print (command )
3534
@@ -40,7 +39,7 @@ def merge_new_with_existing(clusters, contexts, users, filename, tenant):
4039 # them with the above created items
4140 existing_kubeconfig = {}
4241 if Path (filename ).exists ():
43- with open (filename , 'r' ) as f :
42+ with open (filename , 'r' , encoding = 'utf-8' ) as f :
4443 existing_kubeconfig = yaml .safe_load (f ) or {}
4544
4645 prefix = f'{ tenant } -'
@@ -57,16 +56,10 @@ def merge_new_with_existing(clusters, contexts, users, filename, tenant):
5756 if not user .get ('name' , '' ).startswith (prefix ):
5857 users .append (user )
5958
60- kubeconfig = {
61- 'apiVersion' : 'v1' ,
62- 'clusters' : clusters ,
63- 'contexts' : contexts ,
64- 'users' : users ,
65- 'kind' : 'Config'
66- }
59+ kubeconfig = {'apiVersion' : 'v1' , 'clusters' : clusters , 'contexts' : contexts , 'users' : users , 'kind' : 'Config' }
6760
6861 # write out the config file
69- with open (filename , 'w' ) as f :
62+ with open (filename , 'w' , encoding = 'utf-8' ) as f :
7063 yaml .safe_dump (kubeconfig , f , default_flow_style = False , encoding = 'utf-8' )
7164
7265
@@ -90,7 +83,7 @@ def parse_profiles(profiles, aliases):
9083 'cert' : profile ['cert' ],
9184 'escaped_profile' : escaped_profile_str ,
9285 'profile' : f"{ profile ['app' ]} /{ profile ['env' ]} /{ profile ['profile' ]} " .lower (),
93- 'alias' : alias
86+ 'alias' : alias ,
9487 }
9588 cluster_names [env_profile ]['apps' ].append (sanitize (profile ['app' ]))
9689 return [cluster_names , assigned_aliases ]
@@ -110,24 +103,28 @@ def valid_cert(cert: str, profile: str, cli: BritiveCli):
110103
111104
112105def build_tenant_config (tenant , cluster_names , username , cli : BritiveCli ):
113- users = [
114- {
115- 'name' : username ,
116- 'user' : {
117- 'exec' : {
118- 'apiVersion' : 'client.authentication.k8s.io/v1beta1' ,
119- 'command' : 'pybritive-kube-exec' ,
120- 'args' : [
121- '-t' ,
122- tenant
123- ],
124- 'env' : None ,
125- 'interactiveMode' : 'Never' ,
126- 'provideClusterInfo' : True
127- }
106+ kube_exec_full_path = shutil .which ('pybritive-kube-exec' )
107+ if not kube_exec_full_path :
108+ kube_exec_full_path = 'pybritive-kube-exec'
109+ users = (
110+ [
111+ {
112+ 'name' : username ,
113+ 'user' : {
114+ 'exec' : {
115+ 'apiVersion' : 'client.authentication.k8s.io/v1beta1' ,
116+ 'command' : kube_exec_full_path ,
117+ 'args' : ['-t' , tenant ],
118+ 'env' : None ,
119+ 'interactiveMode' : 'Never' ,
120+ 'provideClusterInfo' : True ,
121+ }
122+ },
128123 }
129- }
130- ] if len (cluster_names .keys ()) > 0 else []
124+ ]
125+ if len (cluster_names .keys ()) > 0
126+ else []
127+ )
131128 contexts = []
132129 clusters = []
133130
@@ -153,22 +150,17 @@ def build_tenant_config(tenant, cluster_names, username, cli: BritiveCli):
153150 'extensions' : [
154151 {
155152 'name' : 'client.authentication.k8s.io/exec' ,
156- 'extension' : {
157- 'britive-profile' : details .get ('alias' ) or details ['escaped_profile' ]
158- }
153+ 'extension' : {'britive-profile' : details .get ('alias' ) or details ['escaped_profile' ]},
159154 }
160- ]
161- }
155+ ],
156+ },
162157 }
163158 )
164159
165160 contexts .append (
166161 {
167162 'name' : details .get ('alias' ) or f'{ tenant } -{ name } ' ,
168- 'context' : {
169- 'cluster' : f'{ tenant } -{ name } ' ,
170- 'user' : username
171- }
163+ 'context' : {'cluster' : f'{ tenant } -{ name } ' , 'user' : username },
172164 }
173165 )
174166 return [clusters , contexts , users ]
@@ -189,10 +181,7 @@ def build_kube_config(profiles: list, config: ConfigManager, username: str, cli:
189181
190182 # establish the 3 elements of the config
191183 clusters , contexts , users = build_tenant_config (
192- tenant = tenant ,
193- cluster_names = cluster_names ,
194- username = username ,
195- cli = cli
184+ tenant = tenant , cluster_names = cluster_names , username = username , cli = cli
196185 )
197186
198187 # calculate the path for the config
@@ -202,13 +191,7 @@ def build_kube_config(profiles: list, config: ConfigManager, username: str, cli:
202191
203192 # merge any existing config with the new config
204193 # and write it to disk
205- merge_new_with_existing (
206- clusters = clusters ,
207- contexts = contexts ,
208- users = users ,
209- tenant = tenant ,
210- filename = filename
211- )
194+ merge_new_with_existing (clusters = clusters , contexts = contexts , users = users , tenant = tenant , filename = filename )
212195
213196 # if required ensure we tell the user they need to modify their KUBECONFIG env var
214197 # in order to pick up the Britive managed kube config file
0 commit comments