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 support of kubectl_local_env_vars (#698) #702

Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ You can install ``collection_prep`` using

After installation, you can update documentation

# collection_prep_add_docs -p /<PATH_OF_COLLECTION>/kubernetes/core
# collection_prep_add_docs -p /<PATH_OF_COLLECTION>/kubernetes.core
yurnov marked this conversation as resolved.
Show resolved Hide resolved

Review the changes and create a pull request using updated files.

Expand Down
yurnov marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- kubectl - added support of local enviroment variable that will be used for kubectl and may be requried for establishing connections ifself (https://github.com/ansible-collections/kubernetes.core/pull/702)

Check failure on line 3 in changelogs/fragments/20240426-add-support-of-kubectl-local-env-vars-for-connection-plugin.yml

View workflow job for this annotation

GitHub Actions / ansible-lint

yaml[new-line-at-end-of-file]

No new line character at the end of file
21 changes: 21 additions & 0 deletions docs/kubernetes.core.kubectl_connection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,27 @@ Parameters
<div>The configuration can be provided as dictionary. Added in version 2.4.0.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>kubectl_local_env_vars</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">dictionary</span>
</div>
<div style="font-style: italic; font-size: small; color: darkgreen">added in 3.1.0</div>
</td>
<td>
<b>Default:</b><br/><div style="color: blue">{}</div>
</td>
<td>
<div>var: ansible_kubectl_local_env_vars</div>
</td>
<td>
<div>Local enviromantal variable to be passed locally to the kubectl command line.</div>
<div>Please be aware that this passes information directly on the command line and it could expose sensitive data.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down
36 changes: 34 additions & 2 deletions plugins/connection/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
- name: ansible_kubectl_extra_args
env:
- name: K8S_AUTH_EXTRA_ARGS
kubectl_local_env_vars:
description:
- Local enviromantal variable to be passed locally to the kubectl command line.
- Please be aware that this passes information directly on the command line and it could expose sensitive data.
default: {}
type: dict
version_added: 3.1.0
vars:
- name: ansible_kubectl_local_env_vars
kubectl_kubeconfig:
description:
- Path to a kubectl config file. Defaults to I(~/.kube/config)
Expand Down Expand Up @@ -301,6 +310,20 @@ def _build_exec_cmd(self, cmd):

return local_cmd, censored_local_cmd

def _local_env(self):
"""Return a dict of local environment variables to pass to the kubectl command"""
local_env = {}
local_local_env_vars_name = "{0}_local_env_vars".format(self.transport)
local_env_vars = self.get_option(local_local_env_vars_name)
if local_env_vars:
if isinstance(local_env_vars, dict):
local_env_vars = json.dumps(local_env_vars)
local_env = dict(os.environ)
yurnov marked this conversation as resolved.
Show resolved Hide resolved
local_env.update(json.loads(local_env_vars))
return local_env
else:
return None
yurnov marked this conversation as resolved.
Show resolved Hide resolved

def _connect(self, port=None):
"""Connect to the container. Nothing to do"""
super(Connection, self)._connect()
Expand Down Expand Up @@ -329,6 +352,7 @@ def exec_command(self, cmd, in_data=None, sudoable=False):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self._local_env(),
)

stdout, stderr = p.communicate(in_data)
Expand Down Expand Up @@ -378,7 +402,11 @@ def put_file(self, in_path, out_path):
args = [to_bytes(i, errors="surrogate_or_strict") for i in args]
try:
p = subprocess.Popen(
args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE
args,
stdin=in_file,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self._local_env(),
)
except OSError:
raise AnsibleError(
Expand Down Expand Up @@ -415,7 +443,11 @@ def fetch_file(self, in_path, out_path):
) as out_file:
try:
p = subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=out_file, stderr=subprocess.PIPE
args,
stdin=subprocess.PIPE,
stdout=out_file,
stderr=subprocess.PIPE,
env=self._local_env(),
)
except OSError:
raise AnsibleError(
Expand Down
Loading