From 499b24215016bb1a306bb5bf5e7959f8a8a5a21b Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Sat, 27 Jul 2024 17:06:46 +0200 Subject: [PATCH] kubed.el: Refine 'kubed-create-cronjob' --- lisp/net/kubed-transient.el | 42 +++++++++++++++++++++++++++---------- lisp/net/kubed.el | 42 ++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/lisp/net/kubed-transient.el b/lisp/net/kubed-transient.el index 5031869877b..0ee2feb2936 100644 --- a/lisp/net/kubed-transient.el +++ b/lisp/net/kubed-transient.el @@ -131,6 +131,19 @@ (transient-setup 'kubed-transient-run nil nil :scope '("run"))) +;;;###autoload +(transient-define-prefix kubed-transient-apply () + "Apply configuration to Kubernetes resource." + ["Options" + ("-f" "Definition file" "--filename=" + :reader kubed-transient-read-resource-definition-file-name)] + ["Actions" + ("*" "apply" kubed-apply) + ("!" "Command line" kubed-kubectl-command)] + (interactive) + (transient-setup 'kubed-transient-apply nil nil + :scope '("apply"))) + ;;;###autoload (transient-define-prefix kubed-transient-create () "Create Kubernetes resource." @@ -139,26 +152,33 @@ :reader kubed-transient-read-resource-definition-file-name)] ["Kinds" ("d" "deployment" kubed-transient-create-deployment) - ("n" "namespace" kubed-create-namespace)] + ("n" "namespace" kubed-create-namespace) + ("c" "cronjob" kubed-transient-create-cronjob)] ["Actions" - ("c" "Create" kubed-create) + ("+" "Create" kubed-create) ("!" "Command line" kubed-kubectl-command)] (interactive) (transient-setup 'kubed-transient-create nil nil :scope '("create"))) ;;;###autoload -(transient-define-prefix kubed-transient-apply () - "Apply configuration to Kubernetes resource." +(transient-define-prefix kubed-transient-create-cronjob () + "Create Kubernetes cronjob." ["Options" - ("-f" "Definition file" "--filename=" - :reader kubed-transient-read-resource-definition-file-name)] + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace) + ("-I" "Image" "--image=" + :prompt "Image to run: ") + ("-S" "Schedule" "--schedule=" + :prompt "Cron schedule: ") + ("--" "Command" "-- =" + :prompt "Command: ")] ["Actions" - ("*" "apply" kubed-apply) + ("+" "Create" kubed-create-cronjob) ("!" "Command line" kubed-kubectl-command)] (interactive) - (transient-setup 'kubed-transient-apply nil nil - :scope '("apply"))) + (transient-setup 'kubed-transient-create-cronjob nil nil + :scope '("create" "cronjob"))) ;;;###autoload (transient-define-prefix kubed-transient-create-deployment () @@ -168,7 +188,7 @@ :prompt "Namespace" :reader kubed-transient-read-namespace) ("-r" "Replicas" "--replicas=" :prompt "Number of replicas: " :reader transient-read-number-N+) - ("-i" "Image" "--image=" + ("-I" "Image" "--image=" :prompt "Images to deploy: " :multi-value repeat) ("-p" "Port" "--port=" @@ -176,7 +196,7 @@ ("--" "Command" "-- =" :prompt "Command: ")] ["Actions" - ("c" "Create" kubed-create) + ("+" "Create" kubed-create-deployment) ("!" "Command line" kubed-kubectl-command)] (interactive) (transient-setup 'kubed-transient-create-deployment nil nil diff --git a/lisp/net/kubed.el b/lisp/net/kubed.el index 93601a78074..6e75be640cf 100644 --- a/lisp/net/kubed.el +++ b/lisp/net/kubed.el @@ -1094,7 +1094,47 @@ optional command to run in the images." (suspend ".spec.suspend" 20) (lastschedule ".status.lastScheduleTime" 20) (lastsuccess ".status.lastSuccessfulTime" 20) - (activejob ".status.active[0].name" 36))) + (activejob ".status.active[0].name" 36)) + :create + ((name image schedule &optional namespace command) + "Schedule IMAGE to run in a cronjon with name NAME according to SCHEDULE. + +Optional argument NAMESPACE is the namespace to use for the deployment, +defaulting to the current namespace. COMMAND is a list of strings that +represent a program followed by its arguments, if it non-nil then it +overrides the default command IMAGE runs." + (interactive + (let ((name (read-string "Create cronjob with name: ")) + (image nil) + (schedule nil) + (command nil) + (namespace nil)) + (dolist (arg (kubed-transient-args 'kubed-transient-create-cronjob)) + (cond + ((string-match "--image=\\(.+\\)" arg) + (setq image (match-string 1 arg))) + ((string-match "--schedule=\\(.+\\)" arg) + (setq schedule (match-string 1 arg))) + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "-- =\\(.+\\)" arg) + (setq command (split-string-and-unquote (match-string 1 arg)))))) + (unless image + (setq image (read-string "Image to run: " nil 'kubed-container-images-history))) + (unless schedule + (setq schedule (read-string "Cron schedule: " "* * * * *"))) + (list name image schedule namespace command))) + (unless (zerop + (apply #'call-process + kubed-kubectl-program nil nil nil + "create" "cronjob" name + "--image" image "--schedule" schedule + (append + (when namespace (list "--namespace" namespace)) + (when command (cons "--" command))))) + (user-error "Failed to create Kubernetes cronjob `%s'" name)) + (message "Created Kubernetes cronjob `%s'." name) + (kubed-update-cronjobs))) ;; TODO: Events may be numerous. Need to only get a few. ;; ;;;###autoload (autoload 'kubed-list-events "kubed" nil t) -- 2.39.2