emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature to allow selection of container when connecting to kubernete


From: Aaron Gonzales
Subject: Re: Feature to allow selection of container when connecting to kubernetes pod
Date: Fri, 30 Dec 2022 12:58:50 -0800

Including the patch since I sent it as an attachment before.

From ae1269815941c4319eaf69c3c3d2730ef4e3d8dc Mon Sep 17 00:00:00 2001
From: Aaron Gonzales <aaronzinho@ucla.edu>
Date: Fri, 30 Dec 2022 04:11:40 -0800
Subject: [PATCH] Add ability to access specific kubernetes pod's container

* lisp/net/tramp-container.el (tramp-kubernetes--pod-containers)
(tramp-kubernetes--pod-container): New functions.

* lisp/net/tramp.el (tramp-expand-args): Call function or expand string
---
 doc/misc/tramp.texi         | 10 ++++++++++
 etc/NEWS                    |  6 ++++++
 lisp/net/tramp-container.el | 21 +++++++++++++++++++++
 lisp/net/tramp.el           |  4 +++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi
index 80874049fa..bb9cd488e6 100644
--- a/doc/misc/tramp.texi
+++ b/doc/misc/tramp.texi
@@ -927,6 +927,16 @@ Inline methods
 
 This method does not support user names.
 
+@item @option{kubernetes}
+@cindex method @option{kubernetes}
+@cindex @option{kubernetes} method
+
+Integration for selecting a container in a Kubernetes pods.  The
+containers are returned by
+@samp{"kubectl get po pod -o jsonpath='{.spec.containers[*].name}'"}.
+If there is more than one pod and enable-recursive-minibuffers is
+enabled then the user is prompted to select a container.
+
 @end table
 
 
diff --git a/etc/NEWS b/etc/NEWS
index 50937f5e96..cf4c96958e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -117,6 +117,12 @@ point is not in a comment or a string.  It is by default bound to
 
 * Lisp Changes in Emacs 30.1
 
+** Tramp
+
+---
+*** Allow user to select kubernetes container when connecting to pod.
+Allow selection of a container when connecting to kubernetes pod via Tramp.
+
 ** New or changed byte-compilation warnings
 
 ---
diff --git a/lisp/net/tramp-container.el b/lisp/net/tramp-container.el
index 7b94253226..f4eca27a1d 100644
--- a/lisp/net/tramp-container.el
+++ b/lisp/net/tramp-container.el
@@ -116,6 +116,25 @@ tramp-docker--completion-function
                      lines)))
     (mapcar (lambda (m) (list nil m)) (delq nil names))))
 
+;;;###tramp-autoload
+(defun tramp-kubernetes--pod-containers (pod)
+  "Return list of containers in Kubernetes Pod POD."
+  (split-string (shell-command-to-string
+ (concat tramp-kubernetes-program
+                         " get po "
+                         pod
+                         " -o jsonpath='{.spec.containers[*].name}' "))))
+
+;;;###tramp-autoload
+(defun tramp-kubernetes--pod-container (pod)
+  "Get container in Kubernetes Pod POD.
+
+If `enable-recusrive-minibuffers' set to nil or a pod has a single container then return first container in list by default.  Otherwise allow the user to select from a list of containers"
+  (let ((containers (tramp-kubernetes--pod-containers pod)))
+    (cond ((not enable-recursive-minibuffers) (nth 0 containers))
+          ((length= containers 1) (nth 0 containers))
+          (t (completing-read "Select container: " containers  nil t)))))
+
 ;;;###tramp-autoload
 (defun tramp-kubernetes--completion-function (&rest _args)
   "List Kubernetes pods available for connection.
@@ -185,6 +204,8 @@ tramp-default-remote-shell
                 (tramp-login-args (("exec")
                                    ("%h")
                                    ("-it")
+                                   ("-c")
+                                   (lambda (vec) (tramp-kubernetes--pod-container (tramp-file-name-host vec)))
                                    ("--")
            ("%l")))
  (tramp-config-check tramp-kubernetes--current-context-data)
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index acbd50dc0f..92a5961221 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -4729,7 +4729,7 @@ tramp-compute-multi-hops
 (defun tramp-expand-args (vec parameter &rest spec-list)
   "Expand login arguments as given by PARAMETER in `tramp-methods'.
 PARAMETER is a symbol like `tramp-login-args', denoting a list of
-list of strings from `tramp-methods', containing %-sequences for
+list of strings or lambdas that expect `tramp-file-name' type parameter from `tramp-methods', containing %-sequences for
 substitution.  SPEC-LIST is a list of char/value pairs used for
 `format-spec-make'."
   (let ((args (tramp-get-method-parameter vec parameter))
@@ -4738,6 +4738,8 @@ tramp-expand-args
     (tramp-compat-flatten-tree
      (mapcar
       (lambda (x)
+        (cond ((functionp x) (setq x (list (funcall x vec))))
+              (t (setq x (mapcar (lambda (y) (format-spec y spec)) x))))
  (setq x (mapcar (lambda (y) (format-spec y spec)) x))
  (unless (member "" x) x))
       args))))
--
2.39.0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]