From 87344b93fcdc20d2c56b98b814cfec071035a5e3 Mon Sep 17 00:00:00 2001 From: Yuriy Novostavskiy Date: Fri, 3 Oct 2025 16:44:40 +0300 Subject: [PATCH] Add support of local environment variables in kustomize lookup plugin (#786) SUMMARY kustomize doesn't support an environment that makes it impossible to use HTTP_PROXY or provide some templatized parameters. This PR is the result of the issue #783 ISSUE TYPE Feature Pull Request COMPONENT NAME kubernetes.core.kustomize lookup plugin Reviewed-by: Bikouo Aubin Reviewed-by: Yuriy Novostavskiy Reviewed-by: Bianca Henderson --- ...of-evrion-for-kustomize-lookup-plugin.yaml | 2 + docs/kubernetes.core.kustomize_lookup.rst | 28 ++++++++++ plugins/lookup/kustomize.py | 39 ++++++++++++- .../targets/lookup_kustomize/tasks/main.yml | 55 ++++++++++++++++++- 4 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/20241030-support-of-evrion-for-kustomize-lookup-plugin.yaml diff --git a/changelogs/fragments/20241030-support-of-evrion-for-kustomize-lookup-plugin.yaml b/changelogs/fragments/20241030-support-of-evrion-for-kustomize-lookup-plugin.yaml new file mode 100644 index 00000000..21128dd9 --- /dev/null +++ b/changelogs/fragments/20241030-support-of-evrion-for-kustomize-lookup-plugin.yaml @@ -0,0 +1,2 @@ +minor_changes: + - kustomize - add support of local environ (https://github.com/ansible-collections/kubernetes.core/pull/786). diff --git a/docs/kubernetes.core.kustomize_lookup.rst b/docs/kubernetes.core.kustomize_lookup.rst index 423e8aaf..70befc75 100644 --- a/docs/kubernetes.core.kustomize_lookup.rst +++ b/docs/kubernetes.core.kustomize_lookup.rst @@ -95,6 +95,26 @@ Parameters
Enable the helm chart inflation generator
+ + +
+ environment + +
+ raw +
+
added in 6.2.0
+ + + Default:
{}
+ + + + +
The environment variables to pass to the kustomize or kubectl command.
+
This can be a dictionary or a string in the format key=value, multiple pairs separated by space.
+ +
@@ -145,6 +165,14 @@ 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 in string format + kubernetes.core.k8s: + definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment='HTTP_PROXY=http://proxy.example.com:3128') }}" + + - name: Create kubernetes resources for lookup output with environment variables in dict format + kubernetes.core.k8s: + definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment={'HTTP_PROXY': 'http://proxy.example.com:3128'}) }}" + Return Values diff --git a/plugins/lookup/kustomize.py b/plugins/lookup/kustomize.py index c4e84737..029b5854 100644 --- a/plugins/lookup/kustomize.py +++ b/plugins/lookup/kustomize.py @@ -34,6 +34,13 @@ DOCUMENTATION = """ description: - Enable the helm chart inflation generator default: "False" + environment: + description: + - The environment variables to pass to the kustomize or kubectl command. + - This can be a dictionary or a string in the format key=value, multiple pairs separated by space. + type: raw + default: {} + version_added: 6.2.0 requirements: - "python >= 3.6" @@ -55,6 +62,14 @@ EXAMPLES = """ - 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 in string format + kubernetes.core.k8s: + definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment='HTTP_PROXY=http://proxy.example.com:3128') }}" + +- name: Create kubernetes resources for lookup output with environment variables in dict format + kubernetes.core.k8s: + definition: "{{ lookup('kubernetes.core.kustomize', binary_path='/path/to/kubectl', environment={'HTTP_PROXY': 'http://proxy.example.com:3128'}) }}" """ RETURN = """ @@ -72,6 +87,7 @@ RETURN = """ key1: val1 """ +import os import subprocess from ansible.errors import AnsibleLookupError @@ -92,8 +108,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 @@ -107,6 +125,7 @@ class LookupModule(LookupBase): binary_path=None, opt_dirs=None, enable_helm=False, + environment=None, **kwargs ): executable_path = binary_path @@ -141,7 +160,21 @@ class LookupModule(LookupBase): 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, multiple pairs separated by space" + ) + 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( diff --git a/tests/integration/targets/lookup_kustomize/tasks/main.yml b/tests/integration/targets/lookup_kustomize/tasks/main.yml index 3e285868..d37d73e4 100644 --- a/tests/integration/targets/lookup_kustomize/tasks/main.yml +++ b/tests/integration/targets/lookup_kustomize/tasks/main.yml @@ -94,6 +94,52 @@ 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') }}" + + - name: Test kustomize lookup plugin with environment variables in the dict format + set_fact: + resource_kustomize: "{{ lookup('kubernetes.core.kustomize', dir=_tmp_dir_kustomize.path, environment={'HTTPS_PROXY': 'http://localhost:8888', 'VAR2': 'Flase'}) }}" + + + - 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: @@ -105,4 +151,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