unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Albinus via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eshel Yaron <me@eshelyaron.com>
Cc: Aaron Gonzales <aarongonzales1@gmail.com>,
	59797@debbugs.gnu.org, Filipp Gunbin <fgunbin@fastmail.fm>,
	Warren Lynn <wrn.lynn@gmail.com>
Subject: bug#59797: 30.0.50; [wishlist] Using namespaces in Tramp's kubernetes integration
Date: Wed, 24 Jul 2024 18:48:04 +0200	[thread overview]
Message-ID: <87plr2ka7v.fsf@gmx.de> (raw)
In-Reply-To: <87plr4nqmh.fsf@gmx.de> (Michael Albinus's message of "Tue, 23 Jul 2024 16:13:10 +0200")

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

Michael Albinus <michael.albinus@gmx.de> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 4078 bytes --]

diff --git a/lisp/tramp-container.el b/lisp/tramp-container.el
index 02512e64..e211a017 100644
--- 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=<name>"
+;;     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 format."
 	    (push (concat elt "." (car line)) names)))
 	(mapcar (lambda (name) (list nil name)) (delq nil names))))))

+;; <https://kubernetes.io/docs/concepts/overview/working-with-objects/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=%s" context))
-     ,(when tramp-kubernetes-namespace
-	(format "--namespace=%s" tramp-kubernetes-namespace)))
+     ,(when-let ((namespace (tramp-kubernetes--namespace vec)))
+	(format "--namespace=%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

  reply	other threads:[~2024-07-24 16:48 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-03  9:20 bug#59797: 30.0.50; [wishlist] Using namespaces in Tramp's kubernetes integration Michael Albinus
2022-12-05 14:05 ` Filipp Gunbin
2022-12-05 15:54   ` Michael Albinus
2022-12-06 13:56     ` Filipp Gunbin
2022-12-06 15:13       ` Michael Albinus
2023-06-23 19:47         ` Michael Albinus
2023-07-02  8:37           ` Michael Albinus
     [not found]           ` <m1cyn4cq38.fsf@eshelyaron.com>
2024-07-23 12:29             ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-23 12:59               ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-23 14:13                 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-24 16:48                   ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-07-24 17:30                     ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-25  8:20                       ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-25 10:48                         ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-07-25 11:42                           ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors

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=87plr2ka7v.fsf@gmx.de \
    --to=bug-gnu-emacs@gnu.org \
    --cc=59797@debbugs.gnu.org \
    --cc=aarongonzales1@gmail.com \
    --cc=fgunbin@fastmail.fm \
    --cc=me@eshelyaron.com \
    --cc=michael.albinus@gmx.de \
    --cc=wrn.lynn@gmail.com \
    /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).