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 local environment variables in kustomize lookup plugin #786

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b1bc8f0
add support of local environ for kustomize lookup plugin
yurnov Oct 29, 2024
1e2d5fa
add changelog fragment (assuming next PR number)
yurnov Oct 30, 2024
56f312b
fix W293: blank line contains whitespace
yurnov Oct 30, 2024
99e502c
fix using of evrion
yurnov Oct 30, 2024
ca39be5
reformat with black
yurnov Oct 30, 2024
053147a
change approach of support enviromental variable
yurnov Oct 30, 2024
c29e399
fix typo envion e -> envrion
yurnov Oct 30, 2024
6afefc7
fix typo envion e -> envrion
yurnov Oct 30, 2024
f0691f5
attemp to fix linter
yurnov Oct 30, 2024
605cf01
Merge branch 'ansible-collections:main' into support_local_env_for_ku…
yurnov Nov 2, 2024
9249d9a
temprarly commit to test ansible-network/github_actions/pull/162
yurnov Nov 2, 2024
540a701
empty test commit
yurnov Nov 2, 2024
deff6ee
fix sanity test with ansible-core 2.18 and 2.19
yurnov Nov 2, 2024
0642c72
revert back ignore-2.14.txt that should not be changed
yurnov Nov 2, 2024
fa2caed
Revert "temprarly commit to test ansible-network/github_actions/pull/…
yurnov Nov 2, 2024
7ee1ae3
Merge branch 'ansible-collections:main' into support_local_env_for_ku…
yurnov Nov 4, 2024
27b7c51
apply code-review suggestion from @abikouo
yurnov Nov 21, 2024
e8fa5e6
fix typo
yurnov Nov 21, 2024
99e6e88
fix typo
yurnov Nov 21, 2024
27fd508
Initial commit with integration tests
yurnov Nov 21, 2024
ad80ce1
Fix integration tests
yurnov Nov 21, 2024
0570b35
Fix linter in integration tests
yurnov Nov 21, 2024
60c5ced
Fix linter in integration tests
yurnov Nov 21, 2024
4316b63
Fix integration tests
yurnov Nov 21, 2024
91fe408
another attempt to integration tests
yurnov Nov 21, 2024
749cad0
another attempt to integration tests
yurnov Nov 21, 2024
ad60093
another attempt to integration tests
yurnov Nov 21, 2024
4eded57
Refactor integration test for kustomize environ support
yurnov Nov 21, 2024
f24d82a
Merge branch 'ansible-collections:main' into support_local_env_for_ku…
yurnov Dec 10, 2024
c3d3759
Merge remote-tracking branch 'upstream/main' into support_local_env_f…
yurnov Dec 17, 2024
0aa1db0
change version_added to 5.1.0
yurnov Dec 17, 2024
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
Next Next commit
add support of local environ for kustomize lookup plugin
yurnov committed Oct 29, 2024
commit b1bc8f04f5a34b8b55f032d0ea94dbfa0d838928
30 changes: 30 additions & 0 deletions docs/kubernetes.core.kustomize_lookup.rst
Original file line number Diff line number Diff line change
@@ -112,6 +112,29 @@ Parameters
<div>An optional list of directories to search for the executable in addition to PATH.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>use_local_env</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">boolean</span>
</div>
<div style="font-style: italic; font-size: small; color: darkgreen">added in 3.3.0</div>
</td>
<td>
<ul style="margin: 0; padding: 0"><b>Choices:</b>
<li>no</li>
<li>yes</li>
</ul>
<b>Default:</b><br/><div style="color: blue">"False"</div>
</td>
<td>
</td>
<td>
<div>Use the local environment varaible for the command execution</div>
</td>
</tr>
</table>
<br/>

@@ -145,6 +168,13 @@ Examples
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', dir='/path/to/kustomization', enable_helm=True) }}"
- name: Create kubernetes resources for lookup output considering the local environment variables
environment:
CI: true
HTTP_PROXY: http://proxy.example.com:8080
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', use_local_env=True) }}"
Return Values
22 changes: 20 additions & 2 deletions plugins/lookup/kustomize.py
Original file line number Diff line number Diff line change
@@ -34,6 +34,12 @@
description:
- Enable the helm chart inflation generator
default: "False"
use_local_env:
description:
- Use the local environment varaible for the command execution
default: "False"
type: bool
version_added: 3.3.0
requirements:
- "python >= 3.6"
@@ -55,6 +61,13 @@
- name: Create kubernetes resources for lookup output with `--enable-helm` set
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', dir='/path/to/kustomization', enable_helm=True) }}"
- name: Create kubernetes resources for lookup output considering the local environment variables
environment:
CI: true
HTTP_PROXY: http://proxy.example.com:8080
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', use_local_env=True) }}"
"""

RETURN = """
@@ -72,6 +85,7 @@
key1: val1
"""

import os
import subprocess

from ansible.errors import AnsibleLookupError
@@ -92,8 +106,8 @@ def get_binary_from_path(name, opt_dirs=None):
return None


def run_command(command):
cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def run_command(command, environ=None):
cmd = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environ)
stdout, stderr = cmd.communicate()
return cmd.returncode, stdout, stderr

@@ -107,6 +121,7 @@ def run(
binary_path=None,
opt_dirs=None,
enable_helm=False,
use_local_env=False,
**kwargs
):
executable_path = binary_path
@@ -140,6 +155,9 @@ def run(

if enable_helm:
command += ["--enable-helm"]

if use_local_env:
environ = dict(os.environ)

(ret, out, err) = run_command(command)
if ret != 0: