6
6
import json
7
7
import os
8
8
import os .path
9
- from kubernetes import client , config
9
+
10
+ from kubeshell .client import KubernetesClient
10
11
11
12
class KubectlCompleter (Completer ):
12
13
@@ -17,6 +18,7 @@ def __init__(self):
17
18
self .global_opts = []
18
19
self .inline_help = True
19
20
self .namespace = ""
21
+ self .kube_client = KubernetesClient ()
20
22
21
23
try :
22
24
DATA_DIR = os .path .dirname (os .path .realpath (__file__ ))
@@ -124,7 +126,7 @@ def parse_tokens(self, cmdline):
124
126
elif state == "KUBECTL_ARG" :
125
127
if token .startswith ("--" ):
126
128
continue
127
- resources = self .get_resources (arg )
129
+ resources = self .kube_client . get_resource (arg )
128
130
if resources :
129
131
for resource_name , namespace in resources :
130
132
if token == resource_name :
@@ -182,7 +184,7 @@ def get_completions(self, document, complete_event, smart_completion=None):
182
184
yield Completion (suggestion , - len (last_token ), display = suggestion , display_meta = self .help_msg )
183
185
if word_before_cursor == "" :
184
186
if last_token == "--namespace" :
185
- namespaces = self .get_resources ("namespace" )
187
+ namespaces = self .kube_client . get_resource ("namespace" )
186
188
for ns in namespaces :
187
189
yield Completion (ns [0 ])
188
190
return
@@ -223,7 +225,7 @@ def get_completions(self, document, complete_event, smart_completion=None):
223
225
yield Completion (arg , - len (last_token ))
224
226
elif word_before_cursor == "" :
225
227
if last_token == "--namespace" :
226
- namespaces = self .get_resources ("namespace" )
228
+ namespaces = self .kube_client . get_resource ("namespace" )
227
229
for ns in namespaces :
228
230
yield Completion (ns [0 ])
229
231
return
@@ -239,11 +241,11 @@ def get_completions(self, document, complete_event, smart_completion=None):
239
241
last_token = tokens [- 1 ]
240
242
if word_before_cursor == "" :
241
243
if last_token == "--namespace" :
242
- namespaces = self .get_resources ("namespace" )
244
+ namespaces = self .kube_client . get_resource ("namespace" )
243
245
for ns in namespaces :
244
246
yield Completion (ns [0 ])
245
247
return
246
- resources = self .get_resources (arg , namespace )
248
+ resources = self .kube_client . get_resource (arg , namespace )
247
249
if resources :
248
250
for resourceName , namespace in resources :
249
251
yield Completion (resourceName , display = resourceName , display_meta = namespace )
@@ -265,105 +267,10 @@ def get_completions(self, document, complete_event, smart_completion=None):
265
267
help_msg = self .kubectl_dict ['kubectl' ]['options' ][global_opt ]['help' ]
266
268
yield Completion (global_opt , - len (word_before_cursor ), display = global_opt , display_meta = self .help_msg )
267
269
if last_token == "--namespace" :
268
- namespaces = self .get_resources ("namespace" )
270
+ namespaces = self .kube_client . get_resource ("namespace" )
269
271
for ns in namespaces :
270
272
yield Completion (ns [0 ])
271
273
return
272
274
else :
273
275
pass
274
276
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