23
23
import os
24
24
from kubernetes import client , config
25
25
26
- global path_set
27
- path_set = False
28
26
global api_client
29
27
api_client = None
28
+ global config_path
29
+ config_path = None
30
30
31
31
32
32
class Authentication (metaclass = abc .ABCMeta ):
@@ -61,7 +61,7 @@ def load_kube_config(self):
61
61
62
62
def config_check (self ):
63
63
"""
64
- Method for setting your Kubernetes configuration to a certain file
64
+ Method for checking if a user is authenticated via token and server or with their own config file
65
65
"""
66
66
pass
67
67
@@ -99,9 +99,9 @@ def login(self) -> str:
99
99
"""
100
100
This function is used to log in to a Kubernetes cluster using the user's API token and API server address.
101
101
Depending on the cluster, a user can choose to login in with `--insecure-skip-tls-verify` by setting `skip_tls`
102
- to `True` or `--certificate-authority` by setting `skip_tls` to false and providing a path to a ca bundle with `ca_cert_path`.
102
+ to `True` or `--certificate-authority` by setting `skip_tls` to False and providing a path to a ca bundle with `ca_cert_path`.
103
103
"""
104
- global path_set
104
+ global config_path
105
105
global api_client
106
106
try :
107
107
configuration = client .Configuration ()
@@ -113,16 +113,16 @@ def login(self) -> str:
113
113
else :
114
114
configuration .verify_ssl = False
115
115
api_client = client .ApiClient (configuration )
116
- path_set = False
116
+ config_path = None
117
117
return "Logged into %s" % self .server
118
118
except client .ApiException as exception :
119
119
return exception
120
120
121
- def api_config_handler ():
121
+ def api_config_handler () -> str :
122
122
"""
123
123
This function is used to load the api client if the user has logged in
124
124
"""
125
- if api_client != None and path_set == False :
125
+ if api_client != None and config_path == None :
126
126
return api_client
127
127
else :
128
128
return None
@@ -131,10 +131,11 @@ def logout(self) -> str:
131
131
"""
132
132
This function is used to logout of a Kubernetes cluster.
133
133
"""
134
- global path_set
135
- path_set = False
134
+ global config_path
135
+ config_path = None
136
136
global api_client
137
137
api_client = None
138
+ return "Successfully logged out of %s" % self .server
138
139
139
140
140
141
class KubeConfigFileAuthentication (KubeConfiguration ):
@@ -150,24 +151,27 @@ def load_kube_config(self):
150
151
"""
151
152
Function for loading a user's own predefined Kubernetes config file.
152
153
"""
153
- global path_set
154
+ global config_path
154
155
global api_client
155
156
try :
156
- path_set = True
157
+ if self .kube_config_path == None :
158
+ return "Please specify a config file path"
159
+ config_path = self .kube_config_path
157
160
api_client = None
158
- config .load_kube_config (self .kube_config_path )
159
161
response = "Loaded user config file at path %s" % self .kube_config_path
160
162
except config .ConfigException :
161
- path_set = False
163
+ config_path = None
162
164
raise Exception ("Please specify a config file path" )
163
165
return response
164
166
165
- def config_check ():
167
+ def config_check () -> str :
166
168
"""
167
169
Function for loading the config file at the default config location ~/.kube/config if the user has not
168
170
specified their own config file or has logged in with their token and server.
169
171
"""
170
- global path_set
172
+ global config_path
171
173
global api_client
172
- if path_set == False and api_client == None :
174
+ if config_path == None and api_client == None :
173
175
config .load_kube_config ()
176
+ if config_path != None and api_client == None :
177
+ return config_path
0 commit comments