Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for propagating environment variables #110

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ Alternatively, you can hit `E` to then select the resource type and the resource
- auto - default, use `kubectl auth can-i list namespace` to determine if we can list namespaces
- on - always assume we can list namespaces
- off - always assume we cannot list namespaces
- If you want to propagate environment variables, you can use
customize on `kubel-env-variables` to propagate it. This is useful
for scenarios where you are using something like [envrc](https://github.com/purcell/envrc) to
manage multiple clusters:

``` emacs-lisp
(customize-set-variable 'kubel-env-variables '("KUBECONFIG" "AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY"))
```

## Releases

Expand Down
17 changes: 14 additions & 3 deletions kubel.el
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@
:type '(file :must-match t)
:group 'kubel)

(defcustom kubel-env-variables nil
"The environment variables that needs to be propagated in kubel session."
:type '(list string)
:group 'kubel)

(defconst kubel--process-buffer "*kubel-process*"
"Kubel process buffer name.")

Expand Down Expand Up @@ -256,14 +261,20 @@ CMD is the kubectl command as a list."
(kubel--append-to-process-buffer
(format "[%s]\ncommand: %s" process-name str-cmd))))

(defun kubel--env-values ()
"Utility function to propgate kubernetes related environment variables."
(mapcar (lambda(env) `(:name ,env :value ,(getenv env))) kubel-env-variables))

(defun kubel--exec-to-string (cmd)
"Replace \"shell-command-to-string\" to log to process buffer.

CMD is the command string to run."
(kubel--log-command "kubectl-command" cmd)
(with-output-to-string
(with-current-buffer standard-output
(shell-command cmd t "*kubel stderr*"))))
(let ((env-values (kubel--env-values)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the only place where you need to do that? what about kubel--exec?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only place where it was required for me to unblock it and have a working kubel version in my envrc based setup.

Note that it still isn't perfect and has some shortcomings which I plan to address in a subsequent PR. But I believe those changes are going to be slightly big (I have a working copy locally, but I'm still testing out various scenarios).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect things are going to be very wonky if the changes are only applied to this function and not also kubel--exec

(with-output-to-string
(with-current-buffer standard-output
(mapcar (lambda (env) (setenv (plist-get env ':name) (plist-get env ':value))) env-values)
(shell-command cmd t "*kubel stderr*")))))

(defvar kubel-namespace "default"
"Current namespace.")
Expand Down