Improve nfs role 84/84684/3
authorMichal Zegan <m.zegan@samsung.com>
Tue, 9 Apr 2019 13:21:52 +0000 (15:21 +0200)
committerSamuli Silvius <s.silvius@partner.samsung.com>
Wed, 17 Apr 2019 08:18:00 +0000 (08:18 +0000)
This commit improves the nfs role by
using cluster_ip variable instead of ansible_host for getting
nfs client/server address, and
using /etc/exports.d instead of /etc/exports for
exporting nfs filesystem.
Also, this commit fixes other
issues in molecule found along the way.

Change-Id: Ibd4ea5d4fd937116e2dc9e1b6f85c3e019f62e17
Issue-ID: OOM-1770
Signed-off-by: Michal Zegan <m.zegan@samsung.com>
ansible/roles/nfs/molecule/default/cleanup.yml [new file with mode: 0644]
ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml [new file with mode: 0644]
ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml [new file with mode: 0644]
ansible/roles/nfs/molecule/default/molecule.yml
ansible/roles/nfs/molecule/default/tests/test_kubernetes-server.py
ansible/roles/nfs/molecule/default/tests/test_nfs-server.py
ansible/roles/nfs/tasks/main.yml
ansible/roles/nfs/templates/exports.j2
ansible/test/roles/cleanup-nfs/tasks/main.yml [new file with mode: 0644]

diff --git a/ansible/roles/nfs/molecule/default/cleanup.yml b/ansible/roles/nfs/molecule/default/cleanup.yml
new file mode 100644 (file)
index 0000000..a085bd5
--- /dev/null
@@ -0,0 +1,6 @@
+---
+- name: Cleanup
+  hosts: all
+  ignore_unreachable: true
+  roles:
+    - cleanup-nfs
diff --git a/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-1.yml
new file mode 100644 (file)
index 0000000..27a521b
--- /dev/null
@@ -0,0 +1,2 @@
+---
+cluster_ip: "{{ ansible_default_ipv4.address }}"
diff --git a/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml b/ansible/roles/nfs/molecule/default/host_vars/kubernetes-node-2.yml
new file mode 100644 (file)
index 0000000..27a521b
--- /dev/null
@@ -0,0 +1,2 @@
+---
+cluster_ip: "{{ ansible_default_ipv4.address }}"
index 71e08d0..f6610ec 100644 (file)
@@ -14,6 +14,7 @@ platforms:
     groups:
       - kubernetes
       - nfs-server
+    purge_networks: true
     networks:
       - name: nfs-net
     volumes:
@@ -27,15 +28,21 @@ platforms:
     command: ${MOLECULE_DOCKER_COMMAND:-""}
     groups:
       - kubernetes
+    purge_networks: true
     networks:
       - name: nfs-net
     volumes:
       - /sys/fs/cgroup:/sys/fs/cgroup:ro
 provisioner:
   name: ansible
+  playbooks:
+    cleanup: cleanup.yml
+  env:
+    ANSIBLE_ROLES_PATH: "../../../../test/roles"
   inventory:
     links:
       group_vars: ../../../../group_vars
+      host_vars: host_vars
   lint:
     name: ansible-lint
 verifier:
index b702a73..0e3710c 100644 (file)
@@ -1,5 +1,6 @@
 import os
 
+import testinfra
 import testinfra.utils.ansible_runner
 
 testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
@@ -7,9 +8,11 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 
 
 def test_nfs_mount(host):
+    node1_ip = testinfra.get_host("docker://kubernetes-node-1").interface(
+        "eth0").addresses[0]
     mp = host.mount_point("/dockerdata-nfs")
     assert mp.exists
-    assert mp.filesystem == "nfs"
-    assert mp.device == "kubernetes-node-1:/dockerdata-nfs"
+    assert mp.filesystem == "nfs" or mp.filesystem == "nfs4"
+    assert mp.device == node1_ip + ":/dockerdata-nfs"
     assert host.file("/etc/fstab").\
-        contains("kubernetes-node-1:/dockerdata-nfs /dockerdata-nfs nfs")
+        contains(node1_ip + ":/dockerdata-nfs /dockerdata-nfs nfs")
index 078c653..88ba0a6 100644 (file)
@@ -1,6 +1,7 @@
 import os
 import pytest
 
+import testinfra
 import testinfra.utils.ansible_runner
 
 testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
@@ -19,7 +20,9 @@ def test_svc(host, svc):
 
 
 def test_exports(host):
-    f = host.file("/etc/exports")
+    node2_ip = testinfra.get_host("docker://kubernetes-node-2").interface(
+        "eth0").addresses[0]
+    f = host.file("/etc/exports.d/dockerdata-nfs.exports")
     assert f.exists
     assert f.content_string == \
-        """/dockerdata-nfs  kubernetes-node-2(rw,sync,no_root_squash,no_subtree_check)"""  # noqa: E501
+        """/dockerdata-nfs  """ + node2_ip + """(rw,sync,no_root_squash,no_subtree_check)"""  # noqa: E501
index e7580b6..1d84887 100644 (file)
@@ -23,7 +23,7 @@
     - name: Add hosts to exports
       template:
         src: exports.j2
-        dest: /etc/exports
+        dest: /etc/exports.d/dockerdata-nfs.exports
       notify:
         - reload nfs
   when:
@@ -35,7 +35,7 @@
 - name: Mount dockerdata-nfs
   mount:
     path: "{{ nfs_mount_path }}"
-    src: "{{ hostvars[groups['nfs-server'].0].ansible_host | default(hostvars[groups['nfs-server'].0].inventory_hostname) }}:{{ nfs_mount_path }}"
+    src: "{{ hostvars[groups['nfs-server'].0].cluster_ip }}:{{ nfs_mount_path }}"
     fstype: nfs
     state: mounted
   when:
index 465c9d2..f49c499 100644 (file)
@@ -1,3 +1,3 @@
 {% for host in groups.kubernetes | difference(groups['nfs-server']) -%}
-    {{ nfs_mount_path }}  {{ hostvars[host].ansible_host | default(hostvars[host].inventory_hostname) }}(rw,sync,no_root_squash,no_subtree_check)
+    {{ nfs_mount_path }}  {{ hostvars[host].cluster_ip }}(rw,sync,no_root_squash,no_subtree_check)
 {% endfor %}
diff --git a/ansible/test/roles/cleanup-nfs/tasks/main.yml b/ansible/test/roles/cleanup-nfs/tasks/main.yml
new file mode 100644 (file)
index 0000000..e0ca4a4
--- /dev/null
@@ -0,0 +1,5 @@
+- name: "Unmount /dockerdata-nfs if mounted"
+  ignore_errors: true
+  mount:
+    path: /dockerdata-nfs
+    state: unmounted