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 00000000000..21128dd927f
--- /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 423e8aaff6c..a305bdc4389 100644
--- a/docs/kubernetes.core.kustomize_lookup.rst
+++ b/docs/kubernetes.core.kustomize_lookup.rst
@@ -95,6 +95,25 @@ Parameters
@@ -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
diff --git a/plugins/lookup/kustomize.py b/plugins/lookup/kustomize.py
index c4e84737805..6d88cc44bda 100644
--- a/plugins/lookup/kustomize.py
+++ b/plugins/lookup/kustomize.py
@@ -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: 5.1.0
requirements:
- "python >= 3.6"
@@ -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 = """
@@ -72,6 +82,7 @@
key1: val1
"""
+import os
import subprocess
from ansible.errors import AnsibleLookupError
@@ -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
@@ -107,6 +120,7 @@ def run(
binary_path=None,
opt_dirs=None,
enable_helm=False,
+ environment=None,
**kwargs
):
executable_path = binary_path
@@ -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(
diff --git a/tests/integration/targets/helm_registry_auth/tasks/main.yaml b/tests/integration/targets/helm_registry_auth/tasks/main.yaml
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/tests/integration/targets/lookup_kustomize/tasks/main.yml b/tests/integration/targets/lookup_kustomize/tasks/main.yml
index dafdcdd0bd9..c5b4184bc41 100644
--- a/tests/integration/targets/lookup_kustomize/tasks/main.yml
+++ b/tests/integration/targets/lookup_kustomize/tasks/main.yml
@@ -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:
@@ -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
|