-
Notifications
You must be signed in to change notification settings - Fork 4
/
timonier-mode.el
546 lines (444 loc) · 18.4 KB
/
timonier-mode.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
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
;;; timonier-mode.el --- Mode for timonier
;; Copyright (C) 2016 Nicolas Lamirault <[email protected]>
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;;; Code:
(require 'widget)
(require 'cl-lib)
(require 'all-the-icons)
(require 'f)
(require 'hydra)
(require 's)
(require 'timonier-custom)
(require 'timonier-io)
(require 'timonier-k8s)
;; ------------------
;; Customization
;; ------------------
(defgroup timonier-k8s-mode nil
"Customization group for `timonier-k8s-mode'."
:prefix "timonier-k8s-mode-"
:tag "Timonier Kubernetes Mode"
:group 'timonier)
(defcustom timonier-k8s-mode-buffer "*timonier-k8s*"
"The Timonier kubernetes buffer name."
:type 'string
:group 'timonier-k8s-mode)
(defcustom timonier-k8s-description-mode-buffer "*timonier-k8s-desc*"
"The Timonier kubernetes entity description buffer name."
:type 'string
:group 'timonier-k8s-mode)
(defcustom timonier-k8s-keymap-prefix "C-c C-k"
"Prefix for `timonier-k8s-mode'."
:group 'timonier-k8s-mode)
(defcustom timonier-k8s-mode-line
" Timonier/Kubernetes "
"Mode line lighter for Timonier."
:group 'timonier-k8s-mode
:type 'sexp
:risky t)
;; ------------------
;; Faces
;; ------------------
(defgroup timonier-k8s-mode-faces '((timonier-k8s-mode custom-group))
"Customization group for the faces of `timonier-k8s-mode'."
:prefix "timonier-k8s-mode-"
:tag "Timonier Kubernetes mode faces"
:group 'timonier-k8s-mode)
(defface timonier-k8s-mode-title-face
'((t :inherit font-lock-builtin-face))
"Face used for buffer's title."
:group 'timonier-k8s-mode)
(defface timonier-k8s-mode-key-face
'((t :weight bold :inherit font-lock-keyword-face))
"Face used in the Timonier buffer."
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-pod-face
'((t :weight bold :inherit font-lock-warning-name-face))
"Face used in the Timonier buffer."
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-service-face
'((t :weight bold :inherit font-lock-warning-name-face))
"Face used in the Timonier buffer."
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-node-face
'((t :weight bold :inherit font-lock-warning-name-face))
"Face used in the Timonier buffer."
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-namespace-face
'((t :inherit font-lock-doc-face))
"Face used in the Timonier buffer"
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-pod-status-face
'((t :inherit font-lock-string-face))
"Face used in the Timonier buffer"
:group 'timonier-k8s-mode-faces)
(defface timonier-k8s-mode-namespace-status-face
'((t :inherit font-lock-string-face))
"Face used in the Timonier buffer"
:group 'timonier-k8s-mode-faces)
(defvar timonier-k8s-mode-padding 2
"The number of columns used for padding on the left side of the buffer.")
;; ------------------
;; Modes
;; ------------------
(defmacro timonier-k8s-mode-with-widget (title &rest body)
`(progn
(set-buffer (get-buffer-create timonier-k8s-mode-buffer))
(switch-to-buffer timonier-k8s-mode-buffer)
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer)
(remove-overlays)
(widget-insert (format "\n[%s]\n\n" ,title))
,@body)
(use-local-map widget-keymap)
(widget-setup)
(timonier-k8s-mode)
(widget-minor-mode)
(goto-char 0)))
(defmacro timonier-k8s-mode-description-with-widget (title &rest body)
`(progn
(set-buffer (get-buffer-create timonier-k8s-description-mode-buffer))
(switch-to-buffer timonier-k8s-description-mode-buffer)
(kill-all-local-variables)
(let ((inhibit-read-only t))
(erase-buffer)
(remove-overlays)
(widget-insert (format "\n[%s]\n\n" ,title))
,@body)
(use-local-map widget-keymap)
(widget-setup)
(timonier-k8s-description-mode)
(widget-minor-mode)
(goto-char 0)))
(defvar timonier-k8s-mode-hook nil)
(defun timonier-k8s-mode-quit ()
"Timonier exit."
(interactive)
(kill-buffer timonier-k8s-mode-buffer))
(defun timonier--k8s-mode-current-entity (type)
"Return the current Kubernetes entities at point using `TYPE'."
(get-text-property (point) type))
(defun timonier--k8s-mode-next-entity (type)
"Move point to the next Kubernetes entity specified by `TYPE'."
(interactive)
(let ((pos (next-single-property-change (point) type)))
(if pos
(progn
(goto-char pos)
(unless (timonier--k8s-mode-current-entity type)
(let ((pos (next-single-property-change pos type)))
(when pos
(goto-char pos)))))
(message "No current position: %s" type))))
(defun timonier--k8s-mode-prev-entity (type)
"Move point to the previous Kubernetes entity specified by `TYPE'."
(interactive)
(let ((pos (previous-single-property-change (point) type)))
(if pos
(progn
(goto-char pos)
(unless (timonier--k8s-mode-current-entity type)
(let ((pos (previous-single-property-change pos type)))
(when pos
(goto-char pos)))))
(message "No current position: %s" type))))
(defun timonier-k8s-mode-next-pod ()
"Move point to the next Kubernetes pod."
(interactive)
(timonier--k8s-mode-next-entity :k8s-pod))
(defun timonier-k8s-mode-prev-pod ()
"Move point to the previous Kubernetes pod."
(interactive)
(timonier--k8s-mode-prev-entity :k8s-pod))
(defun timonier-k8s-mode-next-service ()
"Move point to the next Kubernetes service."
(interactive)
(timonier--k8s-mode-next-entity :k8s-service))
(defun timonier-k8s-mode-prev-service ()
"Move point to the previous Kubernetes service."
(interactive)
(timonier--k8s-mode-prev-entity :k8s-service))
(defun timonier-k8s-mode-next-node ()
"Move point to the next Kubernetes node."
(interactive)
(timonier--k8s-mode-next-entity :k8s-node))
(defun timonier-k8s-mode-prev-node ()
"Move point to the previous Kubernetes node."
(interactive)
(timonier--k8s-mode-prev-entity :k8s-node))
(defun timonier-k8s-mode-describe-node ()
(interactive)
(let ((node (timonier--k8s-mode-current-entity :k8s-node)))
(if node
(progn
(let ((node-data (timonier--k8s-extract-node-description node)))
(timonier-k8s-mode-description-with-widget
(propertize "Kubernetes / Nodes"
'face 'timonier-k8s-mode-title-face)
(widget-insert
(format "\n%s:"
(propertize "Details"
'face 'timonier-k8s-mode-title-face))
(format "\n%s: %s"
(propertize "Name"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'name)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Labels"
'face 'timonier-k8s-mode-key-face)
(propertize
(s-join " " (plist-get node-data 'labels))))
(format "\n%s: %s"
(propertize "External ID"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'external-id)
'face 'timonier-k8s-mode-pod-face))
(format "\n\n%s:"
(propertize "System info"
'face 'timonier-k8s-mode-title-face))
(format "\n%s: %s"
(propertize "System UUID"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'system-uuid)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Boot ID"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'boot-id)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Kernel Version"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'kernel-version)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "OS Image"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'os-image)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Container Runtime Version"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'container-runtime)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Kubelet Version"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'kubelet-version)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Kube-Proxy Version"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'kubeproxy-version)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Operation system"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'os)
'face 'timonier-k8s-mode-pod-face))
(format "\n%s: %s"
(propertize "Architecture"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get node-data 'architecture)
'face 'timonier-k8s-mode-pod-face))
))))
(message "No node available."))))
(defun timonier-k8s-mode-describe-pod ()
(interactive)
(let ((pod (timonier--k8s-mode-current-entity :k8s-pod)))
(if pod
(message "Pod %s" pod)
(message "No pod available."))))
(defun timonier-k8s-mode-describe-service ()
(interactive)
(let ((service (timonier--k8s-mode-current-entity :k8s-service)))
(if service
(message "%s" service)
(message "No service available."))))
(defhydra timonier-k8s-hydra (:color blue :hint none)
"
[ Timonier / Kubernetes ]
^Node^ ^Service^ ^Pod^
--------------------------------------------------------------------------------------
_i_: go to next node _k_: go to next service _g_: go to next pod
_j_: go to previous node _l_: go to previous service _h_: go to previous pod
_N_: describe current node _S_: describe current service _P_: describe current pod
_q_: quit
"
("g" timonier-k8s-mode-next-pod)
("h" timonier-k8s-mode-prev-pod)
("P" timonier-k8s-mode-describe-pod)
("k" timonier-k8s-mode-next-service)
("l" timonier-k8s-mode-prev-service)
("S" timonier-k8s-mode-describe-service)
("i" timonier-k8s-mode-next-node)
("j" timonier-k8s-mode-prev-node)
("N" timonier-k8s-mode-describe-node)
("q" nil "quit" :hint t :color red))
(defvar timonier-k8s-mode-map
(let ((map (make-keymap)))
(define-key map (kbd timonier-k8s-keymap-prefix) 'timonier-k8s-hydra/body)
map))
(define-derived-mode timonier-k8s-mode tabulated-list-mode
"Timonier Kubernetes mode"
"Major mode for Timonier."
:group 'timonier
)
(defun timonier-k8s-description-mode-quit ()
"Quit the Kubernetes description mode."
(interactive)
(kill-buffer timonier-k8s-description-mode-buffer))
(defvar timonier-k8s-description-mode-map
(let ((map (make-keymap)))
(define-key map (kbd "q") 'timonier-k8s-description-mode-quit)
map))
(define-derived-mode timonier-k8s-description-mode tabulated-list-mode
"Timonier Kubernetes mode"
"Major mode for Timonier."
:group 'timonier
)
(defun timonier--k8s-mode-render-pod (pod)
"Render a Kubernetes `POD' to the Timonier buffer."
;; (message "Pod: %s" pod)
(let* ((pod-data (timonier--k8s-extract-pod-informations pod)))
(insert (all-the-icons-octicon "package"))
(let ((start (point)))
(widget-insert
(format " %s %s"
(propertize (plist-get pod-data 'name)
'face 'timonier-k8s-mode-pod-face)
(propertize (plist-get pod-data 'status)
'face 'timonier-k8s-mode-pod-status-face)))
(put-text-property start (point) :k8s-pod pod)
(widget-insert
(format "\n %s: %s\n\n"
(propertize "Namespace"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get pod-data 'namespace)
'face 'timonier-k8s-mode-namespace-face))))))
(defun timonier--k8s-mode-render-pods (pods)
"Render Kubernetes `PODS'."
(widget-insert (format "\n== PODS ==\n\n"))
(dotimes (i (length pods))
(let ((pod (elt pods i)))
(timonier--k8s-mode-render-pod pod)))
(widget-insert "\n"))
(defun timonier--k8s-mode-render-service (service)
"Render a Kubernetes `SERVICE' to the Timonier buffer."
(let* ((service-data (timonier--k8s-extract-service-informations service)))
(insert (all-the-icons-octicon "link-external"))
(let ((start (point)))
(widget-insert
(format " %s"
(propertize (plist-get service-data 'name)
'face 'timonier-k8s-mode-service-face)))
(put-text-property start (point) :k8s-service service)
(widget-insert
(format "\n %s: %s"
(propertize "Namespace"
'face 'timonier-k8s-mode-key-face)
(propertize (plist-get service-data 'namespace)
'face 'timonier-k8s-mode-namespace-face))
(format "\n %s: %s"
(propertize "ClusterIP"
'face 'timonier-k8s-mode-key-face)
(plist-get service-data 'cluster-ip))
(format "\n %s: %s"
(propertize "Endpoints"
'face 'timonier-k8s-mode-key-face)
(propertize
(s-join ":" (mapcar (lambda (elt)
(format "%s" elt))
(plist-get service-data 'ports)))))
(format "\n %s: %s\n\n"
(propertize "Labels"
'face 'timonier-k8s-mode-key-face)
(propertize
(s-join " " (plist-get service-data 'labels))))))))
(defun timonier--k8s-mode-render-services (services)
"Render Kubernetes `SERVICES'."
(widget-insert (format "\n== SERVICES ==\n\n"))
(dotimes (i (length services))
(let ((service (elt services i)))
(timonier--k8s-mode-render-service service)))
(widget-insert "\n"))
(defun timonier--k8s-mode-render-node (node)
"Render a Kubernetes `NODE' to the Timonier buffer."
(let* ((node-data (timonier--k8s-extract-node-informations node)))
(insert (all-the-icons-octicon "server"))
(let ((start (point)))
(widget-insert
(format " %s %s"
(propertize (plist-get node-data 'name)
'face 'timonier-k8s-mode-service-face)
(propertize (plist-get node-data 'creation)
'face 'timonier-k8s-mode-namespace-face)))
(put-text-property start (point) :k8s-node node)
(widget-insert
(format "\n %s: %s\n\n"
(propertize "Labels"
'face 'timonier-k8s-mode-key-face)
(propertize
(s-join " " (plist-get node-data 'labels))))))))
(defun timonier--k8s-mode-render-nodes (nodes)
"Render Kubernetes `NODES'."
(widget-insert (format "\n== NODES ==\n\n"))
(dotimes (i (length nodes))
(let ((node (elt nodes i)))
(timonier--k8s-mode-render-node node)))
(widget-insert "\n"))
(defun timonier--k8s-mode-render-namespace (namespace)
"Render a Kubernetes `NAMESPACE' to the Timonier buffer."
(let* ((namespace-data (timonier--k8s-extract-namespace-informations namespace)))
(insert (all-the-icons-octicon "organization"))
(let ((start (point)))
(widget-insert
(format " %s %s\n"
(propertize (plist-get namespace-data 'name)
'face 'timonier-k8s-mode-pod-face)
(propertize (plist-get namespace-data 'status)
'face 'timonier-k8s-mode-namespace-status-face)))
(put-text-property start (point) :k8s-namespace namespace))))
(defun timonier--k8s-mode-render-namespaces (namespaces)
"Render Kubernetes `NAMESPACES'."
(widget-insert (format "\n== NAMESPACES ==\n\n"))
(dotimes (i (length namespaces))
(let ((namespace (elt namespaces i)))
(timonier--k8s-mode-render-namespace namespace)))
(widget-insert "\n"))
;; ------------------
;; API
;; ------------------
(defvar timonier-k8s-mode-history nil)
;;;###autoload
(defun timonier-k8s ()
"Display informations about the Kubernetes cluster."
(interactive)
(timonier--with-k8s
(timonier-k8s-mode-with-widget
(propertize "Kubernetes"
'face 'timonier-k8s-mode-title-face)
(let ((namespaces (timonier--assoc-cdr 'items (timonier--k8s-get-namespaces)))
(nodes (timonier--assoc-cdr 'items (timonier--k8s-get-nodes)))
(services (timonier--assoc-cdr 'items (timonier--k8s-get-services)))
(pods (timonier--assoc-cdr 'items (timonier--k8s-get-pods))))
(timonier--k8s-mode-render-namespaces namespaces)
(timonier--k8s-mode-render-nodes nodes)
(timonier--k8s-mode-render-services services)
(timonier--k8s-mode-render-pods pods)
))))
(provide 'timonier-mode)
;;; timonier-mode.el ends here