3.4 Context and Namespace

Kubed uses kubectl to interact with Kubernetes clusters, and you can use multiple kubectl contexts in Kubed to work with different Kubernetes clusters in parallel. Within each context, Kubed lets you work in multiple namespaces too. In general, each buffer has its own local context and local namespace, and Kubed commands use these local context and namespace by default. This is similar to how different buffers have different values of default-directory (see (emacs)File Names), and how that affects commands that interact with the filesystem.

For Kubed buffers that display a Kubernetes resource (see Display Resource) or a list of resources (see 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 kubed-default-context-and-namespace-alist determines the default local context and namespace.

In addition to kubectl contexts, Kubed is aware of the host on which it invokes kubectl. When default-directory is a remote location (see (emacs)Remote Files), Kubed runs kubectl on that remote host rather than on the local machine. Each host has its own independent set of kubectl contexts, so you can simultaneously work with Kubernetes clusters reachable from different hosts. See Kubed over Tramp for more details.

User Option: kubed-default-context-and-namespace-alist

Association list mapping hosts to their default kubectl context and Kubernetes namespace for Kubed commands. Each entry is a cons cell (host . context-and-namespace), where host is either a string identifying a connection to the host (as returned by file-remote-p, e.g. /ssh:[email protected]:), or nil for the local host. context-and-namespace is either a cons cell (context . namespace), or nil. If it is nil, Kubed initializes the entry to a cons cell with the current kubectl context and the default namespace of that context on the first time Kubed consults this option for the given host.

For example, you can configure Kubed to use the my-context context and my-namespace namespace on the local host by default as follows:

(setopt kubed-default-context-and-namespace-alist
        '((nil . ("my-context" . "my-namespace"))))

;; Or, if you want to preserves other entries:

(setf (alist-get nil kubed-default-context-and-namespace-alist)
      '("my-context" . "my-namespace"))
Variable: kubed-default-context-and-namespace

This is an obsolete variable that served a similar purpose as kubed-default-context-and-namespace-alist prior to Kubed gaining support for remote hosts. For backward compatibility with user configurations that set this variable, Kubed by default initializes the local host entry in kubed-default-context-and-namespace-alist from the value of this variable if it is set.

As mentioned above, most Kubed commands use the buffer-local context and namespace if available by default. It is also conventional for Kubed commands to let you choose a different namespace if you invoke them with a prefix argument (C-u), and to also let you choose a different context if you invoke them with a double prefix argument (C-u C-u).

The following functions return the buffer-local context and namespace for the buffer in which you call them:

Function: kubed-local-context

Return the local kubectl context for the current buffer.

Function: kubed-local-namespace

Return the local Kubernetes namespace for the current buffer.

Function: kubed-local-context-and-namespace

Return the local context and namespace for the current buffer as a cons cell (context . namespace).