aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2022-10-25 16:34:42 +0200
committerMichael Albinus2022-10-25 16:34:42 +0200
commitfe816fc679ead2100cddb4e51bc81c329bcb4265 (patch)
tree5a8b9214e16e0ee5f05344bed773ff02546521ea
parent8c3b8c36677eedfc3839488e3cef9f6a5937baa3 (diff)
downloademacs-fe816fc679ead2100cddb4e51bc81c329bcb4265.tar.gz
emacs-fe816fc679ead2100cddb4e51bc81c329bcb4265.zip
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) <kubernetes>: 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.
-rw-r--r--doc/misc/tramp.texi13
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/net/tramp-cache.el27
-rw-r--r--lisp/net/tramp-container.el25
-rw-r--r--lisp/net/tramp-sh.el31
-rw-r--r--lisp/net/tramp.el7
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
925name returned by @samp{kubectl get pods}. The first container in a 925name returned by @samp{kubectl get pods}. The first container in a
926pod is used. 926pod is used.
927 927
928@samp{kubectl get pods} returns pods in the current context and
929namespace. Current namespace can be changed with @samp{kubectl config
930set-context --current --namespace=<name>}. After invoking this or
931other command which modifies Kubernetes environment outside of Emacs,
932call @code{tramp-cleanup-all-connections} to reset Tramp cache data.
933
934This method does not support user names. 928This method does not support user names.
935 929
936@end table 930@end table
@@ -3538,9 +3532,14 @@ the @file{~/.authinfo.gpg} authentication file. The user option
3538@code{tramp-completion-use-auth-sources} controls, whether such a 3532@code{tramp-completion-use-auth-sources} controls, whether such a
3539search is performed during completion. 3533search is performed during completion.
3540 3534
3535@vindex tramp-completion-use-cache
3541Remote hosts previously visited or hosts whose connections are kept 3536Remote hosts previously visited or hosts whose connections are kept
3542persistently (@pxref{Connection caching}) will be included in the 3537persistently (@pxref{Connection caching}) will be included in the
3543completion lists. 3538completion lists. If you want to suppress this completion because
3539there are invalid entries in the persistency file, for example if the
3540host configuration changes often, or if you plug your laptop to
3541different networks frequently, you can set the user option
3542@code{tramp-completion-use-cache} to nil.
3544 3543
3545After remote host name completion comes completion of file names on 3544After remote host name completion comes completion of file names on
3546the remote host. It works the same as with local host file completion 3545the 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
2517target. This has always been needed, just the password prompt and the 2517target. This has always been needed, just the password prompt and the
2518related 'auth-sources' entry were wrong. 2518related 'auth-sources' entry were wrong.
2519 2519
2520+++
2521*** New user option 'tramp-completion-use-cache'.
2522During user and host name completion in the minibuffer, results from
2523Tramp's connection cache are taken into account. This can be disabled
2524by setting the user option 'tramp-completion-use-cache' to nil.
2525
2520** Browse URL 2526** Browse URL
2521 2527
2522--- 2528---
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
@@ -602,18 +602,29 @@ PROPERTIES is a list of file properties (strings)."
602 #'tramp-dump-connection-properties))) 602 #'tramp-dump-connection-properties)))
603 603
604;;;###tramp-autoload 604;;;###tramp-autoload
605(defcustom tramp-completion-use-cache t
606 "Whether to use the Tramp cache for completion of user and host names.
607Set it to nil if there are invalid entries in the cache, for
608example if the host configuration changes often, or if you plug
609your laptop to different networks frequently."
610 :group 'tramp
611 :version "29.1"
612 :type 'boolean)
613
614;;;###tramp-autoload
605(defun tramp-parse-connection-properties (method) 615(defun tramp-parse-connection-properties (method)
606 "Return a list of (user host) tuples allowed to access for METHOD. 616 "Return a list of (user host) tuples allowed to access for METHOD.
607This function is added always in `tramp-get-completion-function' 617This function is added always in `tramp-get-completion-function'
608for all methods. Resulting data are derived from connection history." 618for all methods. Resulting data are derived from connection history."
609 (mapcar 619 (and tramp-completion-use-cache
610 (lambda (key) 620 (mapcar
611 (and (tramp-file-name-p key) 621 (lambda (key)
612 (string-equal method (tramp-file-name-method key)) 622 (and (tramp-file-name-p key)
613 (not (tramp-file-name-localname key)) 623 (string-equal method (tramp-file-name-method key))
614 (list (tramp-file-name-user key) 624 (not (tramp-file-name-localname key))
615 (tramp-file-name-host key)))) 625 (list (tramp-file-name-user key)
616 (hash-table-keys tramp-cache-data))) 626 (tramp-file-name-host key))))
627 (hash-table-keys tramp-cache-data))))
617 628
618;; When "emacs -Q" has been called, both variables are nil. We do not 629;; When "emacs -Q" has been called, both variables are nil. We do not
619;; load the persistency file then, in order to have a clean test environment. 630;; 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 @@
101 101
102This function is used by `tramp-set-completion-function', please 102This function is used by `tramp-set-completion-function', please
103see its function help for a description of the format." 103see its function help for a description of the format."
104 (when-let ((raw-list (shell-command-to-string 104 (when-let ((default-directory tramp-compat-temporary-file-directory)
105 (raw-list (shell-command-to-string
105 (concat tramp-docker-program 106 (concat tramp-docker-program
106 " ps --format '{{.ID}}\t{{.Names}}'"))) 107 " ps --format '{{.ID}}\t{{.Names}}'")))
107 (lines (split-string raw-list "\n" 'omit)) 108 (lines (split-string raw-list "\n" 'omit))
@@ -121,7 +122,8 @@ see its function help for a description of the format."
121 122
122This function is used by `tramp-set-completion-function', please 123This function is used by `tramp-set-completion-function', please
123see its function help for a description of the format." 124see its function help for a description of the format."
124 (when-let ((raw-list (shell-command-to-string 125 (when-let ((default-directory tramp-compat-temporary-file-directory)
126 (raw-list (shell-command-to-string
125 (concat tramp-kubernetes-program 127 (concat tramp-kubernetes-program
126 " get pods --no-headers " 128 " get pods --no-headers "
127 "-o custom-columns=NAME:.metadata.name"))) 129 "-o custom-columns=NAME:.metadata.name")))
@@ -130,6 +132,24 @@ see its function help for a description of the format."
130 (list nil name)) 132 (list nil name))
131 names))) 133 names)))
132 134
135(defun tramp-kubernetes--current-context-data (vec)
136 "Return Kubernetes current context data as JSONPATH string."
137 (with-temp-buffer
138 (when (zerop
139 (tramp-call-process
140 vec tramp-kubernetes-program nil t nil
141 "config" "current-context"))
142 (goto-char (point-min))
143 (let ((current-context (buffer-substring (point) (line-end-position))))
144 (erase-buffer)
145 (when (zerop
146 (tramp-call-process
147 vec tramp-kubernetes-program nil t nil
148 "config" "view" "-o"
149 (format
150 "jsonpath='{.contexts[?(@.name == \"%s\")]}'" current-context)))
151 (buffer-string))))))
152
133;;;###tramp-autoload 153;;;###tramp-autoload
134(defvar tramp-default-remote-shell) ;; Silence byte compiler. 154(defvar tramp-default-remote-shell) ;; Silence byte compiler.
135 155
@@ -165,6 +185,7 @@ see its function help for a description of the format."
165 ("-it") 185 ("-it")
166 ("--") 186 ("--")
167 ("%l"))) 187 ("%l")))
188 (tramp-config-check tramp-kubernetes--current-context-data)
168 (tramp-remote-shell ,tramp-default-remote-shell) 189 (tramp-remote-shell ,tramp-default-remote-shell)
169 (tramp-remote-shell-login ("-l")) 190 (tramp-remote-shell-login ("-l"))
170 (tramp-remote-shell-args ("-i" "-c")))) 191 (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."
4472 ;; Check whether the output of "uname -sr" has been changed. If 4472 ;; Check whether the output of "uname -sr" has been changed. If
4473 ;; yes, this is a strong indication that we must expire all 4473 ;; yes, this is a strong indication that we must expire all
4474 ;; connection properties. We start again with 4474 ;; connection properties. We start again with
4475 ;; `tramp-maybe-open-connection', it will be caught there. 4475 ;; `tramp-maybe-open-connection', it will be caught there. The same
4476 ;; check will be applied with the function kept in`tramp-config-check'.
4476 (tramp-message vec 5 "Checking system information") 4477 (tramp-message vec 5 "Checking system information")
4477 (let* ((old-uname (tramp-get-connection-property vec "uname")) 4478 (let* ((old-uname (tramp-get-connection-property vec "uname"))
4478 (uname 4479 (uname
@@ -4481,8 +4482,23 @@ process to set up. VEC specifies the connection."
4481 old-uname 4482 old-uname
4482 (tramp-set-connection-property 4483 (tramp-set-connection-property
4483 vec "uname" 4484 vec "uname"
4484 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))) 4485 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
4485 (when (and (stringp old-uname) (not (string-equal old-uname uname))) 4486 (config-check-function
4487 (tramp-get-method-parameter vec 'tramp-config-check))
4488 (old-config-check
4489 (and config-check-function
4490 (tramp-get-connection-property vec "config-check-data")))
4491 (config-check
4492 (and config-check-function
4493 ;; If we are in `make-process', we don't need to recompute.
4494 (if (and old-config-check
4495 (tramp-get-connection-property vec "process-name"))
4496 old-config-check
4497 (tramp-set-connection-property
4498 vec "config-check-data"
4499 (tramp-compat-funcall config-check-function vec))))))
4500 (when (and (stringp old-uname) (stringp uname)
4501 (not (string-equal old-uname uname)))
4486 (tramp-message 4502 (tramp-message
4487 vec 3 4503 vec 3
4488 "Connection reset, because remote host changed from `%s' to `%s'" 4504 "Connection reset, because remote host changed from `%s' to `%s'"
@@ -4490,6 +4506,15 @@ process to set up. VEC specifies the connection."
4490 ;; We want to keep the password. 4506 ;; We want to keep the password.
4491 (tramp-cleanup-connection vec t t) 4507 (tramp-cleanup-connection vec t t)
4492 (throw 'uname-changed (tramp-maybe-open-connection vec))) 4508 (throw 'uname-changed (tramp-maybe-open-connection vec)))
4509 (when (and (stringp old-config-check) (stringp config-check)
4510 (not (string-equal old-config-check config-check)))
4511 (tramp-message
4512 vec 3
4513 "Connection reset, because remote configuration changed from `%s' to `%s'"
4514 old-config-check config-check)
4515 ;; We want to keep the password.
4516 (tramp-cleanup-connection vec t t)
4517 (throw 'uname-changed (tramp-maybe-open-connection vec)))
4493 4518
4494 ;; Try to set up the coding system correctly. 4519 ;; Try to set up the coding system correctly.
4495 ;; CCC this can't be the right way to do it. Hm. 4520 ;; 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:
281 Until now, just \"ssh\"-based, \"sshfs\"-based and 281 Until now, just \"ssh\"-based, \"sshfs\"-based and
282 \"adb\"-based methods do. 282 \"adb\"-based methods do.
283 283
284 * `tramp-config-check'
285 A function to be called with one argument, VEC. It should
286 return a string which is used to check, whether the
287 configuration of the remote host has been changed (which
288 would require to flush the cache data). This string is kept
289 as connection property \"config-check-data\".
290
284 * `tramp-copy-program' 291 * `tramp-copy-program'
285 This specifies the name of the program to use for remotely copying 292 This specifies the name of the program to use for remotely copying
286 the file; this might be the absolute filename of scp or the name of 293 the file; this might be the absolute filename of scp or the name of