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: Thu, 25 Jul 2024 10:20:40 +0200	[thread overview]
Message-ID: <87ed7hkhlz.fsf@gmx.de> (raw)
In-Reply-To: <m1ttgeu28a.fsf@dazzs-mbp.home> (Eshel Yaron's message of "Wed, 24 Jul 2024 19:30:29 +0200")

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

Eshel Yaron <me@eshelyaron.com> writes:

> Hi Michael,

Hi Eshel,

> From a quick test, this mostly works fine.  There's one issue (typo?) in
> tramp-kubernetes--namespace that causes a regression when there's no
> explicit namespace and tramp-kubernetes-namespace is nil, it returns an
> empty string instead of nil.

I'fixed this, see appended revised patch. I've changed also the default
value of tramp-kubernetes-namespace to nil; we shouldn't assume that
everybody wants "default" as default.

Could you give it a short test, please?

> Cheers,
>
> Eshel

Best regards, Michael.


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

diff --git a/lisp/tramp-container.el b/lisp/tramp-container.el
index 02512e64..bd5736e6 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).
 ;;
 ;;
 ;;
@@ -151,10 +147,12 @@ If it is nil, the default context will be used."
   :type '(choice (const :tag "Use default" nil)
                  (string)))

-(defcustom tramp-kubernetes-namespace "default"
-  "Namespace of Kubernetes."
+(defcustom tramp-kubernetes-namespace nil
+  "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"
+  :version "31.1"
   :type 'string)

 ;;;###tramp-autoload
@@ -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,16 @@ 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.
+Use `tramp-kubernetes-namespace' otherwise."
+  (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)
@@ -355,6 +371,11 @@ BODY is the backend specific code."
 	   (cond
 	    ((null ,vec) tramp-null-hop)
 	    ((equal (tramp-file-name-method ,vec) tramp-kubernetes-method)
+	     ;; Sanity check.  We don't support `user' or `port' in
+	     ;; kubernetes file names.
+	     (when (or (tramp-file-name-user-domain ,vec)
+		       (tramp-file-name-port ,vec))
+	       (tramp-user-error ,vec "Wrong kubernetes file name syntax"))
 	     (if (tramp-file-name-hop ,vec)
 		 (tramp-dissect-hop-name (tramp-file-name-hop ,vec))
 	       tramp-null-hop))
@@ -400,8 +421,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 +638,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-25  8:20 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
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 [this message]
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=87ed7hkhlz.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).