From fe816fc679ead2100cddb4e51bc81c329bcb4265 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Tue, 25 Oct 2022 16:34:42 +0200 Subject: [PATCH] Handle context changes in Tramp kubernetes method * doc/misc/tramp.texi (Inline methods): Remove note about cache reset. (File name completion): Add tramp-completion-use-cache. * etc/NEWS: Add 'tramp-completion-use-cache'. * lisp/net/tramp-cache.el (tramp-completion-use-cache): New defcustom. (tramp-parse-connection-properties): Use it. * lisp/net/tramp-container.el (tramp-docker--completion-function) (tramp-kubernetes--completion-function): Ensure the processes run locally. (tramp-kubernetes--current-context-data): New defun. (tramp-methods) : Add `tramp-config-check'. * lisp/net/tramp-sh.el (tramp-open-connection-setup-interactive-shell): Handle `tramp-login-args'. * lisp/net/tramp.el (tramp-methods): Adapt docstring. --- doc/misc/tramp.texi | 13 ++++++------- etc/NEWS | 6 ++++++ lisp/net/tramp-cache.el | 27 +++++++++++++++++++-------- lisp/net/tramp-container.el | 25 +++++++++++++++++++++++-- lisp/net/tramp-sh.el | 31 ++++++++++++++++++++++++++++--- lisp/net/tramp.el | 7 +++++++ 6 files changed, 89 insertions(+), 20 deletions(-) diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index e74d382fa18..99a268367b8 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -925,12 +925,6 @@ Integration for containers in Kubernetes pods. The host name is a pod name returned by @samp{kubectl get pods}. The first container in a pod is used. -@samp{kubectl get pods} returns pods in the current context and -namespace. Current namespace can be changed with @samp{kubectl config -set-context --current --namespace=}. After invoking this or -other command which modifies Kubernetes environment outside of Emacs, -call @code{tramp-cleanup-all-connections} to reset Tramp cache data. - This method does not support user names. @end table @@ -3538,9 +3532,14 @@ the @file{~/.authinfo.gpg} authentication file. The user option @code{tramp-completion-use-auth-sources} controls, whether such a search is performed during completion. +@vindex tramp-completion-use-cache Remote hosts previously visited or hosts whose connections are kept persistently (@pxref{Connection caching}) will be included in the -completion lists. +completion lists. If you want to suppress this completion because +there are invalid entries in the persistency file, for example if the +host configuration changes often, or if you plug your laptop to +different networks frequently, you can set the user option +@code{tramp-completion-use-cache} to nil. After remote host name completion comes completion of file names on the remote host. It works the same as with local host file completion diff --git a/etc/NEWS b/etc/NEWS index aacad8bc4df..6622f2d4ad6 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2517,6 +2517,12 @@ the user requesting such a connection, and not of the user who is the target. This has always been needed, just the password prompt and the related 'auth-sources' entry were wrong. ++++ +*** New user option 'tramp-completion-use-cache'. +During user and host name completion in the minibuffer, results from +Tramp's connection cache are taken into account. This can be disabled +by setting the user option 'tramp-completion-use-cache' to nil. + ** Browse URL --- diff --git a/lisp/net/tramp-cache.el b/lisp/net/tramp-cache.el index 4d7d35a4de6..912ea5f8bbd 100644 --- a/lisp/net/tramp-cache.el +++ b/lisp/net/tramp-cache.el @@ -601,19 +601,30 @@ PROPERTIES is a list of file properties (strings)." (remove-hook 'kill-emacs-hook #'tramp-dump-connection-properties))) +;;;###tramp-autoload +(defcustom tramp-completion-use-cache t + "Whether to use the Tramp cache for completion of user and host names. +Set it to nil if there are invalid entries in the cache, for +example if the host configuration changes often, or if you plug +your laptop to different networks frequently." + :group 'tramp + :version "29.1" + :type 'boolean) + ;;;###tramp-autoload (defun tramp-parse-connection-properties (method) "Return a list of (user host) tuples allowed to access for METHOD. This function is added always in `tramp-get-completion-function' for all methods. Resulting data are derived from connection history." - (mapcar - (lambda (key) - (and (tramp-file-name-p key) - (string-equal method (tramp-file-name-method key)) - (not (tramp-file-name-localname key)) - (list (tramp-file-name-user key) - (tramp-file-name-host key)))) - (hash-table-keys tramp-cache-data))) + (and tramp-completion-use-cache + (mapcar + (lambda (key) + (and (tramp-file-name-p key) + (string-equal method (tramp-file-name-method key)) + (not (tramp-file-name-localname key)) + (list (tramp-file-name-user key) + (tramp-file-name-host key)))) + (hash-table-keys tramp-cache-data)))) ;; When "emacs -Q" has been called, both variables are nil. We do not ;; load the persistency file then, in order to have a clean test environment. diff --git a/lisp/net/tramp-container.el b/lisp/net/tramp-container.el index e104babed27..0879d6f1856 100644 --- a/lisp/net/tramp-container.el +++ b/lisp/net/tramp-container.el @@ -101,7 +101,8 @@ This function is used by `tramp-set-completion-function', please see its function help for a description of the format." - (when-let ((raw-list (shell-command-to-string + (when-let ((default-directory tramp-compat-temporary-file-directory) + (raw-list (shell-command-to-string (concat tramp-docker-program " ps --format '{{.ID}}\t{{.Names}}'"))) (lines (split-string raw-list "\n" 'omit)) @@ -121,7 +122,8 @@ see its function help for a description of the format." This function is used by `tramp-set-completion-function', please see its function help for a description of the format." - (when-let ((raw-list (shell-command-to-string + (when-let ((default-directory tramp-compat-temporary-file-directory) + (raw-list (shell-command-to-string (concat tramp-kubernetes-program " get pods --no-headers " "-o custom-columns=NAME:.metadata.name"))) @@ -130,6 +132,24 @@ see its function help for a description of the format." (list nil name)) names))) +(defun tramp-kubernetes--current-context-data (vec) + "Return Kubernetes current context data as JSONPATH string." + (with-temp-buffer + (when (zerop + (tramp-call-process + vec tramp-kubernetes-program nil t nil + "config" "current-context")) + (goto-char (point-min)) + (let ((current-context (buffer-substring (point) (line-end-position)))) + (erase-buffer) + (when (zerop + (tramp-call-process + vec tramp-kubernetes-program nil t nil + "config" "view" "-o" + (format + "jsonpath='{.contexts[?(@.name == \"%s\")]}'" current-context))) + (buffer-string)))))) + ;;;###tramp-autoload (defvar tramp-default-remote-shell) ;; Silence byte compiler. @@ -165,6 +185,7 @@ see its function help for a description of the format." ("-it") ("--") ("%l"))) + (tramp-config-check tramp-kubernetes--current-context-data) (tramp-remote-shell ,tramp-default-remote-shell) (tramp-remote-shell-login ("-l")) (tramp-remote-shell-args ("-i" "-c")))) diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index d74afc84126..3904348232b 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -4472,7 +4472,8 @@ process to set up. VEC specifies the connection." ;; Check whether the output of "uname -sr" has been changed. If ;; yes, this is a strong indication that we must expire all ;; connection properties. We start again with - ;; `tramp-maybe-open-connection', it will be caught there. + ;; `tramp-maybe-open-connection', it will be caught there. The same + ;; check will be applied with the function kept in`tramp-config-check'. (tramp-message vec 5 "Checking system information") (let* ((old-uname (tramp-get-connection-property vec "uname")) (uname @@ -4481,8 +4482,23 @@ process to set up. VEC specifies the connection." old-uname (tramp-set-connection-property vec "uname" - (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))) - (when (and (stringp old-uname) (not (string-equal old-uname uname))) + (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\"")))) + (config-check-function + (tramp-get-method-parameter vec 'tramp-config-check)) + (old-config-check + (and config-check-function + (tramp-get-connection-property vec "config-check-data"))) + (config-check + (and config-check-function + ;; If we are in `make-process', we don't need to recompute. + (if (and old-config-check + (tramp-get-connection-property vec "process-name")) + old-config-check + (tramp-set-connection-property + vec "config-check-data" + (tramp-compat-funcall config-check-function vec)))))) + (when (and (stringp old-uname) (stringp uname) + (not (string-equal old-uname uname))) (tramp-message vec 3 "Connection reset, because remote host changed from `%s' to `%s'" @@ -4490,6 +4506,15 @@ process to set up. VEC specifies the connection." ;; We want to keep the password. (tramp-cleanup-connection vec t t) (throw 'uname-changed (tramp-maybe-open-connection vec))) + (when (and (stringp old-config-check) (stringp config-check) + (not (string-equal old-config-check config-check))) + (tramp-message + vec 3 + "Connection reset, because remote configuration changed from `%s' to `%s'" + old-config-check config-check) + ;; We want to keep the password. + (tramp-cleanup-connection vec t t) + (throw 'uname-changed (tramp-maybe-open-connection vec))) ;; Try to set up the coding system correctly. ;; CCC this can't be the right way to do it. Hm. diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index c06adb01e8c..63f313dc509 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -281,6 +281,13 @@ pair of the form (KEY VALUE). The following KEYs are defined: Until now, just \"ssh\"-based, \"sshfs\"-based and \"adb\"-based methods do. + * `tramp-config-check' + A function to be called with one argument, VEC. It should + return a string which is used to check, whether the + configuration of the remote host has been changed (which + would require to flush the cache data). This string is kept + as connection property \"config-check-data\". + * `tramp-copy-program' This specifies the name of the program to use for remotely copying the file; this might be the absolute filename of scp or the name of -- 2.39.5