From d1c486c7a0885b59c9280a19339a822fa23a5576 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Wed, 14 Aug 2024 15:21:41 +0200 Subject: [PATCH] Teach deletion commands about buffer-local contexts and namespaces * kubed.el (kubed-delete-resources): Refine interactive spec. (kubed-define-resource): Refine interactive spec for generated 'kubed-delete-RESOURCES' commands. * kubed-transient.el (kubed-transient-delete): New transient. (kubed-transient): Bind it. --- kubed-transient.el | 31 ++++++++++++- kubed.el | 108 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 112 insertions(+), 27 deletions(-) diff --git a/kubed-transient.el b/kubed-transient.el index e70d4c1..1641050 100644 --- a/kubed-transient.el +++ b/kubed-transient.el @@ -51,7 +51,7 @@ ("RET" "Display" kubed-transient-display) ("+" "Create" kubed-transient-create) ("*" "Apply" kubed-transient-apply) - ("E" "Explain" kubed-explain)] + ("D" "Delete" kubed-transient-delete)] ;; Second column. [("r" "Run" kubed-transient-run) ("a" "Attach" kubed-transient-attach) @@ -61,6 +61,7 @@ [("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 (autoload 'kubed-transient-rollout "kubed-transient" nil t) @@ -227,7 +228,7 @@ ("S" "Secret" kubed-display-secret) ("N" "Namespace" kubed-display-namespace) ("i" "Ingress" kubed-display-ingress) - ("RET" "Any" kubed-display-resource) + ("RET" "Any type" kubed-display-resource) ("!" "Command line" kubed-kubectl-command)] ["Options" ("-n" "Namespace" "--namespace=" @@ -238,6 +239,32 @@ (transient-setup 'kubed-transient-display nil nil :scope '("get"))) +;;;###autoload (autoload 'kubed-transient-delete "kubed-transient" nil t) +(transient-define-prefix kubed-transient-delete () + "Delete Kubernetes resource." + ["Kubernetes Delete\n" + ["Kinds" + ("p" "Pod" kubed-delete-pods) + ("d" "Deployment" kubed-delete-deployments) + ("j" "Job" kubed-delete-jobs) + ("c" "CronJob" kubed-delete-cronjobs) + ("s" "Service" kubed-delete-services)] + ["More" + :pad-keys t + ("S" "Secret" kubed-delete-secrets) + ("N" "Namespace" kubed-delete-namespaces) + ("i" "Ingress" kubed-delete-ingresses) + ("D" "Any type" kubed-delete-resources) + ("!" "Command line" kubed-kubectl-command)] + ["Options" + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace) + ("-C" "Context" "--context=" + :prompt "Context" :reader kubed-transient-read-context)]] + (interactive) + (transient-setup 'kubed-transient-delete nil nil + :scope '("delete"))) + ;;;###autoload (autoload 'kubed-transient-create-cronjob "kubed-transient" nil t) (transient-define-prefix kubed-transient-create-cronjob () "Create Kubernetes cronjob." diff --git a/kubed.el b/kubed.el index 8fad0ec..8515ce3 100644 --- a/kubed.el +++ b/kubed.el @@ -1005,22 +1005,41 @@ mode as their parent." (add-hook 'context-menu-functions #'kubed-list-context-menu nil t)) (defun kubed-delete-resources (type resources context &optional namespace) - "Delete Kubernetes RESOURCES of type TYPE." + "Delete Kubernetes RESOURCES of type TYPE in CONTEXT. + +For namespaced resource types, NAMESPACE is the namespace of RESOURCE. + +Interactively, use the current context and namespace by default, and +prompt for TYPE and RESOURCES. With a prefix argument \ +\\[universal-argument], +prompt for NAMESPACE. With a double prefix argument \ +\\[universal-argument] \\[universal-argument], +prompt for CONTEXT as well." (interactive - (let* ((type (kubed-read-resource-type "Resource type to delete")) - (context (kubed-current-context)) - (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-delete)) - (let ((cur (kubed-current-namespace))) - (if current-prefix-arg - (kubed-read-namespace "Namespace" cur) - cur)))))) - (list type (kubed-read-resource-name type "Delete" nil t nil namespace) + (let ((type nil) (context nil) (namespace nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-delete)) + (cond + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if (equal current-prefix-arg '(16)) + (kubed-read-context "Context" cxt) + cxt)))) + (unless type + (setq type (kubed-read-resource-type "Type of resource to delete" + nil context))) + (when (and (kubed-namespaced-p type context) (null namespace)) + (setq namespace + (let ((cur (kubed-local-namespace context))) + (if current-prefix-arg + (kubed-read-namespace "Namespace" cur nil context) + cur)))) + (list type (kubed-read-resource-name type "Delete" nil t + context namespace) context namespace))) (unless resources (user-error "You didn't specify %s to delete" type)) (message (format "Deleting Kubernetes %s `%s'..." @@ -1263,18 +1282,57 @@ Interactively, use the current context. With a prefix argument (defun ,dlt-name (,plrl-var &optional context . ,(when namespaced '(namespace))) - ,(format "Delete Kubernetes %S %s." plrl-var - (upcase (symbol-name plrl-var))) - (interactive ,(if namespaced - `(let ((namespace (and current-prefix-arg - (kubed-read-namespace - "Namespace" (kubed-current-namespace))))) - (list (,read-fun "Delete" nil t nil namespace) nil namespace)) - `(list (,read-fun "Delete" nil t)))) + ,(if namespaced + (format "Delete Kubernetes %S %s in CONTEXT and NAMESPACE. + +Interactively, use the current context and namespace by default. With a +prefix argument \\[universal-argument], prompt for NAMESPACE. With a +double prefix argument \\[universal-argument] \\[universal-argument], \ +prompt for CONTEXT as well." plrl-var (upcase (symbol-name plrl-var))) + (format "Delete Kubernetes %S %s in context CONTEXT. + +Interactively, use the current context. With a prefix argument +\\[universal-argument], prompt for CONTEXT." plrl-var (upcase (symbol-name plrl-var)))) + (interactive + ,(if namespaced + `(let ((context nil) (namespace nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-delete)) + (cond + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if (equal current-prefix-arg '(16)) + (kubed-read-context "Context" cxt) + cxt)))) + (unless namespace + (setq namespace + (let ((cur (kubed-local-namespace context))) + (if current-prefix-arg + (kubed-read-namespace "Namespace" cur nil context) + cur)))) + (list (,read-fun "Delete" nil t context namespace) context namespace)) + `(let ((context nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-delete)) + (cond + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if current-prefix-arg + (kubed-read-context "Context" cxt) + cxt)))) + (list (,read-fun "Delete" nil t context) context)))) (unless ,plrl-var (user-error ,(format "You didn't specify %S to delete" plrl-var))) - (kubed-delete-resources ,(symbol-name plrl-var) ,plrl-var context - . ,(when namespaced '(namespace)))) + (let ((context (or context (kubed-local-context)))) + (kubed-delete-resources + ,(symbol-name plrl-var) ,plrl-var context + . ,(when namespaced '((or namespace (kubed-local-namespace context))))))) ,(if crt-spec `(defun ,crt-name . ,crt-spec) `(defun ,crt-name (definition) -- 2.39.2