Skip to content

Commit 6f71543

Browse files
yahoonHong Yao
andauthored
dynamically get the nodeport host (#4158)
Greetings, This is an update to the example doc of [`Using Kubevirt`](https://ansible.readthedocs.io/projects/molecule/examples/kubevirt). The purpose is to **dynamically** get the NodePort host to replace the hardcoded hostname that is used for the VM SSH connection. **The problem we solved** By fetching the `hostname` dynamically, we avoid using a pre-defined host for the VM SSH connection, that will reduce the the potential failures by the connection problem with the host. In this update, the hostname for the SSH connection is fetched dynamically, which is the K8S node where the VM is running. I get this tested in our environment with @jangel97 Thanks. Co-authored-by: Hong Yao <[email protected]>
1 parent 2714d30 commit 6f71543

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

docs/examples/kubevirt.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ rules:
4040
- apiGroups: [""]
4141
resources: ["services"]
4242
verbs: ["get", "list", "watch", "create", "delete", "patch", "edit"]
43+
- apiGroups: [""]
44+
resources: ["pods"]
45+
verbs: ["get", "list"]
4346
---
4447
apiVersion: rbac.authorization.k8s.io/v1
4548
kind: RoleBinding
@@ -67,17 +70,17 @@ You will need to substitute the following placeholders:
6770

6871
- This example is using ephemeral VMs, which enhance the speed of VM creation and cleanup. However, it is important to note that any data in the system will not be retained if the VM is rebooted.
6972
- You don't need to worry about setting up SSH keys. The `create.yml` Ansible playbook takes care of configuring a temporary SSH key.
73+
- The hostname for SSH connection to the VM is fetched dynamically, which is the K8S node where the VM is running.
7074

7175
## Config playbook
7276

7377
```yaml title="molecule.yml"
7478
{!tests/fixtures/integration/test_command/molecule/kubevirt/molecule.yml!}
7579
```
7680

77-
Please, replace the following parameters:
81+
Please, replace the following parameter:
7882

7983
- `<Kubernetes VM Namespace>`: This should be replaced with the namespace in Kubernetes where you intend to create the KubeVirt VMs.
80-
- `<Kubernetes Node FQDN>`: Change this to the fully qualified domain name (FQDN) of the Kubernetes node that Ansible will attempt to SSH into via the Service NodePort.
8184

8285
```yaml title="requirements.yml"
8386
{!tests/fixtures/integration/test_command/molecule/kubevirt/requirements.yml!}

tests/fixtures/integration/test_command/molecule/kubevirt/destroy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
loop_control:
1515
loop_var: vm
1616

17-
- name: Delete VM Instance in KubeVirt
17+
- name: Delete NodePort Service in KubeVirt
1818
kubernetes.core.k8s:
1919
state: absent
2020
kind: Service

tests/fixtures/integration/test_command/molecule/kubevirt/molecule.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ platforms:
1010
namespace: <Kubernetes VM Namespace>
1111
ssh_service:
1212
type: NodePort
13-
nodeport_host: <Kubernetes Node FQDN>
1413
ansible_user: cloud-user
1514
memory: 1Gi
1615
- name: rhel8
1716
image: registry.redhat.io/rhel8/rhel-guest-image
1817
namespace: <Kubernetes VM Namespace>
1918
ssh_service:
2019
type: NodePort
21-
nodeport_host: <Kubernetes Node FQDN>
2220
ansible_user: cloud-user
2321
memory: 1Gi
2422
provisioner:

tests/fixtures/integration/test_command/molecule/kubevirt/tasks/create_vm.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,16 @@
5757
- [ sudo, yum, install, -y, qemu-guest-agent ] # Installs qemu-guest-agent
5858
- [ sudo, systemctl, start, qemu-guest-agent ] # Starts qemu-guest-agent
5959
name: cloudinitdisk
60+
61+
- name: Fetch VM pod info
62+
kubernetes.core.k8s_info:
63+
api_version: v1
64+
kind: Pod
65+
label_selectors:
66+
- "vm.kubevirt.io/name={{ vm.name }}"
67+
namespace: "{{ vm.namespace }}"
68+
register: vm_pod_info
69+
70+
- name: Extract the nodename from the VM pod info
71+
ansible.builtin.set_fact:
72+
nodeport_host: "{{ vm_pod_info.resources | map(attribute='spec.nodeName') | list | first }}"

tests/fixtures/integration/test_command/molecule/kubevirt/tasks/create_vm_dictionary.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
{{
1818
molecule_systems | default({}) | combine({
1919
vm.name: {
20-
'ansible_user': 'cloud-user',
21-
'ansible_host': vm.ssh_service.nodeport_host,
20+
'ansible_user': vm.ansible_user,
21+
'ansible_host': nodeport_host,
2222
'ansible_ssh_port': ssh_service_address,
2323
'ansible_ssh_private_key_file': temporary_ssh_key_path
2424
}

0 commit comments

Comments
 (0)