From: Eshel Yaron Date: Wed, 18 Sep 2024 14:48:52 +0000 (+0200) Subject: New commands 'kubed-{transient-}scale-deployment' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c194308e8ba90787496dcea5bba2be63a64373cc;p=kubed.git New commands 'kubed-{transient-}scale-deployment' * kubed-transient.el (kubed-transient-scale-deployment): New transient. (kubed-transient): Bind it. * kubed.el (kubed-scale-deployment) (kubed-deployments-scale): New commands. * kubed.texi (Scale Deployment): New section. (Usage): Update menu. * NEWS.org: Announce new commands. --- diff --git a/NEWS.org b/NEWS.org index 676404b..e3b938d 100644 --- a/NEWS.org +++ b/NEWS.org @@ -10,6 +10,14 @@ for Kubernetes. For further details, see the Kubed manual: [[https://eshelyaron.com/sweep.html][https://eshelyaron.com/kubed.html]]. +* Version 0.4.1 on in development + +** New command and transient menu for scaling deployments + +You can now scale Kubernetes deployment with Kubed, directly with +~kubed-scale-deployment~, from the deployments table buffer, or via +the new transient menu ~kubed-transient-scale-deployment~. + * Version 0.4.0 on 2024-08-23 ** ~kubed-list-update~ is now bound to ~g~ in resource table buffers. diff --git a/kubed-transient.el b/kubed-transient.el index 60e519c..8d8fe17 100644 --- a/kubed-transient.el +++ b/kubed-transient.el @@ -69,7 +69,8 @@ Return an RFC3339 string representation of the selected date." ("d" "Diff" kubed-transient-diff) ("P" "Patch" kubed-transient-patch) ("R" "Rollout" kubed-transient-rollout)] - [("E" "Explain" kubed-explain) + [("$" "Scale" kubed-transient-scale-deployment) + ("E" "Explain" kubed-explain) ("!" "Command line" kubed-kubectl-command)]]) (defmacro kubed-transient-logs-for-resource (resource &optional plural) @@ -157,6 +158,24 @@ defaults to \"RESOURCEs\"." (transient-setup 'kubed-transient-logs nil nil :scope '("logs"))) +;;;###autoload (autoload 'kubed-transient-scale-deployment "kubed-transient" nil t) +(transient-define-prefix kubed-transient-scale-deployment () + "Scale Kubernetes deployments." + ["Kubernetes Scale Deployment\n" + ["Action" + ("$" "Scale" kubed-scale-deployment) + ("!" "Command line" kubed-kubectl-command)] + ["Options" + ("-n" "Namespace" "--namespace=" + :prompt "Namespace" :reader kubed-transient-read-namespace) + ("-C" "Context" "--context=" + :prompt "Context" :reader kubed-transient-read-context) + ("-r" "Replicas" "--replicas=" + :prompt "Number of replicas: " :reader transient-read-number-N+)]] + (interactive) + (transient-setup 'kubed-transient-scale-deployment nil nil + :scope '("scale" "deployment"))) + ;;;###autoload (autoload 'kubed-transient-rollout "kubed-transient" nil t) (transient-define-prefix kubed-transient-rollout () "Manage Kubernetes deployments." diff --git a/kubed.el b/kubed.el index 97ecdf9..d5b140e 100644 --- a/kubed.el +++ b/kubed.el @@ -1965,6 +1965,49 @@ NAMESPACE too. With a double prefix argument, also prompt for CONTEXT." "Whether to pop up a progress buffer when restarting Kubernetes deployments." :type 'boolean) +;;;###autoload +(defun kubed-scale-deployment (dep reps &optional context namespace) + "Scale deployment DEP in namespace NAMESPACE via CONTEXT to REPS replicas. + +Optional argument CONTEXT is the `kubectl' context to use, defaulting to +the current context; NAMESPACE is the namespace of DEP, defaulting to +the current namespace. + +Interactively, prompt for DEP. With a prefix argument, prompt for +NAMESPACE too. With a double prefix argument, also prompt for CONTEXT." + (interactive + (let (reps context namespace) + (dolist (arg (kubed-transient-args 'kubed-transient-rollout)) + (cond + ((string-match "--context=\\(.+\\)" arg) + (setq context (match-string 1 arg))) + ((string-match "--namespace=\\(.+\\)" arg) + (setq namespace (match-string 1 arg))) + ((string-match "--replicas=\\(.+\\)" arg) + (setq reps (string-to-number (match-string 1 arg)))))) + (unless context + (setq context + (let ((cxt (kubed-local-context))) + (if (equal current-prefix-arg '(16)) + (kubed-read-context "Context" cxt) + cxt)))) + (unless namespace + (setq namespace (kubed--namespace context current-prefix-arg))) + (list (kubed-read-deployment "Scale deployment" nil nil context namespace) + (or reps (read-number "Number of replicas: ")) context namespace))) + (let* ((context (or context (kubed-local-context))) + (namespace (or namespace (kubed--namespace context)))) + (unless (zerop + (apply #'call-process + kubed-kubectl-program nil nil nil + "scale" "deployment" dep + "--replicas" (number-to-string reps) + (append + (when namespace (list "-n" namespace)) + (when context (list "--context" context))))) + (user-error "Failed to scale Kubernetes deployment `%s'" dep)) + (message "Scaled Kubernetes deployment `%s' to %d replicas." dep reps))) + ;;;###autoload (defun kubed-restart-deployment (dep &optional context namespace) "Restart Kubernetes deployment DEP in namespace NAMESPACE via CONTEXT. @@ -2038,11 +2081,13 @@ NAMESPACE too. With a double prefix argument, also prompt for CONTEXT." :right-align t) ( creationtimestamp ".metadata.creationTimestamp" 20)) :prefix (("R" "Restart" kubed-restart-deployment) - ("W" "Watch" kubed-watch-deployment-status)) + ("W" "Watch" kubed-watch-deployment-status) + ("$" "Scale" kubed-scale-deployment)) :logs t :suffixes ([("L" "Logs" kubed-transient-logs-for-deployment) ("W" "Watch" kubed-deployments-watch) - ("R" "Restart" kubed-deployments-restart)]) + ("R" "Restart" kubed-deployments-restart) + ("$" "Scale" kubed-deployments-scale)]) :create ((name images &optional context namespace replicas port command) "Deploy IMAGES to Kubernetes in deployment with name NAME. @@ -2102,7 +2147,12 @@ optional command to run in the images." (message "Deployment restarting") (kubed-list-update t))) (watch "W" "Watch" (kubed-watch-deployment-status - deployment kubed-list-context kubed-list-namespace))) + deployment kubed-list-context kubed-list-namespace)) + (scale "$" "Scale" (kubed-scale-deployment + deployment (if current-prefix-arg + (prefix-numeric-value current-prefix-arg) + (read-number "Number of replicas: ")) + kubed-list-context kubed-list-namespace))) ;;;###autoload (autoload 'kubed-display-replicaset "kubed" nil t) ;;;###autoload (autoload 'kubed-edit-replicaset "kubed" nil t) diff --git a/kubed.texi b/kubed.texi index 228c1a5..0ba19f2 100644 --- a/kubed.texi +++ b/kubed.texi @@ -176,6 +176,7 @@ The following sections describe in detail the various Kubed commands. * Browse Resources:: * Context and Namespace:: * Transient Menus:: +* Scale Deployment:: @end menu @node Display Resource @@ -604,6 +605,35 @@ In Kubed resource list buffers, type @kbd{?} to pop up a transient menu with commands that are specific to the type of resources the buffer displays. @xref{Browse Resources}. +@node Scale Deployment +@section Scale Deployment + +You can use Kubed to @dfn{scale} Kubernetes a deployment, which means +setting the size of the deployment in terms of the number of replicas +it governs. + +@deffn Command kubed-scale-deployment +Scale a Kubernetes deployment. +@end deffn + +@code{kubed-scale-deployment} is bound to @kbd{$} in the +@code{kubed-deployment-prefix-map}, so if you have +@code{kubed-prefix-map} bound to @kbd{C-c k} then you can invoke it +with @kbd{C-c k d $}. @xref{Definition of kubed-prefix-map}. This +command prompts you for a deployment and then lets you select a new +number of replicas for that deployment. By default you are prompted +for deployment in the local context and namespace (@pxref{Context and +Namespace}); with a prefix argument, you can specify a different +namespace; with a double prefix argument (@kbd{C-u C-u}), you can +specify a different context too. + +In the deployments list buffer, you can scale the deployment at point +by simply typing @kbd{$}. @xref{Browse Resources}. + +Lastly, you can scale deployments using the transient menu +@code{kubed-transient-scale-deployment}, which is available from the +main Kubed transient by pressing @kbd{$}. @xref{Transient Menus}. + @node Extending Kubed @chapter Extending Kubed