From: Michael Albinus Date: Thu, 25 Jul 2024 11:38:27 +0000 (+0200) Subject: Tramp's method "kubernetes" supports now optional namespace X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2aac21bba3b33f62154d8ac1a166aad9b57481be;p=emacs.git Tramp's method "kubernetes" supports now optional namespace * doc/misc/tramp.texi (Inline methods): Extend Kubernetes host name syntax by an optional namespace. (Kubernetes setup): Mention optional namespace part of the host name. * etc/NEWS: Tramp's method "kubernetes" supports now optional namespace. Fix typos. * lisp/net/tramp-container.el (tramp-kubernetes-namespace): Initialize with nil. Adapt docstring. (tramp-kubernetes--name-regexp): New defconst. (tramp-kubernetes--host-name-regexp): Change in order to cover also an optional namespace. (Bug#59797) (tramp-kubernetes--namespace): New defun. (tramp-kubernetes--context-namespace): Use it. (tramp-skeleton-kubernetes-vector): Add sanity check for superfluous user name or port number. (cherry picked from commit d2cb9f2bf6ef9f3fcd8c21455ca1f1e624e61bf4) --- diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index 88e46c05864..774711567c7 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -999,9 +999,11 @@ if desired. @cindex @option{kubernetes} method Integration for containers in Kubernetes pods. The host name is -@samp{@var{pod}}, or @samp{@var{container}.@var{pod}} if an -explicit container name shall be used. Otherwise, the first container -in a pod is used. +@samp{@var{pod}}, or @samp{@var{container}.@var{pod}} if an explicit +container name shall be used. Otherwise, the first container in a pod +is used. @samp{@var{pod}%@var{namespace}} as host name allows to use +another namespace but the default one. This can also be combined with +the @samp{@var{container}.} prefix. This method does not support user names. @@ -3246,7 +3248,9 @@ by setting the pod annotation @vindex tramp-kubernetes-namespace @value{tramp} uses the default Kubernetes context and namespace. If another context or namespace shall be used, configure the user options -@code{tramp-kubernetes-context} and @code{tramp-kubernetes-namespace}. +@code{tramp-kubernetes-context} or @code{tramp-kubernetes-namespace}, +respectively. An explicit @samp{%@var{namespace}} postfix in the host +name takes precedence. @node Auto-save File Lock and Backup diff --git a/etc/NEWS b/etc/NEWS index b29a09ec8b8..d496d79f79f 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -24,11 +24,12 @@ applies, and please also update docstrings as needed. * Installation Changes in Emacs 31.1 -** When using GCC 4 or later to build Emacs on 32-bit x86 systems, -'configure' now defaults to using the GCC options -mfpmath=sse (if the -host system supports SSE2) or -fno-tree-sra (if not). These GCC options -work around GCC bug 58416, which can cause Emacs to behave incorrectly -in rare cases. +** Changed GCC default options on 32-bit x86 systems. +When using GCC 4 or later to build Emacs on 32-bit x86 systems, +'configure' now defaults to using the GCC options '-mfpmath=sse' (if the +host system supports SSE2) or '-fno-tree-sra' (if not). These GCC +options work around GCC bug 58416, which can cause Emacs to behave +incorrectly in rare cases. * Startup Changes in Emacs 31.1 @@ -46,13 +47,13 @@ in rare cases. --- *** 'whitespace-cleanup' now adds missing newline at end of file. -If 'whitespace-style' includes 'missing-newline-at-eof (which is the +If 'whitespace-style' includes 'missing-newline-at-eof' (which is the default), the 'whitespace-cleanup' function will now add the newline. ** Eshell --- -*** New option 'eshell-command-async-buffer'. +*** New user option 'eshell-command-async-buffer'. This option lets you tell 'eshell-command' how to respond if its output buffer is already in use by another invocation of 'eshell-command', much like 'async-shell-command-buffer' does for 'shell-command'. By default, @@ -62,7 +63,7 @@ necessary. To restore the previous behavior, set this option to +++ *** 'eshell-execute-file' is now an interactive command. -Interactively, this now prompts for a script file to execute. With the +Interactively, this now prompts for a script file to execute. With the prefix argument, it will also insert any output into the current buffer at point. @@ -73,8 +74,8 @@ where to send the standard error output. See the "(eshell) Entry Points" node in the Eshell manual for more details. +++ -*** Eshell's built-in "wait" command now accepts a timeout. -By passing "-t" or "--timeout", you can specify a maximum time to wait +*** Eshell's built-in 'wait' command now accepts a timeout. +By passing '-t' or '--timeout', you can specify a maximum time to wait for the processes to exit. Additionally, you can now wait for external processes by passing their PIDs. @@ -85,7 +86,7 @@ processes by passing their PIDs. Sliced images allow for more intuitive scrolling up/down by letting you scroll past each slice, instead of jumping past the entire image. Previously, SHR sliced images when zoomed to their original size, no -matter how large or small that was). Now, SHR slices any images taller +matter how large or small that was. Now, SHR slices any images taller than 'shr-sliced-image-height'. For more information, see the "(eww) Advanced" node in the EWW manual. @@ -105,13 +106,13 @@ point. If a region is active, it runs all the unit tests under the region. It is bound to 'C-c C-t t' in 'go-ts-mode'. The 'go-ts-mode-test-this-file' command runs all unit tests in the current -file. It is bound to 'C-c C-t f' in 'go-ts-mode'. +file. It is bound to 'C-c C-t f' in 'go-ts-mode'. The 'go-ts-mode-test-this-package' command runs all unit tests under the package of the current buffer. It is bound to 'C-c C-t p' in 'go-ts-mode'. -The 'go-ts-mode-build-tags' variable is available to set a list of build -tags for the test commands. +The 'go-ts-mode-build-tags' user option is available to set a list of +build tags for the test commands. ** Emacs Lisp mode @@ -148,8 +149,18 @@ positives. --- *** Dedicated buffer for plain text contents. When switching to the plain text contents with 'doc-view-open-text', -DocView now creates a dedicated buffer to display it. 'C-c C-c' gets you +DocView now creates a dedicated buffer to display it. 'C-c C-c' gets you back to real DocView buffer if it still exists. + +** Tramp + ++++ +*** Connection method "kubernetes" supports now optional namespace. +The host name for Kubernetes connections can be of kind +[CONTAINER.]POD[%NAMESPACE], in order to specify the namespace to be +used. This overrides the setiing in 'tramp-kubernetes-namespace', if +any. + * New Modes and Packages in Emacs 31.1 @@ -176,9 +187,9 @@ It has been obsolete since Emacs 26.1. Use the group 'text' instead. --- ** Process execution has been optimized on Android. -The run-time performance of subprocesses on recent Android releases -where a userspace executable loader is required has been optimized on -systems featuring Linux 3.5.0 and above. +The run-time performance of subprocesses on recent Android releases, +where a userspace executable loader is required, has been optimized on +systems featuring GNU/Linux 3.5.0 and above. ---------------------------------------------------------------------- diff --git a/lisp/net/tramp-container.el b/lisp/net/tramp-container.el index 02512e64ef6..27bad70753c 100644 --- a/lisp/net/tramp-container.el +++ b/lisp/net/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=" +;; NAMESPACE is the namespace to be used (optional). ;; ;; ;; @@ -151,11 +147,14 @@ 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" - :type 'string) + :version "31.1" + :type '(choice (const :tag "Use default" nil) + (string))) ;;;###tramp-autoload (defcustom tramp-toolbox-program "toolbox" @@ -324,10 +323,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)))))) +;; +;; `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 +352,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 +372,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 +422,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 +639,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