mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-03-26 21:33:02 +00:00
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 <yuriy@novostavskiy.kyiv.ua> Reviewed-by: Bianca Henderson <beeankha@gmail.com>
This commit is contained in:
committed by
GitHub
parent
200d64f5ea
commit
87344b93fc
@@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- kustomize - add support of local environ (https://github.com/ansible-collections/kubernetes.core/pull/786).
|
||||
@@ -95,6 +95,26 @@ 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">raw</span>
|
||||
</div>
|
||||
<div style="font-style: italic; font-size: small; color: darkgreen">added in 6.2.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>
|
||||
<div>This can be a dictionary or a string in the format key=value, multiple pairs separated by space.</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">
|
||||
<div class="ansibleOptionAnchor" id="parameter-"></div>
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user