From a259d0dda3878a64373b808627c4b4cab3971194 Mon Sep 17 00:00:00 2001 From: Filipp Gunbin Date: Wed, 5 Oct 2022 02:46:40 +0300 Subject: [PATCH] Add tramp-kubernetes integration * doc/misc/tramp.texi (Inline methods): Add kubernetes. (Customizing Methods): Remove kubernetes-tramp. * etc/NEWS: Mention new Tramp method "kubernetes". * lisp/net/tramp-compat.el (kubernetes-tramp): Warn if that package is used. * lisp/net/tramp-container.el (tramp-kubernetes-program): New defcustom. (tramp-kubernetes-method): New defconst. (tramp-kubernetes--completion-function): New function. --- doc/misc/tramp.texi | 17 +++--- etc/NEWS | 5 +- lisp/net/tramp-compat.el | 5 +- lisp/net/tramp-container.el | 104 +++++++++++++++++++++++++++--------- 4 files changed, 95 insertions(+), 36 deletions(-) diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi index 064a2cb6023..395df00bf7f 100644 --- a/doc/misc/tramp.texi +++ b/doc/misc/tramp.texi @@ -917,6 +917,14 @@ running container's name or ID, as returned by @samp{docker ps}. Podman is an alternative to @option{docker} which may be run rootless, if desired. +@item @option{kubernetes} +@cindex method @option{kubernetes} +@cindex @option{kubernetes} method + +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. + @end table @@ -1777,15 +1785,6 @@ They can be installed with Emacs's Package Manager. This includes @c @item ibuffer-tramp.el @c Contact Svend Sorensen -@item kubernetes-tramp -@cindex method @option{kubectl} -@cindex @option{kubectl} method -Integration for Docker containers deployed in a Kubernetes cluster. A -container is accessed via -@file{@trampfn{kubectl,user@@container,/path/to/file}}, @samp{user} -and @samp{container} have the same meaning as with the @option{docker} -method. - @item lxc-tramp @cindex method @option{lxc} @cindex @option{lxc} method diff --git a/etc/NEWS b/etc/NEWS index d475c28e45e..916abbc4363 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2450,8 +2450,9 @@ and friends. ** Tramp +++ -*** New connection methods "docker" and "podman". -It allows accessing environments provided by Docker and similar programs. +*** New connection methods "docker", "podman" and "kubernetes". +They allow accessing environments provided by Docker and similar +programs. --- *** Tramp supports abbreviating remote home directories now. diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el index f6cc1034ca5..a1d1d284edb 100644 --- a/lisp/net/tramp-compat.el +++ b/lisp/net/tramp-compat.el @@ -52,7 +52,10 @@ (with-eval-after-load 'docker-tramp (warn (concat "Package `docker-tramp' has been obsoleted, " - "please use integrated package `tramp-docker'"))) + "please use integrated package `tramp-container'"))) +(with-eval-after-load 'kubernetes-tramp + (warn (concat "Package `kubernetes-tramp' has been obsoleted, " + "please use integrated package `tramp-container'"))) ;; For not existing functions, obsolete functions, or functions with a ;; changed argument list, there are compiler warnings. We want to diff --git a/lisp/net/tramp-container.el b/lisp/net/tramp-container.el index d1c8f2bb4c4..e104babed27 100644 --- a/lisp/net/tramp-container.el +++ b/lisp/net/tramp-container.el @@ -39,6 +39,21 @@ ;; Where: ;; USER is the user on the container to connect as (optional) ;; CONTAINER is the container to connect to +;; +;; +;; Open file in a Kubernetes container: +;; +;; C-x C-f /kubernetes:POD:/path/to/file +;; +;; Where: +;; POD is the pod to connect to. +;; 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=" ;;; Code: @@ -60,6 +75,14 @@ :type '(choice (const "podman") (string))) +;;;###tramp-autoload +(defcustom tramp-kubernetes-program "kubectl" + "Name of the Kubernetes client program." + :group 'tramp + :version "29.1" + :type '(choice (const "kubectl") + (string))) + ;;;###tramp-autoload (defconst tramp-docker-method "docker" "Tramp method name to use to connect to Docker containers.") @@ -68,6 +91,10 @@ (defconst tramp-podman-method "podman" "Tramp method name to use to connect to Podman containers.") +;;;###tramp-autoload +(defconst tramp-kubernetes-method "kubernetes" + "Tramp method name to use to connect to Kubernetes containers.") + ;;;###tramp-autoload (defun tramp-docker--completion-function (&rest _args) "List Docker-like containers available for connection. @@ -88,34 +115,59 @@ see its function help for a description of the format." lines))) (mapcar (lambda (m) (list nil m)) (delq nil names)))) +;;;###tramp-autoload +(defun tramp-kubernetes--completion-function (&rest _args) + "List Kubernetes pods available for connection. + +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 + (concat tramp-kubernetes-program + " get pods --no-headers " + "-o custom-columns=NAME:.metadata.name"))) + (names (split-string raw-list "\n" 'omit))) + (mapcar (lambda (name) + (list nil name)) + names))) + ;;;###tramp-autoload (defvar tramp-default-remote-shell) ;; Silence byte compiler. ;;;###tramp-autoload (tramp--with-startup - (push `(,tramp-docker-method - (tramp-login-program ,tramp-docker-program) - (tramp-login-args (("exec") - ("-it") - ("-u" "%u") - ("%h") - ("%l"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-i" "-c"))) - tramp-methods) - - (push `(,tramp-podman-method - (tramp-login-program ,tramp-podman-program) - (tramp-login-args (("exec") - ("-it") - ("-u" "%u") - ("%h") - ("%l"))) - (tramp-remote-shell ,tramp-default-remote-shell) - (tramp-remote-shell-login ("-l")) - (tramp-remote-shell-args ("-i" "-c"))) - tramp-methods) + (add-to-list 'tramp-methods + `(,tramp-docker-method + (tramp-login-program ,tramp-docker-program) + (tramp-login-args (("exec") + ("-it") + ("-u" "%u") + ("%h") + ("%l"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-i" "-c")))) + (add-to-list 'tramp-methods + `(,tramp-podman-method + (tramp-login-program ,tramp-podman-program) + (tramp-login-args (("exec") + ("-it") + ("-u" "%u") + ("%h") + ("%l"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-i" "-c")))) + (add-to-list 'tramp-methods + `(,tramp-kubernetes-method + (tramp-login-program ,tramp-kubernetes-program) + (tramp-login-args (("exec") + ("%h") + ("-it") + ("--") + ("%l"))) + (tramp-remote-shell ,tramp-default-remote-shell) + (tramp-remote-shell-login ("-l")) + (tramp-remote-shell-args ("-i" "-c")))) (tramp-set-completion-function tramp-docker-method @@ -123,7 +175,11 @@ see its function help for a description of the format." (tramp-set-completion-function tramp-podman-method - '((tramp-docker--completion-function "")))) + '((tramp-docker--completion-function ""))) + + (tramp-set-completion-function + tramp-kubernetes-method + '((tramp-kubernetes--completion-function "")))) (add-hook 'tramp-unload-hook (lambda () -- 2.39.2