From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Aaron Gonzales Newsgroups: gmane.emacs.devel Subject: Re: Feature to allow selection of container when connecting to kubernetes pod Date: Fri, 30 Dec 2022 12:58:50 -0800 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000043caf05f111dfad" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22506"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Dec 30 21:59:41 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBMTd-0005dA-Mj for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Dec 2022 21:59:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pBMT5-0004Fr-B7; Fri, 30 Dec 2022 15:59:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pBMT3-0004Fj-SX for emacs-devel@gnu.org; Fri, 30 Dec 2022 15:59:05 -0500 Original-Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pBMT1-0001Hm-Qc for emacs-devel@gnu.org; Fri, 30 Dec 2022 15:59:05 -0500 Original-Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-46d4840b51fso250594967b3.12 for ; Fri, 30 Dec 2022 12:59:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=NcEYZnCu0BYMrpz3hI8mdz1OjnJGryUzpGswD3Rd5oY=; b=aAoikqM9xv4Ri4nx7IO4WndR0Sb34EJ5jFo+7jGwivrohuRfzYkXlP30fiNjB2g+db fb8nvF5BYM49869iswMpPLy19GiKwql5PHvBW2S1yU+ZPFe4RrGrjdOTf+p0V4lbMQge HVmuJPyDtFRA2Tydi0QEROljvGUJYESXi7mhwJBan0rJIHokm+tQdqwo0/K32BBAqxWw pA2TYPbc790bGgz0pxAkUHnXaOJvLEj2MZ4jtOmwWx27VozldXxfnB6WBU7oKGeUxxdZ recDQE9dQ+kWx350DsGHKVd+2mJLqeKV6xKlsbBPETMwn5iP5VXctMnWuBypDk6aeezv hYEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NcEYZnCu0BYMrpz3hI8mdz1OjnJGryUzpGswD3Rd5oY=; b=kX+s5DMqqwjRmBffZco9FuVsPNpT7KPTTAnxRImBPxyEotKIAuLbOTYVd061bAqVOB 4khNUEVTjcplHeWSBem/pr6usIwqOozbR2K2YnJf93aGo24a1JSao2yvC3aN80cmUxCy OQb24GaZaxRrr43+MNZDtN1DvwQbEDSjQcZ75aX2Wrrddn7A2kyRZdVVFA93qJgXtuP5 Z3LKJ93pWnhjgO0gzzzVXFlbvJ3sGPZAXF948V3WcaTroGCi1S4K+P+rKqROtX0jUZJF vgrkvuaO4tC05oW54ekpZ7rJKO9J0TWCxi8roh1BpIrk/r1GptMX8++jmvDzrIn2YFJe 7/gA== X-Gm-Message-State: AFqh2kqedm//De74WrjWCQmhUMyiXOi938kq9T80CA+iTv5BWtfXx5RY 6mnGuTIB0g75ZlCCanJ5XHxdU8HGGVMiBtzJRJbAWr3avFQ= X-Google-Smtp-Source: AMrXdXs4Y57ULP0rug0Ex7EXWu3U9Q0QAJUHVgrU1I6aDqc8dowDTKLYITUhzjSnu3hFH6VjMUeW2YkQDIrAhsb51OE= X-Received: by 2002:a81:17ca:0:b0:46f:bd6:957d with SMTP id 193-20020a8117ca000000b0046f0bd6957dmr2127776ywx.383.1672433941886; Fri, 30 Dec 2022 12:59:01 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::1135; envelope-from=aarongonzales1@gmail.com; helo=mail-yw1-x1135.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:302136 Archived-At: --000000000000043caf05f111dfad Content-Type: text/plain; charset="UTF-8" Including the patch since I sent it as an attachment before. >From ae1269815941c4319eaf69c3c3d2730ef4e3d8dc Mon Sep 17 00:00:00 2001 From: Aaron Gonzales 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 --000000000000043caf05f111dfad Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Including the patch since I sent it as an attachment befor= e.

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

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

* lisp/= net/tramp.el (tramp-expand-args): Call function or expand string
---
= =C2=A0doc/misc/tramp.texi =C2=A0 =C2=A0 =C2=A0 =C2=A0 | 10 ++++++++++
= =C2=A0etc/NEWS =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0| =C2=A06 ++++++
=C2=A0lisp/net/tramp-container.el | 21 ++++++= +++++++++++++++
=C2=A0lisp/net/tramp.el =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 | =C2=A04 +++-
=C2=A04 files changed, 40 insertions(+), 1 deletion(-= )

diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi
index 80= 874049fa..bb9cd488e6 100644
--- a/doc/misc/tramp.texi
+++ b/doc/misc/= tramp.texi
@@ -927,6 +927,16 @@ Inline methods
=C2=A0
=C2=A0This m= ethod does not support user names.
=C2=A0
+@item @option{kubernetes}<= br>+@cindex method @option{kubernetes}
+@cindex @option{kubernetes} meth= od
+
+Integration for selecting a container in a Kubernetes pods.=C2= =A0 The
+containers are returned by
+@samp{"kubectl get po pod -= o jsonpath=3D'{.spec.containers[*].name}'"}.
+If there is m= ore than one pod and enable-recursive-minibuffers is
+enabled then the u= ser is prompted to select a container.
+
=C2=A0@end table
=C2=A0=C2=A0
diff --git a/etc/NEWS b/etc/NEWS
index 50937f5e96..cf4c96958= e 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -117,6 +117,12 @@ point = is not in a comment or a string.=C2=A0 It is by default bound to
=C2=A0= =0C
=C2=A0* Lisp Changes in Emacs 30.1
=C2=A0
+** Tramp
+
+-= --
+*** Allow user to select kubernetes container when connecting to pod= .
+Allow selection of a container when connecting to kubernetes pod via = Tramp.
+
=C2=A0** New or changed byte-compilation warnings
=C2=A0<= br>=C2=A0---
diff --git a/lisp/net/tramp-container.el b/lisp/net/tramp-c= ontainer.el
index 7b94253226..f4eca27a1d 100644
--- a/lisp/net/tramp-= container.el
+++ b/lisp/net/tramp-container.el
@@ -116,6 +116,25 @@ t= ramp-docker--completion-function
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 lines)))
=C2=A0 =C2=A0 =C2=A0(map= car (lambda (m) (list nil m)) (delq nil names))))
=C2=A0
+;;;###tramp= -autoload
+(defun tramp-kubernetes--pod-containers (pod)
+ =C2=A0&quo= t;Return list of containers in Kubernetes Pod POD."
+ =C2=A0(split-= string (shell-command-to-string
+ (concat tramp-kubernetes-program
= + =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 " get po "
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pod
+ =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 " -o j= sonpath=3D'{.spec.containers[*].name}' "))))
+
+;;;###tr= amp-autoload
+(defun tramp-kubernetes--pod-container (pod)
+ =C2=A0&q= uot;Get container in Kubernetes Pod POD.
+
+If `enable-recusrive-mini= buffers' set to nil or a pod has a single container then return first c= ontainer in list by default.=C2=A0 Otherwise allow the user to select from = a list of containers"
+ =C2=A0(let ((containers (tramp-kubernetes--= pod-containers pod)))
+ =C2=A0 =C2=A0(cond ((not enable-recursive-minibu= ffers) (nth 0 containers))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0((length= =3D containers 1) (nth 0 containers))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0(t (completing-read "Select container: " containers =C2=A0nil = t)))))
+
=C2=A0;;;###tramp-autoload
=C2=A0(defun tramp-kubernetes-= -completion-function (&rest _args)
=C2=A0 =C2=A0"List Kubernete= s pods available for connection.
@@ -185,6 +204,8 @@ tramp-default-remot= e-shell
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(t= ramp-login-args (("exec")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 ("%h")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 ("-it")
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 ("-c")
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (lambda (vec) (tramp-kubernetes--pod-container (tramp-file-name-host ve= c)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ("--"= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ("%l")))
=C2= =A0 (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
=C2=A0(defun tramp-expand-args (vec= parameter &rest spec-list)
=C2=A0 =C2=A0"Expand login argument= s as given by PARAMETER in `tramp-methods'.
=C2=A0PARAMETER is a sym= bol like `tramp-login-args', denoting a list of
-list of strings fro= m `tramp-methods', containing %-sequences for
+list of strings or la= mbdas that expect `tramp-file-name' type parameter from `tramp-methods&= #39;, containing %-sequences for
=C2=A0substitution.=C2=A0 SPEC-LIST is = a list of char/value pairs used for
=C2=A0`format-spec-make'."<= br>=C2=A0 =C2=A0(let ((args (tramp-get-method-parameter vec parameter))
= @@ -4738,6 +4738,8 @@ tramp-expand-args
=C2=A0 =C2=A0 =C2=A0(tramp-compa= t-flatten-tree
=C2=A0 =C2=A0 =C2=A0 (mapcar
=C2=A0 =C2=A0 =C2=A0 =C2= =A0(lambda (x)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0(cond ((functionp x) (setq x= (list (funcall x vec))))
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0(t (setq x (mapcar (lambda (y) (format-spec y spec)) x))))
=C2=A0 = (setq x (mapcar (lambda (y) (format-spec y spec)) x))
=C2=A0 (unless (me= mber "" x) x))
=C2=A0 =C2=A0 =C2=A0 =C2=A0args))))
--
2= .39.0


--000000000000043caf05f111dfad--