From: Eshel Yaron Date: Thu, 25 Jul 2024 16:07:32 +0000 (+0200) Subject: New command 'kubed-run' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=a835687464ae315d5796121c1cef1d0c578ff7ef;p=emacs.git New command 'kubed-run' --- diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index 1d2a206c566..885a2581dc8 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -1124,6 +1124,83 @@ completion candidates." "create" "-f" (expand-file-name definition) "-o" "jsonpath={.metadata.name}"))))) +;;;###autoload +(defun kubed-run + (pod image &optional namespace port attach stdin tty rm envs command args) + "Run IMAGE in Kubernetes POD. + +Optional argument NAMESPACE is the namespace to use for the created pod, +defaulting to the current namespace. PORT is the port to expose, +defaulting to none. If ATTACH is non-nil, then attach to the craeted +image with a `comint-mode' buffer, and pop to that buffer. Non-nil +STDIN says to keep the standard input of the container open; non-nil TTY +says to allocate a TTY for the container; and non-nil RM says to remove +the container after it exits. ENVS is a list of strings \"VAR=VAL\" +which specify environment variables VAR and values VAL to give them in +the created container. ARGS are command line arguments for the +container command. If COMMAND is non-nil, ARGS consist of a complete +command line, that overrides the container command instead of just +providing it with arguments." + (interactive + (let ((pod (read-string "Run image in pod with name: ")) + (image nil) (port nil) (namespace nil) (attach nil) (stdin nil) + (tty nil) (rm nil) (envs nil) (command nil) (args nil)) + (dolist (arg (when (and (fboundp 'transient-args) + (fboundp 'kubed-transient-run)) + (transient-args 'kubed-transient-run))) + (cond + ((string-match "--image=\\(.+\\)" arg) + (setq image (match-string 1 arg))) + ((string-match "--port=\\(.+\\)" arg) + (setq port (string-to-number (match-string 1 arg)))) + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((equal "--attach" arg) (setq attach t)) + ((equal "--stdin" arg) (setq stdin t)) + ((equal "--tty" arg) (setq tty t)) + ((equal "--rm" arg) (setq rm t)) + ((equal "--command" arg) (setq command t)) + ((string-match "--env=\\(.+\\)" arg) + (push (match-string 1 arg) envs)) + ((string-match "-- =\\(.+\\)" arg) + (setq args (split-string-and-unquote (match-string 1 arg)))))) + (unless image + (setq image (read-string "Image to run: " nil 'kubed-container-images-history))) + (list pod image namespace port attach stdin tty rm envs command args))) + (if attach + (pop-to-buffer + (apply #'make-comint "kubed-run" kubed-kubectl-program nil + "run" pod (concat "--image=" image) "--attach" + (append + (mapcar (lambda (env) (concat "--env=" env)) + (cons "TERM=dumb" envs)) + (when namespace (list (concat "--namespace=" namespace))) + (when stdin '("-i")) + (when tty '("-t")) + (when rm '("--rm")) + (when port (list (format "--port=%d" port))) + (when command '("--command")) + (when args (cons "--" args))))) + (unless (zerop + (apply #'call-process + kubed-kubectl-program nil nil nil + "run" pod (concat "--image=" image) + (append + (mapcar (lambda (env) (concat "--env=" env)) envs) + (when namespace (list (concat "--namespace=" namespace))) + (when stdin '("-i")) + (cond + (attach '("--attach")) + (stdin '("--attach=false"))) + (when tty '("-t")) + (when rm '("--rm")) + (when port (list (format "--port=%d" port))) + (when command '("--command")) + (when args (cons "--" args))))) + (user-error "Failed to run image `%s'" image)) + (message "Image `%s' is now running in pod `%s'." image pod)) + (kubed-update-pods)) + (defun kubed-pod-containers (pod &optional k8sns) "Return list of containers in Kubernetes pod POD in namespace K8SNS." (string-split @@ -1312,6 +1389,7 @@ Interactively, prompt for COMMAND with completion for `kubectl' arguments." "C" #'kubed-use-context "A" #'kubed-all-namespaces-mode "+" #'kubed-create + "R" #'kubed-run "!" #'kubed-kubectl-command) (provide 'kubed)