Skip to content

Commit

Permalink
dynamically get the nodeport host
Browse files Browse the repository at this point in the history
Signed-off-by: Hong Yao <[email protected]>
  • Loading branch information
Hong Yao committed Mar 28, 2024
1 parent 2390257 commit c2e8421
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions docs/examples/kubevirt.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ rules:
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "list", "watch", "create", "delete", "patch", "edit"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
Expand Down Expand Up @@ -67,17 +70,17 @@ You will need to substitute the following placeholders:

- 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.
- You don't need to worry about setting up SSH keys. The `create.yml` Ansible playbook takes care of configuring a temporary SSH key.
- The hostname for SSH connection to the VM is fetched dynamically, which is the K8S node where the VM is running.

## Config playbook

```yaml title="molecule.yml"
{!../molecule/kubevirt/molecule.yml!}
```

Please, replace the following parameters:
Please, replace the following parameter:

- `<Kubernetes VM Namespace>`: This should be replaced with the namespace in Kubernetes where you intend to create the KubeVirt VMs.
- `<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.

```yaml title="requirements.yml"
{!../molecule/kubevirt/requirements.yml!}
Expand Down
2 changes: 1 addition & 1 deletion molecule/kubevirt/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
loop_control:
loop_var: vm

- name: Delete VM Instance in KubeVirt
- name: Delete NodePort Service in KubeVirt
kubernetes.core.k8s:
state: absent
kind: Service
Expand Down
2 changes: 0 additions & 2 deletions molecule/kubevirt/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ platforms:
namespace: <Kubernetes VM Namespace>
ssh_service:
type: NodePort
nodeport_host: <Kubernetes Node FQDN>
ansible_user: cloud-user
memory: 1Gi
- name: rhel8
image: registry.redhat.io/rhel8/rhel-guest-image
namespace: <Kubernetes VM Namespace>
ssh_service:
type: NodePort
nodeport_host: <Kubernetes Node FQDN>
ansible_user: cloud-user
memory: 1Gi
provisioner:
Expand Down
13 changes: 13 additions & 0 deletions molecule/kubevirt/tasks/create_vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@
- [ sudo, yum, install, -y, qemu-guest-agent ] # Installs qemu-guest-agent
- [ sudo, systemctl, start, qemu-guest-agent ] # Starts qemu-guest-agent
name: cloudinitdisk

- name: Fetch VM pod info
kubernetes.core.k8s_info:
api_version: v1
kind: Pod
label_selectors:
- "vm.kubevirt.io/name={{ vm.name }}"
namespace: "{{ vm.namespace }}"
register: vm_pod_info

- name: Extract the nodename from the VM pod info
ansible.builtin.set_fact:
nodeport_host: "{{ vm_pod_info.resources | map(attribute='spec.nodeName') | list | first }}"
4 changes: 2 additions & 2 deletions molecule/kubevirt/tasks/create_vm_dictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
{{
molecule_systems | default({}) | combine({
vm.name: {
'ansible_user': 'cloud-user',
'ansible_host': vm.ssh_service.nodeport_host,
'ansible_user': vm.ansible_user,
'ansible_host': nodeport_host,
'ansible_ssh_port': ssh_service_address,
'ansible_ssh_private_key_file': tempoary_ssh_key_path
}
Expand Down

0 comments on commit c2e8421

Please sign in to comment.