]> git.eshelyaron.com Git - kubed.git/commitdiff
New commands for patching resources
authorEshel Yaron <me@eshelyaron.com>
Mon, 29 Jul 2024 14:50:59 +0000 (16:50 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 29 Jul 2024 14:53:05 +0000 (16:53 +0200)
* kubed-transient.el (kubed-transient-patch): New command.
(kubed-transient): Bind it.
(kubed-transient-apply): Fix capitalization.
* kubed.el (kubed-patch-history): New variable.
(kubed-read-patch): New function.
(kubed-patch): New command.
(kubed-prefix-map): Bind it.
(kubed-define-resource): Add 'kubed-RESOURCEs-patch'
commands that patch the resource at point/mouse in resource
list buffers.

kubed-transient.el
kubed.el

index 7154d1883183b2f21e0478967ed8024d3c049e4a..acfbdc7d3000b3e5f7a11a47941774c6a86d730c 100644 (file)
@@ -45,6 +45,7 @@
    ("a" "Attach"  kubed-transient-attach)
    ("d" "Diff"    kubed-transient-diff)
    ("e" "Exec"    kubed-transient-exec)
+   ("P" "Patch"   kubed-transient-patch)
    ("E" "Explain" kubed-explain)
    ("!" "Command line" kubed-kubectl-command)])
 
    ("-f" "Definition file" "--filename="
     :reader kubed-transient-read-resource-definition-file-name)]
   ["Actions"
-   ("*" "apply" kubed-apply)
+   ("*" "Apply" kubed-apply)
    ("!" "Command line" kubed-kubectl-command)]
   (interactive)
   (transient-setup 'kubed-transient-apply nil nil
                    :scope '("apply")))
 
+;;;###autoload
+(transient-define-prefix kubed-transient-patch ()
+  "Apply patch to Kubernetes resource."
+  ["Options"
+   ("-n" "Namespace" "--namespace="
+    :prompt "Namespace" :reader kubed-transient-read-namespace)
+   ("-t" "Patch type" "--type="
+    :prompt "Patch type: "
+    :choices ("strategic" "merge" "json"))]
+  ["Actions"
+   ("P" "Patch" kubed-patch)
+   ("!" "Command line" kubed-kubectl-command)]
+  (interactive)
+  (transient-setup 'kubed-transient-patch nil nil
+                   :scope '("patch")))
+
 ;;;###autoload
 (transient-define-prefix kubed-transient-create ()
   "Create Kubernetes resource."
index b60e3f5eb9c83c60c6225dbae9c6895cdfb92c75..2009e8bb3734db051e1ebf973fccbee10fc14a9a 100644 (file)
--- a/kubed.el
+++ b/kubed.el
@@ -232,17 +232,17 @@ Other keyword arguments that go between PROPERTIES and COMMANDS are:
 
     ;; Extend `commands' with standard commands.
     ;; Commands appear in reverse order in context menu.
-    (dolist (c `((display "C-o" "Display description of"
+    (dolist (c `((patch "P" "Patch"
+                        (kubed-patch ,(symbol-name plrl-var) ,resource
+                                     (kubed-read-patch)
+                                     . ,(when namespaced '(k8sns))))
+                 (display "C-o" "Display description of"
                           (display-buffer
-                           ,(if namespaced
-                                `(,desc-fun ,resource k8sns)
-                              `(,desc-fun ,resource))))
+                           (,desc-fun ,resource . ,(when namespaced '(k8sns)))))
                  (get-in-other-window
                   "o" "Pop to buffer showing description of"
                   (switch-to-buffer-other-window
-                   ,(if namespaced
-                        `(,desc-fun ,resource k8sns)
-                      `(,desc-fun ,resource))))
+                   (,desc-fun ,resource . ,(when namespaced '(k8sns)))))
                  (delete "D" "Delete"
                          ,(if namespaced
                               `(if k8sns
@@ -270,9 +270,7 @@ Other keyword arguments that go between PROPERTIES and COMMANDS are:
                           `(,edt-name ,resource)))
                  (get "RET" "Switch to buffer showing description of"
                       (switch-to-buffer
-                       ,(if namespaced
-                            `(,desc-fun ,resource k8sns)
-                          `(,desc-fun ,resource))))))
+                       (,desc-fun ,resource . ,(when namespaced '(k8sns)))))))
       (push c commands))
 
     ;; Generate code.
@@ -1859,6 +1857,45 @@ with \\[universal-argument] \\[universal-argument]; and TTY is t unless\
            (list "--" command)
            args))))
 
+(defvar kubed-patch-history nil
+  "Minibuffer history for `kubed-read-patch'.")
+
+(defun kubed-read-patch ()
+  "Prompt for a Kubernetes resource patch in JSON or YAML format."
+  (read-string "Patch (JSON or YAML): " nil 'kubed-patch-history))
+
+;;;###autoload
+(defun kubed-patch (type name patch &optional namespace strategy)
+  "Patch Kubernetes resource NAME of TYPE with patch PATCH.
+
+Optional argument NAMESPACE is the namespace in which to look for NAME.
+STRATEGY is the patch type to use, one of \"json\", \"merge\" and
+\"strategic\", defaulting to \"strategic\".
+
+Interactively, prompt for TYPE, NAME and PATCH."
+  (interactive
+   (let ((type (kubed-read-resource-type "Resource type to patch"))
+         (namespace nil) (strategy nil))
+     (dolist (arg (kubed-transient-args 'kubed-transient-apply))
+       (cond
+        ((string-match "--namespace=\\(.+\\)" arg)
+         (setq namespace (match-string 1 arg)))
+        ((string-match "--type=\\(.+\\)" arg)
+         (setq strategy (match-string 1 arg)))))
+     (list type
+           (kubed-read-resource-name type "Resource to patch")
+           (kubed-read-patch) namespace strategy)))
+  (message "Applying patch to `%s'..." name)
+  (unless (zerop
+           (apply #'call-process
+                  kubed-kubectl-program nil nil nil
+                  "patch" type name "-p" patch
+                  (append
+                   (when namespace (list "-n" namespace))
+                   (when strategy (list "--type" strategy)))))
+    (user-error "Patching `%s' failed" name))
+  (message "Applying patch to `%s'...  Done." name))
+
 (with-eval-after-load 'help-mode
   ;; Wait for `help-mode' to define `help-xref'.  It's always loaded by
   ;; the time we actually need it in `kubed-explain'.
@@ -2024,6 +2061,7 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments."
   "R" #'kubed-run
   "=" #'kubed-diff
   "E" #'kubed-explain
+  "P" #'kubed-patch
   "!" #'kubed-kubectl-command)
 
 (defvar reporter-prompt-for-summary-p)