]> git.eshelyaron.com Git - emacs.git/commitdiff
New command 'kubed-run'
authorEshel Yaron <me@eshelyaron.com>
Thu, 25 Jul 2024 16:07:32 +0000 (18:07 +0200)
committerEshel Yaron <me@eshelyaron.com>
Thu, 25 Jul 2024 18:54:08 +0000 (20:54 +0200)
lisp/net/kubed.el

index 1d2a206c566da4cdeb62031617d0e2f679147790..885a2581dc8e7d611adae495ddcefe8555b9054e 100644 (file)
@@ -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)