From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Michael Albinus via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#59797: 30.0.50; [wishlist] Using namespaces in Tramp's kubernetes integration Date: Wed, 24 Jul 2024 18:48:04 +0200 Message-ID: <87plr2ka7v.fsf@gmx.de> References: <87wn78lu7n.fsf@gmx.de> <874ju96e2g.fsf@gmx.de> <87bkogr2e5.fsf@gmx.de> <87y1kaq7ty.fsf@gmx.de> <87ttggnvf5.fsf@gmx.de> <87plr4nqmh.fsf@gmx.de> Reply-To: Michael Albinus Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7309"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Aaron Gonzales , 59797@debbugs.gnu.org, Filipp Gunbin , Warren Lynn To: Eshel Yaron Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jul 24 18:49:15 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1sWfAx-0001iN-0t for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 24 Jul 2024 18:49:15 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sWfAg-0000yC-9B; Wed, 24 Jul 2024 12:48:58 -0400 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 1sWfAe-0000qq-BT for bug-gnu-emacs@gnu.org; Wed, 24 Jul 2024 12:48:56 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sWfAe-0000Wz-31 for bug-gnu-emacs@gnu.org; Wed, 24 Jul 2024 12:48:56 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sWfAj-0004vD-Ko for bug-gnu-emacs@gnu.org; Wed, 24 Jul 2024 12:49:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Michael Albinus Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 24 Jul 2024 16:49:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59797 X-GNU-PR-Package: emacs Original-Received: via spool by 59797-submit@debbugs.gnu.org id=B59797.172183971118873 (code B ref 59797); Wed, 24 Jul 2024 16:49:01 +0000 Original-Received: (at 59797) by debbugs.gnu.org; 24 Jul 2024 16:48:31 +0000 Original-Received: from localhost ([127.0.0.1]:34389 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sWfAE-0004uK-H1 for submit@debbugs.gnu.org; Wed, 24 Jul 2024 12:48:31 -0400 Original-Received: from mout.gmx.net ([212.227.17.21]:36829) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sWfAB-0004u6-RK for 59797@debbugs.gnu.org; Wed, 24 Jul 2024 12:48:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1721839687; x=1722444487; i=michael.albinus@gmx.de; bh=6U2bb07j50u5bWdwdtxefD18wLjKWktSkjKlSed0yEQ=; h=X-UI-Sender-Class:From:To:Cc:Subject:In-Reply-To:References:Date: Message-ID:MIME-Version:Content-Type:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=rTWeH5FdXiWtdZjno863ydAxIrDzsmeqcLXUIsRyX9cKTMX3IS7eTwOxkdSRtWkp oSyv9sutfl/2xuAgLxZaMQH37BLbvvCqLms5IiFWqYOruKWUCQp9laF6jk8yZ/IBS /tTne7s2EcTvT8QLH8bCYvWuB4KDIm+5nvjtXld0I3JwT+mQqCafdsvjg53P6Je/Y /6rfAqGrwlXbSxofE6tpe2uThkhHbZMXY3OG0ZYPQzB6rX1J1TsSPQfUEQ4MLmV5c ffpSPY+alX8IMzDpJITQ6h/XXTISlZRAS8ViiUp6RO5gYnQf7vf70h7deHz9FG6Ha 799IOBsAk9wR2vM3vA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Original-Received: from gandalf.gmx.de ([185.89.38.155]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MGQnF-1sVFb01PvJ-004IOr; Wed, 24 Jul 2024 18:48:07 +0200 In-Reply-To: <87plr4nqmh.fsf@gmx.de> (Michael Albinus's message of "Tue, 23 Jul 2024 16:13:10 +0200") X-Provags-ID: V03:K1:Q2nqfnWAawVF8D1VvBET7L56kZrMGbAHRI8gPDOqi3AABC+5WFQ xXMOH7fPAXeM6qMcApYUh82Q5GhNHBT4DebJaemHRx6K1LwMiOfxJFjVgcbpD2ICqc/DBaB Xg0HN+olVp+aHKGHLEWFo3LDEvS+5Gb4ECR40v/TFOiTZi7GLvdyWh3YTF22pLSiw+Xto5e kpPyYKZbDz3ylZwWZpJtw== UI-OutboundReport: notjunk:1;M01:P0:2RAJICshteo=;6gYGruwZOkfyfPTy1L7HbbigTWO 4qvIbZPdcT/FBU+VSkXZwtf/mhcsYN7KBXPVJXM9zgryPtsktXdALQyW8tOjjOQSnOSetfC28 NuEUFc1uRsdgdCb+6EjMgBU1/iCnnM1TZjUzZpDvc18GSNTL/9kOh3nx7AX6h+Ef0x/vbSPIY pjUez26BuKnYo+4QRHWossi5LUpcuTykJJDe9ccJuLwoWIwdKecoZfaky+B7/QeefTKlIfOpo QtgKgymcBc42W0oiK9GoDuHKQsjcxyQA09y//lulG/BA6hJmw91dHMta6txb/7u9oIZuBi088 yVg4pKa7b+4LKlq1gDVkS8DemzBqi9uY9mn0YMmxum0r6EYvs5MnSnqPtDuEAC2Ehj2YduQCc i1x/R2y9f5tqoQkv9cmvHLrWkvbQZAaYErejhniHsaBlnhN7zGMPsa+AppHz9AHx53qms2iYZ vDgf3oQ/sdfMYBPtYYRev4KQAlnzEOyLEQVzjXBhqdmL1OFZn3KiEyaonoffnnjmhW0pRf22H ubPhRK8y/cp7pYa8Q7UmvLvp08rGqyIdpiewpMrz9guUKGZb3XSIac5aU5gemrpPvUur20BVY 2z6R8kGsWg1+yBpcxtkhQSxJaXa/x5tqwBSlIQcZDpHWSz9aRJQKIPhUBIkNH7Do3zSEe+qhG WvwuGDmxXKRqy0CiT5DkWtG8eyYg7qKA5lDXH/nJFi1BBGmJaYIS50JwU+k/1DJpICMVTW4UW EiRXle5ZpRPYHiXMN2R25ErZqsoPUwpyk0G/DqIKtl2yldI2DjMy+T2Eflb9+A0Cg8O1RTbt X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:289242 Archived-At: --=-=-= Content-Type: text/plain Michael Albinus writes: Hi Eshel, >> You're absolutely right. AFAIU from the Kubernetes documentation[0], >> non-alphanumeric characters other than "-" and "." should be safe. >> Maybe "@" would be the most natural choice? "+" could work too I think. > > "@" is already used for the "user@host" notation in Tramp. We shouldn't > confuse users with another meaning in parallel. "+" might be OK, let's > start with it. It will be a Lisp constant, we can change it any time > later. So we have "/kubernetes:[CONTAINER.]POD[+NAMESPACE]:/..." now. I gave it a first try. It turns out, that "+" is a bad choice, because it doesn't belong to tramp-host-regexp. I would need to rewrite host name handling in Tramp just for that, which is not desirable. tramp-host-regexp is "[%._[:alnum:]-]+". So we could take either "%" or "_" as namespace delimiter. I've decided for "%"; a complete remote file name for kubernetes is now "/kubernetes:[CONTAINER.]POD[%NAMESPACE]:/path/to/file". It works OK acc to my basic tests. I have appended the patch; I would really appreciate if you could give it more testing in your production environment. Please sync first with the Emacs or Tramp git repo; this morning I've pushed other changes there. What's left is file name completion for namespaces. I'm undecided whether we need this in practice; let's see how it goes as-it-is. Best regards, Michael. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment Content-Transfer-Encoding: quoted-printable diff --git a/lisp/tramp-container.el b/lisp/tramp-container.el index 02512e64..e211a017 100644 =2D-- a/lisp/tramp-container.el +++ b/lisp/tramp-container.el @@ -50,18 +50,14 @@ ;; ;; Open file in a Kubernetes container: ;; -;; C-x C-f /kubernetes:[CONTAINER.]POD:/path/to/file +;; C-x C-f /kubernetes:[CONTAINER.]POD[%NAMESPACE]:/path/to/file ;; ;; Where: ;; POD is the pod to connect to. ;; CONTAINER is the container to connect to (optional). ;; By default, the first container in that pod will ;; be used. -;; -;; Completion for POD and accessing it operate in the current -;; namespace, use this command to change it: -;; -;; "kubectl config set-context --current --namespace=3D" +;; NAMESPACE is the namespace to be used (optional). ;; ;; ;; @@ -152,7 +148,9 @@ If it is nil, the default context will be used." (string))) (defcustom tramp-kubernetes-namespace "default" - "Namespace of Kubernetes." + "Namespace of Kubernetes. +If it is nil, the current namespace will be used. An explicit NAMESPACE +in the remote file name host part will override it." :group 'tramp :version "30.1" :type 'string) @@ -324,10 +322,18 @@ see its function help for a description of the forma= t." (push (concat elt "." (car line)) names))) (mapcar (lambda (name) (list nil name)) (delq nil names)))))) +;; +;; `lower' could also match non-ascii letters. But since this regexp +;; is only used for strings matching `tramp-host-regexp', this doesn't +;; hurt. +(defconst tramp-kubernetes--name-regexp (rx (** 1 63 (any lower digit "-"= ))) + "Regexp matching kubernetes names.") + (defconst tramp-kubernetes--host-name-regexp - (rx (? (group (regexp tramp-host-regexp)) ".") - (group (regexp tramp-host-regexp))) - "The CONTAINER.POD syntax of kubernetes host names in Tramp.") + (rx bos (? (group (regexp tramp-kubernetes--name-regexp)) ".") + (group (regexp tramp-kubernetes--name-regexp)) + (? "%" (group (regexp tramp-kubernetes--name-regexp))) eos) + "The CONTAINER.POD%NAMESPACE syntax of kubernetes host names in Tramp."= ) ;;;###tramp-autoload (defun tramp-kubernetes--container (vec) @@ -345,6 +351,15 @@ see its function help for a description of the format= ." (match-string 2 host))) "")) +;;;###tramp-autoload +(defun tramp-kubernetes--namespace (vec) + "Extract the namespace from a kubernetes host name in VEC." + (or (when-let ((_ vec) + (host (tramp-file-name-host vec))) + (and (string-match tramp-kubernetes--host-name-regexp host) + (match-string 3 host))) + tramp-kubernetes-namespace "")) + ;; We must change `vec' and `default-directory' to the previous hop, ;; in order to run `process-file' in a proper environment. (defmacro tramp-skeleton-kubernetes-vector (vec &rest body) @@ -400,8 +415,8 @@ Obey `tramp-kubernetes-context'" #'identity `(,(when-let ((context (tramp-kubernetes--current-context vec))) (format "--context=3D%s" context)) - ,(when tramp-kubernetes-namespace - (format "--namespace=3D%s" tramp-kubernetes-namespace))) + ,(when-let ((namespace (tramp-kubernetes--namespace vec))) + (format "--namespace=3D%s" namespace))) " ")) ;;;###tramp-autoload @@ -617,9 +632,9 @@ see its function help for a description of the format.= " ;; This variable will be eval'ed in `tramp-expand-args'. (tramp-extra-expand-args . (?a (tramp-kubernetes--container (car tramp-current-connection)) - ?h (tramp-kubernetes--pod (car tramp-current-connection)) - ?x (tramp-kubernetes--context-namespace - (car tramp-current-connection))))) + ?h (tramp-kubernetes--pod (car tramp-current-connection)) + ?x (tramp-kubernetes--context-namespace + (car tramp-current-connection))))) "Default connection-local variables for remote kubernetes connections.= ") (connection-local-set-profile-variables --=-=-=--