From 1a1f792afdb66343645a3703f4fccb65ba3c11d1 Mon Sep 17 00:00:00 2001 From: hu xinhui Date: Fri, 29 Sep 2017 17:06:33 +0800 Subject: Modification for kargo to generate dynamic inventory JIRA: - There is some hardcode for kargo inventory.This patch is to generate dynamic inventory for kargo Change-Id: If4a7c5f973e520e4ca24c3c9e0bfa97725a5a44c Signed-off-by: hu xinhui (cherry picked from commit 179cd6db9f1bd9be1606187cf74e128bede7a8e6) --- .../roles/kargo/files/generate_inventories.py | 79 +++++++++---- .../ansible/kubernetes/roles/kargo/tasks/main.yml | 9 +- deploy/compass_conf/flavor/kubernetes.conf | 2 +- deploy/compass_conf/flavor_mapping/kubernetes.conf | 0 .../flavor_metadata/ansible-kubernetes.conf | 0 .../package_installer/ansible-kubernetes.conf | 2 +- deploy/compass_conf/role/kubernetes_ansible.conf | 126 +++------------------ .../kubernetes/hosts/ansible-kubernetes.tmpl | 20 ++-- .../kubernetes/vars/ansible-kubernetes.tmpl | 6 +- .../conf/vm_environment/k8-nosdn-nofeature-ha.yml | 22 ++-- 10 files changed, 98 insertions(+), 168 deletions(-) mode change 100644 => 100755 deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py mode change 100644 => 100755 deploy/compass_conf/flavor/kubernetes.conf mode change 100644 => 100755 deploy/compass_conf/flavor_mapping/kubernetes.conf mode change 100644 => 100755 deploy/compass_conf/flavor_metadata/ansible-kubernetes.conf mode change 100644 => 100755 deploy/compass_conf/package_installer/ansible-kubernetes.conf mode change 100644 => 100755 deploy/compass_conf/role/kubernetes_ansible.conf diff --git a/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py b/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py old mode 100644 new mode 100755 index 62f29d84..2ffb4cae --- a/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py +++ b/deploy/adapters/ansible/kubernetes/roles/kargo/files/generate_inventories.py @@ -1,26 +1,32 @@ import yaml import sys +import os from jinja2 import Environment +try: + import json +except ImportError: + import simplejson as json INVENTORY_TEMPLATE = """ [all] -{% for host, ip in hosts.iteritems() %} -{{ host }} ansible_ssh_host={{ ip }} ansible_ssh_pass=root ansible_user=root +{% for host, vales in hostvars.iteritems() %} +{{ host }} ansible_ssh_host={{ vales['ansible_ssh_host'] }} \ +ansible_ssh_pass=root ansible_user=root {% endfor %} [kube-master] -host1 -host2 +{% for host in kube_master %} +{{ host }} +{% endfor %} [etcd] -host1 -host2 -host3 +{% for host in etcd %} +{{ host }} +{% endfor %} [kube-node] -host2 -host3 -host4 -host5 +{% for host in kube_node %} +{{ host }} +{% endfor %} [k8s-cluster:children] kube-node @@ -31,27 +37,50 @@ kube-master """ -def create_inventory_file(inventories_path, hosts): - content = Environment().from_string(INVENTORY_TEMPLATE).render(hosts=hosts) +def _byteify(data, ignore_dicts=False): + + if isinstance(data, unicode): + return data.encode('utf-8') + if isinstance(data, list): + return [_byteify(item, ignore_dicts=True) for item in data] + if isinstance(data, dict) and not ignore_dicts: + return { + _byteify(key, ignore_dicts=True): + _byteify(value, ignore_dicts=True) + for key, value in data.iteritems() + } + return data + + +def load_inventory(inventory): + if not os.path.exists(inventory): + raise RuntimeError('file: %s not exist' % inventory) + with open(inventory, 'r') as fd: + return json.load(fd, object_hook=_byteify) + + +def create_inventory_file(inventories_path, + hostvars, kube_master, etcd, kube_node): + content = Environment().from_string(INVENTORY_TEMPLATE).render( + hostvars=hostvars, kube_master=kube_master, + etcd=etcd, kube_node=kube_node) with open(inventories_path, 'w+') as f: f.write(content) -def fetch_all_sorted_external_ip(ip_cfg): - hosts = {} - for host, settings in ip_cfg.iteritems(): - external = settings["external"]["ip"] - hosts[host] = external - return hosts - +def main(inventories_path, local_inventory): + inventory_data = load_inventory(local_inventory) + hostvars = inventory_data['_meta']['hostvars'] + kube_node = inventory_data['kube_node']['hosts'] + kube_master = inventory_data['kube_master']['hosts'] + etcd = inventory_data['etcd']['hosts'] -def main(inventories_path, ip_cfg): - hosts = fetch_all_sorted_external_ip(ip_cfg) - create_inventory_file(inventories_path, hosts) + create_inventory_file(inventories_path, + hostvars, kube_master, etcd, kube_node) if __name__ == "__main__": path = yaml.load(sys.argv[1]) - ipv_cfg = yaml.load(sys.argv[2]) + local_inventory = yaml.load(sys.argv[2]) - main(path, ipv_cfg) + main(path, local_inventory) diff --git a/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml b/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml index 4e902606..d3dcb878 100644 --- a/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml +++ b/deploy/adapters/ansible/kubernetes/roles/kargo/tasks/main.yml @@ -58,11 +58,18 @@ tags: - ansible +- name: copy inventoriy.json file + copy: + src: /var/ansible/run/kubernetes-opnfv2/inventories/inventory.json + dest: /tmp/inventory.json + tags: + - ansible + - name: generate kargo inventories shell: > python /tmp/generate_inventories.py \ "/opt/kargo_k8s/inventory/inventory.cfg" \ - "{{ ip_settings | to_json }}" + "/tmp/inventory.json" tags: - ansible diff --git a/deploy/compass_conf/flavor/kubernetes.conf b/deploy/compass_conf/flavor/kubernetes.conf old mode 100644 new mode 100755 index 96b5f95f..35c43155 --- a/deploy/compass_conf/flavor/kubernetes.conf +++ b/deploy/compass_conf/flavor/kubernetes.conf @@ -4,7 +4,7 @@ FLAVORS = [{ 'display_name': 'ansible-kubernetes', 'template': 'ansible-kubernetes.tmpl', 'roles': [ - 'controller', 'compute', 'ha', 'odl', 'onos', 'opencontrail', 'ceph', 'ceph-adm', 'ceph-mon', 'ceph-osd', 'sec-patch', 'ceph-osd-node' + 'kube_master', 'etcd', 'kube_node' ], }] diff --git a/deploy/compass_conf/flavor_mapping/kubernetes.conf b/deploy/compass_conf/flavor_mapping/kubernetes.conf old mode 100644 new mode 100755 diff --git a/deploy/compass_conf/flavor_metadata/ansible-kubernetes.conf b/deploy/compass_conf/flavor_metadata/ansible-kubernetes.conf old mode 100644 new mode 100755 diff --git a/deploy/compass_conf/package_installer/ansible-kubernetes.conf b/deploy/compass_conf/package_installer/ansible-kubernetes.conf old mode 100644 new mode 100755 index c706ccb2..32590c82 --- a/deploy/compass_conf/package_installer/ansible-kubernetes.conf +++ b/deploy/compass_conf/package_installer/ansible-kubernetes.conf @@ -7,7 +7,7 @@ SETTINGS = { 'playbook_file': 'site.yml', 'inventory_file': 'inventory.py', 'inventory_json_file': 'inventory.json', - 'inventory_group': ['controller', 'compute', 'ha', 'odl', 'onos', 'opencontrail', 'ceph_adm', 'ceph_mon', 'ceph_osd', 'moon'], + 'inventory_group': ['kube_master', 'etcd', 'kube_node'], 'group_variable': 'all', 'etc_hosts_path': 'roles/pre-k8s/templates/hosts', 'runner_dirs': ['roles','kubernetes/roles'] diff --git a/deploy/compass_conf/role/kubernetes_ansible.conf b/deploy/compass_conf/role/kubernetes_ansible.conf old mode 100644 new mode 100755 index 89c03d94..ae096f47 --- a/deploy/compass_conf/role/kubernetes_ansible.conf +++ b/deploy/compass_conf/role/kubernetes_ansible.conf @@ -1,115 +1,15 @@ ADAPTER_NAME = 'kubernetes' ROLES = [{ - 'role': 'allinone-compute', - 'display_name': 'all in one', - 'description': 'All in One' -}, { - 'role': 'controller', - 'display_name': 'controller node', - 'description': 'Controller Node' -}, { - 'role': 'compute', - 'display_name': 'compute node', - 'description': 'Compute Node' -}, { - 'role': 'storage', - 'display_name': 'storage node', - 'description': 'Storage Node' -}, { - 'role': 'network', - 'display_name': 'network node', - 'description': 'Network Node' -}, { - 'role': 'compute-worker', - 'display_name': 'Compute worker node', - 'description': 'Compute worker node' -}, { - 'role': 'compute-controller', - 'display_name': 'Compute controller node', - 'description': 'Compute controller node' -}, { - 'role': 'network-server', - 'display_name': 'Network server node', - 'description': 'Network server node' -}, { - 'role': 'database', - 'display_name': 'Database node', - 'description': 'Database node' -}, { - 'role': 'messaging', - 'display_name': 'Messaging queue node', - 'description': 'Messaging queue node' -}, { - 'role': 'image', - 'display': 'Image node', - 'description': 'Image node' -}, { - 'role': 'dashboard', - 'display': 'Dashboard node', - 'description': 'Dashboard node' -}, { - 'role': 'identity', - 'display': 'Identity node', - 'description': 'Identity node' -}, { - 'role': 'storage-controller', - 'display': 'Storage controller node', - 'description': 'Storage controller node' -}, { - 'role': 'storage-volume', - 'display': 'Storage volume node', - 'description': 'Storage volume node' -}, { - 'role': 'network-worker', - 'display': 'Network worker node', - 'description': 'Network worker node' -}, { - 'role': 'odl', - 'display': 'open day light', - 'description': 'odl node', - 'optional': True -}, { - 'role': 'onos', - 'display': 'open network operating system', - 'description': 'onos node', - 'optional': True -}, { - 'role': 'opencontrail', - 'display': 'open contrail', - 'description': 'opencontrail node', - 'optional': True -}, { - 'role': 'ha', - 'display': 'Cluster with HA', - 'description': 'Cluster with HA node' -}, { - 'role': 'ceph-adm', - 'display': 'Ceph Admin Node', - 'description': 'Ceph Admin Node', - 'optional': True -}, { - 'role': 'ceph-mon', - 'display': 'Ceph Monitor Node', - 'description': 'Ceph Monitor Node', - 'optional': True -}, { - 'role': 'ceph-osd', - 'display': 'Ceph Storage Node', - 'description': 'Ceph Storage Node', - 'optional': True -}, { - 'role': 'ceph-osd-node', - 'display': 'Ceph osd install from node', - 'description': '', - 'optional': True -}, { - 'role': 'ceph', - 'display': 'ceph node', - 'description': 'ceph node', - 'optional': True -}, { - 'role': 'sec-patch', - 'display': 'sec-patch node', - 'description': 'Security Patch Node', - 'optional': True -}] + 'role': 'kube_master', + 'display_name': 'kubnernets master node', + 'description': ' include kube-api,kube-scheduler' +}, { + 'role': 'etcd', + 'display_name': 'etcd node', + 'description': 'etcd Node' +}, { + 'role': 'kube_node', + 'display_name': 'kube node', + 'description': 'kube Node' +} +] diff --git a/deploy/compass_conf/templates/ansible_installer/kubernetes/hosts/ansible-kubernetes.tmpl b/deploy/compass_conf/templates/ansible_installer/kubernetes/hosts/ansible-kubernetes.tmpl index 9d628b5e..0a9e3025 100644 --- a/deploy/compass_conf/templates/ansible_installer/kubernetes/hosts/ansible-kubernetes.tmpl +++ b/deploy/compass_conf/templates/ansible_installer/kubernetes/hosts/ansible-kubernetes.tmpl @@ -9,22 +9,22 @@ # localhost 127.0.0.1 localhost -#set controllers = $getVar('controller', []) -#set computes = $getVar('compute', []) -#if not $isinstance($controllers, list) - #set controllers = [$controllers] +#set kube_masters = $getVar('kube_master', []) +#set kube_nodes = $getVar('kube_node', []) +#if not $isinstance($kube_masters, list) + #set kube_masters = [$kube_masters] #end if -#if not $isinstance($compute, list) - #set computes = [$computes] +#if not $isinstance($kube_nodes, list) + #set kube_nodes = [$kube_nodes] #end if -# controller -#for worker in $controllers +# kube_master +#for worker in $kube_masters #set worker_hostname = $worker.hostname #set worker_ip = $ip_settings[$worker_hostname].mgmt.ip $worker_ip $worker_hostname #end for -# compute -#for worker in $computes +# kube_node +#for worker in $kube_nodes #set worker_hostname = $worker.hostname #set worker_ip = $ip_settings[$worker_hostname].mgmt.ip $worker_ip $worker_hostname diff --git a/deploy/compass_conf/templates/ansible_installer/kubernetes/vars/ansible-kubernetes.tmpl b/deploy/compass_conf/templates/ansible_installer/kubernetes/vars/ansible-kubernetes.tmpl index 27211e06..440bf7d7 100644 --- a/deploy/compass_conf/templates/ansible_installer/kubernetes/vars/ansible-kubernetes.tmpl +++ b/deploy/compass_conf/templates/ansible_installer/kubernetes/vars/ansible-kubernetes.tmpl @@ -20,8 +20,8 @@ #set has = $getVar('ha', []) #set ha_vip = $getVar('ha_vip', []) -#set controllers = $getVar('controller', []) -#set computers = $getVar('compute', []) +#set kube_masters = $getVar('kube_master', []) +#set kube_nodes = $getVar('kube_node', []) enable_secgroup: $getVar('enable_secgroup', True) enable_fwaas: $getVar('enable_fwaas', True) @@ -35,7 +35,7 @@ network_cfg: $network_cfg sys_intf_mappings: $sys_intf_mappings deploy_type: $getVar('deploy_type', 'virtual') -public_cidr: $computers[0]['install']['subnet'] +public_cidr: $kube_nodes[0]['install']['subnet'] storage_cidr: "{{ ip_settings[inventory_hostname]['storage']['cidr'] }}" mgmt_cidr: "{{ ip_settings[inventory_hostname]['mgmt']['cidr'] }}" diff --git a/deploy/conf/vm_environment/k8-nosdn-nofeature-ha.yml b/deploy/conf/vm_environment/k8-nosdn-nofeature-ha.yml index d098afba..003f41be 100644 --- a/deploy/conf/vm_environment/k8-nosdn-nofeature-ha.yml +++ b/deploy/conf/vm_environment/k8-nosdn-nofeature-ha.yml @@ -14,29 +14,23 @@ FLAVOR: cluster hosts: - name: host1 roles: - - controller - - ha - - ceph-adm - - ceph-mon + - kube_master + - etcd - name: host2 roles: - - controller - - ha - - ceph-mon + - kube_master + - etcd - name: host3 roles: - - controller - - ha - - ceph-mon + - kube_master + - etcd - name: host4 roles: - - compute - - ceph-osd + - kube_node - name: host5 roles: - - compute - - ceph-osd + - kube_node -- cgit 1.2.3-korg