forked from kubernetes-el/kubernetes-el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes-deployments.el
195 lines (167 loc) · 8.66 KB
/
kubernetes-deployments.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
;;; kubernetes-deployments.el --- Rendering for Kubernetes deployments. -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'dash)
(require 'kubernetes-kubectl)
(require 'kubernetes-modes)
(require 'kubernetes-props)
(require 'kubernetes-state)
(require 'kubernetes-utils)
(require 'kubernetes-vars)
(require 'kubernetes-yaml)
;;;; Components
(defconst kubernetes-deployments--column-heading
["%-45s %10s %10s %10s %6s" "Name Replicas UpToDate Available Age"])
(kubernetes-ast-define-component deployment-detail (deployment)
(-let [(&alist 'metadata (&alist 'namespace ns 'creationTimestamp time)
'spec (&alist 'selector (&alist 'matchLabels
(&alist 'name selector-name
'component component-name)
'matchExpressions match-expressions)))
deployment]
`(,(when selector-name
`(section (selector nil)
(nav-prop (:selector ,selector-name)
(key-value 12 "Selector" ,(propertize selector-name 'face 'kubernetes-selector)))))
,(when component-name
`(section (component nil)
(nav-prop (:component ,component-name)
(key-value 12 "Component" ,(propertize component-name 'face 'kubernetes-component)))))
,(when match-expressions
`(section (expressions nil)
(heading "Match Expressions")
(indent ,(kubernetes-yaml-render match-expressions))))
(section (namespace nil)
(nav-prop (:namespace-name ,ns)
(key-value 12 "Namespace" ,(propertize ns 'face 'kubernetes-namespace))))
(key-value 12 "Created" ,time))))
(kubernetes-ast-define-component deployment-line (state deployment)
(-let* ((current-time (kubernetes-state--get state 'current-time))
(pending-deletion (kubernetes-state--get state 'deployments-pending-deletion))
(marked-deployments (kubernetes-state--get state 'marked-deployments))
((&alist 'metadata (&alist 'name name 'creationTimestamp created-time)
'spec (&alist 'replicas desired)
'status (&alist 'replicas current
'availableReplicas available
'updatedReplicas up-to-date))
deployment)
(current (or current 0))
(desired (or desired 0))
(available (or available 0))
(up-to-date (or up-to-date 0))
([fmt] kubernetes-deployments--column-heading)
(list-fmt (split-string fmt))
(line `(line ,(concat
;; Name
(format (pop list-fmt) (kubernetes-utils-ellipsize name 43))
" "
;; Replicas (current/desired)
(let ((next (pop list-fmt))
(str (format "%s/%s" current desired)))
(cond
((zerop desired)
(format next str))
((zerop current)
(propertize (format next str) 'face 'warning))
((/= current desired)
(format next str))
(t
(propertize (format next str) 'face 'kubernetes-dimmed))))
" "
;; Up-to-date
(let ((next (pop list-fmt)))
(cond
((zerop desired)
(format next up-to-date))
((zerop up-to-date)
(propertize (format next up-to-date) 'face 'warning))
(t
(propertize (format next up-to-date) 'face 'kubernetes-dimmed))))
" "
;; Available
(let ((next (pop list-fmt)))
(cond
((zerop desired)
(format next available))
((zerop available)
(propertize (format next available) 'face 'warning))
(t
(propertize (format next available) 'face 'kubernetes-dimmed))))
" "
;; Age
(let ((start (apply #'encode-time (kubernetes-utils-parse-utc-timestamp created-time))))
(propertize (format (pop list-fmt) (kubernetes-utils-time-diff-string start current-time))
'face 'kubernetes-dimmed))))))
`(nav-prop (:deployment-name ,name)
(copy-prop ,name
,(cond
((member name pending-deletion)
`(propertize (face kubernetes-pending-deletion) ,line))
((member name marked-deployments)
`(mark-for-delete ,line))
((zerop desired)
`(propertize (face kubernetes-dimmed) ,line))
(t
line))))))
(kubernetes-ast-define-component deployment (state deployment)
`(section (,(intern (kubernetes-state-resource-name deployment)) t)
(heading (deployment-line ,state ,deployment))
(section (details nil)
(indent
(deployment-detail ,deployment)
(padding)))))
(kubernetes-ast-define-component deployments-list (state &optional hidden)
(-let (((state-set-p &as &alist 'items deployments) (kubernetes-state--get state 'deployments))
([fmt labels] kubernetes-deployments--column-heading))
`(section (deployments-container ,hidden)
(header-with-count "Deployments" ,deployments)
(indent
(columnar-loading-container ,deployments
,(propertize
(apply #'format fmt (split-string labels))
'face
'magit-section-heading)
,(--map `(deployment ,state ,it) deployments)))
(padding))))
;; Requests and state management
(kubernetes-state-define-refreshers deployments)
(defun kubernetes-deployments-delete-marked (state)
(let ((names (kubernetes-state--get state 'marked-deployments)))
(dolist (name names)
(kubernetes-state-delete-deployment name)
(kubernetes-kubectl-delete "deployment" name kubernetes-props state
(lambda (_)
(message "Deleting deployment %s succeeded." name))
(lambda (_)
(message "Deleting deployment %s failed" name)
(kubernetes-state-mark-deployment name))))
(kubernetes-state-trigger-redraw)))
;;;; Displaying deployments
(defun kubernetes-deployments--read-name (state)
"Read a deployment name from the user.
STATE is the current application state.
Update the deployment state if it not set yet."
(-let* (((&alist 'items deployments)
(or (kubernetes-state--get state 'deployments)
(progn
(message "Getting deployments...")
(let ((response (kubernetes-kubectl-await-on-async kubernetes-props state (-partial #'kubernetes-kubectl-get "deployments"))))
(kubernetes-state-update-deployments response)
response))))
(deployments (append deployments nil))
(names (-map #'kubernetes-state-resource-name deployments)))
(completing-read "Deployment: " names nil t)))
;;###autoload
(defun kubernetes-display-deployment (deployment-name state)
"Display information for a deployment in a new window.
STATE is the current application state.
DEPLOYMENT-NAME is the name of the deployment to display."
(interactive (let ((state (kubernetes-state)))
(list (kubernetes-deployments--read-name state) state)))
(if-let (deployment (kubernetes-state-lookup-deployment deployment-name state))
(select-window
(display-buffer
(kubernetes-yaml-make-buffer kubernetes-display-deployment-buffer-name deployment)))
(error "Unknown deployment: %s" deployment-name)))
(provide 'kubernetes-deployments)
;;; kubernetes-deployments.el ends here