[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
scratch/tramp-kubernetes ec9f897845: Add lisp/net/tramp-kubernetes.el
From: |
Filipp Gunbin |
Subject: |
scratch/tramp-kubernetes ec9f897845: Add lisp/net/tramp-kubernetes.el |
Date: |
Tue, 4 Oct 2022 13:42:33 -0400 (EDT) |
branch: scratch/tramp-kubernetes
commit ec9f89784528b34b0ce03934b22498fae3eaf999
Author: Filipp Gunbin <fgunbin@fastmail.fm>
Commit: Filipp Gunbin <fgunbin@fastmail.fm>
Add lisp/net/tramp-kubernetes.el
* lisp/net/tramp-kubernetes.el: New file.
* 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.
---
doc/misc/tramp.texi | 17 ++++----
etc/NEWS | 4 +-
lisp/net/tramp-compat.el | 3 ++
lisp/net/tramp-kubernetes.el | 102 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 115 insertions(+), 11 deletions(-)
diff --git a/doc/misc/tramp.texi b/doc/misc/tramp.texi
index a182c0510d..9e6de15f1f 100644
--- a/doc/misc/tramp.texi
+++ b/doc/misc/tramp.texi
@@ -912,6 +912,14 @@ Integration for Docker containers. A container is
accessed via
@samp{user} is the (optional) user that you want to use, and
@samp{container} is the id or name of the container.
+@item @option{kubernetes}
+@cindex method @option{kubernetes}
+@cindex @option{kubernetes} method
+
+Integration for Kubernetes pod containers. A container is accessed
+via @file{@trampfn{kubernetes,pod,/path/to/file}}, where @samp{pod} is
+the name of the pod. Currently, the first container in a pod is used.
+
@end table
@@ -1772,15 +1780,6 @@ They can be installed with Emacs's Package Manager.
This includes
@c @item ibuffer-tramp.el
@c Contact Svend Sorensen <svend@@ciffer.net>
-@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 d7bc4b0e0c..6104e71ed4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2409,8 +2409,8 @@ and friends.
** Tramp
+++
-*** New connection method "docker".
-It allows accessing environments provided by Docker.
+*** New connection methods "docker" and "kubernetes".
+They allows accessing environments provided by Docker / Kubernetes.
---
*** Tramp supports abbreviating remote home directories now.
diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el
index f6cc1034ca..c510626eca 100644
--- a/lisp/net/tramp-compat.el
+++ b/lisp/net/tramp-compat.el
@@ -53,6 +53,9 @@
(with-eval-after-load 'docker-tramp
(warn (concat "Package `docker-tramp' has been obsoleted, "
"please use integrated package `tramp-docker'")))
+(with-eval-after-load 'kubernetes-tramp
+ (warn (concat "Package `kubernetes-tramp' has been obsoleted, "
+ "please use integrated package `tramp-kubernetes'")))
;; 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-kubernetes.el b/lisp/net/tramp-kubernetes.el
new file mode 100644
index 0000000000..0ad333f818
--- /dev/null
+++ b/lisp/net/tramp-kubernetes.el
@@ -0,0 +1,102 @@
+;;; tramp-kubernetes.el --- Tramp integration for Kubernetes containers -*-
lexical-binding: t; -*-
+
+;; Copyright © 2022 Free Software Foundation, Inc.
+
+;; Keywords: comm, processes, kubernetes
+;; Package: tramp
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; `tramp-kubernetes' allows Tramp access to environments provided by
+;; Kubernetes.
+;;
+;; The kubectl program should be configured correctly. You may also
+;; want to set the namespace to use:
+;;
+;; kubectl config set-context --current --namespace=<my-namespace>
+;;
+;; ## Usage
+;;
+;; Open a file in 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
+;; connected to.
+
+;;; Code:
+
+(require 'tramp)
+
+;;;###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-kubernetes-method "kubernetes"
+ "Tramp method name to use to connect to Kubernetes containers.")
+
+;;;###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-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-methods)
+
+ (tramp-set-completion-function
+ tramp-kubernetes-method
+ '((tramp-kubernetes--completion-function ""))))
+
+(add-hook 'tramp-unload-hook
+ (lambda ()
+ (unload-feature 'tramp-kubernetes 'force)))
+
+(provide 'tramp-kubernetes)
+
+;;; tramp-kubernetes.el ends here