From: Eshel Yaron Date: Sat, 3 Aug 2024 15:05:27 +0000 (+0200) Subject: New transient menu for displaying resources X-Git-Tag: v0.3.0~8 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=df1cf34e37bcf4b00463463808ba869ccbd0da42;p=kubed.git New transient menu for displaying resources * kubed-transient.el (kubed-transient-display): New command. (kubed-transient): Bind it. * kubed.el (kubed-display-resource-short-description) (kubed-namespaced-p): New functions. (kubed-display-resource): New command, use them. (kubed-display-resource-handle-bookmark) (kubed-display-resource-make-bookmark): Simplify. (kubed-define-resource): Adjust generated 'kubed-display-RESOURCE' commands to honor namespace set in 'kubed-transient-display'. Change binding of these commands from suffix "g" to suffix "RET". (kubed-api-resources): Add optional arg ONLY-NAMESPACED. (kubed-prefix-map, kubed-menu-map): Bind 'kubed-define-resource'. --- diff --git a/kubed-transient.el b/kubed-transient.el index e15b201..1a5f226 100644 --- a/kubed-transient.el +++ b/kubed-transient.el @@ -40,16 +40,20 @@ "Perform Kubernetes operation." ["Kubernetes" ;; First column. - [("+" "Create" kubed-transient-create) - ("*" "Apply" kubed-transient-apply) - ("r" "Run" kubed-transient-run) - ("a" "Attach" kubed-transient-attach) - ("d" "Diff" kubed-transient-diff)] + [ :pad-keys t + ("RET" "Display" kubed-transient-display) + ("+" "Create" kubed-transient-create) + ("*" "Apply" kubed-transient-apply) + ("E" "Explain" kubed-explain)] ;; Second column. - [("X" "Exec" kubed-transient-exec) + [("r" "Run" kubed-transient-run) + ("a" "Attach" kubed-transient-attach) + ("X" "Exec" kubed-transient-exec) + ("R" "Rollout" kubed-transient-rollout)] + ;; Third column. + [("d" "Diff" kubed-transient-diff) ("P" "Patch" kubed-transient-patch) ("R" "Rollout" kubed-transient-rollout) - ("E" "Explain" kubed-explain) ("!" "Command line" kubed-kubectl-command)]]) ;;;###autoload @@ -201,6 +205,30 @@ (transient-setup 'kubed-transient-create nil nil :scope '("create"))) +;;;###autoload +(transient-define-prefix kubed-transient-display () + "Display Kubernetes resource." + ["Kubernetes Display\n" + ["Kinds" + ("p" "Pod" kubed-display-pod) + ("d" "Deployment" kubed-display-deployment) + ("j" "Job" kubed-display-job) + ("c" "CronJob" kubed-display-cronjob) + ("s" "Service" kubed-display-service)] + ["More" + :pad-keys t + ("S" "Secret" kubed-display-secret) + ("N" "Namespace" kubed-display-namespace) + ("i" "Ingress" kubed-display-ingress) + ("RET" "Any" kubed-display-resource) + ("!" "Command line" kubed-kubectl-command)] + ["Options" + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace)]] + (interactive) + (transient-setup 'kubed-transient-display nil nil + :scope '("get"))) + ;;;###autoload (transient-define-prefix kubed-transient-create-cronjob () "Create Kubernetes cronjob." diff --git a/kubed.el b/kubed.el index 55ecc23..1b10b5a 100644 --- a/kubed.el +++ b/kubed.el @@ -125,6 +125,43 @@ the namespace of the resource, or nil if TYPE is not namespaced.") (kubed-display-resource-mode) (current-buffer)))) +(defun kubed-display-resource-short-description + (type resource context namespace) + (concat type "/" resource + (when namespace (concat "@" namespace)) + (when context (concat "[" context "]")))) + +(defun kubed-namespaced-p (type) + "Return non-nil if TYPE is a namespaced Kubernetes resource type." + (member type (kubed-api-resources t))) + +;;;###autoload +(defun kubed-display-resource + (type resource context namespace) + "Display Kubernetes RESOURCE of type TYPE in BUFFER." + (interactive + (let* ((type (kubed-read-resource-type "Resource type to display")) + (namespace + (when (kubed-namespaced-p type) + (or (seq-some + (lambda (arg) + (when (string-match "--namespace=\\(.+\\)" arg) + (match-string 1 arg))) + (kubed-transient-args 'kubed-transient-display)) + (let ((cur (kubed-current-namespace))) + (if current-prefix-arg + (kubed-read-namespace "Namespace" cur) + cur)))))) + (list type (kubed-read-resource-name type "Display" nil namespace) + (kubed-current-context) namespace))) + (display-buffer + (kubed-display-resource-in-buffer + (concat "*Kubed " + (kubed-display-resource-short-description + type resource context namespace) + "*") + type resource context namespace))) + (declare-function bookmark-prop-get "bookmark") (declare-function bookmark-get-front-context-string "bookmark") (declare-function bookmark-get-rear-context-string "bookmark") @@ -138,9 +175,9 @@ the namespace of the resource, or nil if TYPE is not namespaced.") (bookmark-prop-get bookmark 'resource) (set-buffer (kubed-display-resource-in-buffer - (concat "*Kubed " type "/" name - (when namespace (concat "@" namespace)) - (when context (concat "[" context "]")) + (concat "*Kubed " + (kubed-display-resource-short-description + type name context namespace) "*") type name context namespace)) (when-let ((str (bookmark-get-front-context-string bookmark)) @@ -157,9 +194,7 @@ the namespace of the resource, or nil if TYPE is not namespaced.") (require 'bookmark) (seq-let (type name context namespace) kubed-display-resource-info (cons - (concat type "/" name - (when namespace (concat "@" namespace)) - (when context (concat "[" context "]"))) + (kubed-display-resource-short-description type name context namespace) (append (list (cons 'handler #'kubed-display-resource-handle-bookmark) @@ -974,11 +1009,18 @@ Optional argument DEFAULT is the minibuffer default argument." resource) (defun ,dsp-name (,resource . ,(when namespaced '(&optional k8sns))) ,(format "Display Kubernetes %S %s." resource (upcase (symbol-name resource))) - (interactive ,(if namespaced - `(if kubed-all-namespaces-mode - (,read-nms "Display") - (list (,read-fun "Display"))) - `(list (,read-fun "Display")))) + (interactive + ,(if namespaced + `(if kubed-all-namespaces-mode + (,read-nms "Display") + (list (,read-fun "Display" nil nil + ;; Consult transient for --namespace. + (seq-some + (lambda (arg) + (when (string-match "--namespace=\\(.+\\)" arg) + (match-string 1 arg))) + (kubed-transient-args 'kubed-transient-display))))) + `(list (,read-fun "Display")))) (display-buffer (,desc-fun ,resource . ,(when namespaced '(k8sns))))) (add-hook 'kubed-update-hook #',updt-cmd) @@ -1333,7 +1375,7 @@ Optional argument DEFAULT is the minibuffer default argument." resource) "+" #',crt-name "e" #',edt-name "d" #',dlt-name - "g" #',dsp-name + "RET" #',dsp-name "u" #',updt-cmd "E" #',expl-cmd ,@(mapcan @@ -2512,14 +2554,19 @@ Interactively, prompt for TYPE, NAME and PATCH." (defvar kubed-resource-field-history nil "Minibuffer history for `kubed-read-resource-field'.") -(defun kubed-api-resources () - "Return list of resource types in the current Kubernetes context." +(defun kubed-api-resources (&optional only-namespaced) + "Return list of resource types in the current Kubernetes context. + +Non-nil optional argument ONLY-NAMESPACED says to return only namespaced +resource types." (mapcar (lambda (line) (car (split-string line))) - (process-lines - kubed-kubectl-program - "api-resources" "--no-headers"))) + (apply #'process-lines + kubed-kubectl-program + "api-resources" "--no-headers" + (when only-namespaced + '("--namespaced=true"))))) (defun kubed-resource-names (type &optional namespace) "Return list of Kuberenetes resource names of type TYPE in NAMESPACE." @@ -2675,6 +2722,7 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments." "=" #'kubed-diff "E" #'kubed-explain "P" #'kubed-patch + "RET" #'kubed-display-resource "!" #'kubed-kubectl-command) (defvar-keymap kubed-menu-map @@ -2701,7 +2749,8 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments." "" '("Set Current Context" . kubed-use-context) "" '("Run Image" . kubed-run) "" '("Apply Config" . kubed-apply) - "" '("Create Resource" . kubed-create)) + "" '("Create Resource" . kubed-create) + "" '("Display Resource" . kubed-display-resource)) ;;;###autoload (define-minor-mode kubed-menu-bar-mode