2
2
from subprocess import check_output
3
3
from prompt_toolkit .completion import Completer , Completion
4
4
from fuzzyfinder import fuzzyfinder
5
+ import logging
5
6
import shlex
6
7
import json
7
8
import os
8
9
import os .path
9
- from kubernetes import client , config
10
+
11
+ from kubeshell .client import KubernetesClient
12
+
10
13
11
14
class KubectlCompleter (Completer ):
12
15
@@ -17,6 +20,8 @@ def __init__(self):
17
20
self .global_opts = []
18
21
self .inline_help = True
19
22
self .namespace = ""
23
+ self .kube_client = KubernetesClient ()
24
+ self .logger = logging .getLogger (__name__ )
20
25
21
26
try :
22
27
DATA_DIR = os .path .dirname (os .path .realpath (__file__ ))
@@ -25,7 +30,7 @@ def __init__(self):
25
30
self .kubectl_dict = json .load (json_file )
26
31
self .populate_cmds_args_opts (self .kubectl_dict )
27
32
except Exception as ex :
28
- print ("got an exception" + ex .message )
33
+ self . logger . error ("got an exception" + ex .message )
29
34
30
35
def set_inline_help (self , val ):
31
36
self .inline_help = val
@@ -124,7 +129,7 @@ def parse_tokens(self, cmdline):
124
129
elif state == "KUBECTL_ARG" :
125
130
if token .startswith ("--" ):
126
131
continue
127
- resources = self .get_resources (arg )
132
+ resources = self .kube_client . get_resource (arg )
128
133
if resources :
129
134
for resource_name , namespace in resources :
130
135
if token == resource_name :
@@ -182,7 +187,7 @@ def get_completions(self, document, complete_event, smart_completion=None):
182
187
yield Completion (suggestion , - len (last_token ), display = suggestion , display_meta = self .help_msg )
183
188
if word_before_cursor == "" :
184
189
if last_token == "--namespace" :
185
- namespaces = self .get_resources ("namespace" )
190
+ namespaces = self .kube_client . get_resource ("namespace" )
186
191
for ns in namespaces :
187
192
yield Completion (ns [0 ])
188
193
return
@@ -223,7 +228,7 @@ def get_completions(self, document, complete_event, smart_completion=None):
223
228
yield Completion (arg , - len (last_token ))
224
229
elif word_before_cursor == "" :
225
230
if last_token == "--namespace" :
226
- namespaces = self .get_resources ("namespace" )
231
+ namespaces = self .kube_client . get_resource ("namespace" )
227
232
for ns in namespaces :
228
233
yield Completion (ns [0 ])
229
234
return
@@ -239,11 +244,11 @@ def get_completions(self, document, complete_event, smart_completion=None):
239
244
last_token = tokens [- 1 ]
240
245
if word_before_cursor == "" :
241
246
if last_token == "--namespace" :
242
- namespaces = self .get_resources ("namespace" )
247
+ namespaces = self .kube_client . get_resource ("namespace" )
243
248
for ns in namespaces :
244
249
yield Completion (ns [0 ])
245
250
return
246
- resources = self .get_resources (arg , namespace )
251
+ resources = self .kube_client . get_resource (arg , namespace )
247
252
if resources :
248
253
for resourceName , namespace in resources :
249
254
yield Completion (resourceName , display = resourceName , display_meta = namespace )
@@ -265,105 +270,10 @@ def get_completions(self, document, complete_event, smart_completion=None):
265
270
help_msg = self .kubectl_dict ['kubectl' ]['options' ][global_opt ]['help' ]
266
271
yield Completion (global_opt , - len (word_before_cursor ), display = global_opt , display_meta = self .help_msg )
267
272
if last_token == "--namespace" :
268
- namespaces = self .get_resources ("namespace" )
273
+ namespaces = self .kube_client . get_resource ("namespace" )
269
274
for ns in namespaces :
270
275
yield Completion (ns [0 ])
271
276
return
272
277
else :
273
278
pass
274
279
return
275
-
276
- def get_resources (self , resource , namespace = "all" ):
277
- resources = []
278
- try :
279
- config .load_kube_config ()
280
- except Exception as e :
281
- # TODO: log errors to log file
282
- return resources
283
-
284
- v1 = client .CoreV1Api ()
285
- v1Beta1 = client .AppsV1beta1Api ()
286
- extensionsV1Beta1 = client .ExtensionsV1beta1Api ()
287
- autoscalingV1Api = client .AutoscalingV1Api ()
288
- rbacAPi = client .RbacAuthorizationV1beta1Api ()
289
- batchV1Api = client .BatchV1Api ()
290
- batchV2Api = client .BatchV2alpha1Api ()
291
-
292
- ret = None
293
- namespaced_resource = True
294
-
295
- if resource == "pod" :
296
- ret = v1 .list_pod_for_all_namespaces (watch = False )
297
- elif resource == "service" :
298
- ret = v1 .list_service_for_all_namespaces (watch = False )
299
- elif resource == "deployment" :
300
- ret = v1Beta1 .list_deployment_for_all_namespaces (watch = False )
301
- elif resource == "statefulset" :
302
- ret = v1Beta1 .list_stateful_set_for_all_namespaces (watch = False )
303
- elif resource == "node" :
304
- namespaced_resource = False
305
- ret = v1 .list_node (watch = False )
306
- elif resource == "namespace" :
307
- namespaced_resource = False
308
- ret = v1 .list_namespace (watch = False )
309
- elif resource == "daemonset" :
310
- ret = extensionsV1Beta1 .list_daemon_set_for_all_namespaces (watch = False )
311
- elif resource == "networkpolicy" :
312
- ret = extensionsV1Beta1 .list_network_policy_for_all_namespaces (watch = False )
313
- elif resource == "thirdpartyresource" :
314
- namespaced_resource = False
315
- ret = extensionsV1Beta1 .list_third_party_resource (watch = False )
316
- elif resource == "replicationcontroller" :
317
- ret = v1 .list_replication_controller_for_all_namespaces (watch = False )
318
- elif resource == "replicaset" :
319
- ret = extensionsV1Beta1 .list_replica_set_for_all_namespaces (watch = False )
320
- elif resource == "ingress" :
321
- ret = extensionsV1Beta1 .list_ingress_for_all_namespaces (watch = False )
322
- elif resource == "endpoints" :
323
- ret = v1 .list_endpoints_for_all_namespaces (watch = False )
324
- elif resource == "configmap" :
325
- ret = v1 .list_config_map_for_all_namespaces (watch = False )
326
- elif resource == "event" :
327
- ret = v1 .list_event_for_all_namespaces (watch = False )
328
- elif resource == "limitrange" :
329
- ret = v1 .list_limit_range_for_all_namespaces (watch = False )
330
- elif resource == "configmap" :
331
- ret = v1 .list_config_map_for_all_namespaces (watch = False )
332
- elif resource == "persistentvolume" :
333
- namespaced_resource = False
334
- ret = v1 .list_persistent_volume (watch = False )
335
- elif resource == "secret" :
336
- ret = v1 .list_secret_for_all_namespaces (watch = False )
337
- elif resource == "resourcequota" :
338
- ret = v1 .list_resource_quota_for_all_namespaces (watch = False )
339
- elif resource == "componentstatus" :
340
- namespaced_resource = False
341
- ret = v1 .list_component_status (watch = False )
342
- elif resource == "podtemplate" :
343
- ret = v1 .list_pod_template_for_all_namespaces (watch = False )
344
- elif resource == "serviceaccount" :
345
- ret = v1 .list_service_account_for_all_namespaces (watch = False )
346
- elif resource == "horizontalpodautoscaler" :
347
- ret = autoscalingV1Api .list_horizontal_pod_autoscaler_for_all_namespaces (watch = False )
348
- elif resource == "clusterrole" :
349
- namespaced_resource = False
350
- ret = rbacAPi .list_cluster_role (watch = False )
351
- elif resource == "clusterrolebinding" :
352
- namespaced_resource = False
353
- ret = rbacAPi .list_cluster_role_binding (watch = False )
354
- elif resource == "job" :
355
- ret = batchV1Api .list_job_for_all_namespaces (watch = False )
356
- elif resource == "cronjob" :
357
- ret = batchV2Api .list_cron_job_for_all_namespaces (watch = False )
358
- elif resource == "scheduledjob" :
359
- ret = batchV2Api .list_scheduled_job_for_all_namespaces (watch = False )
360
-
361
- if ret :
362
- for i in ret .items :
363
- if namespace == "all" or not namespaced_resource :
364
- resources .append ((i .metadata .name , i .metadata .namespace ))
365
- elif namespace == i .metadata .namespace :
366
- resources .append ((i .metadata .name , i .metadata .namespace ))
367
- return resources
368
- return None
369
-
0 commit comments