From: Eshel Yaron Date: Thu, 25 Jul 2024 18:54:56 +0000 (+0200) Subject: New commands 'kubed{-transient}-attach' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=a28d9abdf18685a28739b6b87832e67a94a45c86;p=emacs.git New commands 'kubed{-transient}-attach' --- diff --git a/lisp/net/kubed-transient.el b/lisp/net/kubed-transient.el index 2217e2e7c2f..010630942d8 100644 --- a/lisp/net/kubed-transient.el +++ b/lisp/net/kubed-transient.el @@ -45,8 +45,26 @@ ["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." diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index 415a834ed7e..12ce8a8de42 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -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)