Skip to content

Commit

Permalink
Add resource-specific suffix for following logs (#230)
Browse files Browse the repository at this point in the history
Contributes to #155.
  • Loading branch information
jinnovation authored Jun 17, 2024
1 parent e7b7877 commit e3a3534
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
Binary file added docs/how-tos/img/kele-resource-logs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions docs/how-tos/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ separate buffer in a table. From here, you can:

You can press `g` in a `kele-get` buffer to re-fetch and refresh the current resource.

### Following logs

When `kele-resource` is called on a resource that supports logs, you can use the
`l` key to follow the logs for a single resource.

![](./img/kele-resource-logs.png)

## Contexts

![](./img/context-prefix.png)
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ With Kele, you can:
namespace](./how-tos/usage.md#contexts);
- [Fetch and display the manifest of a single
resource](./how-tos/usage.md#working-with-resources);
- [Follow logs for a single resource](./how-tos/usage.md#following-logs)
- [List collections of resources](./how-tos/usage.md#working-with-resources);
- Start and stop [proxy servers](./how-tos/usage.md#managing-proxy-servers);
- Use the menu bar to perform basic cluster/config management;
Expand Down
2 changes: 2 additions & 0 deletions docs/references/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ versioning][semver].
- `kele-list` now supports kind-specific columns. For example, listing
Deployments will now show columns `READY`, `UP-TO-DATE`, and `AVAILABLE`,
similar to `kubectl get deployments`
- `kele-resource` now has a keybinding to follow logs for resources that support
it

## 0.6.0

Expand Down
69 changes: 50 additions & 19 deletions kele.el
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pods."
(defvar kele--discovery-last-refresh-time nil
"Timestamp of last successful poll of the discovery cache.")

(defvar kele--loggable-kinds '("pods" "deployments" "jobs" "services")
"Resource kinds that can be passed to kubectl log.")

;; TODO (#80): Display in the `kele-get-mode' header what fields were filtered out
(defcustom kele-filtered-fields '((metadata managedFields)
(metadata annotations kubectl.kubernetes.io/last-applied-configuration))
Expand Down Expand Up @@ -1635,6 +1638,14 @@ prompting and the function simply returns the single option."
kind)
gvs)))))

(defun kele--get-gvk-arg ()
"Get the GVK to use for a command."
(-let* ((kind (kele--get-kind-arg))
(gv (kele--get-groupversion-arg kind))
((group version) (kele--groupversion-split gv)))
(kele--gvk-create :group group :version version :kind kind)))


(defvar kele--list-context nil
"The context corresponding to the current `kele-list-mode' buffer.")

Expand Down Expand Up @@ -1970,16 +1981,10 @@ to query for."
'warning))
(format "Don't have permission to get %s" .kind))))
(interactive
(-let* ((kind (kele--get-kind-arg))
(gv (kele--get-groupversion-arg kind))
((group version) (kele--groupversion-split gv))
(gvk (kele--gvk-create
:group group
:version version
:kind kind))
(-let* ((gvk (kele--get-gvk-arg))
(ns (kele--get-namespace-arg
:group-version gv
:kind kind
:group-version (kele--gv-string gvk)
:kind (oref gvk kind)
:permit-nil t
:use-default nil))
(cands (kele--fetch-resource-names gvk :namespace ns :context (kele--get-context-arg)))
Expand All @@ -1998,18 +2003,13 @@ CONTEXT and NAMESPACE are used to identify where the deployment lives."
(let-alist (oref transient--prefix scope)
(string-equal "deployments" .kind)))
(interactive
(let* ((context (kele--get-context-arg))
(let* ((gvk (kele--gvk-create :kind "deployments" :group "apps" :version "v1"))
(context (kele--get-context-arg))
(ns (kele--get-namespace-arg
:kind "deployments"
:group-version "apps/v1"
:kind (oref gvk kind)
:group-version (kele--gv-string gvk)
:use-default nil))
(cands (kele--fetch-resource-names
(kele--gvk-create
:group "apps"
:version "v1"
:kind "deployments")
:namespace ns
:context context))
(cands (kele--fetch-resource-names gvk :namespace ns :context context))
(name (completing-read "Deployment to restart: "
(-cut kele--resources-complete <> <> <> :cands cands))))
(list context ns name)))
Expand Down Expand Up @@ -2042,6 +2042,7 @@ CONTEXT and NAMESPACE are used to identify where the deployment lives."
(kele--get-kind-for-plural kele--global-discovery-cache it)
(propertize it 'face 'warning))
(propertize "-specific actions" 'face 'transient-heading))))
(kele-resource-follow-logs)
(kele-deployment-restart)]]

(interactive (let* ((context (kele-current-context-name))
Expand All @@ -2061,6 +2062,36 @@ CONTEXT and NAMESPACE are used to identify where the deployment lives."
(kind . ,kind)
(context . ,(kele-current-context-name)))))

;; TODO:
;; - `kele-log-mode-map' with bindings to change parameters and reload the buffer

;; FIXME: Known issues:
;; - Might not support multi-container pods
(transient-define-suffix kele-resource-follow-logs (context namespace gvk name)
:key "l"
:if
(lambda ()
(let-alist (oref transient--prefix scope)
(-contains? kele--loggable-kinds .kind)))
:description "Follow logs"
(interactive
(-let* ((gvk (kele--get-gvk-arg))
(ns (kele--get-namespace-arg
:group-version (kele--gv-string gvk)
:kind (oref gvk kind)
:use-default nil))
(cands (kele--fetch-resource-names gvk :namespace ns :context (kele--get-context-arg)))
(name (completing-read "Name: " (-cut kele--resources-complete <> <> <> :cands cands))))
(list (kele--get-context-arg) ns gvk name)))
(let* ((buf-name (format "*kele: logs: %s/%s*" (kele--string gvk) name))
(name-func (lambda (_) buf-name))
(cmd (format "kubectl logs --follow --context %s --namespace %s %s/%s"
context
namespace
(kele--gvk-kind gvk)
name)))
(compilation-start cmd t name-func)))

(transient-define-prefix kele-dispatch ()
"Work with Kubernetes clusters and configs."
["Work with..."
Expand Down

0 comments on commit e3a3534

Please sign in to comment.