-
Notifications
You must be signed in to change notification settings - Fork 2
/
kubectl.el
391 lines (338 loc) · 15.2 KB
/
kubectl.el
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
;; uses json
;; uses transient
;; TODO port forward mechanism
(defvar-local kubectl--namespace "" "Namespace to pass to kubectl, or empty to pass no namespace")
(defvar-local kubectl--context "" "Context to pass to kubectl, or empty to pass no context")
(defvar-local kubectl--pods-selector "" "Selector to filter pods on, or empty to show all pods")
(defvar kubectl--shell "/bin/bash" "Shell to run when opening a term to a pod")
(defcustom kubectl--tramp-shell "/bin/sh"
"Shell to run when creating a TRAMP session to a pod"
:type 'string
:group 'kubectl)
(defcustom kubectl--known-namespaces nil
"Known namespaces for contexts where listing namespaces is disallowed. Some kubernetes configurations
don't allow namespaces to be listed. By creating an entry here, with the key being the name of the context,
and the value being a list of namespaces, those namespaces will be offered as selection (instead of asking
kubectl, which would fail)"
:group 'kubectl
:type '(alist :key-type string :value-type (repeat string)))
(defvar kubectl--kubectl "/usr/bin/kubectl" "Path to kubectl to use")
(defun kubectl--run (args)
"Builds a kubectl commandline that ends with [args], run it,
and return the resulting output as a string"
(shell-command-to-string (let ((ns (if (string= "" kubectl--namespace) "" (format "--namespace %s" kubectl--namespace) ))
(ctx (if (string= "" kubectl--context) "" (format "--context %s" kubectl--context) )))
(message "%s %s %s %s" kubectl--kubectl ns ctx args)
(format "%s %s %s %s" kubectl--kubectl ns ctx args))))
(defun kubectl--list-args (args)
"Builds a list of kubectl commandline arguments that ends with [args]"
(append
(if (string= "" kubectl--namespace) nil (list "--namespace" kubectl--namespace))
(if (string= "" kubectl--context) nil (list "--context" kubectl--context))
args))
(defun kubectl-tramp--running-pods-of (context namespace)
"Collect kubernetes running pods in given context and namespace.
Return a list of pod names"
(let* ((kubectl--context context)
(kubectl--namespace namespace))
(split-string (kubectl--run "get pods --no-headers=true | awk '{print $1}'") "\n")))
(defun kubectl--tramp--parse-running-pods-of (&optional contextAndNamespace)
"Return a list of (user host) tuples. contextAndNamespace is
expected to be a string with the k8s context, a pipe (|), and
then the namespace."
(let* ((split (split-string contextAndNamespace "|"))
(context (nth 0 split))
(namespace (nth 1 split)))
(cl-loop for name in (kubectl-tramp--running-pods-of context namespace)
collect (list "" name))))
(defun kubectl--tramp-method ()
"Returns the name of the TRAMP method to reach kubernetes pods in the current context and namespace."
;; TRAMP doesn't like "-" or "_" in its method names.
(format "kubectl%s%s" kubectl--context kubectl--namespace))
(defun kubectl--tramp-register-method ()
"Defines a new TRAMP method that will use kubectl with the current context and namespace."
(eval-after-load 'tramp
'(progn
(let* ((context kubectl--context)
(namespace kubectl--namespace)
(method (kubectl--tramp-method))
(contextAndNamespace (format "%s|%s" kubectl--context kubectl--namespace)))
(message "Adding tramp method %s" method)
(add-to-list 'tramp-methods
`(,method
(tramp-login-program ,kubectl--kubectl)
(tramp-login-args (nil ("--context" ,context) ("--namespace" ,namespace) ("exec" "-it") ("-u" "%u") ("%h") ("bash")))
(tramp-remote-shell ,kubectl--tramp-shell)
(tramp-remote-shell-args ("-i" "-c"))))
(setq completions `((kubernetes-tramp--parse-running-containers-of ,contextAndNamespace)))
(message " completions: %s" completions)
;;can't do the below, since it will check if files actually exist before adding to the list...
;;(tramp-set-completion-function method completions)
(add-to-list 'tramp-completion-function-alist (cons method completions))
))))
(defun kubectl--context-names ()
"Invokes kubectl to get a list of contexts"
(split-string (shell-command-to-string (format "%s config get-contexts --no-headers=true -o name" kubectl--kubectl)) "\n"))
(defun kubectl-choose-context (context)
"Select a new context interactively"
(interactive (list (completing-read "Context: " (kubectl--context-names) nil t)))
(setq kubectl--context context)
(call-interactively 'kubectl-choose-namespace))
(defun kubectl--namespace-names ()
"Invokes kubectl to get a list of namespaces"
(message kubectl--context)
(let* (
(known (cdr (assoc kubectl--context kubectl--known-namespaces))))
(if (null known)
;; namespaces aren't configured in kubectl--known-namespaces, so ask kubectl
(let ((kubectl--namespace ""))
(split-string (kubectl--run "get namespaces --no-headers=true | awk '{print $1}'") "\n"))
;; else
known)))
(defun kubectl-choose-namespace (namespace)
"Select a new namespace interactively"
(interactive (list (completing-read "Namespace: " (kubectl--namespace-names) nil t)))
(setq kubectl--namespace namespace)
(call-interactively (cdr (car (minor-mode-key-binding "g")))))
(defun kubectl--show-yaml (bufname args)
"Runs kubectl with [args] and shows the resulting yaml, in a buffer called [bufname]"
(let* (
(all_args (append args (list "-o" "yaml"))))
(when (get-buffer bufname)
(kill-buffer bufname))
(apply #'call-process kubectl--kubectl nil bufname nil (kubectl--list-args all_args))
(switch-to-buffer bufname)
(yaml-mode)
(read-only-mode)
(goto-char 1)))
(defun kubectl--refresh (name args columns)
"Refreshes the current view according to [args] as kubectl
command line, and [columns] as tabulated list columns. The
buffer is renamed to include [name]."
(let ((bufname (if (and (string= "" kubectl--namespace) (string= "" kubectl--context))
(format "*k8s %s*" name)
(format "*k8s %s %s/%s*" name kubectl--context kubectl--namespace))))
(unless (string= bufname (buffer-name))
(when (get-buffer bufname)
(kill-buffer bufname))
(rename-buffer bufname)))
(let ((rows (mapcar (lambda (line)
(let ((items (split-string line)))
(list (car items) (vconcat items))))
(seq-filter (lambda (line) (not (string= "" line)))
(split-string (kubectl--run args) "\n"))))
(oldpos (point)))
(setq tabulated-list-format columns)
(setq tabulated-list-entries rows)
(tabulated-list-init-header)
(tabulated-list-print)
(goto-char oldpos)))
(define-transient-command kubectl--pods-log ()
"Show console log"
["Arguments"
("-f" "Follow" "-f")
("-p" "Previous" "-p")
("-n" "Tail" "--tail=")
]
["Actions"
("l" "Log" kubectl--pods-get-log)])
(defun kubectl--log-kill-process ()
"Kills the process associated with this buffer"
(interactive)
(delete-process (buffer-name)))
(define-minor-mode kubectl-log-mode
"Minor mode to view log files (potentially while following them)"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "k") 'kubectl--log-kill-process)
(define-key map (kbd "q") 'kill-this-buffer)
map))
;;
;; =============== Pods
;;
(defun kubectl--pods-get-log (&optional args)
"Loads the logs of the selected kubernetes pod into a new buffer, passing [args] to the kubectl command"
(interactive (list (transient-args 'kubectl--pods-log)))
(let* ((podname (tabulated-list-get-id))
(bufname (format "*k8s logs:%s*" podname))
(process (format "*kubectl logs:%s" podname)))
(when (get-buffer bufname)
(kill-buffer bufname))
(apply #'start-process process bufname kubectl--kubectl "logs" podname (kubectl--list-args args))
(switch-to-buffer bufname)
(read-only-mode)
(kubectl-log-mode)))
(defun kubectl--pods-term ()
"Opens up a term for the currently selected pod"
(interactive)
(let* ((podname (tabulated-list-get-id))
(termbuf (apply 'make-term
(format "*k8s term:%s*" podname)
kubectl--kubectl
nil
(kubectl--list-args (list "exec" "-ti" podname "--" kubectl--shell)))))
(set-buffer termbuf)
(term-mode)
(term-char-mode)
(switch-to-buffer termbuf)))
(defun kubectl--pods-run (command)
"Runs [command] on the given pod, outputting its results asynchronously to a new buffer."
(interactive "M")
(let* ((podname (tabulated-list-get-id))
(bufname (format "*k8s exec %s:%s" podname command))
(process (format "*kubectl exec %s:%s" podname command))
(args (append (list "exec" podname) (split-string-and-unquote command))))
(when (get-buffer bufname)
(kill-buffer bufname))
(apply #'start-process process bufname kubectl--kubectl (kubectl--list-args args))
(switch-to-buffer bufname)
(read-only-mode)))
(defun kubectl--pods-run-custom (&optional args)
(interactive)
(call-interactively 'kubectl--pods-run))
(defun kubectl--pods-run-1 ()
(interactive)
(kubectl--pods-run "/usr/bin/jstack 1")) ;; we could do (thread-dump-start) after the process completes
(define-transient-command kubectl-pods-run ()
"Execute a command"
[]
["Actions"
("1" "Run jstack" kubectl--pods-run-1)
("r" "Run custom command" kubectl--pods-run-custom)])
(defun kubectl-pods-refresh ()
"Refreshes the current kubernetes pods view"
(interactive)
(let ((sel (if (string= "" kubectl--pods-selector) "" (format "--selector %s" kubectl--pods-selector) )))
(kubectl--refresh (format "pods:%s" kubectl--pods-selector)
(format "get pods --no-headers=true %s" sel)
[("Pod" 66) ("Ready" 10) ("Status" 24) ("Restarts" 11) ("Age" 10)])))
(defun kubectl--pods-inspect ()
"Shows detail about the currently selected pod"
(interactive)
(kubectl--show-yaml
(format "*k8s pod:%s" (tabulated-list-get-id))
(list "get" "pod" (tabulated-list-get-id))))
(defun kubectl--pods-dired ()
"Opens dired in the currently selected pod over TRAMP"
(interactive)
(kubectl--tramp-register-method)
(find-file (format "/%s:%s:" (kubectl--tramp-method) (tabulated-list-get-id))))
(define-minor-mode kubectl-pods-mode
"A minor mode with a keymap for the kubernetes pod list"
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "c") 'kubectl-choose-context)
(define-key map (kbd "s") 'kubectl-choose-namespace)
(define-key map (kbd "g") 'kubectl-pods-refresh)
(define-key map (kbd "l") 'kubectl--pods-log)
(define-key map (kbd "t") 'kubectl--pods-term)
(define-key map (kbd "r") 'kubectl-pods-run)
(define-key map (kbd "i") 'kubectl--pods-inspect)
(define-key map (kbd "d") 'kubectl--pods-dired)
map))
;;
;; =============== Deployments
;;
(defun kubectl--deployments-refresh ()
"Refreshes the current kubernetes deployments view"
(interactive)
(kubectl--refresh "deployments"
"get deployments --no-headers=true"
[("Deployment" 66) ("Desired" 10) ("Current" 10) ("Up-to-date" 10) ("Age" 10)]))
(defun kubectl--deployments-open ()
"Opens the deployment currently selected"
(interactive)
(kubectl-open-deployment (tabulated-list-get-id)))
(defun kubectl--deployments-inspect ()
"Shows detail about the currently selected deployment"
(interactive)
(kubectl--show-yaml
(format "*k8s deployment:%s" (tabulated-list-get-id))
(list "get" "deployment" (tabulated-list-get-id))))
(define-minor-mode kubectl-deployments-mode
"A minor mode with a keymap for the kubernetes deployments list"
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "c") 'kubectl-choose-context)
(define-key map (kbd "s") 'kubectl-choose-namespace)
(define-key map (kbd "g") 'kubectl--deployments-refresh)
(define-key map (kbd "o") 'kubectl--deployments-open)
(define-key map (kbd "RET") 'kubectl--deployments-open)
(define-key map (kbd "i") 'kubectl--deployments-inspect)
map))
(defun kubectl-deployments ()
"Select a context and namespace, and show its deployments"
(interactive)
(switch-to-buffer "*kubernetes*")
(tabulated-list-mode)
(kubectl-deployments-mode)
(call-interactively 'kubectl-choose-context))
(defun kubectl-open-deployment (name)
(let* ((selflink (kubectl--run (format "get deployment.apps %s -o jsonpath={.metadata.selfLink}" name)))
(json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
(scale (json-read-from-string (kubectl--run (format "get --raw %s/scale" selflink))))
(selector (gethash "selector" (gethash "status" scale)))
(ns kubectl--namespace)
(ctx kubectl--context))
(switch-to-buffer "*kubernetes*")
(tabulated-list-mode)
(kubectl-pods-mode)
(setq kubectl--pods-selector selector)
(setq kubectl--namespace ns)
(setq kubectl--context ctx)
(kubectl-pods-refresh)))
;;
;; ===================== Stateful sets
;;
(defun kubectl-statefulsets ()
"Select a context and namespace, and show its statefulsets"
(interactive)
(switch-to-buffer "*kubernetes*")
(tabulated-list-mode)
(kubectl-statefulsets-mode)
(call-interactively 'kubectl-choose-context))
(define-minor-mode kubectl-statefulsets-mode
"A minor mode with a keymap for the kubernetes statefulset list"
:keymap
(let ((map (make-sparse-keymap)))
(define-key map (kbd "c") 'kubectl-choose-context)
(define-key map (kbd "s") 'kubectl-choose-namespace)
(define-key map (kbd "g") 'kubectl--statefulsets-refresh)
(define-key map (kbd "o") 'kubectl--statefulsets-open)
(define-key map (kbd "RET") 'kubectl--statefulsets-open)
(define-key map (kbd "i") 'kubectl--statefulsets-inspect)
map))
(defun kubectl--statefulsets-refresh ()
"Refreshes the current kubernetes statefulsets view"
(interactive)
(kubectl--refresh "statefulsets"
"get statefulsets --no-headers=true"
[("Statefulset" 66) ("Ready" 10) ("Age" 10)]))
(defun kubectl--statefulsets-inspect ()
"Shows detail about the currently selected statefulset"
(interactive)
(kubectl--show-yaml
(format "*k8s statefulset:%s" (tabulated-list-get-id))
(list "get" "statefulset" (tabulated-list-get-id))))
(defun kubectl--statefulsets-open ()
"Opens the deployment currently selected"
(interactive)
(kubectl-open-statefulset (tabulated-list-get-id)))
(defun kubectl-open-statefulset (name)
(let* ((selflink (kubectl--run (format "get statefulset.apps %s -o jsonpath={.metadata.selfLink}" name)))
(json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
(scale (json-read-from-string (kubectl--run (format "get --raw %s/scale" selflink))))
(selector (gethash "selector" (gethash "status" scale)))
(ns kubectl--namespace)
(ctx kubectl--context))
(switch-to-buffer "*kubernetes*")
(tabulated-list-mode)
(kubectl-pods-mode)
(setq kubectl--pods-selector selector)
(setq kubectl--namespace ns)
(setq kubectl--context ctx)
(kubectl-pods-refresh)))
(provide 'kubectl)