]> git.eshelyaron.com Git - emacs.git/commitdiff
New commands 'kubed{-transient}-attach'
authorEshel Yaron <me@eshelyaron.com>
Thu, 25 Jul 2024 18:54:56 +0000 (20:54 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 25 Jul 2024 18:54:56 +0000 (20:54 +0200)
lisp/net/kubed-transient.el
lisp/net/kubed.el

index 2217e2e7c2f592cda899bf6cb3ee746c42e4a918..010630942d86a4e1a9c12e8328460a8003ab833f 100644 (file)
   ["Actions"
    ("c" "Create" kubed-transient-create)
    ("r" "Run" kubed-transient-run)
+   ("a" "Attach" kubed-transient-attach)
    ("!" "Command line" kubed-kubectl-command)])
 
+;;;###autoload
+(transient-define-prefix kubed-transient-attach ()
+  "Attach to running process in container in Kubernetes pod."
+  ["Switches"
+   ("-i" "Open stdin" "--stdin")
+   ("-t" "Allocate TTY" "--tty")]
+  ["Options"
+   ("-n" "Namespace" "--namespace="
+    :prompt "Namespace" :reader kubed-transient-read-namespace)]
+  ["Actions"
+   ("a" "Attach" kubed-attach)
+   ("!" "Command line" kubed-kubectl-command)]
+  (interactive)
+  (transient-setup 'kubed-transient-attach nil nil
+                   :value '("--stdin")
+                   :scope '("attach")))
+
 ;;;###autoload
 (transient-define-prefix kubed-transient-run ()
   "Run container image in a Kubernetes pod."
index 415a834ed7eb6d94c85d8e9b0b2e6770272a06d8..12ce8a8de422e6daeae7ae3d95932ec083d1164e 100644 (file)
@@ -1346,6 +1346,43 @@ Optional argument DEFAULT is the minibuffer default argument."
    (format-prompt prompt default) nil nil nil nil
    'kubed-container-images-history default))
 
+;;;###autoload
+(defun kubed-attach (pod &optional container namespace stdin tty)
+  "Attach to running process in CONTAINER in Kubernetes POD.
+
+Optional argument NAMESPACE is the namespace in which to look for POD.
+pod/foobar created
+Non-nil STDIN says to connect local standard input to remote process.
+Non-nil TTY says to use a TTY for standard input."
+  (interactive
+   (let ((namespace nil) (stdin nil) (tty nil))
+     (dolist (arg (when (and (fboundp 'transient-args)
+                             (fboundp 'kubed-transient-attach))
+                    (transient-args 'kubed-transient-attach)))
+       (cond
+        ((string-match "--namespace=\\(.+\\)" arg)
+         (setq namespace (match-string 1 arg)))
+        ((equal "--stdin" arg) (setq stdin t))
+        ((equal "--tty" arg) (setq tty t))))
+     (if (and kubed-all-namespaces-mode (not namespace))
+         (let* ((p-s (kubed-read-namespaced-pod "Attach to pod"))
+                (p (car p-s))
+                (s (cadr p-s)))
+           (list p (kubed-read-container p "Container" t s) s stdin tty))
+       ;; FIXME: When namespace is set from transient prefix, read pod
+       ;; name in that namespace instead.
+       (let* ((p (kubed-read-pod "Attach to pod"))
+              (c (kubed-read-container p "Container" t)))
+         (list p c namespace stdin tty)))))
+  (pop-to-buffer
+   (apply #'make-comint "kubed-attach" kubed-kubectl-program nil
+          "attach" pod
+          (append
+           (when namespace (list "-n" namespace))
+           (when container (list "-c" container))
+           (when stdin '("-i"))
+           (when tty   '("-t"))))))
+
 (defvar kubed-kubectl-command-history nil
   "Minibuffer history for `kubed-kubectl-command'.")
 
@@ -1390,6 +1427,7 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments."
   "A" #'kubed-all-namespaces-mode
   "+" #'kubed-create
   "R" #'kubed-run
+  "@" #'kubed-attach
   "!" #'kubed-kubectl-command)
 
 (provide 'kubed)