-
Notifications
You must be signed in to change notification settings - Fork 1
/
_ks.py
executable file
·826 lines (660 loc) · 29.4 KB
/
_ks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#!/usr/bin/env python3
# KubeShort - shortcuts to the most common kubectl actions
#
# Shortcuts by default starts with "k."" prefix and are symlinked to this script.
#
# To create symlinks, run:
# $ /path/to/_ks.py install-symlinks -t /target/directory/for/symlinks/
#
# Created symlinks will point to the _ks.py using relative paths.
# Add /target/directory/for/symlinks to your PATH:
# $ export PATH="/target/directory/for/symlinks:${PATH}"
#
# Copyright 2020 Mario Hros (K3A.me)
# License: GNU GPL v3
import sys
import os
import uuid
import argparse
import subprocess
import re
import json
import yaml
import shutil
from shlex import quote
# script name (to which symlinks should point to)
SCRIPT_FILE_NAME = "_ks.py"
# where to store the current namespace
CUR_NS_PATH = os.environ.get("KUBESHORT_CUR_NS_PATH", "/tmp/.k8s-cur-ns")
# prefix for all helpers (and thus also for the creaed symlinks)
HELPER_PREFIX = "k."
# allow even shorter object names (e.g. "cj" for "cronjob") which are not officialy accepted by kubectl get <shortname>
ALLOW_SHORT = os.environ.get("KUBESHORT_ALLOW_SHORT", "1") not in ["0", "false", "off"]
# default number of log lines to return (must be string variable)
DEFAULT_TAIL = os.environ.get("KUBESHORT_DEFAULT_TAIL", "20")
# default user for SSH login
DEFAULT_USER = os.environ.get("KUBESHORT_DEFAULT_USER", "ubuntu")
# known k8s resources (to strip "resource/"NAME prefix)
KNOWN_K8S_RESOURCES = ["bindings", "componentstatuses", "configmaps", "endpoints", "events", "limitranges", "namespaces", "nodes", "persistentvolumeclaims",
"persistentvolumes", "pods", "podtemplates", "replicationcontrollers", "resourcequotas", "secrets", "serviceaccounts", "services",
"mutatingwebhookconfigurations", "validatingwebhookconfigurations", "customresourcedefinitions", "apiservices", "controllerrevisions",
"daemonsets", "deployments", "deployment.apps", "replicasets", "statefulsets", "meshpolicies", "policies", "tokenreviews", "localsubjectaccessreviews",
"selfsubjectaccessreviews", "selfsubjectrulesreviews", "subjectaccessreviews", "horizontalpodautoscalers", "verticalpodautoscalercheckpoints",
"verticalpodautoscalers", "cronjobs", "jobs", "certificatesigningrequests", "certificates", "challenges", "clusterissuers", "issuers",
"orders", "backendconfigs", "adapters", "attributemanifests", "handlers", "httpapispecbindings", "httpapispecs", "instances",
"quotaspecbindings", "quotaspecs", "rules", "templates", "leases", "daemonsets", "deployments", "ingresses", "networkpolicies",
"podsecuritypolicies", "replicasets", "capacityrequests", "nodes", "pods", "managedcertificates", "destinationrules", "envoyfilters",
"gateways", "serviceentries", "sidecars", "syntheticserviceentries", "virtualservices", "ingresses", "networkpolicies",
"runtimeclasses", "updateinfos", "poddisruptionbudgets", "podsecuritypolicies", "clusterrolebindings", "clusterroles", "rolebindings",
"roles", "clusterrbacconfigs", "rbacconfigs", "servicerolebindings", "serviceroles", "scalingpolicies", "priorityclasses", "authorizationpolicies",
"csidrivers", "csinodes", "storageclasses", "volumeattachments",
"cs", "cm", "ep", "ev", "limits", "ns", "no", "pvc", "pv", "po", "rc", "quota", "sa", "svc", "crd", "crds", "apps", "ds", "deploy", "rs", "sts",
"hpa", "vpacheckpoint", "vpa", "cj", "batch", "csr", "cert", "certs", "ds", "deploy", "ing", "netpol", "psp", "rs", "capreq", "mcrt", "dr",
"gw", "se", "vs", "ing", "netpol", "updinf", "pdb", "psp", "pc", "sc"
]
FILTER_ATTRIBUTES = ["annotations.kubectl.kubernetes.io/restartedAt", "metadata.generateName", "metadata.ownerReferences.uid",
"metadata.uid", "metadata.resourceVersion", "metadata.selfLink", "metadata.managedFields", "metadata.creationTimestamp",
"metadata.annotations.kubectl.kubernetes.io/last-applied-configuration",
"spec.containers.imagePullPolicy", "spec.containers.ports.protocol", "spec.containers.terminationMessagePath", "spec.containers.terminationMessagePolicy",
"spec.containers.restartPolicy", "spec.containers.terminationGracePeriodSeconds",
"status.conditions", "status.containerStatuses.lastState"]
# find and run the best shell
SHELL_FINDER="sh -c 'if type bash > /dev/null; then exec bash; else exec sh; fi'"
# fail with an error message
def fail(msg):
print(msg, file=sys.stderr)
sys.exit(-1)
# random 5-character identifier
def rand_ident():
return str(uuid.uuid4().fields[-1])[:5]
# Returns length of the longest value of attrname key-name in the subkeys of obj.
# Example:
# obj:
# - subkey1:
# attrname: "some str"
# - subkey2:
# attrname: "other str"
def max_attr_len(obj, subkeys, attrname):
nlen = 0
for sub in subkeys:
if sub in obj:
for sobj in obj[sub]:
l = len(sobj[attrname])
if l > nlen:
nlen = l
return nlen
# exec kubectl, replacing the current process
def exec_kubectl(args, pager=False, pre_cmd=None):
pager_tool = None
if pre_cmd == None:
pre_cmd = []
if pager and sys.stdout.isatty():
pager_tool = shutil.which("less")
if not pager_tool:
pager_tool = shutil.which("more")
if pager_tool:
args = pre_cmd + ["kubectl"] + args
os.execvp("sh", ["sh", "-c", ' '.join(quote(arg)
for arg in args)+"|"+pager_tool])
else:
pre_cmd.append("kubectl")
os.execvp(pre_cmd[0], pre_cmd + args)
# run kubectl and return stdout
def run_kubectl(args):
res = subprocess.run(['kubectl'] + args, stdout=subprocess.PIPE)
return str(res.stdout, encoding="utf-8")
# run kubectl and return json-decoded array of items
def kubectl_items(args):
j = json.loads(run_kubectl(args))
items = []
if "items" in j:
items = j["items"]
else:
items = [j]
return items
cur_ns = ""
# get currently-selected namespace
def get_ns():
global cur_ns
if len(cur_ns) > 0:
return cur_ns
if os.path.exists(CUR_NS_PATH):
with open(CUR_NS_PATH, "rb") as f:
cur_ns = str(f.read(), encoding="utf-8")
f.close()
return cur_ns
else:
return "default"
# set the current namespace
def set_ns(ns: str):
global cur_ns
with open(CUR_NS_PATH, "wb") as f:
f.write(ns.encode("utf-8"))
cur_ns = ns
f.close()
print("Switched to namespace \"%s\"." % ns, file=sys.stderr)
# list of known helper names (the name after "k.") which will be created
# as symbolic links and assigned argument-mediating functions
symlink_helpers = {}
# the name of the current helper (if run through a symlink)
this_helper = os.path.basename(sys.argv[0])
if this_helper.startswith(HELPER_PREFIX):
this_helper = this_helper[len(HELPER_PREFIX):]
def safe_symlink(src, dst):
print("symlink", dst, "->", src)
try:
os.remove(dst)
except:
pass
os.symlink(src, dst)
def make_symlinks(args):
this_script_dir = os.path.dirname(sys.argv[0])
tgt = this_script_dir
if args.target_path != None:
tgt = args.target_path
rel_path = os.path.relpath(this_script_dir, tgt)
rel_script_target = os.path.join(rel_path, SCRIPT_FILE_NAME)
# basic symlink
symlink_path = os.path.join(tgt, "k")
safe_symlink(rel_script_target, symlink_path)
# helper symlinks
for h in symlink_helpers:
symlink_path = os.path.join(tgt, HELPER_PREFIX + h)
safe_symlink(rel_script_target, symlink_path)
print("Created symbolic links at %s" % tgt)
sys.exit()
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()
def shared_help(help_func, base_form):
def h():
help_func()
print()
if base_form != None:
exec_kubectl(base_form + ["--help"])
return h
def apply_ns(args, p=None):
if p != None and hasattr(p, "namespace"):
args.append("-n")
args.append(p.namespace if len(p.namespace) > 0 else get_ns())
else:
has_ns = False
for a in args:
if a == "-n" or a == "--namespace":
has_ns = True
break
if not has_ns:
args += ["-n", get_ns()]
return args
def strip_resource_prefix(n):
nparts = n.split("/")
if len(nparts) == 2 and (nparts[0].lower() in KNOWN_K8S_RESOURCES or nparts[0].lower()+"s" in KNOWN_K8S_RESOURCES):
return os.path.basename(nparts[1])
else:
return n
def default_func_middleware(base_form, pre_cmd=None):
def f(p, extra_args):
args = []
for a in extra_args:
if a.startswith("-"):
args.append(a)
else:
args.append(strip_resource_prefix(a))
args = apply_ns(args, p)
exec_kubectl(base_form + args, pre_cmd=pre_cmd)
return f
def register_helper(name, description, base_form=None, pre_cmd=None, namespaced=True, func=None):
if base_form == None and func == None:
fail("base_form or func argument must be defined for register_helper()")
if name not in symlink_helpers:
symlink_helpers[name] = []
p = argparse.ArgumentParser(name, description=description)
p.print_help = shared_help(p.print_help, base_form)
if func != None:
p.set_defaults(func=func)
else:
p.set_defaults(func=default_func_middleware(base_form, pre_cmd=pre_cmd))
if namespaced:
p.add_argument("-n", "--namespace",
help="Namespace to work with", default=get_ns())
symlink_helpers[name].append(p)
return p
def register_common_helpers(name, k8s_obj_name, long_name=None, namespaced=True):
if long_name == None:
long_name = k8s_obj_name
register_helper(name, "get "+long_name,
["get", k8s_obj_name], namespaced=namespaced)
register_helper(name+".w", "get "+long_name+" (wide)",
["get", k8s_obj_name, "-o", "wide"], namespaced=namespaced)
register_helper(name+".wa", "watch "+long_name,
["get", k8s_obj_name], pre_cmd=["watch"], namespaced=namespaced)
register_helper(name+".desc", "describe "+long_name,
["describe", k8s_obj_name], namespaced=namespaced)
register_helper(name+".del", "delete "+long_name,
["delete", k8s_obj_name], namespaced=namespaced)
register_helper(name+".ed", "edit "+long_name,
["edit", k8s_obj_name], namespaced=namespaced)
h = register_helper(name+".y", "get YAML representation of "+long_name,
namespaced=namespaced, func=hlp_yaml_middleware(["get", k8s_obj_name, "-o", "yaml"]))
h.add_argument("-f", "--full", help="display full representation (without filtering less useful fields)",
default=False, action="store_true")
register_helper(name+".j", "get JSON representation of "+long_name,
["get", k8s_obj_name, "-o", "json"], namespaced=namespaced)
def dict_with_filtered_attributes(d: dict, filter_spec: list, cur_path=[]):
new_dict = {}
for key in d.keys():
val = d[key]
key_path = cur_path+[key]
if ".".join(key_path) not in filter_spec:
if isinstance(val, dict):
new_dict[key] = dict_with_filtered_attributes(val, filter_spec, cur_path=key_path)
elif isinstance(val, list):
arr = []
new_dict[key] = arr
for v in val:
if isinstance(v, dict):
arr.append(dict_with_filtered_attributes(v, filter_spec, cur_path=key_path))
else:
arr.append(v)
else:
new_dict[key] = val
return new_dict
def hlp_yaml_middleware(base_form: list):
def f(p, extra_args):
args = []
for a in extra_args:
if a.startswith("-"):
args.append(a)
else:
args.append(strip_resource_prefix(a))
args = apply_ns(base_form + args, p)
if p.full:
exec_kubectl(args)
else:
y = yaml.safe_load(run_kubectl(args))
y = dict_with_filtered_attributes(y, FILTER_ATTRIBUTES)
print(yaml.dump(y), end='')
return f
def hlp_no_res(p, extra_args):
out = run_kubectl(["describe", "node"] + extra_args)
pattern = re.compile(
r'(^Name:\s*(.*?)\n)|(^(Allocated resources:.*?)Events)', flags=re.MULTILINE | re.DOTALL)
for (_, name, _, res) in re.findall(pattern, out):
if len(name) > 0:
name = "Name: " + name
print(name, res)
def hlp_no_po(p, extra_args):
out = run_kubectl(["describe", "node"] + extra_args)
pattern = re.compile(
r'(^Name:\s*(.*?)\n)|(^(Non-terminated Pods:.*?)Allocated)', flags=re.MULTILINE | re.DOTALL)
for (_, name, _, res) in re.findall(pattern, out):
if len(name) > 0:
name = "Name: " + name
print(name, res)
def hlp_use(p, extra_args):
if p.set_ns != None:
set_ns(p.set_ns)
else:
print("Current namespace:", get_ns(), file=sys.stderr)
def hlp_ctx(p, extra_args):
if p.set_ctx != None:
set_ns("default")
exec_kubectl(["config", "use-context", p.set_ctx])
else:
exec_kubectl(["config", "get-contexts"])
def hlp_apply_f(p, extra_args):
exec_kubectl(["apply", "-f", p.file_or_url] + extra_args)
def hlp_del_f(p, extra_args):
exec_kubectl(["delete", "-f", p.file_or_url] + extra_args)
def hlp_apply_k(p, extra_args):
exec_kubectl(["apply", "-k", p.file_or_url] + extra_args)
def get_node_external_host(node):
exhost = None
for addr in node["status"]["addresses"]:
if exhost == None and addr["type"] == "ExternalDNS":
exhost = addr["address"]
elif addr["type"] == "ExternalIP":
exhost = addr["address"]
return exhost
# returns dict of node_name => externalIP keys
def get_nodes_with_external_host(get_node_args=[]):
nodes = kubectl_items(["get", "node", "-o", "json"] + get_node_args)
out = {}
for node in nodes:
exhost = get_node_external_host(node)
out[node["metadata"]["name"]] = exhost
return out
def hlp_no_x(p, extra_args):
node_args = []
if p.selector != None:
node_args += ["-l", p.selector]
if p.nodes != None:
node_args += p.nodes
nodes = get_nodes_with_external_host(node_args)
for node_name in nodes:
print("Name:", node_name, file=sys.stderr)
node_host = nodes[node_name]
if node_host == None:
print("(no external access)", file=sys.stderr)
continue
cmd = []
if p.no_sudo != True:
cmd = ["sudo"]
if p.command != None and len(p.command) > 0 and p.command != "sh":
cmd += [p.command]
else:
cmd += [SHELL_FINDER]
if len(nodes) > 1:
subprocess.run(["ssh", "-t", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=off", "-o", "LogLevel=error",
p.user+"@"+node_host] + cmd)
else:
os.execvpe("ssh", ["ssh", "-t", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=off", "-o", "LogLevel=error",
p.user+"@"+node_host] + cmd, env=os.environ)
def hlp_no_df(p, extra_args):
node_args = []
if p.selector != None:
node_args += ["-l", p.selector]
if p.nodes != None:
node_args += p.nodes
nodes = get_nodes_with_external_host(node_args)
for node_name in nodes:
print("Name:", node_name)
node_host = nodes[node_name]
if node_host == None:
print("(no external access)", file=sys.stderr)
continue
cmd = []
if p.no_sudo != True:
cmd = ["sudo"]
cmd += ["df", "-h"]
res = subprocess.run(["ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=off",
p.user+"@"+node_host] + cmd, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE)
for l in str(res.stdout, encoding="utf-8").splitlines():
if l.startswith("/"):
print(l)
def hlp_no_drain(p, extra_args):
args = extra_args
if p.complete:
args += ["--force", "--delete-local-data", "--ignore-daemonsets"]
exec_kubectl(["drain"] + args)
def hlp_logs(p, extra_args):
args = apply_ns(["logs"], p)
positionals = []
container_specified = False
can_use_pager = True
if p.selector != None:
args += ["--selector", p.selector]
args += ["--tail", "100"]
if p.tail != None:
args += ["--tail", p.tail]
if p.container != None:
args += ["--container", p.container]
container_specified = True
if p.follow:
args += ["--follow"]
can_use_pager = False
# when follow is requested without --tail, return DEFAULT_TAIL most recent by default
# to prevent flooding the console with complete log
if p.tail == None:
args += ["--tail", DEFAULT_TAIL]
if p.previous:
args += ["--previous"]
if p.raw:
can_use_pager = False
for a in extra_args:
if not a.startswith("-"):
positionals += [a]
else:
args += [a]
# container as the second positional
if len(positionals) == 2:
args += ["--container", positionals[1]]
container_specified = True
positionals = positionals[:1]
if len(positionals) > 0:
# fix pod/name to just name
positionals[0] = strip_resource_prefix(positionals[0])
# all containers by default
if not container_specified and p.container is None:
args += ["--all-containers"]
args += positionals
exec_kubectl(args, pager=can_use_pager)
def hlp_run(p, extra_args):
args = apply_ns([], p)
if p.name:
args += [p.name]
else:
args += ["kube-short-"+rand_ident()]
if p.image:
args += ["--image", p.image]
if p.generator:
args += ["--generator", p.generator]
if not p.no_rm:
args += ["--rm"]
if not p.no_it:
args += ["-i", "-t"]
exec_kubectl(["run"] + args + extra_args)
def hlp_po_x(p, extra_args):
args = apply_ns([], p)
if p.container:
args += ["-c", p.container]
if not p.no_it:
args += ["-i", "-t"]
num_positionals = 0
for a in extra_args:
if not a.startswith("-"):
num_positionals += 1
if num_positionals == 1:
exec_kubectl(["exec"] + args + extra_args + ["--", "sh", "-c", SHELL_FINDER])
else:
exec_kubectl(["exec"] + args + extra_args)
def hlp_po_co(p, extra_args):
args = apply_ns([], p)
items = kubectl_items(["get", "pods", "-o", "json"] + args + extra_args)
for pod in items:
print("Name:", pod["metadata"]["name"])
spec = pod["spec"]
njust = max_attr_len(spec, ["initContainers", "containers"], "name")
if "initContainers" in spec:
for c in spec["initContainers"]:
print(" I", c["name"].ljust(njust), " ", "image:", c["image"])
if "containers" in spec:
for c in spec["containers"]:
print(" C", c["name"].ljust(njust), " ", "image:", c["image"])
print()
def hlp_scale(p, extra_args):
args = apply_ns([], p)
targets_contain_objtypes = True
targets = []
# extracts name from "name=replicas" definition
def name_from_def(defstr):
return defstr.split("=", 1)[0]
# split extra_args to args and target object names
for a in extra_args:
if a.startswith("-"):
args.append(a)
else:
targets.append(a)
if "/" not in a:
targets_contain_objtypes = False
if not targets_contain_objtypes:
# get names of all possible objects
known_kinds = {} # oair of: lowercase item_name -> array of lowercase kinds
items = kubectl_items(
["get", "deployment,replicaset,replicationcontroller,statefulset", "-o", "json"] + args)
for i in items:
kind = i["kind"].lower()
name = i["metadata"]["name"].lower()
if name not in known_kinds:
known_kinds[name] = []
known_kinds[name].append(kind)
for i, tgtdef in enumerate(targets):
if "/" not in tgtdef:
namekey = name_from_def(tgtdef).lower()
if namekey in known_kinds:
kinds = known_kinds[namekey]
if len(kinds) == 1:
targets[i] = kinds[0] + "/" + tgtdef
elif len(kinds) > 1:
fail("ambiguous scale target " + namekey +
" with kinds " + " ".join(kinds))
for tgtdef in targets:
parts = tgtdef.split("=", 1)
if len(parts) == 2: # name=repl_num format
exec_kubectl(["scale", "--replicas", parts[1], parts[0]] + args)
else: # name format (expect --replicas in args)
exec_kubectl(["scale", parts[0]] + args)
h = register_helper("ev", "get events", [
"get", "events", "--sort-by", ".metadata.creationTimestamp", "-o", "custom-columns=SEEN:.lastTimestamp,SOURCE:.source.component,NAME:.metadata.name,REASON:.reason,MSG:.message"])
h = register_helper(
"run", "run a new temporary deployment with a TTY attached", func=hlp_run)
h.add_argument("--name", help="pod name (default random)")
h.add_argument("-i", "--image", help="image to pull and run", default="alpine")
h.add_argument("-g", "--generator",
help="generator to use for the deployment", default="run-pod/v1")
h.add_argument("--no-rm", default=False, action="store_true",
help="do not remove container upon leaving the shell")
h.add_argument("--no-it", default=False, action="store_true",
help="do not add -i -t arguments")
register_common_helpers("ns", "namespace", "namespaces")
register_common_helpers("po", "pod", "pods")
h = register_helper("po.names", "get the names of the matching pods", [
"get", "pods", "-o", "jsonpath='{.items[*].metadata.name}'"])
h = register_helper("po.first", "get the name of the first matching pod", [
"get", "pods", "-o", "jsonpath={.items[0].metadata.name}"])
h = register_helper(
"po.top", "get table of processes for a pod", ["top", "pod"])
h = register_helper("po.co", "list containers of pod(s)",
["get", "pods"], func=hlp_po_co)
h = register_helper(
"po.x", "execute a command in the container (bash by default)", func=hlp_po_x)
h.add_argument("-c", "--container",
help="container to run the shell in (the first one by default)")
h.add_argument("--no-it", default=False, action="store_true",
help="do not pass -i -t to the kubectl exec (PTY)")
register_common_helpers("svc", "service", "services")
register_common_helpers("rs", "replicaset", "replica sets")
register_common_helpers("rc", "replicationcontroller",
"replication controllers")
register_common_helpers("sts", "statefulset", "stateful sets")
register_common_helpers("ds", "daemonset", "daemon sets")
register_common_helpers("cj", "cronjob", "cron jobs")
register_common_helpers("cm", "configmap", "config maps")
register_common_helpers("pv", "persistentvolume", "persistent volumes")
register_common_helpers("pvc", "persistentvolumeclaim", "persistent volume claims")
register_common_helpers("pdb", "poddisruptionbudget", "pod disruption budgets")
register_common_helpers("hpa", "horizontalpodautoscaler", "horizontal pod autoscalers")
if ALLOW_SHORT:
register_common_helpers("j", "job", "jobs")
register_common_helpers("d", "deployment", "deployments")
register_common_helpers("sec", "secret", "secrets")
else:
register_common_helpers("job", "job", "jobs")
register_common_helpers("deploy", "deployment", "deployments")
register_common_helpers("secret", "secret", "secrets")
register_common_helpers("no", "node", "nodes", namespaced=False)
h = register_helper("no.top", "get table of processes for a node", [
"top", "node"], namespaced=False)
h = register_helper("no.res", "list nodes with resources requested on them", [
"describe", "node"], namespaced=False, func=hlp_no_res)
h = register_helper("no.po", "pods on a node(s)", [
"describe", "nodes"], func=hlp_no_po)
h = register_helper("no.dr", "drain node", [
"drain"], namespaced=False, func=hlp_no_drain)
h.add_argument("-C", "--complete", default=False, action="store_true",
help="drain the node completely (implies --force, --delete-local-data and --ignore-daemonsets)")
h = register_helper("no.x", "execute command in remote node(s)",
namespaced=False, func=hlp_no_x)
h.add_argument("nodes", nargs="*", help="node names")
h.add_argument("-l", "--selector", help="node label selector")
h.add_argument(
"-u", "--user", help="user to connect via ssh to", default=DEFAULT_USER)
h.add_argument("-S", "--no-sudo", help="do not use sudo before command", action="store_true")
h.add_argument("-x", "--command", "--execute",
help="remote command to execute", default="sh")
h = register_helper("no.df", "get node(s) disk usage", [
"get", "node"], namespaced=False, func=hlp_no_df)
h.add_argument("nodes", nargs="*", help="node names")
h.add_argument("-l", "--selector", help="node label selector")
h.add_argument(
"-u", "--user", help="user to connect via ssh to", default=DEFAULT_USER)
h.add_argument("-S", "--no-sudo", help="do not use sudo before command", action="store_true")
h = register_helper(
"use", "set the working namespace or return the current one", namespaced=False, func=hlp_use)
h.add_argument("set_ns", help="namespace to switch to", nargs="?")
h = register_helper(
"ctx", "switch kubeconfig context or return the current one", namespaced=False, func=hlp_ctx)
h.add_argument("set_ctx", help="context name to switch to", nargs="?")
h = register_helper("logs", "get container logs", func=hlp_logs)
h.add_argument("--tail", nargs="?", const=DEFAULT_TAIL,
help="print this number of lines from the end")
h.add_argument("-f", "--follow", action="store_true",
help="stream following lines")
h.add_argument("-p", "--previous", action="store_true",
help="pritn the logs for the previous instance of the container if it exists")
h.add_argument("-l", "--selector", help="label selector")
h.add_argument("-c", "--container",
help="container name of a multi-container pod")
h.add_argument("-r", "--raw", action="store_true",
help="do not use pager (less/more), applies to non --follow log output only")
# in addition to "kubectl scale" arguments, it is possible to use short format with name=replicas
h = register_helper("scale", "scale deployment, replicaset, replication controller or statefulset", [
"scale"], func=hlp_scale)
h = register_helper("apl.f", "apply file",
namespaced=True, func=hlp_apply_f)
h.add_argument("file_or_url", help="file path or URL to the manifest to apply")
h = register_helper("del.f", "apply kustomization file",
namespaced=True, func=hlp_del_f)
h.add_argument(
"file_or_url", help="file path pr URL to kustomization manifest")
h = register_helper("apl.k", "apply kustomization file",
namespaced=False, func=hlp_apply_k)
h.add_argument(
"file_or_url", help="file path pr URL to kustomization manifest")
cmd_install = subparser.add_parser(
"install-symlinks", description="install symbolic links")
cmd_install.set_defaults(func=make_symlinks)
cmd_install.add_argument("-t", "--target-path",
help="target path to install symlinks")
# common actions
if os.path.basename(sys.argv[0]) == "k":
args = apply_ns(sys.argv[1:])
exec_kubectl(args)
sys.exit(0)
elif len(sys.argv) > 1:
if sys.argv[1] == "install-symlinks":
args = parser.parse_args()
if hasattr(args, "func") and args.func != None:
args.func(args)
sys.exit(0)
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
cmd_install.print_help()
for h in symlink_helpers:
for p in symlink_helpers[h]:
print(p.description, end="\n ")
p.print_usage()
print()
exec_kubectl(["--help"])
sys.exit(0)
# attempt to run the helper command
if this_helper in symlink_helpers:
matched = False
for p in symlink_helpers[this_helper]:
try:
args, extra_args = p.parse_known_args(sys.argv[1:])
args.func(args, extra_args)
matched = True
break
except SystemExit:
pass
if matched != True:
# for p in symlink_helpers[this_helper]:
# p.print_usage()
sys.exit(45)
else:
sys.exit() # should not go here normally (helper function has been called)
# run the kubectl command unmodified (we don't have a helper defined!)
argv = sys.argv[1:]
print("Warning: No helper " + this_helper, file=sys.stderr)
print("Warning: Running kubectl " + " ".join(argv) +
" directly without " + SCRIPT_FILE_NAME, file=sys.stderr)
exec_kubectl(argv)