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 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bd3950e
add support of local environ for kustomize lookup plugin
yurnov Oct 29, 2024
5a42b95
add changelog fragment (assuming next PR number)
yurnov Oct 30, 2024
26d85f0
fix W293: blank line contains whitespace
yurnov Oct 30, 2024
4a78413
fix using of evrion
yurnov Oct 30, 2024
4655376
reformat with black
yurnov Oct 30, 2024
e85e95a
change approach of support enviromental variable
yurnov Oct 30, 2024
c6f6723
fix typo envion e -> envrion
yurnov Oct 30, 2024
fcd3517
fix typo envion e -> envrion
yurnov Oct 30, 2024
9ee8385
attemp to fix linter
yurnov Oct 30, 2024
10b8525
temprarly commit to test ansible-network/github_actions/pull/162
yurnov Nov 2, 2024
c4795f5
fix sanity test with ansible-core 2.18 and 2.19
yurnov Nov 2, 2024
bbd760c
revert back ignore-2.14.txt that should not be changed
yurnov Nov 2, 2024
1ac828d
Revert "temprarly commit to test ansible-network/github_actions/pull/…
yurnov Nov 2, 2024
260574d
apply code-review suggestion from @abikouo
yurnov Nov 21, 2024
b575608
fix typo
yurnov Nov 21, 2024
fd8abed
fix typo
yurnov Nov 21, 2024
b10f2ac
Initial commit with integration tests
yurnov Nov 21, 2024
d8b9204
Fix integration tests
yurnov Nov 21, 2024
0e580ba
Fix linter in integration tests
yurnov Nov 21, 2024
f638e65
Fix linter in integration tests
yurnov Nov 21, 2024
756ce7e
Fix integration tests
yurnov Nov 21, 2024
bd00866
another attempt to integration tests
yurnov Nov 21, 2024
e0b3532
another attempt to integration tests
yurnov Nov 21, 2024
00e2525
another attempt to integration tests
yurnov Nov 21, 2024
61d950e
Refactor integration test for kustomize environ support
yurnov Nov 21, 2024
246773d
change version_added to 5.1.0
yurnov Dec 17, 2024
4ea5735
update version_added to 5.2.0 as 5.1.0 released already
yurnov Jan 21, 2025
8360fb0
bump version_added to 6.0.0
yurnov Jan 21, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- kustomize - add support of local environ (https://github.com/ansible-collections/kubernetes.core/pull/786).
23 changes: 23 additions & 0 deletions docs/kubernetes.core.kustomize_lookup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ Parameters
<div>Enable the helm chart inflation generator</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>environment</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 6.0.0</div>
</td>
<td>
<b>Default:</b><br/><div style="color: blue">{}</div>
</td>
<td>
</td>
<td>
<div>The environment variables to pass to the kustomize or kubectl command.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down Expand Up @@ -145,6 +164,10 @@ Examples
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', dir='/path/to/kustomization', enable_helm=True) }}"

- name: Create kubernetes resources for lookup output with environment variables
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment='HTTP_PROXY=http://proxy.example.com:3128') }}"



Return Values
Expand Down
34 changes: 31 additions & 3 deletions plugins/lookup/kustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
description:
- Enable the helm chart inflation generator
default: "False"
environment:
description:
- The environment variables to pass to the kustomize or kubectl command.
type: dict
default: {}
version_added: 6.0.0

requirements:
- "python >= 3.6"
Expand All @@ -55,6 +61,10 @@
- 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 with environment variables
kubernetes.core.k8s:
definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment='HTTP_PROXY=http://proxy.example.com:3128') }}"
"""

RETURN = """
Expand All @@ -72,6 +82,7 @@
key1: val1
"""

import os
import subprocess

from ansible.errors import AnsibleLookupError
Expand All @@ -92,8 +103,10 @@ 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

Expand All @@ -107,6 +120,7 @@ def run(
binary_path=None,
opt_dirs=None,
enable_helm=False,
environment=None,
**kwargs
):
executable_path = binary_path
Expand Down Expand Up @@ -141,7 +155,21 @@ def run(
if enable_helm:
command += ["--enable-helm"]

(ret, out, err) = run_command(command)
environ = None
if environment:
environ = os.environ.copy()
if isinstance(environment, str):
if not all(env.count("=") == 1 for env in environment.split(" ")):
raise AnsibleLookupError(
"environment should be dict or string in the format key=value"
)
for env in environment.split(" "):
key, value = env.split("=")
environ[key] = value
if isinstance(environment, dict):
environ.update(environment)

(ret, out, err) = run_command(command, environ=environ)
if ret != 0:
if err:
raise AnsibleLookupError(
Expand Down
51 changes: 50 additions & 1 deletion tests/integration/targets/lookup_kustomize/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,48 @@
namespace: "{{ kustomize_ns }}"
definition: "{{ lookup('kubernetes.core.kustomize', dir=kustomize_dir, opt_dirs=tmp_dir_path) }}"

- name: Create temporarly directory for test
ansible.builtin.tempfile:
state: directory
suffix: .testkustomize
register: _tmp_dir_kustomize

- name: Download helloWorld example
ansible.builtin.get_url:
url: "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/refs/heads/master/examples/loadHttp/kustomization.yaml"
dest: "{{ _tmp_dir_kustomize.path }}"

- name: Run tinyproxy in docker
# Replace the 'app: hello' with 'app: ${TEST_APP}'
ansible.builtin.command: "docker run --rm -d -p 8888:8888 --name=tinyproxy dannydirect/tinyproxy ANY"

- name: Ensure that tinyproxy is running
ansible.builtin.wait_for:
host: localhost
port: 8888
state: started

- name: Test kustomize lookup plugin with environment variables in the string format
set_fact:
resource_kustomize: "{{ lookup('kubernetes.core.kustomize', dir=_tmp_dir_kustomize.path, environment='HTTPS_PROXY=http://localhost:8888 VAR2=Flase') }}"
ignore_errors: true

- name: Stop tinyproxy
ansible.builtin.command: "docker stop tinyproxy"

- name: Ensure kustomize lookup plugin fail with proxy down
set_fact:
resource_kustomize: "{{ lookup('kubernetes.core.kustomize', dir=_tmp_dir_kustomize.path, environment='HTTPS_PROXY=http://localhost:8888 VAR2=Flase') }}"
register: result
ignore_errors: true

- name: Assert that kustomize lookup plugin failed
ansible.builtin.assert:
that:
- result.failed
- "'proxyconnect tcp: dial' in result.msg"
- "'connection refused' in result.msg"

always:
- name: Delete namespace
k8s:
Expand All @@ -105,4 +147,11 @@
- name: Delete temporary directory
file:
state: absent
path: "{{ tmp_dir_path }}"
path: "{{ item }}"
with_items:
- "{{ tmp_dir_path }}"
- "{{ _tmp_dir_kustomize.path }}"

- name: Stop tinyproxy
ansible.builtin.command: "docker stop tinyproxy"
ignore_errors: true
Loading