"Name of `kubectl' executable to use for interacting with Kubernetes."
:type 'string)
+(defcustom kubed-default-context-and-namespace nil
+ "Default `kubectl' context and Kubernetes namespace.
+
+This is either a cons cell (CONTEXT . NAMESPACE), or nil. If nil, Kubed
+initializes this option to a non-nil value next time Kubed consults it."
+ :type '(choice (const :tag "Initialize from `kubectl' on first use" nil)
+ (cons :tag "Context and namespace"
+ (string :tag "Context")
+ (string :tag "Namespace"))))
+
(defcustom kubed-yaml-setup-hook '(yaml-ts-mode view-mode)
"List of functions to call in Kubernetes resource description YAML buffers.
nil context)))
(when (and (kubed-namespaced-p type context) (null namespace))
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
nil context)))
(when (and (kubed-namespaced-p type context) (null namespace))
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
nil context)))
(when (and (kubed-namespaced-p type context) (null namespace))
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(let ((context (or context (kubed-local-context))))
(kubed-display-resource
,(symbol-name plrl-var) ,resource context
- . ,(when namespaced '((or namespace (kubed-local-namespace context)))))))
+ . ,(when namespaced '((or namespace (kubed-local-namespace)))))))
(defun ,edt-name (,resource &optional context . ,(when namespaced '(namespace)))
,(if namespaced
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(let ((context (or context (kubed-local-context))))
(kubed-edit-resource
,(symbol-name plrl-var) ,resource context
- . ,(when namespaced '((or namespace (kubed-local-namespace context)))))))
+ . ,(when namespaced '((or namespace (kubed-local-namespace)))))))
(defun ,dlt-name (,plrl-var &optional context
. ,(when namespaced '(namespace)))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(let ((context (or context (kubed-local-context))))
(kubed-delete-resources
,(symbol-name plrl-var) ,plrl-var context
- . ,(when namespaced '((or namespace (kubed-local-namespace context)))))))
+ . ,(when namespaced '((or namespace (kubed-local-namespace)))))))
,(if crt-spec `(defun ,crt-name . ,crt-spec)
`(defun ,crt-name (definition &optional context)
context)))
(list context
. ,(when namespaced
- '((let ((cur (kubed-local-namespace context)))
+ '((let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur)))))))
;; introduced in Emacs 31. See Bug#59797.
(kubed--static-if (<= 31 emacs-major-version)
(dired (concat "/kubernetes:" pod "%" kubed-list-namespace ":"))
+ ;; FIXME: Also check context.
(unless (string= kubed-list-namespace (kubed-current-namespace))
(if (y-or-n-p
(format "Starting Dired in a pod in a different namespace \
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(format "Create job `%s' from cronjob" name) nil nil context namespace)
context namespace)))
(let ((context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context))))
+ (namespace (or namespace (kubed-local-namespace))))
(unless (zerop
(call-process
kubed-kubectl-program nil nil nil
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
context namespace)))
(let ((buf (get-buffer-create "*kubed-deployment-status*"))
(context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context))))
+ (namespace (or namespace (kubed-local-namespace))))
(with-current-buffer buf (erase-buffer))
(make-process
:name "*kubed-watch-deployment-status*"
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
context namespace)
context namespace)))
(let ((context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context))))
+ (namespace (or namespace (kubed-local-namespace))))
(unless (zerop
(apply #'call-process
kubed-kubectl-program nil nil nil
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(list name images context namespace replicas port command)))
(let ((context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context))))
+ (namespace (or namespace (kubed-local-namespace))))
(unless (zerop
(apply #'call-process
kubed-kubectl-program nil nil nil
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
"Return current Kubernetes context."
(car (process-lines kubed-kubectl-program "config" "current-context")))
+(defun kubed-default-context-and-namespace ()
+ (or kubed-default-context-and-namespace
+ (setq kubed-default-context-and-namespace
+ (let ((context (kubed-current-context)))
+ (cons context
+ (kubed-current-namespace context))))))
+
+(defun kubed-default-context ()
+ (car (kubed-default-context-and-namespace)))
+
+(defun kubed-default-namespace ()
+ (cdr (kubed-default-context-and-namespace)))
+
(defun kubed-local-context ()
"Return Kubernetes context local to the current buffer."
(or kubed-list-context
(nth 2 kubed-display-resource-info)
- (kubed-current-context)))
+ (kubed-default-context)))
(defvar kubed-context-history nil
"History list for `kubed-read-context'.")
(format "jsonpath={.contexts[?(.name==\"%s\")].context.namespace}"
(or context (kubed-current-context))))))
-(defun kubed-local-namespace (&optional context)
+(defun kubed-local-namespace ()
"Return Kubernetes namespace in CONTEXT local to the current buffer."
(or kubed-list-namespace
(nth 3 kubed-display-resource-info)
(and (string-match "[/|]kubernetes:.*%\\([a-z0-9-]+\\):"
default-directory)
(match-string 1 default-directory)))
- (kubed-current-namespace (or context (kubed-local-context)))))
+ (kubed-default-namespace)))
(defun kubed-local-context-and-namespace ()
"Return (CONTEXT . NAMESPACE) pair local to the current buffer."
(cons context
(or (nth 3 kubed-display-resource-info)
(kubed-current-namespace context))))
- (let ((context (kubed-current-context)))
+ (let ((context (kubed-default-context)))
(cons context
(or (kubed--static-if (<= 31 emacs-major-version)
(and (string-match "[/|]kubernetes:.*%\\([a-z0-9-]+\\):"
default-directory)
(match-string 1 default-directory)))
- (kubed-current-namespace context))))))
+ (kubed-default-namespace))))))
;;;###autoload
(defun kubed-set-namespace (namespace &optional context)
(context (if current-prefix-arg
(kubed-read-context "Context" context)
context)))
- (list (kubed-read-namespace "Set namespace" (kubed-local-namespace context)
+ (list (kubed-read-namespace "Set namespace" (kubed-local-namespace)
nil context)
context)))
(unless (zerop
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(context (if (equal current-prefix-arg '(16))
(kubed-read-context "Context" context)
context))
- (n (kubed-local-namespace context))
+ (n (kubed-local-namespace))
(n (if current-prefix-arg
(kubed-read-namespace "Namespace" n nil context)
n))
(c (kubed-read-container p "Container" nil context n)))
(list p c context n)))
(let* ((context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context)))
+ (namespace (or namespace (kubed-local-namespace)))
(buf (generate-new-buffer
(format "*kubed-logs %s[%s] in %s[%s]*"
pod container namespace context))))
(c (if (equal current-prefix-arg '(16))
(kubed-read-context "Context" c)
c))
- (n (kubed-local-namespace c))
+ (n (kubed-local-namespace))
(n (if current-prefix-arg
(kubed-read-namespace "Namespace" n nil c)
n))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
cxt))))
(unless namespace
(setq namespace
- (let ((cur (kubed-local-namespace context)))
+ (let ((cur (kubed-local-namespace)))
(if current-prefix-arg
(kubed-read-namespace "Namespace" cur nil context)
cur))))
(defun kubed-resource-names (type &optional context namespace)
"Return list of Kuberenetes resources of type TYPE in NAMESPACE via CONTEXT."
(let ((context (or context (kubed-local-context)))
- (namespace (or namespace (kubed-local-namespace context))))
+ (namespace (or namespace (kubed-local-namespace))))
(unless (kubed--alist type context namespace)
(let ((proc (kubed-update type context namespace)))
(while (process-live-p proc)
@menu
* Display Resource::
* Browse Resources::
+* Context and Namespace::
* Transient Menus::
@end menu
current namespace, but if you invoke @code{kubed-display-resource}
with a prefix argument (@kbd{C-u}) and choose a namespaced resource
type, then it also lets you select the namespace of the resource.
+With a double prefix argument, it lets you select which
+@command{kubectl} context to use too. @xref{Context and Namespace}.
If you have @code{kubed-prefix-map} bound to @kbd{C-c k}, you can
invoke @code{kubed-display-resource} with @kbd{C-c k @key{RET}}. This
is available for column names and values. To clear the filter, just
exit the minibuffer with an empty input.
+@node Context and Namespace
+@section Context and Namespace
+
+Kubed uses @command{kubectl} to interact with Kubernetes clusters, and
+you can use multiple @command{kubectl} @dfn{contexts} in Kubed to work
+with different Kubernetes clusters in parallel. Within each context,
+Kubed lets you work in multiple @dfn{namespaces} too. In general,
+each buffer has its own @dfn{local context} and @dfn{local namespace},
+and Kubed commands use these local context and namespace by default.
+This is similar to how different buffers have different values of
+@code{default-directory} (@pxref{File Names,,,emacs}), and how that
+affects commands that interact with the filesystem.
+
+For Kubed buffers that display a Kubernetes resource (@pxref{Display
+Resource}) or a list of resources (@pxref{Browse Resources}), the
+buffer-local context and namespace are the context and namespace of
+the resources the buffer displays. For non-Kubed buffers, the user
+option @code{kubed-default-context-and-namespace} determines the
+default local context and namespace.
+
+@defopt kubed-default-context-and-namespace
+Default @command{kubectl} context and Kubernetes namespace for Kubed
+commands. The value of this option is either a cons cell
+@w{@code{(@var{context} . @var{namespace})}} or @code{nil}. If it is
+@code{nil}, Kubed initializes this option to a cons cell with the
+current @command{kubectl} context and namespace for that context on
+the first time Kubed consults this option.
+@end defopt
+
+As mentioned above, most Kubed commands use the local context and
+namespace by default---it is also conventional for Kubed commands to
+let you choose a different namespace if you invoke them with a prefix
+argument (@kbd{C-u}), and to also let you choose a different context
+if you invoke them with a double prefix argument (@kbd{C-u C-u}).
+
+The following functions return the local context and namespace for
+buffer in which you call them:
+
+@defun kubed-local-context
+Return the local @command{kubectl} context for the current buffer.
+@end defun
+
+@defun kubed-local-namespace
+Return the local Kubernetes namespace for the current buffer.
+@end defun
+
+@defun kubed-local-context-and-namespace
+Return the local context and namespace for the current buffer as a
+cons cell @w{@code{(@var{context} . @var{namespace})}}.
+@end defun
+
@node Transient Menus
@section Transient Menus