Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Set the connection plugin and transport separately, add tests #208

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
84 changes: 84 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,87 @@

roles:
- helm

post_tasks:
- name: Ensure namespace exists
k8s:
api_version: v1
kind: Namespace
name: inventory

- name: Add a deployment
k8s:
definition:
apiVersion: apps/v1
kind: Deployment
metadata:
name: inventory
namespace: inventory
spec:
replicas: 1
selector:
matchLabels:
app: "{{ k8s_pod_name }}"
template: "{{ k8s_pod_template }}"
wait: yes
wait_timeout: 120
vars:
k8s_pod_name: inventory
k8s_pod_image: python
k8s_pod_command:
- python
- '-m'
- http.server
k8s_pod_env:
- name: TEST
value: test

- meta: refresh_inventory

- name: Verify inventory and connection plugins
hosts: namespace_inventory_pods
gather_facts: no

vars:
file_content: |
Hello world
tasks:
- name: End play if host not running (TODO should we not add these to the inventory?)
meta: end_host
when: pod_phase != "Running"

- debug: var=hostvars
- setup:

- debug: var=ansible_facts

- name: Assert the TEST environment variable was retrieved
assert:
that: ansible_facts.env.TEST == 'test'

- name: Copy a file into the host
copy:
content: '{{ file_content }}'
dest: /tmp/test_file

- name: Retrieve the file from the host
slurp:
src: /tmp/test_file
register: slurped_file

- name: Assert the file content matches expectations
assert:
that: (slurped_file.content|b64decode) == file_content

- name: Delete inventory namespace
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: Remove inventory namespace
k8s:
api_version: v1
kind: Namespace
name: inventory
state: absent
5 changes: 5 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ platforms:
provisioner:
name: ansible
log: true
config_options:
inventory:
enable_plugins: community.kubernetes.k8s
lint: {}
inventory:
hosts:
plugin: community.kubernetes.k8s
host_vars:
localhost:
ansible_python_interpreter: '{{ ansible_playbook_python }}'
Expand Down
3 changes: 3 additions & 0 deletions molecule/default/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ k8s_pod_spec:
- /bin/true
resources: "{{ k8s_pod_resources }}"
ports: "{{ k8s_pod_ports }}"
env: "{{ k8s_pod_env }}"


k8s_pod_service_account: default
Expand All @@ -30,6 +31,8 @@ k8s_pod_command: []

k8s_pod_ports: []

k8s_pod_env: []

k8s_pod_template:
metadata: "{{ k8s_pod_metadata }}"
spec: "{{ k8s_pod_spec }}"
Expand Down
3 changes: 2 additions & 1 deletion plugins/inventory/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class K8sInventoryException(Exception):
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleMixin):
NAME = 'community.kubernetes.k8s'

connection_plugin = 'kubectl'
transport = 'kubectl'

def parse(self, inventory, loader, path, cache=True):
Expand Down Expand Up @@ -276,7 +277,7 @@ def get_pods_for_namespace(self, client, name, namespace):
self.inventory.set_variable(container_name, 'container_state', 'Waiting')
self.inventory.set_variable(container_name, 'container_ready', container.ready)
self.inventory.set_variable(container_name, 'ansible_remote_tmp', '/tmp/')
self.inventory.set_variable(container_name, 'ansible_connection', self.transport)
self.inventory.set_variable(container_name, 'ansible_connection', self.connection_plugin)
self.inventory.set_variable(container_name, 'ansible_{0}_pod'.format(self.transport),
pod_name)
self.inventory.set_variable(container_name, 'ansible_{0}_container'.format(self.transport),
Expand Down