unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Aaron Gonzales <aarongonzales1@gmail.com>
To: emacs-devel@gnu.org
Subject: Re: Feature to allow selection of container when connecting to kubernetes pod
Date: Fri, 30 Dec 2022 12:58:50 -0800	[thread overview]
Message-ID: <CAB3ao9yF=rZp=yE4HOBj7Jm+vQW6=Vo2Vw_5xDmnc2XYf6VA0g@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4662 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 5595 bytes --]

             reply	other threads:[~2022-12-30 20:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-30 20:58 Aaron Gonzales [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-12-30 13:53 Feature to allow selection of container when connecting to kubernetes pod Aaron Gonzales
2022-12-31 10:22 ` Michael Albinus
2023-01-03  1:14   ` Filipp Gunbin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAB3ao9yF=rZp=yE4HOBj7Jm+vQW6=Vo2Vw_5xDmnc2XYf6VA0g@mail.gmail.com' \
    --to=aarongonzales1@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).