Skip to content

Commit

Permalink
feature: suffix for restarting deployment (#188)
Browse files Browse the repository at this point in the history
Closes #187.

This PR introduces the notion of kind-specific suffixes. A dedicated
collection of suffixes for `kele-resource` will show up depending on the
resource kind selected and whether or not special-case actions have been
defined on them.

This PR introduces the first such "special-case action" in Kele. Namely,
for Deployments, we build a suffix for restarting them, leveraging
`kubectl rollout restart` under the hood.
  • Loading branch information
jinnovation authored Apr 15, 2024
1 parent 60c1a94 commit 8fa0056
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Binary file added docs/how-tos/img/resource-prefix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions docs/how-tos/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ to accomplish, reach for `kele-dispatch`.

## Working with Resources

Interacting with resources in Kele centers around the `s-k r` prefix, which is
bound to `kele-resource`.

`kele-resource` allows you to act on specific resource kinds. With
`kele-resource`, you can, for example:
`s-k r` will invoke `kele-resource`, allowing you to act on specific resource
kinds. With `kele-resource`, you can, for example:

- Look up a given Kubernetes object by name, fetch its manifest, and display it
in a separate buffer;
- List out all resources of a given type.

![](./img/resource-prefix.png)

!!! tip inline ""

`kele-resource` supports [custom resources] too!
Expand All @@ -51,6 +50,12 @@ work with, after which you can choose to **get** a specific object of that kind
by name. If the resource is namespaced, you will also be presented with the
option to choose the namespace to select from.

Certain resource kinds have actions unique to them. When you select a resource
kind that has such actions defined, these are presented in a dedicated section
with their own key bindings. For example, `s-k r "deployments"` will, in
addition to the general actions for getting and listing Deployments, also allow
you to restart a specific Deployment.

### Getting a single resource

You can get a single resource of the given kind with:
Expand Down
6 changes: 6 additions & 0 deletions docs/references/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ versioning][semver].

## Unreleased

### Added

- Implemented `kele-deployment-restart` for restarting Deployments.
- `kele-resource` now has a dedicated section for **kind-specific actions** that
populates based on the resource kind you selected.

### Changed

- Added dependency `memoize`
Expand Down
52 changes: 51 additions & 1 deletion kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -1676,17 +1676,67 @@ instead of \"pod.\""
:namespace namespace
:context context))))

(transient-define-suffix kele-deployment-restart (context namespace deployment-name)
"Restart DEPLOYMENT-NAME.
CONTEXT and NAMESPACE are used to identify where the deployment lives."
:key "R"
:description "Restart a deployment"
:if
(lambda ()
(let-alist (oref transient--prefix scope)
(string-equal "deployments" .kind)))
(interactive
(let* ((context (kele--get-context-arg))
(ns (kele--get-namespace-arg
:kind "deployments"
:group-version "apps/v1"
:use-default nil))
(cands (kele--fetch-resource-names
"apps"
"v1"
"deployments"
:namespace ns
:context context))
(name (completing-read "Deployment to restart: "
(-cut kele--resources-complete <> <> <> :cands cands))))
(list context ns name)))
;; TODO: Ask user for confirmation?
(with-temp-buffer
(let ((exit-code (call-process kele-kubectl-executable
nil
(current-buffer)
nil
"rollout"
"restart"
(format "deployment/%s" deployment-name)
(format "--context=%s" context)
(format "--namespace=%s" namespace))))
(if (= 0 exit-code)
(message (buffer-string))
(error (buffer-string))))))

(transient-define-prefix kele-resource (group-versions kind)
"Work with Kubernetes resources."
["Arguments"
(kele--context-infix)
(kele--groupversions-infix)
(kele--namespace-infix)]

["Actions"
[["General Actions"
(kele-get)
(kele-list)]

[:description
(lambda ()
(string-join
(list (--> (oref transient--prefix scope)
(alist-get 'kind it)
(capitalize it)
(propertize it 'face 'warning))
(propertize "-specific actions" 'face 'transient-heading))))
(kele-deployment-restart)]]

(interactive (let* ((context (kele-current-context-name))
(kind (completing-read
(format "Choose a kind to work with (context: `%s'): " context)
Expand Down

0 comments on commit 8fa0056

Please sign in to comment.