forked from abrochard/kubel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkubel.el
More file actions
600 lines (502 loc) · 20.1 KB
/
kubel.el
File metadata and controls
600 lines (502 loc) · 20.1 KB
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
;;; kubel.el --- extension for controlling Kubernetes with limited permissions -*- lexical-binding: t; -*-
;; Copyright (C) 2018, Adrien Brochard
;; This file is NOT part of Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
;; USA
;; Version: 1.0
;; Author: Adrien Brochard
;; Keywords: kubernetes k8s tools processes
;; URL: https://github.com/abrochard/kubel
;; License: GNU General Public License >= 3
;; Package-Requires: ((transient "0.1.0") (emacs "25.3"))
;;; Commentary:
;; Emacs extension for controlling Kubernetes with limited permissions.
;; Mostly focuses on pod management for now.
;;; Usage:
;; To list the pods in your current context and namespace, call
;;
;; M-x kubel
;;
;; To set said namespace and context, respectively call
;;
;; M-x kubel-set-namespace
;; M-x kubel-set-context
;;
;; Note that namespace will autocomplete but not context,
;; this is because I interact with kubernetes through a user who
;; does not have permissions to list namespaces.
;;; Shortcuts:
;; On the kubel screen, place your cursor on the pod
;;
;; enter => get pod details
;; h => help popup
;; C => set context
;; n => set namespace
;; g => refresh pods
;; p => port forward pod
;; e => exec into pod
;; d => describe popup
;; l => log popup
;; c => copy popup
;; k => delete pod
;; j => jab deployment to force rolling update
;; f => set a substring filter for pod name
;; r => see the rollout history for resource
;;
;;; Customize:
;; By default, kubel log tails from the last 100 lines, you can change the `kubel-log-tail-n` variable to set another line number.
;;; Code:
(require 'transient)
(require 'helm)
(require 'kubel-cust)
(require 'kubel-helm)
(defconst kubel--list-format
[("Name" 50 t)
("Ready" 10 t)
("Status" 20 t)
("Restarts" 10 t)
("Age" 15 t)]
"List format.")
(defconst kubel--list-sort-key
'("Name" . nil)
"Sort table on this key.")
(defconst kubel--status-colors
'(("Running" . "green")
("Error" . "red")
("Completed" . "yellow")
("CrashLoopBackOff" . "red")
("Terminating" . "blue"))
"Associative list of status to color.")
(defvar kubel-namespace ""
"Current namespace.")
(defvar kubel-context
(replace-regexp-in-string
"\n" "" (shell-command-to-string (concat kubel-executable " config current-context")))
"Current context. Tries to smart default.")
(defvar kubel-pod-filter ""
"Substring filter for pod name.")
(defvar kubel-namespace-history '()
"List of previously used namespaces.")
(defvar kubel-log-tail-n "100"
"Number of lines to tail.")
(defun kubel--buffer-name ()
"Return kubel buffer name."
(concat "*kubel (" kubel-namespace ") [" kubel-context "]*"))
(defun kubel--extract-pod-line ()
"Return a vector from the pod line."
(let ((name (match-string 1))
(ready (match-string 2))
(status (match-string 3))
(restarts (match-string 4))
(age (match-string 5)))
(vector (kubel--propertize-pod-attribute name name)
(kubel--propertize-pod-attribute name ready)
(kubel--propertize-status status)
(kubel--propertize-pod-attribute name restarts)
(kubel--propertize-pod-attribute name age))))
(defun kubel--propertize-pod-attribute (name attribute)
"Return the pod attribute in proper font color based on active filter.
NAME is the pod name.
ATTRIBUTE is the attribute to propertize."
(if (or (equal kubel-pod-filter "") (string-match-p kubel-pod-filter name))
attribute
(propertize attribute 'font-lock-face '(:foreground "darkgrey"))))
(defun kubel--propertize-status (status)
"Return the status in proper font color.
STATUS is the pod status string."
(let ((pair (cdr (assoc status kubel--status-colors))))
(if pair
(propertize status 'font-lock-face `(:foreground ,pair))
status)))
(defun kubel--list-entries ()
"Create the entries for the service list."
(let ((temp (list)))
(with-temp-buffer
(insert (shell-command-to-string (concat (kubel--get-command-prefix) " get pods --no-headers=true")))
(goto-char (point-min))
(while (re-search-forward "^\\([a-z0-9\-]+\\) +\\([0-9]+/[0-9]+\\) +\\(\\w+\\) +\\([0-9]+\\) +\\([0-9a-z]+\\)$" (point-max) t)
(setq temp (append temp (list (list (match-string 1) (kubel--extract-pod-line)))))))
temp))
(defun kubel--pop-to-buffer (name)
"Utility function to pop to buffer or create it.
NAME is the buffer name."
(unless (get-buffer name)
(get-buffer-create name))
(pop-to-buffer name))
(defun kubel--exec (buffer-name async args)
"Utility function to run commands in the proper context and namespace.
\"BUFFER-NAME\" is the buffer-name. Default to *kubel-command*.
ASYNC is a bool. If true will run async.
ARGS is a ist of arguments."
(when (equal buffer-name "")
(setq buffer-name "*kubel-command*"))
(when (get-buffer buffer-name)
(kill-buffer buffer-name))
(if async
(apply #'start-process buffer-name buffer-name kubel-executable (append (kubel--get-context-namespace) args))
(apply #'call-process kubel-executable nil buffer-name nil (append (kubel--get-context-namespace) args)))
(pop-to-buffer buffer-name))
(defun kubel--get-pod-under-cursor ()
"Utility function to get the name of the pod under the cursor."
(aref (tabulated-list-get-entry) 0))
(defun kubel--get-context-namespace ()
"Utility function to return the proper context and namespace arguments."
(append
(unless (equal kubel-context "")
(list "--context" kubel-context))
(unless (equal kubel-namespace "")
(list "-n" kubel-namespace))))
(defun kubel--get-command-prefix ()
"Utility function to prefix the kubectl command with proper context and namespace."
(mapconcat 'identity (append (list kubel-executable) (kubel--get-context-namespace)) " "))
(defun kubel--get-containers (pod-name)
"List the containers in a pod.
POD-NAME is the name of the pod."
(split-string
(shell-command-to-string
(format "%s get pod %s -o jsonpath='{.spec.containers[*].name}'" (kubel--get-command-prefix) pod-name)) " "))
(defun kubel--select-resource (name)
"Prompt user to select an instance out of a list of resources.
NAME is the string name of the resource."
(let ((cmd (format "%s get %s -o=jsonpath='{.items[*].metadata.name}'"
(kubel--get-command-prefix) name)))
(completing-read (concat (s-upper-camel-case name) ": ")
(split-string (shell-command-to-string cmd) " "))))
(defun kubel--describe-resource (name &optional yaml)
"Describe a specific resource.
NAME is the string name of the resource to decribe.
YAML is boolean to show resource as yaml"
(let* ((resource (kubel--select-resource name))
(buffer-name (format "*kubel - %s - %s*" name resource)))
(if yaml
(kubel--exec buffer-name nil (list "get" name "-o" "yaml" resource))
(kubel--exec buffer-name nil (list "describe" name resource)))
(when yaml
(yaml-mode)
(kubel-yaml-editing-mode))
(beginning-of-buffer)))
(defun kubel--show-rollout-revision (type)
"Show a specific revision of a certain resource.
TYPE is the resource type to prompt you to select a specific one."
(let* ((name (kubel--select-resource type))
(typename (format "%s/%s" type name))
(revision (car (split-string (kubel--select-rollout typename))))
(buffer-name (format "*kubel - rollout - %s - %s*" typename revision)))
(kubel--exec buffer-name nil
(list "rollout" "history" typename (format "--revision=%s" revision)))
(beginning-of-buffer)))
(defun kubel--list-rollout (typename)
"Return a list of revisions with format '%number %cause'.
TYPENAME is the resource type/name."
(let ((cmd (format "%s rollout history %s" (kubel--get-command-prefix) typename)))
(nthcdr 2 (split-string (shell-command-to-string cmd) "\n"))))
(defun kubel--select-rollout (typename)
"Select a rollout version.
TYPENAME is the resource type/name."
(let ((prompt (format "Select a rollout of %s: " typename))
(rollouts (kubel--list-rollout typename)))
(completing-read prompt rollouts)))
;; interactive
(define-minor-mode kubel-yaml-editing-mode
:init-value nil
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'kubel-apply)
map))
(defun kubel-apply ()
"Save the current buffer to a temp file and try to kubectl apply it."
(interactive)
(let ((filename (format "/tmp/kubel/%s-%s.yaml"
(replace-regexp-in-string "\*\\| " "" (buffer-name))
(floor (float-time)))))
(unless (file-exists-p "/tmp/kubel")
(make-directory "/tmp/kubel"))
(write-region (point-min) (point-max) filename)
(kubel--exec (format "*kubectl - apply - %s*" filename) nil (list "apply" "-f" filename))
(message "Applied %s" filename)))
(defun kubel-get-pod-details ()
"Get the details of the pod under the cursor."
(interactive)
(let* ((pod (kubel--get-pod-under-cursor))
(buffer-name (format "*kubel - pod - %s*" pod)))
(kubel--exec buffer-name nil (list "describe" "pod" (kubel--get-pod-under-cursor)))
(beginning-of-buffer)))
(defun kubel--default-tail-arg (args)
"Ugly function to make sure that there is at least the default tail.
ARGS is the arg list from transient."
(if (car (remove nil (mapcar (lambda (x)
(string-prefix-p "--tail=" x)) args)))
args
(append args (list (concat "--tail=" kubel-log-tail-n)))))
(defun kubel-get-pod-logs (&optional args)
"Get the last N logs of the pod under the cursor.
ARGS is the arguments list from transient."
(interactive
(list (transient-args 'kubel-log-popup)))
(let* ((pod (kubel--get-pod-under-cursor))
(containers (kubel--get-containers pod))
(container (if (equal (length containers) 1)
(car containers)
(completing-read "Select container: " containers)))
(buffer-name (format "*kubel - logs - %s - %s*" pod container))
(async nil))
(when (member "-f" args)
(setq async t))
(kubel--exec buffer-name async
(append '("logs") (kubel--default-tail-arg args) (list pod container)))))
(defun kubel-copy-pod-name ()
"Copy the name of the pod under the cursor."
(interactive)
(kill-new (kubel--get-pod-under-cursor))
(message "Pod name copied to kill-ring"))
(defun kubel-copy-log-command ()
"Copy the streaming log command of the pod under the cursor."
(interactive)
(kill-new (concat (kubel--get-command-prefix)
" logs -f --tail=" kubel-log-tail-n " "
(kubel--get-pod-under-cursor)))
(message "Log command copied to kill-ring"))
(defun kubel-copy-command-prefix ()
"Copy the kubectl command prefix."
(interactive)
(kill-new (kubel--get-command-prefix))
(message "Command prefix copied to kill-ring"))
(defun kubel-set-namespace ()
"Set the namespace."
(interactive)
(let ((namespace (kubel-get-available-namespaces)))
(if namespace
(progn
(when (get-buffer (kubel--buffer-name))
(kill-buffer (kubel--buffer-name)))
(setq kubel-namespace namespace)
(kubel)))
)
)
(defun kubel-set-context ()
"Set the context."
(interactive)
(when (get-buffer (kubel--buffer-name)) ;; kill buffer for previous context if possible
(kill-buffer (kubel--buffer-name)))
(setq kubel-context
(completing-read
"Select context: "
(split-string (shell-command-to-string
(concat kubel-executable " config view -o jsonpath='{.contexts[*].name}'")) " ")))
(kubel))
(defun kubel-port-forward-pod (p)
"Port forward a pod to your local machine.
P is the port as integer."
(interactive "nPort: ")
(let* ((port (format "%s" p))
(pod (kubel--get-pod-under-cursor))
(buffer-name (format "*kubel - port-forward - %s:%s*" pod port)))
(kubel--exec buffer-name t (list "port-forward" pod (format "%s:%s" port port)))))
(defun kubel-describe-ingress (&optional arg)
"Show the ingress details.
ARG is the optional param to see yaml."
(interactive "P")
(if (or arg (transient-args 'kubel-describe-popup))
(kubel--describe-resource "ingress" t)
(kubel--describe-resource "ingress")))
(defun kubel-describe-service (&optional arg)
"Descibe a service.
ARG is the optional param to see yaml."
(interactive "P")
(if (or arg (transient-args 'kubel-describe-popup))
(kubel--describe-resource "service" t)
(kubel--describe-resource "service")))
(defun kubel-describe-configmaps (&optional arg)
"Describe a configmap.
ARG is the optional param to see yaml."
(interactive "P")
(if (or arg (transient-args 'kubel-describe-popup))
(kubel--describe-resource "configmap" t)
(kubel--describe-resource "configmap")))
(defun kubel-describe-deployment (&optional arg)
"Describe a deployment.
ARG is the optional param to see yaml."
(interactive "P")
(if (or arg (transient-args 'kubel-describe-popup))
(kubel--describe-resource "deployment" t)
(kubel--describe-resource "deployment")))
(defun kubel-describe-job (&optional arg)
"Describe a job.
ARG is the optional param to see yaml."
(interactive "P")
(if (or arg (transient-args 'kubel-describe-popup))
(kubel--describe-resource "job" t)
(kubel--describe-resource "job")))
;; deprecated. will remove soon
;; (defun kubel-exec-pod ()
;; "Kubectl exec into the pod under the cursor."
;; (interactive)
;; (let* ((pod (kubel--get-pod-under-cursor))
;; (containers (kubel--get-containers pod))
;; (container (if (equal (length containers) 1)
;; (car containers)
;; (completing-read "Select container: " containers))))
;; (eshell)
;; (insert (format "%s exec -it %s -c %s /bin/sh" (kubel--get-command-prefix) pod container))))
(defun kubel-exec-pod ()
"Setup a TRAMP to exec into the pod under the cursor."
(interactive)
(setq tramp-methods (delete (assoc "kubectl" tramp-methods) tramp-methods)) ;; cleanup previous tramp method
(add-to-list 'tramp-methods
`("kubectl"
(tramp-login-program "kubectl")
(tramp-login-args (,(kubel--get-context-namespace) ("exec" "-it") ("-u" "%u") ("%h") ("sh")))
(tramp-remote-shell "sh")
(tramp-remote-shell-args ("-i" "-c")))) ;; add the current context/namespace to tramp methods
(find-file (format "/kubectl:%s:/" (kubel--get-pod-under-cursor))))
(defun kubel-delete-pod ()
"Kubectl delete pod under cursor."
(interactive)
(let* ((pod (kubel--get-pod-under-cursor))
(buffer-name (format "*kubel - delete pod -%s" pod))
(args (list "delete" "pod" pod)))
(when (transient-args 'kubel-delete-popup)
(setq args (append args (list "--force" "--grace-period=0"))))
(kubel--exec buffer-name t args)))
(defun kubel-jab-deployment ()
"Make a trivial patch to force a new deployment.
See https://github.com/kubernetes/kubernetes/issues/27081"
(interactive)
(let* ((deployment (kubel--select-resource "deployment"))
(buffer-name (format "*kubel - bouncing - %s*" deployment)))
(kubel--exec buffer-name nil (list "patch" "deployment" deployment "-p"
(format "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"%s\"}}}}}"
(round (time-to-seconds)))))))
(defun kubel-set-filter (filter)
"Set the pod filter.
FILTER is the filter string."
(interactive "MFilter: ")
(setq kubel-pod-filter filter)
(kubel-mode))
(defun kubel-rollout-history-deployment ()
"See rollout history of a deployment."
(interactive)
(kubel--show-rollout-revision "deployment"))
(defun kubel-rollout-history-service ()
"See rollout history of a service."
(interactive)
(kubel--show-rollout-revision "service"))
(defun kubel-rollout-history-job ()
"See rollout history of a job."
(interactive)
(kubel--show-rollout-revision "job"))
(defun kubel-rollout-history-ingress ()
"See a rollout history of an ingress."
(interactive)
(kubel--show-rollout-revision "ingress"))
(defun kubel-rollout-history-configmap ()
"See a rollout history of a configmap."
(interactive)
(kubel--show-rollout-revision "configmap"))
;; popups
(define-transient-command kubel-log-popup ()
"Kubel Log Menu"
["Arguments"
("-f" "Follow" "-f")
("-p" "Previous" "-p")
("-n" "Tail" "--tail=")]
["Actions"
("l" "Tail pod logs" kubel-get-pod-logs)])
(define-transient-command kubel-copy-popup ()
"Kubel Copy Menu"
["Actions"
("c" "Copy pod name" kubel-copy-pod-name)
("l" "Copy pod log command" kubel-copy-log-command)
("p" "Copy command prefix" kubel-copy-command-prefix)])
(define-transient-command kubel-delete-popup ()
"Kubel Delete menu"
["Arguments"
("-f" "Force" "--force --grace-period=0")]
["Actions"
("k" "Delete pod" kubel-delete-pod)])
(define-transient-command kubel-describe-popup ()
"Kubel Describe Menu"
["Arguments"
("-y" "Yaml" "-o yaml")]
["Actions"
("d" "Deployment" kubel-describe-deployment)
("s" "Service" kubel-describe-service)
("j" "Job" kubel-describe-job)
("i" "Ingress" kubel-describe-ingress)
("c" "Configmap" kubel-describe-configmaps)])
(define-transient-command kubel-rollout-popup ()
"Kubel Rollout Menu"
["Actions"
("d" "Deployment" kubel-rollout-history-deployment)
("s" "Service" kubel-rollout-history-service)
("j" "Job" kubel-rollout-history-job)
("i" "Ingress" kubel-rollout-history-ingress)
("c" "Configmap" kubel-rollout-history-configmap)])
(define-transient-command kubel-help-popup ()
"Kubel Menu"
["Actions"
("ENTER" "Pod details" kubel-get-pod-details)
("C" "Set context" kubel-set-context)
("n" "Set namespace" kubel-set-namespace)
("g" "Refresh" kubel-mode)
("p" "Port forward" kubel-port-forward-pod)
("l" "Logs" kubel-log-popup)
("c" "Copy" kubel-copy-popup)
("d" "Describe" kubel-describe-popup)
("e" "Exec" kubel-exec-pod)
("k" "Delete" kubel-delete-popup)
("j" "Jab" kubel-jab-deployment)
("f" "Filter" kubel-set-filter)
("r" "Rollout" kubel-rollout-popup)])
;; mode map
(defvar kubel-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'kubel-get-pod-details)
(define-key map (kbd "C") 'kubel-set-context)
(define-key map (kbd "n") 'kubel-set-namespace)
(define-key map (kbd "g") 'kubel-mode)
(define-key map (kbd "p") 'kubel-port-forward-pod)
(define-key map (kbd "l") 'kubel-log-popup)
(define-key map (kbd "c") 'kubel-copy-popup)
(define-key map (kbd "h") 'kubel-help-popup)
(define-key map (kbd "d") 'kubel-describe-popup)
(define-key map (kbd "e") 'kubel-exec-pod)
(define-key map (kbd "k") 'kubel-delete-popup)
(define-key map (kbd "j") 'kubel-jab-deployment)
(define-key map (kbd "f") 'kubel-set-filter)
(define-key map (kbd "r") 'kubel-rollout-popup)
map)
"Keymap for `kubel-mode'.")
;;;###autoload
(defun kubel ()
"Invoke the kubel buffer."
(interactive)
(kubel--pop-to-buffer (kubel--buffer-name))
(kubel-mode)
)
(define-derived-mode kubel-mode tabulated-list-mode "Kubel"
"Special mode for kubel buffers."
(buffer-disable-undo)
(kill-all-local-variables)
(setq truncate-lines t)
(setq mode-name "Kubel")
(setq major-mode 'kubel-mode)
(use-local-map kubel-mode-map)
(setq tabulated-list-format kubel--list-format)
(setq tabulated-list-entries 'kubel--list-entries)
(setq tabulated-list-sort-key kubel--list-sort-key)
(tabulated-list-init-header)
(tabulated-list-print)
(hl-line-mode 1)
(run-mode-hooks 'kubel-mode-hook))
(provide 'kubel)
;;; kubel.el ends here