├── screenshot.png ├── .dir-locals.el ├── test └── kubel-test.el ├── .circleci ├── make.el └── config.yml ├── README.md ├── kubel-evil.el ├── CHANGELOG.md ├── LICENSE └── kubel.el /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abrochard/kubel/HEAD/screenshot.png -------------------------------------------------------------------------------- /.dir-locals.el: -------------------------------------------------------------------------------- 1 | ;;; Directory Local Variables 2 | ;;; For more information see (info "(emacs) Directory Variables") 3 | 4 | ((emacs-lisp-mode . ((indent-tabs-mode . nil)))) 5 | -------------------------------------------------------------------------------- /test/kubel-test.el: -------------------------------------------------------------------------------- 1 | ;;; package --- Summary 2 | 3 | ;;; Commentary: 4 | 5 | ;;; Code: 6 | 7 | (require 'ert) 8 | (require 'cl-macs) 9 | 10 | (ert-deftest kubel--test-extract-value () 11 | (let ((line "0123456789")) 12 | (should (equal "0" (kubel--extract-value line 0 1))) 13 | (should (equal "0123" (kubel--extract-value line 0 4))) 14 | (should (equal "34567" (kubel--extract-value line 3 8))) 15 | (should (equal "56789" (kubel--extract-value line 5 "end"))) 16 | (should (equal "-" (kubel--extract-value " " 0 5))) 17 | (should (equal "-" (kubel--extract-value " " 0 "end"))))) 18 | 19 | ;; (ert "kubel--test-.*") 20 | 21 | (provide 'kubel-test) 22 | ;;; kubel-test.el ends here 23 | -------------------------------------------------------------------------------- /.circleci/make.el: -------------------------------------------------------------------------------- 1 | (require 'package) 2 | 3 | (defconst make-packages 4 | '(transient dash yaml-mode s)) 5 | 6 | (defun make-init () 7 | (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) 8 | (package-initialize)) 9 | 10 | (defun make-install-packages () 11 | (make-init) 12 | (package-refresh-contents) 13 | (package-install 'package-lint) 14 | (dolist (pkg make-packages) 15 | (package-install pkg))) 16 | 17 | (defun make-ert () 18 | (make-init) 19 | (load-file "/kubel/kubel.el") 20 | (load-file "/kubel/test/kubel-test.el") 21 | (ert-run-tests-batch-and-exit)) 22 | 23 | (defun make-compile () 24 | (make-init) 25 | (setq byte-compile-error-on-warn nil) 26 | (batch-byte-compile)) 27 | 28 | (defun make-lint () 29 | (make-init) 30 | (require 'package-lint) 31 | (setq package-lint-batch-fail-on-warnings t) 32 | (package-lint-batch-and-exit)) 33 | 34 | (provide 'make) 35 | ;;; make.el ends here 36 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | steps: &steps 4 | working_directory: /kubel 5 | steps: 6 | - checkout 7 | - run: 8 | name: Install packages 9 | command: | 10 | emacs -Q --batch -l .circleci/make.el --funcall make-install-packages 11 | 12 | - run: 13 | name: ERT tests 14 | command: | 15 | emacs -Q --batch -l .circleci/make.el --funcall make-ert 16 | 17 | - run: 18 | name: Compile 19 | command: | 20 | emacs -Q --batch -l .circleci/make.el --funcall make-compile kubel.el 21 | 22 | - run: 23 | name: Lint 24 | command: | 25 | emacs -Q --batch -l .circleci/make.el --funcall make-lint kubel.el 26 | 27 | jobs: 28 | emacs-29: 29 | docker: 30 | - image: silex/emacs:29.1-ci 31 | <<: *steps 32 | emacs-28: 33 | docker: 34 | - image: silex/emacs:28.2-ci 35 | <<: *steps 36 | emacs-27: 37 | docker: 38 | - image: silex/emacs:27.2-ci 39 | <<: *steps 40 | emacs-26: 41 | docker: 42 | - image: silex/emacs:26.3-ci 43 | <<: *steps 44 | 45 | workflows: 46 | version: 2 47 | build: 48 | jobs: 49 | - emacs-26 50 | - emacs-27 51 | - emacs-28 52 | - emacs-29 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![MELPA](https://melpa.org/packages/kubel-badge.svg)](https://melpa.org/#/kubel) 2 | [![abrochard](https://circleci.com/gh/abrochard/kubel.svg?style=svg)](https://app.circleci.com/pipelines/github/abrochard/kubel) 3 | 4 | # kubel 5 | 6 | Emacs extension for controlling Kubernetes with limited permissions. 7 | 8 | ![screenshot](screenshot.png) 9 | 10 | You can [watch how kubel started](https://www.youtube.com/watch?v=w3krYEeqnyk) or [read about it](https://gist.github.com/abrochard/dd610fc4673593b7cbce7a0176d897de). 11 | 12 | ## Features 13 | We now support managing pretty much any resource! 14 | 15 | - switch context and namespace 16 | - show any resource (pods/services/deployments/etc) 17 | - highlight a resource by name 18 | - copy resource name to clipboard 19 | - show and edit resource details 20 | - show rollout history for a resource 21 | - delete a resource 22 | - tail container logs (possibly with `-f` follow flag) 23 | - copy container log command to clipboard 24 | - port forward a pod to your localhost 25 | - exec into a pod using tramp 26 | - quick run shell-command 27 | - scale replicas 28 | - multiple kubel buffers, each one with different context, namespace, and resource. 29 | 30 | ## Installation 31 | 32 | Get it from Melpa, or copy and load the `kubel.el` file. 33 | 34 | If you want to have the evil compatibility package, get it from Melpa as well or 35 | load the `kubel-evil.el` file. 36 | 37 | ## Setup 38 | 39 | ```elisp 40 | (require 'kubel) 41 | (kubel-vterm-setup) ; If you wish to have vterm as an option when using exec 42 | ``` 43 | 44 | Or if one uses `use-package` 45 | 46 | ```elisp 47 | (use-package kubel 48 | :after (vterm) 49 | :config (kubel-vterm-setup)) 50 | ``` 51 | 52 | ## Usage 53 | 54 | To list the pods in your current context and namespace, call 55 | ``` 56 | M-x kubel 57 | ``` 58 | To set said namespace and context, respectively call 59 | ``` 60 | M-x kubel-set-namespace 61 | M-x kubel-set-context 62 | ``` 63 | Note that context will autocomplete but not necessarily namespaces 64 | depending on your permissions and cluster setup. 65 | See the [customize section](#Customize) on how to tune `kubel-use-namespace-list`. 66 | 67 | To switch to showing a different resource, use the `R` command or 68 | ``` 69 | M-x kubel-set-resource 70 | ``` 71 | This will let you select a resource and re-display the kubel buffer. 72 | 73 | 74 | To programmatically open a session for a specific context/namespace/resource, call `kubel-open` from elisp 75 | 76 | ```lisp 77 | (kubel-open "" "" "") 78 | ``` 79 | 80 | Each kubel buffer will automatically be renamed using the following template: 81 | ``` 82 | *kubel session: ||||* 83 | ``` 84 | 85 | ## Shortcuts 86 | 87 | On the kubel screen, place your cursor on a resource 88 | ``` 89 | enter => get resource details 90 | C-u enter => describe resource 91 | h => help popup 92 | ? => help popup 93 | ! => quick run shell-command 94 | E => quick edit any resource 95 | g => refresh 96 | k => delete popup 97 | r => see the rollout history for resource 98 | p => port forward pod 99 | l => log popup 100 | e => exec popup 101 | j => jab deployment to force rolling update 102 | S => scale resource replicas 103 | C => set context 104 | n => set namespace 105 | R => set resource 106 | K => set kubectl config file 107 | F => set output format 108 | f => set a substring filter for resource name 109 | M-n => jump to the next highlighted resource 110 | M-p => jump to previous highlighted resource 111 | m => mark item 112 | u => unmark item 113 | M => mark all items 114 | U => unmark all items 115 | c => copy popup 116 | $ => show process buffer 117 | s => show only resources with specified label value 118 | ``` 119 | 120 | ## Editing a resource 121 | 122 | After describing a resource in YAML format, you can edit the buffer and hit `C-c C-c` to apply your changes. 123 | 124 | For example, if you want to edit a deployment, you can switch the resource with `R` and select "Deployments", then press return with the cursor on the row you wish to edit, edit the buffer, and hit `C-c C-c` to apply. 125 | 126 | Alternatively, you can hit `E` to then select the resource type and the resource name of what you want to edit. 127 | 128 | ## Customize 129 | 130 | - By default, kubel log tails from the last 100 lines, you can change the `kubel-log-tail-n` variable to set another line number. 131 | - If you need to switch or set your kubectl config file by setting your `KUBECONFIG` environment variable, you can use the wrapper function `kubel-set-kubectl-config-file` or the `K` shortcut. 132 | - Namespace listing for auto-completion is controlled by `kubel-use-namespace-list`: 133 | - auto - default, use `kubectl auth can-i list namespace` to determine if we can list namespaces 134 | - on - always assume we can list namespaces 135 | - off - always assume we cannot list namespaces 136 | 137 | ## Releases 138 | 139 | See the [CHANGELOG.md](CHANGELOG.md) for the list of changes and maybe upcoming features. 140 | -------------------------------------------------------------------------------- /kubel-evil.el: -------------------------------------------------------------------------------- 1 | ;;; kubel-evil.el --- extension for kubel to provide evil keybindings -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2019, Marcel Patzwahl 4 | 5 | ;; This file is NOT part of Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2 of the 10 | ;; License, or (at your option) any later version. 11 | 12 | ;; This program is distributed in the hope that it will be useful, but 13 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | ;; General Public License for more details. 16 | 17 | ;; You should have received a copy of the GNU General Public License 18 | ;; along with this program; if not, write to the Free Software 19 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 20 | ;; USA 21 | 22 | ;; Version: 1.0 23 | ;; Author: Marcel Patzwahl 24 | ;; Keywords: kubernetes k8s tools processes evil keybindings 25 | ;; URL: https://github.com/abrochard/kubel 26 | ;; License: GNU General Public License >= 3 27 | ;; Package-Requires: ((kubel "1.0") (evil "1.0") (emacs "25.3")) 28 | 29 | ;;; Commentary: 30 | 31 | ;; Emacs extension for controlling Kubel with evil keybindings. 32 | 33 | ;;; Shortcuts: 34 | 35 | ;; On the kubel screen, place your cursor on the pod 36 | ;; 37 | ;; enter => get resource details 38 | ;; h => help popup 39 | ;; C => set context 40 | ;; n => set namespace 41 | ;; R => set resource 42 | ;; g => refresh pods 43 | ;; E => quick edit 44 | ;; p => port forward pod 45 | ;; e => exec into pod 46 | ;; o => describe popup 47 | ;; l => log popup 48 | ;; c => copy popup 49 | ;; d => delete pod 50 | ;; a => jab deployment to force rolling update 51 | ;; S => scale replicas 52 | ;; 53 | 54 | ;;; Customize: 55 | (require 'evil) 56 | (require 'kubel) 57 | 58 | (defgroup kubel-evil nil 59 | "Provides integration of kubel and evil." 60 | :group 'kubel 61 | :prefix "kubel-evil-") 62 | 63 | (defvar kubel-evil-mode-map (make-sparse-keymap)) 64 | 65 | ;;; Code: 66 | (define-minor-mode kubel-evil-mode 67 | "Brings evil keybindings to kubel" 68 | :lighter " kubel-evil" 69 | :keymap kubel-evil-mode-map 70 | :group 'kubel-evil) 71 | 72 | (add-hook 'kubel-mode-hook 'kubel-evil-mode) 73 | 74 | (transient-define-prefix kubel-evil-help-popup () 75 | "Kubel Evil Menu" 76 | [["Actions" 77 | ;; global 78 | ("RET" "Resource details" kubel-describe-popup) 79 | ("E" "Quick edit" kubel-quick-edit) 80 | ("g" "Refresh" kubel-refresh) 81 | ("d" "Delete" kubel-delete-popup) ;; can't use k here 82 | ("r" "Rollout" kubel-rollout-history)] 83 | ["" ;; based on current view 84 | ("p" "Port forward" kubel-port-forward-pod) 85 | ("l" "Logs" kubel-log-popup) 86 | ("e" "Exec" kubel-exec-popup) 87 | ("a" "Jab" kubel-jab-deployment) ;; can't use j here 88 | ("S" "Scale replicas" kubel-scale-replicas)] 89 | ["Settings" 90 | ("C" "Set context" kubel-set-context) 91 | ("n" "Set namespace" kubel-set-namespace) 92 | ("R" "Set resource" kubel-set-resource) 93 | ("K" "Set kubectl config file" kubel-set-kubectl-config-file) 94 | ("F" "Set output format" kubel-set-output-format)] 95 | ["Filter" 96 | ("f" "Filter" kubel-set-filter) 97 | ("M-n" "Next highlight" kubel-jump-to-next-highlight) 98 | ("M-p" "Previous highlight" kubel-jump-to-previous-highlight)] 99 | ["Marking" 100 | ("m" "Mark item" kubel-mark-item) 101 | ("u" "Unmark item" kubel-unmark-item) 102 | ("M" "Mark all items" kubel-mark-all) 103 | ("U" "Unmark all items" kubel-unmark-all)] 104 | ["Utilities" 105 | ("c" "Copy to clipboad..." kubel-copy-popup) 106 | ("$" "Show Process buffer" kubel-show-process-buffer)]]) 107 | 108 | (evil-set-initial-state 'kubel-mode 'motion) 109 | 110 | (evil-define-key 'motion kubel-evil-mode-map 111 | (kbd "RET") #'kubel-get-resource-details 112 | (kbd "K") #'kubel-set-kubectl-config-file 113 | (kbd "C") #'kubel-set-context 114 | (kbd "n") #'kubel-set-namespace 115 | (kbd "g") #'kubel-refresh 116 | (kbd "h") #'kubel-evil-help-popup 117 | (kbd "?") #'kubel-evil-help-popup 118 | (kbd "F") #'kubel-set-output-format 119 | (kbd "R") #'kubel-set-resource 120 | (kbd "d") #'kubel-delete-popup 121 | (kbd "f") #'kubel-set-filter 122 | (kbd "r") #'kubel-rollout-history 123 | (kbd "E") #'kubel-quick-edit 124 | (kbd "M-n") #'kubel-jump-to-next-highlight 125 | (kbd "M-p") #'kubel-jump-to-previous-highlight 126 | (kbd "$") #'kubel-show-process-buffer 127 | (kbd "p") #'kubel-port-forward-pod 128 | (kbd "l") #'kubel-log-popup 129 | (kbd "c") #'kubel-copy-popup 130 | (kbd "e") #'kubel-exec-popup 131 | (kbd "a") #'kubel-jab-deployment 132 | (kbd "S") #'kubel-scale-replicas 133 | (kbd "u") #'kubel-unmark-item 134 | (kbd "M") #'kubel-mark-all 135 | (kbd "U") #'kubel-unmark-all) 136 | 137 | (provide 'kubel-evil) 138 | 139 | ;;; kubel-evil.el ends here 140 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | ## [Unreleased] 5 | - more unit tests 6 | - `q` to exit resource details (`quit-window` or `kill-buffer`) 7 | - show diff and confirmation before applying a change 8 | - easily get logs for a previous pod instance 9 | - top po command + sort by usage 10 | - safe way to help with rollbacks? 11 | - run `kubectl describe pod` 12 | - `kubectl` apply a buffer to current context/namespace 13 | 14 | ## [3.1.0] - 15 | ### Added 16 | - Support for multiple kubel buffers, each one with different context, namespace, and resource 17 | - Support for customization of shell buffer names 18 | - `kubel-kill-buffer` command to cancel an edit buffer without applying changes 19 | - Support for "wide" list view 20 | - Support for `eat` terminal emulator 21 | - `kubel-set-namespace` support for optional parameter to refresh the namespace cache 22 | - Custom default namespace `kubel-default-namespace` with default to `"default"` 23 | 24 | ### Changed 25 | - Remove suffix "(default)" when acting on resources of StorageClasses 26 | - Better parsing of the kubectl version string via JSON 27 | - Fix describe resources not going to top of buffer 28 | - using -- for all kubectl exec uses 29 | - Fix `kubel--exec` doesn't work on remote systems 30 | - Fix `dir-prefix` for TRAMP without explicitly specifically the username 31 | - vterm sessions to use their own buffers 32 | - case consistent resource names ("pods) 33 | - Fix: don't assume that kubectl will use the namespace default implictely 34 | 35 | ### Removed 36 | - `kubel-kubernetes-version` no longer cares about version patch 37 | - support for kubernetes v1.13 and below 38 | - support for emacs 25 and below 39 | 40 | ## [3.0.0] - 2022-09-09 41 | ### Added 42 | - tailing logs of init container 43 | - exec directly with shell & eshell 44 | - "evicted" state to status list 45 | - kubel process buffer (bound to `$`) to log executed `kubectl` commands and exit codes with errors 46 | - `c C` to copy last command ran to clipboard 47 | - `kubel--exec-to-string` to replace `shell-exec-to-string` and log to process buffer 48 | - can mark multiple items and perform bulk action: 49 | - delete multiple items of the same resource (like multiple pods) 50 | - tail logs of multiple pods 51 | - jab multiple deployments 52 | - `s` command to filter by selector 53 | - added context to fetch api-resources 54 | - support selecting container on exec function 55 | - `kubel-exec-pod-by-shell-command` by using shell-command we can quickly run a command 56 | - support multiple words column headers (and fix output parsing for cronjobs.batch) 57 | - `kubel-scale-replicas` to scale a deployment, replica set, replication controller, or stateful set 58 | - proper sort by duration for "AGE", "DURATION", and "LAST SCHEDULE" columns 59 | - vterm support for `kubectl exec` 60 | - ansi-term support for `kubectl exec` 61 | - `kubel-open` function to programatically open to a specific context/namespace/resource 62 | 63 | ### Changed 64 | - `e` keybinding now opens a popup for exec options 65 | - `kubel--exec` is now completely async 66 | - support selecting container on exec function 67 | - pod label parsing updated for newer version of k8s 68 | - empty space "" from showing up in selection candidates 69 | - fix position calculations in kubel--parse-body 70 | - string regex to ignore warning on `can-i` list namespace 71 | - update transient's definition macros by their new names 72 | - use `kubel-kubectl` variable to find `kubectl` path rather than hardcode it 73 | - kill kubel buffer after prompting for new the context 74 | - changed kubel-exec-vterm-pod to use to /bin/sh for stability 75 | - defcustom :type entries 76 | - status color is now derived from theme 77 | - better stderr handling for context selection 78 | - better line tracking between refreshes using tabulated-list-mode 79 | - vterm dep is now optional 80 | 81 | ### Removed 82 | - remove deprecated function bound to `d` 83 | - use of `goto-line` outside of interactive use 84 | 85 | ## [2.1.0] - 2020-11-16 86 | ### Added 87 | - CI tests 88 | - dynamically list namespaces if permissions allow for it 89 | - function to set the kubeconfig 90 | - caching of kubectl version & resource lists 91 | - support to operate remote cluster via TRAMP 92 | - support to tail logs by pod label 93 | - port forwarding prompt supports local:container port format 94 | 95 | ### Changed 96 | - log buffer is readonly by default 97 | - ask y-or-n before applying resource 98 | - re-use same window when switching namespace 99 | 100 | ## [2.0.0] - 2020-03-12 101 | ### Added 102 | - show any resource with some color coding 103 | - resource selector with kubectl version awareness 104 | - context based functions (log, port-forward, etc) 105 | - quick edit any resource from any context 106 | - can set output format 107 | - can delete any resource 108 | - jump to next and previous highlighted resource shortcuts 109 | - save line position before refreshing so cursor doesn't jump back to top 110 | 111 | ### Changed 112 | - limited resource editing capability to work with the resource selecting capability 113 | 114 | ### Removed 115 | - `d` keybinding to edit a resource 116 | 117 | ## [1.0.0] - 2020-02-22 118 | ### Added 119 | - show pods with color coding 120 | - multiple context & namespaces with limited permissions 121 | - copy to clipboard functions 122 | - pod logs functions 123 | - port forward 124 | - rollout history 125 | - tramp exec 126 | - some resource editing capabilities 127 | - pod deletion 128 | - regex based filter for highlighting a row 129 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /kubel.el: -------------------------------------------------------------------------------- 1 | ;;; kubel.el --- Control Kubernetes with limited permissions -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2018, Adrien Brochard 4 | 5 | ;; This file is NOT part of Emacs. 6 | 7 | ;; This program is free software; you can redistribute it and/or 8 | ;; modify it under the terms of the GNU General Public License as 9 | ;; published by the Free Software Foundation; either version 2 of the 10 | ;; License, or (at your option) any later version. 11 | 12 | 13 | 14 | ;; This program is distributed in the hope that it will be useful, but 15 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | ;; General Public License for more details. 18 | 19 | ;; You should have received a copy of the GNU General Public License 20 | ;; along with this program; if not, write to the Free Software 21 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 22 | ;; USA 23 | 24 | ;; Version: 1.0 25 | ;; Author: Adrien Brochard 26 | ;; Keywords: kubernetes k8s tools processes 27 | ;; URL: https://github.com/abrochard/kubel 28 | ;; License: GNU General Public License >= 3 29 | ;; Package-Requires: ((transient "0.1.0") (emacs "25.3") (dash "2.12.0") (s "1.2.0") (yaml-mode "0.0.14")) 30 | 31 | ;;; Commentary: 32 | 33 | ;; Emacs extension for controlling Kubernetes with limited permissions. 34 | 35 | ;;; Usage: 36 | 37 | ;; To list the pods in your current context and namespace, call 38 | ;; 39 | ;; M-x kubel 40 | ;; 41 | ;; To set the path to the kubectl config file, call: 42 | ;; M-x kubel-set-kubectl-config-file 43 | ;; 44 | ;; or 45 | ;; 46 | ;; (kubel-set-kubectl-config-file ) 47 | ;; ex: (kubel-set-kubectl-config-file "~/.kube/another-config") 48 | ;; 49 | ;; To set said namespace and context, respectively call 50 | ;; 51 | ;; M-x kubel-set-namespace 52 | ;; M-x kubel-set-context 53 | ;; 54 | ;; Note that namespace will autocomplete but not context, 55 | ;; this is because I interact with kubernetes through a user who 56 | ;; does not have permissions to list namespaces. 57 | ;; 58 | ;; To switch to showing a different resource, use the `R` command or 59 | ;; 60 | ;; M-x kubel-set-resource 61 | ;; 62 | ;; This will let you select a resource and re-display the kubel buffer. 63 | 64 | ;;; Shortcuts: 65 | 66 | ;; On the kubel screen, place your cursor on a resource 67 | ;; 68 | ;; enter => get resource details 69 | ;; C-u enter => describe resource 70 | ;; h => help popup 71 | ;; ? => help popup 72 | ;; E => quick edit any resource 73 | ;; g => refresh 74 | ;; k => delete popup 75 | ;; r => see the rollout history for resource 76 | ;; p => port forward pod 77 | ;; l => log popup 78 | ;; e => exec popup 79 | ;; j => jab deployment to force rolling update 80 | ;; S => scale replicas 81 | ;; C => set context 82 | ;; n => set namespace 83 | ;; R => set resource 84 | ;; K => set kubectl config file 85 | ;; F => set output format 86 | ;; f => set a substring filter for resource name 87 | ;; M-n => jump to the next highlighted resource 88 | ;; M-p => jump to previous highlighted resource 89 | ;; m => mark item 90 | ;; u => unmark item 91 | ;; M => mark all items 92 | ;; U => unmark all items 93 | ;; c => copy popup 94 | ;; $ => show process buffer 95 | ;; s => show only resources with specified label value 96 | 97 | ;;; Customize: 98 | 99 | ;; By default, kubel log tails from the last 100 lines, you can change the `kubel-log-tail-n` variable to set another line number. 100 | 101 | ;;; Code: 102 | 103 | (require 'transient) 104 | (require 'dash) 105 | (require 's) 106 | (require 'yaml-mode) 107 | (require 'tramp) 108 | (require 'subr-x) 109 | (require 'eshell) 110 | (require 'dired) 111 | (require 'json) 112 | 113 | (defgroup kubel nil "Customisation group for kubel." 114 | :group 'extensions) 115 | 116 | (defconst kubel--list-sort-key 117 | '("NAME" . nil) 118 | "Sort table on this key.") 119 | 120 | (defface kubel-status-running 121 | '((default . (:inherit success))) 122 | "The face to use for the Running status.") 123 | 124 | (defface kubel-status-healthy 125 | '((default . (:inherit success))) 126 | "The face to use for the Healthy status.") 127 | 128 | (defface kubel-status-active 129 | '((default . (:inherit success))) 130 | "The face to use for the Active status.") 131 | 132 | (defface kubel-status-ready 133 | '((default . (:inherit success))) 134 | "The face to use for the Ready status.") 135 | 136 | (defface kubel-status-true 137 | '((default . (:inherit success))) 138 | "The face to use for the True status.") 139 | 140 | (defface kubel-status-unknown 141 | '((default . (:inherit warning))) 142 | "The face to use for the Unknown status.") 143 | 144 | (defface kubel-status-error 145 | '((default . (:inherit error))) 146 | "The face to use for the Error status.") 147 | 148 | (defface kubel-status-evicted 149 | '((default . (:inherit error))) 150 | "The face to use for the Evicted status.") 151 | 152 | (defface kubel-status-memory-pressure 153 | '((default . (:inherit error))) 154 | "The face to use for the Memory Pressure status.") 155 | 156 | (defface kubel-status-pid-pressure 157 | '((default . (:inherit error))) 158 | "The face to use for the PID Pressure status.") 159 | 160 | (defface kubel-status-disk-pressure 161 | '((default . (:inherit error))) 162 | "The face to use for the Disk Pressure status.") 163 | 164 | (defface kubel-status-revision-missing 165 | '((default . (:inherit error))) 166 | "The face to use for the Revision Missing status.") 167 | 168 | (defface kubel-status-revision-failed 169 | '((default . (:inherit error))) 170 | "The face to use for the Revision Failed status.") 171 | 172 | (defface kubel-status-network-unavailable 173 | '((default . (:inherit error))) 174 | "The face to use for the Network Unavailable status.") 175 | 176 | (defface kubel-status-completed 177 | '((default . (:foreground "yellow"))) 178 | "The face to use for the Completed status.") 179 | 180 | (defface kubel-status-crash-loop-backoff 181 | '((default . (:inherit error))) 182 | "The face to use for the Crash Loop Backoff status.") 183 | 184 | (defface kubel-status-terminating 185 | '((default . (:foreground "blue"))) 186 | "The face to use for the Terminating status.") 187 | 188 | (defcustom kubel-status-faces 189 | '(("Running" . kubel-status-running) 190 | ("Healthy" . kubel-status-healthy) 191 | ("Active" . kubel-status-active) 192 | ("Ready" . kubel-status-ready) 193 | ("True" . kubel-status-true) 194 | ("Unknown" . kubel-status-unknown) 195 | ("Error" . kubel-status-error) 196 | ("Evicted" . kubel-status-evicted) 197 | ("MemoryPressure" . kubel-status-memory-pressure) 198 | ("PIDPressure" . kubel-status-pid-pressure) 199 | ("DiskPressure" . kubel-status-disk-pressure) 200 | ("RevisionMissing" . kubel-status-revision-missing) 201 | ("RevisionFailed" . kubel-status-revision-failed) 202 | ("NetworkUnavailable" . kubel-status-network-unavailable) 203 | ("Completed" . kubel-status-completed) 204 | ("CrashLoopBackOff" . kubel-status-crash-loop-backoff) 205 | ("Terminating" . kubel-status-terminating)) 206 | "Associative list of status to face." 207 | :type '(alist :key-type string 208 | :value-type face) 209 | :group 'kubel) 210 | 211 | (defcustom kubel-kubectl "kubectl" 212 | "Kubectl binary path." 213 | :type '(file :must-match t) 214 | :group 'kubel) 215 | 216 | (defconst kubel--process-buffer "*kubel-process*" 217 | "Kubel process buffer name.") 218 | 219 | (defcustom kubel-output "yaml" 220 | "Format for output: json|yaml|wide|custom-columns=..." 221 | :type 'string 222 | :group 'kubel) 223 | 224 | (defcustom kubel-log-tail-n 100 225 | "Default number of lines to tail." 226 | :type 'integer 227 | :group 'kubel) 228 | 229 | (defcustom kubel-list-wide nil 230 | "Control whether list views show additional colums. 231 | 232 | true - use '-o wide' for list views to show additional columns 233 | false - do not use '-o wide' for list views, hiding additional columns" 234 | :type 'boolean 235 | :group 'kubel) 236 | 237 | 238 | (defcustom kubel-use-namespace-list 'auto 239 | "Control behavior for namespace completion. 240 | 241 | auto - default, use `kubectl auth can-i list namespace` to determine if we can 242 | list namespaces 243 | on - always assume we can list namespaces 244 | off - always assume we cannot list namespaces" 245 | :type '(choice (const :tag "Auto" auto) 246 | (const :tag "On" on) 247 | (const :tag "Off" off)) 248 | :group 'kubel) 249 | 250 | (defcustom kubel-shell-buffer-name-format "kubel:%C:%n:%t:%c@%p" 251 | "Define the name format used for pod shell buffers. 252 | 253 | This is a format string with %-sequences that will be substituted 254 | with information about the shell's connection. The following 255 | %-sequences are defined: 256 | 257 | %t: The shell type. Examples of this are `shell` and `eshell` 258 | %c: The container name 259 | %p: The pod name 260 | %n: The current namespace 261 | %C: The current context" 262 | :type 'string 263 | :group 'kubel) 264 | 265 | (defcustom kubel-kill-buffer-query t 266 | "Non-nil means that killing a modified resource buffer has to be confirmed. 267 | This is used by `kubel-kill-buffer'." 268 | :type 'boolean 269 | :group 'kubel) 270 | 271 | (defcustom kubel-default-namespace "default" 272 | "Default namespace for kubel to use. Change if you have no resources in 273 | `default' namespace." 274 | :type 'string 275 | :group 'kubel) 276 | 277 | (defun kubel--append-to-process-buffer (str) 278 | "Append string STR to the process buffer." 279 | (with-current-buffer (get-buffer-create kubel--process-buffer) 280 | (read-only-mode -1) 281 | (goto-char (point-max)) 282 | (insert (format "%s\n" str)))) 283 | 284 | (defvar-local kubel--last-command nil) 285 | 286 | (defun kubel--log-command (process-name cmd) 287 | "Log the kubectl command to the process buffer. 288 | 289 | PROCESS-NAME is the name of the process. 290 | CMD is the kubectl command as a list." 291 | (let ((str-cmd (if (equal 'string (type-of cmd)) cmd (mapconcat #'identity cmd " ")))) 292 | (setq kubel--last-command str-cmd) 293 | (kubel--append-to-process-buffer 294 | (format "[%s]\ncommand: %s" process-name str-cmd)))) 295 | 296 | (defun kubel--exec-to-string (cmd) 297 | "Replace \"shell-command-to-string\" to log to process buffer. 298 | 299 | CMD is the command string to run." 300 | (kubel--log-command "kubectl-command" cmd) 301 | (with-output-to-string 302 | (with-current-buffer standard-output 303 | (shell-command cmd t "*kubel stderr*")))) 304 | 305 | (defvar-local kubel-namespace kubel-default-namespace 306 | "Current namespace.") 307 | 308 | (defvar-local kubel-resource "pods" 309 | "Current resource.") 310 | 311 | (defvar-local kubel-context 312 | (replace-regexp-in-string 313 | "\n" "" (kubel--exec-to-string "kubectl config current-context")) 314 | "Current context. Tries to smart default.") 315 | 316 | (defvar-local kubel-resource-filter "" 317 | "Substring filter for resource name.") 318 | 319 | (defvar-local kubel-selector "" 320 | "Label selector for resources.") 321 | 322 | (defvar kubel-namespace-history '() 323 | "List of previously used namespaces.") 324 | 325 | (defvar kubel-selector-history '() 326 | "List of previously used selectors.") 327 | 328 | (defvar-local kubel--can-get-namespace-cached nil) 329 | 330 | (defvar kubel--namespace-list-cached nil) 331 | 332 | (defvar-local kubel--label-values-cached nil) 333 | 334 | (defvar-local kubel--selected-items '()) 335 | 336 | (defvar-local kubel--kubernetes-resources-list-cached nil) 337 | 338 | (defun kubel--kubernetes-resources-list () 339 | "Get list of resources from cache or from fetching the api resource." 340 | (if (null kubel--kubernetes-resources-list-cached) 341 | (setq kubel--kubernetes-resources-list-cached 342 | (kubel--fetch-api-resource-list)) 343 | kubel--kubernetes-resources-list-cached)) 344 | 345 | (defun kubel--invalidate-context-caches () 346 | "Invalidate the context caches." 347 | (setq kubel--kubernetes-resources-list-cached nil) 348 | (setq kubel--can-get-namespace-cached nil) 349 | (setq kubel--namespace-list-cached nil) 350 | (setq kubel--label-values-cached nil)) 351 | 352 | (defun kubel--populate-list () 353 | "Return a list with a tabulated list format and \"tabulated-list-entries\"." 354 | (let* ((body (kubel--exec-to-string (concat (kubel--get-command-prefix) " get " kubel-resource (if kubel-list-wide " -o wide" "")))) 355 | (entrylist (kubel--parse-body body))) 356 | (when (string-prefix-p "No resources found" body) 357 | (message "No resources found")) ;; TODO exception here 358 | (list (kubel--get-list-format entrylist) (kubel--get-list-entries entrylist)))) 359 | 360 | (defun kubel--age-to-secs (age) 361 | "Convert AGE in format 1d2h3m4s to seconds." 362 | (let ((rex (rx bol 363 | (opt (group (one-or-more digit)) "d") 364 | (opt (group (one-or-more digit)) "h") 365 | (opt (group (one-or-more digit)) "m") 366 | (opt (group (one-or-more digit)) "s") 367 | eol))) 368 | (if (string-match rex age) 369 | (-sum (--map-indexed 370 | (* (--if-let (match-string (1+ it-index) age) 371 | (string-to-number it) 372 | 0) 373 | it) 374 | '(86400 3600 60 1))) 375 | 0))) 376 | 377 | (defun kubel--make-age-comparator (colnum) 378 | "Return a function that compares two ages at given column COLNUM." 379 | (lambda (row1 row2) 380 | (let ((age1 (elt (cadr row1) colnum)) 381 | (age2 (elt (cadr row2) colnum))) 382 | (< (kubel--age-to-secs age1) 383 | (kubel--age-to-secs age2))))) 384 | 385 | (defun kubel--column-entry (entrylist) 386 | "Return a function of colnum to retrieve an entry in a given column for ENTRYLIST." 387 | (function 388 | (lambda (colnum) 389 | (let* ((name (kubel--column-header entrylist colnum)) 390 | (width (+ 4 (kubel--column-width entrylist colnum))) 391 | (sort (if (member name '("AGE" "DURATION" "LAST SCHEDULE")) 392 | (kubel--make-age-comparator colnum) 393 | t))) 394 | (list name width sort))))) 395 | 396 | (defun kubel--get-list-format (entrylist) 397 | "Get the list format. 398 | 399 | ENTRYLIST is the output of the parsed body." 400 | (defun kubel--get-column-entry (colnum) 401 | (let ((kubel--get-entry (kubel--column-entry entrylist))) 402 | (funcall kubel--get-entry colnum))) 403 | (cl-map 'vector #'kubel--get-column-entry (number-sequence 0 (- (kubel--ncols entrylist) 1)))) 404 | 405 | (defun kubel--update-selected-items (entries) 406 | "Check that all selected items still exist. 407 | 408 | ENTRIES are all resources." 409 | (dolist (i (-difference kubel--selected-items (mapcar #'car entries))) 410 | (setq kubel--selected-items (delete i kubel--selected-items)))) 411 | 412 | (defun kubel--get-list-entries (entrylist) 413 | "Get the entries. 414 | 415 | ENTRYLIST is the output of the parsed body." 416 | (let ((entries (cdr entrylist))) 417 | (kubel--update-selected-items entries) 418 | (mapcar (lambda (x) 419 | (list (car x) 420 | (vconcat [] (mapcar #'kubel--propertize-status x)))) 421 | entries))) 422 | 423 | (defun kubel--parse-body (body) 424 | "Parse the body of kubectl get resource call into a list. 425 | 426 | BODY is the raw output of kubectl get resource." 427 | (let* ((lines (or (nbutlast (split-string body "\n")) '(""))) 428 | (header (car lines)) 429 | ;; Cronjobs have a "LAST SCHEDULE" column, so need to split on 2+ whitespace chars. 430 | (starts (cl-loop for start = 0 then (match-end 0) 431 | while (string-match (rx (>= 2 whitespace)) header start) 432 | collect (match-end 0))) 433 | (position (-zip-with 'cons (cons 0 starts) (append starts '("end")))) 434 | (parse-line (lambda (line) 435 | (mapcar (lambda (pos) 436 | (kubel--extract-value line (car pos) (cdr pos))) 437 | position)))) 438 | (mapcar parse-line lines))) 439 | 440 | (defun kubel--extract-value (line min max) 441 | "Extract value from LINE between MIN and MAX. 442 | If it's just white space, return -, else trim space. 443 | If MAX is the end of the line, dynamically adjust." 444 | (let* ((maxx (if (equal max "end") (length line) max)) 445 | (str (substring-no-properties line min maxx))) 446 | (if (string-match "^ +$" str) 447 | "-" 448 | (string-trim str)))) 449 | 450 | (defun kubel--ncols (entrylist) 451 | "Return the number of columns in ENTRYLIST." 452 | (length (car entrylist))) 453 | 454 | (defun kubel--nrows (entrylist) 455 | "Return the nubmer of rows in ENTRYLIST." 456 | (length entrylist)) 457 | 458 | (defun kubel--column-header (entrylist colnum) 459 | "Return the header for a specific COLNUM in ENTRYLIST." 460 | (nth colnum (car entrylist))) 461 | 462 | (defun kubel--column-width (entrylist colnum) 463 | "Return the width of a specific COLNUM in ENTRYLIST." 464 | (seq-max (mapcar (lambda (x) (length (nth colnum x) )) entrylist))) 465 | 466 | (defun kubel--buffer-name-from-parameters (context namespace resource) 467 | "Return a preconfigured kubel buffer name." 468 | (concat (format "*kubel:%s:%s:%s*" context namespace resource))) 469 | 470 | (defun kubel--buffer-name () 471 | "Return kubel buffer name." 472 | (concat (kubel--buffer-name-from-parameters kubel-context kubel-namespace kubel-resource) 473 | (unless (equal kubel-selector "") 474 | (format " (%s)" kubel-selector)))) 475 | 476 | (defun kubel--items-selected-p () 477 | "Return non-nil if there are items selected." 478 | (>= (length kubel--selected-items) 1)) 479 | 480 | (defun kubel--propertize-status (status) 481 | "Return the status in proper font color. 482 | 483 | STATUS is the pod status string." 484 | (let ((status-face (cdr (assoc status kubel-status-faces))) 485 | (match (or (equal kubel-resource-filter "") (string-match-p kubel-resource-filter status))) 486 | (selected (and (kubel--items-selected-p) (-contains? kubel--selected-items status)))) 487 | (cond (status-face (propertize status 'face status-face)) 488 | (selected (propertize (concat "*" status) 'face 'dired-marked)) 489 | ((not match) (propertize status 'face 'shadow)) 490 | (t status)))) 491 | 492 | (defun kubel--pop-to-buffer (name) 493 | "Utility function to pop to buffer or create it. 494 | 495 | NAME is the buffer name." 496 | (unless (get-buffer name) 497 | (get-buffer-create name)) 498 | (pop-to-buffer-same-window name)) 499 | 500 | (defun kubel--process-error-buffer (process-name) 501 | "Return the error buffer name for the PROCESS-NAME." 502 | (format "*%s:err*" process-name)) 503 | 504 | (defun kubel--sentinel (callback) 505 | "Sentinel function used by KUBEL--EXEC. 506 | 507 | CALLBACK is called when process completes successfully. 508 | " 509 | (lambda (process event) 510 | (let ((process-name (process-name process)) 511 | (exit-status (process-exit-status process))) 512 | (kubel--append-to-process-buffer (format "[%s]\nexit-code: %s" process-name exit-status)) 513 | (if (eq 0 exit-status) 514 | (when callback (funcall callback)) 515 | (let ((err (with-current-buffer (kubel--process-error-buffer process-name) 516 | (buffer-string)))) 517 | (kubel--append-to-process-buffer (format "error: %s" err)) 518 | (error (format "Kubel process %s error: %s" process-name err))))))) 519 | 520 | (defun kubel--exec (process-name args &optional readonly callback) 521 | "Utility function to run commands in the proper context and namespace. 522 | 523 | PROCESS-NAME is an identifier for the process. Default to \"kubel-command\". 524 | ARGS is a ist of arguments. 525 | CALLBACK is a function that will be executed when the command completes. 526 | READONLY If true buffer will be in readonly mode(view-mode)." 527 | (when (equal process-name "") 528 | (setq process-name "kubel-command")) 529 | (let ((buffer-name (format "*kubel-resource:%s:%s:%s*" kubel-context kubel-namespace (string-join args "_"))) 530 | (error-buffer (kubel--process-error-buffer process-name)) 531 | (cmd (append (list kubel-kubectl) (kubel--get-context-namespace) args))) 532 | (when (get-buffer buffer-name) 533 | (kill-buffer buffer-name)) 534 | (when (get-buffer error-buffer) 535 | (kill-buffer error-buffer)) 536 | (kubel--log-command process-name cmd) 537 | (make-process :name process-name 538 | :buffer buffer-name 539 | :sentinel (kubel--sentinel callback) 540 | :file-handler t 541 | :stderr (get-buffer-create error-buffer) 542 | :command cmd) 543 | (pop-to-buffer buffer-name) 544 | (if readonly 545 | (with-current-buffer buffer-name 546 | (view-mode))))) 547 | 548 | (defun kubel--get-resource-under-cursor () 549 | "Utility function to get the name of the resource under the cursor. 550 | Strip the `*` prefix if the resource is selected" 551 | (string-remove-suffix " (default)" ;; see https://github.com/abrochard/kubel/issues/106 552 | (replace-regexp-in-string 553 | "^\*" "" (aref (tabulated-list-get-entry) 0)))) 554 | 555 | (defun kubel--get-context-namespace () 556 | "Utility function to return the proper context and namespace arguments." 557 | (append 558 | (unless (equal kubel-context "") 559 | (list "--context" kubel-context)) 560 | (unless (equal kubel-namespace "") 561 | (list "-n" kubel-namespace)))) 562 | 563 | (defun kubel--get-selector () 564 | "Utility function to return current label selector." 565 | (unless (equal kubel-selector "") 566 | (list "--selector" kubel-selector))) 567 | 568 | (defun kubel--get-command-prefix () 569 | "Utility function to prefix the kubectl command with proper context and namespace." 570 | (mapconcat 'identity (append (list kubel-kubectl) (kubel--get-context-namespace) (kubel--get-selector)) " ")) 571 | 572 | (defun kubel--get-containers (pod-name &optional type) 573 | "List the containers in a pod. 574 | 575 | POD-NAME is the name of the pod. 576 | TYPE is containers or initContainers." 577 | (unless type (setq type "containers")) 578 | (split-string 579 | (kubel--exec-to-string 580 | (format "%s get pod %s -o jsonpath='{.spec.%s[*].name}'" (kubel--get-command-prefix) pod-name type)) " ")) 581 | 582 | (defun kubel--get-pod-labels () 583 | "List labels of pods in a current namespace." 584 | (let* ((raw-labels 585 | (split-string 586 | (replace-regexp-in-string 587 | (regexp-quote ":") "=" 588 | (replace-regexp-in-string 589 | "map\\[\\(.+?\\)\\]" "\\1" 590 | (kubel--exec-to-string 591 | (format "%s get pod -o jsonpath='{.items[*].metadata.labels}'" (kubel--get-command-prefix))))))) 592 | (splitted (mapcan (lambda (s) (split-string s ",")) 593 | raw-labels)) 594 | (cleaned (mapcar (lambda (s) (replace-regexp-in-string "[{|\"|}]" "" s)) splitted)) 595 | (unique (-distinct cleaned))) 596 | unique)) 597 | 598 | (defun kubel--select-resource (name) 599 | "Prompt user to select an instance out of a list of resources. 600 | 601 | NAME is the string name of the resource." 602 | (let ((cmd (format "%s get %s -o=jsonpath='{.items[*].metadata.name}'" 603 | (kubel--get-command-prefix) name))) 604 | (completing-read (concat (s-upper-camel-case name) ": ") 605 | (split-string (kubel--exec-to-string cmd) " ")))) 606 | 607 | (defun kubel--describe-resource (name &optional describe) 608 | "Describe a specific resource. 609 | 610 | NAME is the string name of the resource to decribe. 611 | DESCRIBE is boolean to describe instead of get resource details" 612 | (let* ((resource (kubel--select-resource name)) 613 | (process-name (format "kubel - %s - %s" name resource)) 614 | (callback (lambda () 615 | (set-buffer-modified-p nil) 616 | (goto-char (point-min))))) 617 | (if describe 618 | (kubel--exec process-name (list "describe" name resource) nil callback) 619 | (kubel--exec process-name (list "get" name "-o" kubel-output resource) nil callback)) 620 | (when (string-equal kubel-output "yaml") 621 | (kubel-yaml-editing-mode)))) 622 | 623 | (defun kubel--show-rollout-revision (type name) 624 | "Show a specific revision of a certain resource. 625 | 626 | TYPE is the resource type. 627 | NAME is the resource name." 628 | (let* ((typename (format "%s/%s" type name)) 629 | (revision (car (split-string (kubel--select-rollout typename)))) 630 | (process-name (format "kubel - rollout - %s - %s" typename revision)) 631 | (callback (goto-char (point-min)))) 632 | (kubel--exec process-name 633 | (list "rollout" "history" typename (format "--revision=%s" revision)) nil callback))) 634 | 635 | (defun kubel--list-rollout (typename) 636 | "Return a list of revisions with format '%number %cause'. 637 | 638 | TYPENAME is the resource type/name." 639 | (let ((cmd (format "%s rollout history %s" (kubel--get-command-prefix) typename))) 640 | (nthcdr 2 (split-string (kubel--exec-to-string cmd) "\n" t)))) 641 | 642 | (defun kubel--select-rollout (typename) 643 | "Select a rollout version. 644 | 645 | TYPENAME is the resource type/name." 646 | (let ((prompt (format "Select a rollout of %s: " typename)) 647 | (rollouts (kubel--list-rollout typename))) 648 | (completing-read prompt rollouts))) 649 | 650 | (defun kubel--is-pod-view () 651 | "Return non-nil if this is the pod view." 652 | (equal (capitalize kubel-resource) "Pods")) 653 | 654 | (defun kubel--is-deployment-view () 655 | "Return non-nil if this is a deployment view." 656 | (-contains? '("Deployments" "deployments" "deployments.apps") kubel-resource)) 657 | 658 | (defun kubel--is-scalable () 659 | "Return non-nil if the resource can be scaled." 660 | (or 661 | (kubel--is-deployment-view) 662 | (-contains? '("ReplicaSets" "replicasets" "replicasets.apps") kubel-resource) 663 | (-contains? '("StatefulSets" "statefulsets" "statefulsets.apps") kubel-resource))) 664 | 665 | (defun kubel-kill-buffer () 666 | "Kill the current buffer." 667 | (interactive) 668 | (when (or (not (buffer-modified-p)) 669 | (not kubel-kill-buffer-query) 670 | (yes-or-no-p "Resource modified; kill anyway? ")) 671 | (kill-buffer (current-buffer)))) 672 | 673 | (defvar kubel-yaml-editing-mode-map 674 | (let ((map (make-sparse-keymap))) 675 | (define-key map (kbd "C-c C-c") #'kubel-apply) 676 | (define-key map (kbd "C-c C-k") #'kubel-kill-buffer) 677 | map) 678 | "Keymap used in `kubel-yaml-editing-mode' buffers.") 679 | 680 | ;; interactive 681 | ;;;###autoload 682 | (define-derived-mode kubel-yaml-editing-mode yaml-mode "kubel/e" 683 | "Kubel Yaml editing mode. 684 | 685 | Allows simple apply of the changes made. 686 | 687 | \\{kubel-yaml-editing-mode-map}") 688 | 689 | (defun kubel-apply () 690 | "Save the current buffer to a temp file and try to kubectl apply it." 691 | (interactive) 692 | (setq dir-prefix (or 693 | (when (tramp-tramp-file-p default-directory) 694 | (with-parsed-tramp-file-name default-directory nil 695 | (format "/%s%s:%s:" (or hop "") method (if user (concat user "@" host) host)))) 696 | "")) 697 | (let* ((filename-without-tramp-prefix (format "/tmp/kubel/%s-%s.yaml" 698 | (replace-regexp-in-string "/" "_" 699 | (replace-regexp-in-string "\*\\| " "" (buffer-name))) 700 | (floor (float-time)))) 701 | (filename (format "%s%s" dir-prefix filename-without-tramp-prefix))) 702 | (when (y-or-n-p "Apply the changes? ") 703 | (unless (file-exists-p (format "%s/tmp/kubel" dir-prefix)) 704 | (make-directory (format "%s/tmp/kubel" dir-prefix) t)) 705 | (write-region (point-min) (point-max) filename) 706 | (kubel--exec (format "kubectl - apply - %s" filename) (list "apply" "-f" filename-without-tramp-prefix) nil (lambda () (message "Applied %s" filename)))))) 707 | 708 | (defun kubel-get-resource-details (&optional describe) 709 | "Get the details of the resource under the cursor. 710 | 711 | DESCRIBE is the optional param to describe instead of get." 712 | (interactive "P") 713 | (let* ((resource (kubel--get-resource-under-cursor)) 714 | (ctx kubel-context) 715 | (ns kubel-namespace) 716 | (res kubel-resource) 717 | (process-name (format "kubel - %s - %s" kubel-resource resource)) 718 | (callback (lambda () 719 | (set-buffer-modified-p nil) 720 | (goto-char (point-min))))) 721 | (if describe 722 | (kubel--exec process-name (list "describe" kubel-resource (kubel--get-resource-under-cursor)) nil callback) 723 | (kubel--exec process-name (list "get" kubel-resource (kubel--get-resource-under-cursor) "-o" kubel-output) nil callback)) 724 | (when (or (string-equal kubel-output "yaml") (transient-args 'kubel-describe-popup)) 725 | (kubel-yaml-editing-mode) 726 | (setq kubel-context ctx) 727 | (setq kubel-namespace ns) 728 | (setq kubel-resource res)))) 729 | 730 | (defun kubel--default-tail-arg (args) 731 | "Ugly function to make sure that there is at least the default tail. 732 | 733 | ARGS is the arg list from transient." 734 | (if (car (remove nil (mapcar (lambda (x) 735 | (string-prefix-p "--tail=" x)) args))) 736 | args 737 | (append args (list (concat "--tail=" (format "%s" kubel-log-tail-n)))))) 738 | 739 | (defun kubel-get-pod-logs (&optional args type) 740 | "Get the last N logs of the pod under the cursor. 741 | 742 | ARGS is the arguments list from transient. 743 | TYPE is containers or initContainers." 744 | (interactive 745 | (list (transient-args 'kubel-log-popup))) 746 | (dolist (pod (if (kubel--is-pod-view) 747 | (if (kubel--items-selected-p) 748 | kubel--selected-items 749 | (list (kubel--get-resource-under-cursor))) 750 | (list (kubel--select-resource "Pods")))) 751 | (let* ((type (or type "containers")) 752 | (containers (kubel--get-containers pod type)) 753 | (container (if (equal (length containers) 1) 754 | (car containers) 755 | (completing-read "Select container: " containers))) 756 | (process-name (format "kubel - logs - %s - %s" pod container))) 757 | (kubel--exec process-name 758 | (append '("logs") (kubel--default-tail-arg args) (list pod container)) t nil)))) 759 | 760 | (defun kubel-get-pod-logs--initContainer (&optional args) 761 | "Get the last N logs of the pod under the cursor. 762 | 763 | ARGS is the arguments list from transient." 764 | (interactive 765 | (list (transient-args 'kubel-log-popup))) 766 | (kubel-get-pod-logs args "initContainers")) 767 | 768 | (defun kubel-get-logs-by-labels (&optional args) 769 | "Get the last N logs of the pods by labels. 770 | ARGS is the arguments list from transient." 771 | (interactive 772 | (list (transient-args 'kubel-log-popup))) 773 | (let* ((labels (kubel--get-pod-labels)) 774 | (label (completing-read "Select container: " labels)) 775 | (process-name (format "kubel - logs - %s" label))) 776 | (kubel--exec process-name 777 | (append '("logs") (kubel--default-tail-arg args) '("-l") (list label)) t nil))) 778 | 779 | (defun kubel-copy-resource-name () 780 | "Copy the name of the pod under the cursor." 781 | (interactive) 782 | (kill-new (kubel--get-resource-under-cursor)) 783 | (message "Resource name copied to kill-ring")) 784 | 785 | (defun kubel-copy-log-command () 786 | "Copy the streaming log command of the pod under the cursor." 787 | (interactive) 788 | (kill-new 789 | (format "%s logs -f --tail=%s %s" 790 | (kubel--get-command-prefix) 791 | kubel-log-tail-n 792 | (if (kubel--is-pod-view) 793 | (kubel--get-resource-under-cursor) 794 | (kubel--select-resource "Pods")))) 795 | (message "Log command copied to kill-ring")) 796 | 797 | (defun kubel-copy-command-prefix () 798 | "Copy the kubectl command prefix." 799 | (interactive) 800 | (kill-new (kubel--get-command-prefix)) 801 | (message "Command prefix copied to kill-ring")) 802 | 803 | (defun kubel-copy-last-command () 804 | "Copy the last kubectl command ran." 805 | (interactive) 806 | (kill-new kubel--last-command) 807 | (message (concat "Last command copied: " kubel--last-command))) 808 | 809 | (defun kubel-set-kubectl-config-file (configfile) 810 | "Set the path to the kubectl CONFIGFILE." 811 | (interactive "f") 812 | (let ((configfile (or configfile "~/.kube/config"))) 813 | (if (file-exists-p (expand-file-name configfile)) 814 | (setenv "KUBECONFIG" (expand-file-name configfile)) 815 | (error "Kubectl config file '%s' does not exist!" configfile)))) 816 | 817 | (defun kubel--can-get-namespace () 818 | "Determine if permissions allow for `kubectl get namespace` in current context." 819 | (cond ((eq kubel-use-namespace-list 'on) t) 820 | ((eq kubel-use-namespace-list 'auto) 821 | (progn 822 | (unless kubel--can-get-namespace-cached 823 | (setq kubel--can-get-namespace-cached 824 | (string-match-p "yes\n" 825 | (kubel--exec-to-string 826 | (format "%s --context %s auth can-i list namespaces" kubel-kubectl kubel-context)))))) 827 | kubel--can-get-namespace-cached))) 828 | 829 | (defun kubel--get-namespace () 830 | "Get namespaces for current context, try to recover from cache first." 831 | (unless kubel--namespace-list-cached 832 | (setq kubel--namespace-list-cached 833 | (split-string (kubel--exec-to-string 834 | (format "%s --context %s get namespace -o jsonpath='{.items[*].metadata.name}'" kubel-kubectl kubel-context)) " "))) 835 | kubel--namespace-list-cached) 836 | 837 | (defun kubel--list-namespace () 838 | "List namespace, either from history, or dynamically if possible." 839 | (if (kubel--can-get-namespace) 840 | (kubel--get-namespace) 841 | kubel-namespace-history)) 842 | 843 | (defun kubel--add-namespace-to-history (namespace) 844 | "Add NAMESPACE to history if it isn't there already." 845 | (unless (member namespace kubel-namespace-history) 846 | (push namespace kubel-namespace-history))) 847 | 848 | (defun kubel-set-namespace (&optional refresh) 849 | "Set the namespace. 850 | If called with a prefix argument REFRESH, refreshes 851 | the context caches, including the cached resource list." 852 | (interactive "P") 853 | (when refresh (kubel--invalidate-context-caches)) 854 | (let* ((namespace (completing-read "Namespace: " (kubel--list-namespace) 855 | nil nil nil nil "default")) 856 | (kubel--buffer (get-buffer (kubel--buffer-name))) 857 | (last-default-directory (when kubel--buffer 858 | (with-current-buffer kubel--buffer default-directory)))) 859 | (with-current-buffer (clone-buffer) 860 | (setq kubel-namespace namespace) 861 | (kubel--add-namespace-to-history namespace) 862 | (switch-to-buffer (current-buffer)) 863 | (kubel-refresh last-default-directory)))) 864 | 865 | (defun kubel-set-context () 866 | "Set the context." 867 | (interactive) 868 | (let* ((kubel--buffer (get-buffer (kubel--buffer-name))) 869 | (last-default-directory (when kubel--buffer (with-current-buffer kubel--buffer default-directory)))) 870 | (with-current-buffer (clone-buffer) 871 | (setq kubel-context 872 | (completing-read 873 | "Select context: " 874 | (split-string (kubel--exec-to-string (format "%s config view -o jsonpath='{.contexts[*].name}'" kubel-kubectl)) " "))) 875 | (kubel--invalidate-context-caches) 876 | (setq kubel-namespace "default") 877 | (switch-to-buffer (current-buffer)) 878 | (kubel-refresh last-default-directory)))) 879 | 880 | (defun kubel--add-selector-to-history (selector) 881 | "Add SELECTOR to history if it isn't there already." 882 | (unless (member selector kubel-selector-history) 883 | (push selector kubel-selector-history))) 884 | 885 | (defun kubel--get-all-selectors () 886 | "Get all selectors." 887 | (unless kubel--label-values-cached 888 | (let ((labels (kubel--get-pod-labels))) 889 | (setq kubel--label-values-cached labels))) 890 | kubel--label-values-cached) 891 | 892 | (defun kubel--list-selectors () 893 | "List selector expressions from history." 894 | (delete-dups 895 | (append '("none") (kubel--get-all-selectors) 896 | kubel-selector-history))) 897 | 898 | (defun kubel-set-label-selector () 899 | "Set the selector." 900 | (interactive) 901 | (with-current-buffer (clone-buffer) 902 | (let ((selector (completing-read 903 | "Selector: " 904 | (kubel--list-selectors)))) 905 | (when (equal selector "none") 906 | (setq selector "")) 907 | (setq kubel-selector selector)) 908 | (kubel--add-selector-to-history kubel-selector) 909 | ;; Update pod list according to the label selector 910 | (switch-to-buffer (current-buffer)) 911 | (kubel-refresh))) 912 | 913 | (defun kubel--fetch-api-resource-list () 914 | "Fetch the API resource list." 915 | (split-string (kubel--exec-to-string 916 | (format "%s --context %s api-resources -o name --no-headers=true" kubel-kubectl kubel-context)) "\n" t)) 917 | 918 | (defun kubel-set-resource (&optional refresh) 919 | "Set the resource. 920 | If called with a prefix argument REFRESH, refreshes 921 | the context caches, including the cached resource list." 922 | (interactive "P") 923 | (when refresh (kubel--invalidate-context-caches)) 924 | (let* ((current-buffer-name (kubel--buffer-name)) 925 | (resource-list (kubel--kubernetes-resources-list)) 926 | (kubel--buffer (get-buffer current-buffer-name)) 927 | (last-default-directory (when kubel--buffer (with-current-buffer kubel--buffer default-directory)))) 928 | (with-current-buffer (clone-buffer) 929 | (setq kubel-resource 930 | (completing-read "Select resource: " resource-list)) 931 | (switch-to-buffer (current-buffer)) 932 | (kubel-refresh last-default-directory)))) 933 | 934 | (defun kubel-set-output-format () 935 | "Set output format of kubectl." 936 | (interactive) 937 | (setq kubel-output 938 | (completing-read 939 | "Set output format: " 940 | '("yaml" "json" "wide" "custom-columns=")))) 941 | 942 | (defun kubel-port-forward-pod (p) 943 | "Port forward a pod to your local machine. 944 | 945 | P can be a single number or a localhost:container port pair." 946 | (interactive "sPort: ") 947 | (let* ((port (if (string-match-p ":" p) p (format "%s:%s" p p))) 948 | (pod (if (kubel--is-pod-view) 949 | (kubel--get-resource-under-cursor) 950 | (kubel--select-resource "Pods"))) 951 | (process-name (format "kubel - port-forward - %s:%s" pod port))) 952 | (kubel--exec process-name (list "port-forward" pod port)))) 953 | 954 | (defun kubel-setup-tramp () 955 | "Setup a kubectl TRAMP." 956 | (setq tramp-methods (delete (assoc "kubectl" tramp-methods) tramp-methods)) ;; cleanup previous tramp method 957 | ;; TODO error message if resource is not pod 958 | (add-to-list 'tramp-methods 959 | `("kubectl" 960 | (tramp-login-program ,kubel-kubectl) 961 | (tramp-login-args (,(kubel--get-context-namespace) ("exec" "-it") ("-c" "%u") ("%h") ("--" "sh"))) 962 | (tramp-remote-shell "sh") 963 | (tramp-remote-shell-args ("-i" "-c"))))) ;; add the current context/namespace to tramp methods 964 | 965 | (defun kubel--get-container-under-cursor () 966 | "Get `(container . pod)' name under cursor." 967 | (let* ((pod (if (kubel--is-pod-view) 968 | (kubel--get-resource-under-cursor) 969 | (kubel--select-resource "Pods"))) 970 | (containers (kubel--get-containers pod)) 971 | (container (if (equal (length containers) 1) 972 | (car containers) 973 | (completing-read "Select container: " containers)))) 974 | (cons container pod))) 975 | 976 | (defun kubel--dir-prefix () 977 | "Return the current directory prefix for a TRAMP connection." 978 | (or 979 | (when (tramp-tramp-file-p default-directory) 980 | (with-parsed-tramp-file-name default-directory nil 981 | (format "%s%s:%s|" (or hop "") method (if user (concat user "@" host) host)))) 982 | "")) 983 | 984 | (defun kubel-exec-pod () 985 | "Exec into the pod under the cursor -> `find-file." 986 | (interactive) 987 | (kubel-setup-tramp) 988 | (let* ((dir-prefix (kubel--dir-prefix)) 989 | (con-pod (kubel--get-container-under-cursor))) 990 | (find-file (format "/%skubectl:%s@%s:/" dir-prefix (car con-pod) (cdr con-pod))))) 991 | 992 | (defun kubel--shell-buffer-name (shell-type container pod) 993 | "Generate the name for a pod's shell buffer. 994 | 995 | This uses `kubel-shell-buffer-name-format' as the buffer name 996 | format. See the documentation for it for more information on how 997 | to set this format. 998 | 999 | The values for the current namespace and context are pulled from 1000 | the variables `kubel-namespace' and `kubel-context', respectively." 1001 | (format-spec kubel-shell-buffer-name-format 1002 | `((?t . ,shell-type) 1003 | (?c . ,container) 1004 | (?p . ,pod) 1005 | (?n . ,kubel-namespace) 1006 | (?C . ,kubel-context)))) 1007 | 1008 | (defun kubel-exec-shell-pod () 1009 | "Exec into the pod under the cursor -> shell." 1010 | (interactive) 1011 | (kubel-setup-tramp) 1012 | (let* ((dir-prefix (kubel--dir-prefix)) 1013 | (con-pod (kubel--get-container-under-cursor)) 1014 | (container (car con-pod)) 1015 | (pod (cdr con-pod)) 1016 | (default-directory (format "/%skubectl:%s@%s:/" dir-prefix container pod))) 1017 | (shell (kubel--shell-buffer-name "shell" container pod)))) 1018 | 1019 | (defun kubel-exec-eshell-pod () 1020 | "Exec into the pod under the cursor -> eshell." 1021 | (interactive) 1022 | (kubel-setup-tramp) 1023 | (let* ((dir-prefix (kubel--dir-prefix)) 1024 | (con-pod (kubel--get-container-under-cursor)) 1025 | (container (car con-pod)) 1026 | (pod (cdr con-pod)) 1027 | (default-directory (format "/%skubectl:%s@%s:/" dir-prefix container pod)) 1028 | (eshell-buffer-name 1029 | (kubel--shell-buffer-name "eshell" container pod))) 1030 | (eshell))) 1031 | 1032 | (defun kubel-exec-vterm-pod () 1033 | "Exec into the pod under the cursor -> vterm." 1034 | (interactive) 1035 | (kubel-setup-tramp) 1036 | (let* ((dir-prefix (kubel--dir-prefix)) 1037 | (con-pod (kubel--get-container-under-cursor)) 1038 | (container (car con-pod)) 1039 | (pod (cdr con-pod)) 1040 | (default-directory (format "/%skubectl:%s@%s:/" dir-prefix container pod)) 1041 | (vterm-buffer-name 1042 | (kubel--shell-buffer-name "vterm" container pod)) 1043 | (vterm-shell "/bin/sh")) 1044 | (vterm nil))) 1045 | 1046 | ;;;###autoload 1047 | (defun kubel-vterm-setup () 1048 | "Adds a vterm enty to the KUBEL-EXEC-POP." 1049 | (require 'vterm) 1050 | (transient-append-suffix 'kubel-exec-popup "e" 1051 | '("v" "Vterm" kubel-exec-vterm-pod))) 1052 | 1053 | (defun kubel-exec-ansi-term-pod () 1054 | "Exec into the pod under the cursor -> `ansi-term'." 1055 | (interactive) 1056 | (let* ((con-pod (kubel--get-container-under-cursor)) 1057 | (container (car con-pod)) 1058 | (pod (cdr con-pod)) 1059 | (command (format "%s exec %s -c %s -i -t -- /usr/bin/env sh" (kubel--get-command-prefix) pod container))) 1060 | (with-current-buffer (ansi-term "bash" (kubel--shell-buffer-name "ansi-term" container pod)) 1061 | (process-send-string (current-buffer) (format "%s\n" command))))) 1062 | 1063 | (defun kubel-exec-eat-pod () 1064 | "Exec into the pod under the cursor -> eat." 1065 | (interactive) 1066 | (unless (fboundp 'eat-other-window) 1067 | (user-error "This command requires the `eat' package.")) 1068 | (kubel-setup-tramp) 1069 | (let* ((dir-prefix (kubel--dir-prefix)) 1070 | (con-pod (kubel--get-container-under-cursor)) 1071 | (container (car con-pod)) 1072 | (pod (cdr con-pod)) 1073 | (default-directory (format "/%skubectl:%s@%s:/" dir-prefix container pod)) 1074 | (eat-buffer-name (format "*eat:%s" default-directory))) 1075 | (eat-other-window))) 1076 | 1077 | (defun kubel-exec-pod-by-shell-command () 1078 | "Prompt shell with kubectl exec command at pod under cursor." 1079 | (interactive) 1080 | (kubel-setup-tramp) 1081 | (let* ((con-pod (kubel--get-container-under-cursor)) 1082 | (command (read-string "Shell command: " (format "%s exec %s -c %s -- " (kubel--get-command-prefix) (cdr con-pod) (car con-pod))))) 1083 | (shell-command command))) 1084 | 1085 | (defun kubel-delete-resource () 1086 | "Kubectl delete resource under cursor." 1087 | (interactive) 1088 | (dolist (pod (if (kubel--items-selected-p) 1089 | kubel--selected-items 1090 | (list (kubel--get-resource-under-cursor)))) 1091 | (let* ((process-name (format "kubel - delete %s - %s" kubel-resource pod)) 1092 | (args (list "delete" kubel-resource pod))) 1093 | (when (transient-args 'kubel-delete-popup) 1094 | (setq args (append args (list "--force" "--grace-period=0")))) 1095 | (kubel--exec process-name args)))) 1096 | 1097 | (defun kubel-jab-deployment () 1098 | "Make a trivial patch to force a new deployment. 1099 | 1100 | See https://github.com/kubernetes/kubernetes/issues/27081" 1101 | (interactive) 1102 | (dolist (deployment (if (kubel--is-deployment-view) 1103 | (if (kubel--items-selected-p) 1104 | kubel--selected-items 1105 | (list (kubel--get-resource-under-cursor))) 1106 | (list (kubel--select-resource "Deployments")))) 1107 | (let ((process-name (format "kubel - bouncing - %s" deployment))) 1108 | (kubel--exec process-name (list "patch" "deployment" deployment "-p" 1109 | (format "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"date\":\"%s\"}}}}}" 1110 | (round (time-to-seconds)))))))) 1111 | 1112 | (defun kubel-scale-replicas (replicas) 1113 | "Scale resource replicas. 1114 | 1115 | REPLICAS is the number of desired replicas." 1116 | (interactive (list (read-number "Replicas: "))) 1117 | (if (kubel--is-scalable) 1118 | (let* ((resource (kubel--get-resource-under-cursor)) 1119 | (process-name (format "kubel:scale:%s/%s" kubel-resource resource))) 1120 | (kubel--exec process-name (list "scale" kubel-resource resource "--replicas" (number-to-string replicas)))) 1121 | (message 1122 | "[%s] cannot be scaled.\nOnly these resources can be scaled: [deployment, replica set, replication controller, and stateful set]." 1123 | kubel-resource))) 1124 | 1125 | (defun kubel-set-filter (filter) 1126 | "Set the pod filter. 1127 | 1128 | FILTER is the filter string." 1129 | (interactive "MFilter: ") 1130 | (setq kubel-resource-filter filter) 1131 | (kubel-refresh)) 1132 | 1133 | (defun kubel--jump-to-highlight (init search reset) 1134 | "Base function to jump to highlight. 1135 | 1136 | INIT is to be called before searching. 1137 | SEARCH is to apply the search and can be repeated safely. 1138 | RESET is to be called if the search is nil after the first attempt." 1139 | (unless (equal kubel-resource-filter "") 1140 | (funcall init) 1141 | (unless (funcall search) 1142 | (funcall reset) 1143 | (funcall search)) 1144 | (beginning-of-line))) 1145 | 1146 | (defun kubel-jump-to-next-highlight () 1147 | "Jump to the next hightlighted resrouce." 1148 | (interactive) 1149 | (kubel--jump-to-highlight 1150 | #'end-of-line 1151 | (lambda () (re-search-forward kubel-resource-filter (point-max) t)) 1152 | #'beginning-of-buffer)) 1153 | 1154 | (defun kubel-jump-to-previous-highlight () 1155 | "Jump to the previou highlighted resrouce." 1156 | (interactive) 1157 | (kubel--jump-to-highlight 1158 | #'beginning-of-line 1159 | (lambda () (re-search-backward kubel-resource-filter (point-min) t)) 1160 | #'end-of-buffer)) 1161 | 1162 | (defun kubel-rollout-history () 1163 | "See rollout history for resource under cursor." 1164 | (interactive) 1165 | (kubel--show-rollout-revision kubel-resource (kubel--get-resource-under-cursor))) 1166 | 1167 | (defun kubel-changelog () 1168 | "Opens up the changelog." 1169 | (interactive) 1170 | (browse-url "https://github.com/abrochard/kubel/blob/master/CHANGELOG.md")) 1171 | 1172 | (defun kubel-quick-edit () 1173 | "Quickly edit any resource." 1174 | (interactive) 1175 | (kubel--describe-resource 1176 | (completing-read "Select resource: " (kubel--kubernetes-resources-list)))) 1177 | 1178 | (defun kubel-show-process-buffer () 1179 | "Show the kubel-process-buffer." 1180 | (interactive) 1181 | (pop-to-buffer kubel--process-buffer) 1182 | (special-mode)) 1183 | 1184 | (defun kubel-mark-item () 1185 | "Mark or unmark the item under cursor." 1186 | (interactive) 1187 | (let ((item (kubel--get-resource-under-cursor))) 1188 | (unless (-contains? kubel--selected-items item) 1189 | (progn 1190 | (push item kubel--selected-items) 1191 | (forward-line 1) 1192 | (kubel-refresh))))) 1193 | 1194 | (defun kubel-unmark-item () 1195 | "Unmark the item under cursor." 1196 | (interactive) 1197 | (let ((item (kubel--get-resource-under-cursor))) 1198 | (when (-contains? kubel--selected-items item) 1199 | (progn 1200 | (setq kubel--selected-items (delete item kubel--selected-items)) 1201 | (kubel-refresh))))) 1202 | 1203 | (defun kubel-mark-all () 1204 | "Mark all items." 1205 | (interactive) 1206 | (setq kubel--selected-items '()) 1207 | (save-excursion 1208 | (goto-char (point-min)) 1209 | (while (not (eobp)) 1210 | (push (kubel--get-resource-under-cursor) kubel--selected-items) 1211 | (forward-line 1))) 1212 | (kubel-refresh)) 1213 | 1214 | (defun kubel-unmark-all () 1215 | "Unmark all items." 1216 | (interactive) 1217 | (setq kubel--selected-items '()) 1218 | (kubel-refresh)) 1219 | 1220 | (defun kubel--read-buffer () 1221 | "Return the list of all buffers of kubel pattern." 1222 | (let* ((other-buffer (other-buffer (current-buffer))) 1223 | (other-name (buffer-name other-buffer)) 1224 | (buffers)) 1225 | (dolist (buf (buffer-list)) 1226 | (when (string-prefix-p "*kubel:" (buffer-name buf)) 1227 | (push buf buffers))) 1228 | (let ((predicate 1229 | (lambda (buffer) 1230 | ;; BUFFER is an entry (BUF-NAME . BUF-OBJ) of Vbuffer_alist. 1231 | (memq (cdr buffer) buffers)))) 1232 | (read-buffer 1233 | "Switch to buffer: " 1234 | (when (funcall predicate (cons other-name other-buffer)) other-name) 1235 | nil 1236 | predicate)))) 1237 | 1238 | (defun kubel-switch-to-buffer (buffer-or-name) 1239 | "Display buffer BUFFER-OR-NAME in the selected window. 1240 | When called interactively, prompts for a buffer belonging to kubel." 1241 | (interactive (list (kubel--read-buffer))) 1242 | (switch-to-buffer buffer-or-name)) 1243 | 1244 | ;; popups 1245 | 1246 | (transient-define-prefix kubel-exec-popup () 1247 | "Kubel Exec Menu" 1248 | ["Actions" 1249 | ("!" "Shell command" kubel-exec-pod-by-shell-command) 1250 | ("d" "Dired" kubel-exec-pod) 1251 | ("e" "Eshell" kubel-exec-eshell-pod) 1252 | ("a" "Ansi-term" kubel-exec-ansi-term-pod) 1253 | ("t" "eat" kubel-exec-eat-pod) 1254 | ("s" "Shell" kubel-exec-shell-pod)]) 1255 | 1256 | (transient-define-prefix kubel-log-popup () 1257 | "Kubel Log Menu" 1258 | ["Arguments" 1259 | ("-f" "Follow" "-f") 1260 | ("-p" "Previous" "-p") 1261 | ("-n" "Tail" "--tail=")] 1262 | ["Actions" 1263 | ("l" "Tail pod logs" kubel-get-pod-logs) 1264 | ("i" "Tail initContainer logs" kubel-get-pod-logs--initContainer) 1265 | ("L" "Tail by labels" kubel-get-logs-by-labels)]) 1266 | 1267 | (transient-define-prefix kubel-copy-popup () 1268 | "Kubel Copy Menu" 1269 | ["Actions" 1270 | ("c" "Copy resource name" kubel-copy-resource-name) 1271 | ("l" "Copy pod log command" kubel-copy-log-command) 1272 | ("p" "Copy command prefix" kubel-copy-command-prefix) 1273 | ("C" "Copy last command" kubel-copy-last-command)]) 1274 | 1275 | (transient-define-prefix kubel-delete-popup () 1276 | "Kubel Delete menu" 1277 | ["Arguments" 1278 | ("-f" "Force" "--force --grace-period=0")] 1279 | ["Actions" 1280 | ("k" "Delete resource(s)" kubel-delete-resource)]) 1281 | 1282 | (transient-define-prefix kubel-describe-popup () 1283 | "Kubel Describe Menu" 1284 | ["Arguments" 1285 | ("-y" "Yaml" "-o yaml")] 1286 | ["Actions" 1287 | ("RET" "Describe" kubel-get-resource-details)]) 1288 | 1289 | (transient-define-prefix kubel-help-popup () 1290 | "Kubel Menu" 1291 | [["Actions" 1292 | ;; global 1293 | ("RET" "Resource details" kubel-describe-popup) 1294 | ("E" "Quick edit" kubel-quick-edit) 1295 | ("g" "Refresh" kubel-refresh) 1296 | ("b" "Buffers" kubel-switch-to-buffer) 1297 | ("k" "Delete" kubel-delete-popup) 1298 | ("r" "Rollout" kubel-rollout-history)] 1299 | ["" ;; based on current view 1300 | ("p" "Port forward" kubel-port-forward-pod) 1301 | ("l" "Logs" kubel-log-popup) 1302 | ("e" "Exec" kubel-exec-popup) 1303 | ("j" "Jab" kubel-jab-deployment) 1304 | ("S" "Scale replicas" kubel-scale-replicas)] 1305 | ["Settings" 1306 | ("C" "Set context" kubel-set-context) 1307 | ("n" "Set namespace" kubel-set-namespace) 1308 | ("R" "Set resource" kubel-set-resource) 1309 | ("K" "Set kubectl config file" kubel-set-kubectl-config-file) 1310 | ("F" "Set output format" kubel-set-output-format)] 1311 | ["Filter" 1312 | ("f" "Filter" kubel-set-filter) 1313 | ("M-n" "Next highlight" kubel-jump-to-next-highlight) 1314 | ("M-p" "Previous highlight" kubel-jump-to-previous-highlight) 1315 | ("s" "Set label selector" kubel-set-label-selector)] 1316 | ["Marking" 1317 | ("m" "Mark item" kubel-mark-item) 1318 | ("u" "Unmark item" kubel-unmark-item) 1319 | ("M" "Mark all items" kubel-mark-all) 1320 | ("U" "Unmark all items" kubel-unmark-all)] 1321 | ["Utilities" 1322 | ("c" "Copy to clipboad..." kubel-copy-popup) 1323 | ("$" "Show Process buffer" kubel-show-process-buffer)]]) 1324 | 1325 | ;; mode map 1326 | (defvar kubel-mode-map 1327 | (let ((map (make-sparse-keymap))) 1328 | ;; global 1329 | (define-key map (kbd "RET") 'kubel-get-resource-details) 1330 | (define-key map (kbd "K") 'kubel-set-kubectl-config-file) 1331 | (define-key map (kbd "C") 'kubel-set-context) 1332 | (define-key map (kbd "n") 'kubel-set-namespace) 1333 | (define-key map (kbd "g") 'kubel-refresh) 1334 | (define-key map (kbd "h") 'kubel-help-popup) 1335 | (define-key map (kbd "?") 'kubel-help-popup) 1336 | (define-key map (kbd "F") 'kubel-set-output-format) 1337 | (define-key map (kbd "R") 'kubel-set-resource) 1338 | (define-key map (kbd "k") 'kubel-delete-popup) 1339 | (define-key map (kbd "f") 'kubel-set-filter) 1340 | (define-key map (kbd "r") 'kubel-rollout-history) 1341 | (define-key map (kbd "E") 'kubel-quick-edit) 1342 | (define-key map (kbd "M-n") 'kubel-jump-to-next-highlight) 1343 | (define-key map (kbd "M-p") 'kubel-jump-to-previous-highlight) 1344 | (define-key map (kbd "$") 'kubel-show-process-buffer) 1345 | (define-key map (kbd "s") 'kubel-set-label-selector) 1346 | (define-key map (kbd "b") 'kubel-switch-to-buffer) 1347 | ;; based on view 1348 | (define-key map (kbd "p") 'kubel-port-forward-pod) 1349 | (define-key map (kbd "S") 'kubel-scale-replicas) 1350 | (define-key map (kbd "l") 'kubel-log-popup) 1351 | (define-key map (kbd "c") 'kubel-copy-popup) 1352 | (define-key map (kbd "e") 'kubel-exec-popup) 1353 | (define-key map (kbd "!") 'kubel-exec-pod-by-shell-command) 1354 | (define-key map (kbd "j") 'kubel-jab-deployment) 1355 | 1356 | (define-key map (kbd "m") 'kubel-mark-item) 1357 | (define-key map (kbd "u") 'kubel-unmark-item) 1358 | (define-key map (kbd "M") 'kubel-mark-all) 1359 | (define-key map (kbd "U") 'kubel-unmark-all) 1360 | 1361 | map) 1362 | "Keymap for `kubel-mode'.") 1363 | 1364 | (defun kubel--current-state () 1365 | "Show in the Echo Area the current context, namespace, and resource." 1366 | (message (concat 1367 | (format "[Context: %s] [Namespace: %s] [Resource: %s]" kubel-context kubel-namespace kubel-resource) 1368 | (unless (equal kubel-selector "") 1369 | (format " (%s)" kubel-selector))))) 1370 | 1371 | ;;;###autoload 1372 | (defun kubel-refresh (&optional directory) 1373 | "Refresh the current kubel buffer, calling kubectl using the configured 1374 | context, namespace, and resource. 1375 | 1376 | DIRECTORY is optional for TRAMP support." 1377 | (interactive) 1378 | (when directory (setq default-directory directory)) 1379 | (let ((name (kubel--buffer-name))) 1380 | ;; Remove old buffer if exist but not is current buffer 1381 | (if (get-buffer name) 1382 | (unless (equal (buffer-name (current-buffer)) name) 1383 | (kill-buffer (get-buffer name)))) 1384 | (rename-buffer name) 1385 | (message (format "Running kubectl for: %s..." name))) 1386 | (let ((entries (kubel--populate-list))) 1387 | (setq tabulated-list-format (car entries)) 1388 | (setq tabulated-list-entries (cadr entries))) ; TODO handle "No resource found" 1389 | (setq tabulated-list-sort-key kubel--list-sort-key) 1390 | (setq tabulated-list-sort-key nil) 1391 | (tabulated-list-init-header) 1392 | (let ((line-num (line-number-at-pos (point))) 1393 | (current-id (tabulated-list-get-id))) 1394 | (tabulated-list-print t) 1395 | (unless (string-equal current-id (tabulated-list-get-id)) 1396 | ;; tabulated-list could not follow the current entry, then fallback on 1397 | ;; keeping the same line. 1398 | (goto-char (point-min)) 1399 | (forward-line (1- line-num)))) 1400 | (kubel--current-state)) 1401 | 1402 | ;;;###autoload 1403 | (defun kubel-open (context namespace resource &optional directory) 1404 | "Create a new kubel buffer using passed parameters CONTEXT NAMESPACE RESOURCE. 1405 | DIRECTORY is optional for TRAMP support." 1406 | (let ((tmpname "*kubel-tmp*") 1407 | (name (kubel--buffer-name-from-parameters context namespace resource))) 1408 | (if (get-buffer name) 1409 | (pop-to-buffer-same-window name) 1410 | (with-current-buffer (get-buffer-create tmpname) 1411 | (kubel-mode) 1412 | (setq kubel-context context) 1413 | (setq kubel-namespace namespace) 1414 | (setq kubel-resource resource) 1415 | (pop-to-buffer-same-window tmpname) 1416 | (kubel-refresh directory))))) 1417 | 1418 | ;;;###autoload 1419 | (defun kubel (&optional directory) 1420 | "Invoke the kubel buffer. 1421 | DIRECTORY is optional for TRAMP support." 1422 | (interactive) 1423 | (let* ((name (kubel--buffer-name)) 1424 | (buf (or (get-buffer name) 1425 | (get-buffer-create name)))) 1426 | (with-current-buffer buf 1427 | (switch-to-buffer (current-buffer)) 1428 | (unless (eq major-mode 'kubel-mode) 1429 | (kubel-mode)) 1430 | (kubel-refresh directory)))) 1431 | 1432 | (define-derived-mode kubel-mode tabulated-list-mode "Kubel" 1433 | "Special mode for kubel buffers." 1434 | (buffer-disable-undo) 1435 | (kill-all-local-variables) 1436 | (setq truncate-lines t) 1437 | (setq mode-name "Kubel") 1438 | (setq major-mode 'kubel-mode) 1439 | (use-local-map kubel-mode-map) 1440 | (hl-line-mode 1) 1441 | (run-mode-hooks 'kubel-mode-hook)) 1442 | 1443 | (provide 'kubel) 1444 | ;;; kubel.el ends here 1445 | --------------------------------------------------------------------------------