From 0fce663636e80736c955bf47b64f88b490ed3e34 Mon Sep 17 00:00:00 2001 From: Daniel Caires Date: Thu, 29 Jun 2023 13:15:46 -0300 Subject: [PATCH] Kubernetes dashboard installation stage -Add a new stage that install Kubernetes dashboard and saves a file with the access token in the $HOME of the user. -Change config files folder structure. Test Plan: PASS: kubernetes dashboard is successfully initiated PASS: token.txt is sent to $HOME Story: 2005051 Task: 47937 Change-Id: Id5a872e14c2620e6064e8593ce28d177a113ffaf Signed-off-by: Daniel Caires --- virtualbox/pybox/Parser.py | 23 +++++ virtualbox/pybox/README.md | 6 +- .../ansibleFiles}/localhost.yml | 0 .../pybox/config/kubeFiles/admin-login.yaml | 28 ++++++ .../config/kubeFiles/dashboard-values.yaml | 12 +++ .../labSetupFiles}/lab_setup.conf | 0 .../labSetupFiles}/lab_setup.sh | 0 .../labSetupFiles}/lab_setup1.sh | 0 virtualbox/pybox/install_vbox.py | 93 ++++++++++++++++++- 9 files changed, 156 insertions(+), 6 deletions(-) rename virtualbox/pybox/{configs/aio-sx => config/ansibleFiles}/localhost.yml (100%) create mode 100644 virtualbox/pybox/config/kubeFiles/admin-login.yaml create mode 100644 virtualbox/pybox/config/kubeFiles/dashboard-values.yaml rename virtualbox/pybox/{configs/aio-sx => config/labSetupFiles}/lab_setup.conf (100%) rename virtualbox/pybox/{configs/aio-sx => config/labSetupFiles}/lab_setup.sh (100%) rename virtualbox/pybox/{configs/aio-sx => config/labSetupFiles}/lab_setup1.sh (100%) diff --git a/virtualbox/pybox/Parser.py b/virtualbox/pybox/Parser.py index 006af7d..a7f3a78 100644 --- a/virtualbox/pybox/Parser.py +++ b/virtualbox/pybox/Parser.py @@ -190,6 +190,14 @@ def parse_config_location(parser: ArgumentParser): Path to the config file to use """, action='append') + parser.add_argument("--kubernetes-config-files", help= + """ + Path to a local YAML files (admin-login.yaml and + dashboard-values.yml) that will be copied to the + home directory of the controller-0 for the kubernetes + dashboard instalation and configuration. + """, + type=str) def parse_disk_info(parser: ArgumentParser): @@ -284,6 +292,21 @@ def parse_networking(parser: ArgumentParser): installed. """, type=str) + parser.add_argument("--horizon-dashboard-port", help= + """ + Port for the visualization of the StarlingX + Horizon dashboard. If no port value is set, it defaults to port 8080 + """, + type=str, + default='8080') + parser.add_argument("--kubernetes-dashboard-port", help= + """ + Port for the visualization of the kubernetes + dashboard. If no port value is set, it default + to port 32000 + """, + type=str, + default='32000') def parse_custom_scripts(parser: ArgumentParser): diff --git a/virtualbox/pybox/README.md b/virtualbox/pybox/README.md index 357b612..9020cdb 100644 --- a/virtualbox/pybox/README.md +++ b/virtualbox/pybox/README.md @@ -129,7 +129,6 @@ will be configured and used. ```shell VBoxManage natnetwork add --netname NatNetwork --network 10.10.10.0/24 --dhcp off --ipv6 on - VBoxManage natnetwork modify --netname NatNetwork --port-forward-4 http-8080:tcp:[]:8080:[10.10.10.3]:8080 ``` 3. Checkout the repository, and set up Python's Virtual Environment with: @@ -163,8 +162,9 @@ running it): --setup-type AIO-SX \ --iso-location "$HOME/Downloads/stx-8.iso" \ --labname StarlingX --install-mode serial \ - --config-files-dir ./configs/aio-sx/ \ - --ansible-controller-config ./configs/aio-sx/localhost.yml \ + --config-files-dir ./config/labSetupFiles/ \ + --ansible-controller-config ./config/ansibleFiles/localhost.yml \ + --kubernetes-config-files ./config/kubeFiles/ \ --vboxnet-type nat \ --vboxnet-name NatNetwork \ --nat-controller0-local-ssh-port 3122 \ diff --git a/virtualbox/pybox/configs/aio-sx/localhost.yml b/virtualbox/pybox/config/ansibleFiles/localhost.yml similarity index 100% rename from virtualbox/pybox/configs/aio-sx/localhost.yml rename to virtualbox/pybox/config/ansibleFiles/localhost.yml diff --git a/virtualbox/pybox/config/kubeFiles/admin-login.yaml b/virtualbox/pybox/config/kubeFiles/admin-login.yaml new file mode 100644 index 0000000..cd33790 --- /dev/null +++ b/virtualbox/pybox/config/kubeFiles/admin-login.yaml @@ -0,0 +1,28 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: admin-user + namespace: kube-system +--- +apiVersion: v1 +kind: Secret +type: kubernetes.io/service-account-token +metadata: + name: admin-user-sa-token + namespace: kube-system + annotations: + kubernetes.io/service-account.name: admin-user +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: admin-user +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-admin +subjects: + kind: ServiceAccount + name: admin-user + namespace: kube-system diff --git a/virtualbox/pybox/config/kubeFiles/dashboard-values.yaml b/virtualbox/pybox/config/kubeFiles/dashboard-values.yaml new file mode 100644 index 0000000..90c993a --- /dev/null +++ b/virtualbox/pybox/config/kubeFiles/dashboard-values.yaml @@ -0,0 +1,12 @@ +--- +service: + type: NodePort + nodePort: 32000 + +rbac: + create: true + clusterAdminRole: true + +serviceAccount: + create: true + name: kubernetes-dashboard diff --git a/virtualbox/pybox/configs/aio-sx/lab_setup.conf b/virtualbox/pybox/config/labSetupFiles/lab_setup.conf similarity index 100% rename from virtualbox/pybox/configs/aio-sx/lab_setup.conf rename to virtualbox/pybox/config/labSetupFiles/lab_setup.conf diff --git a/virtualbox/pybox/configs/aio-sx/lab_setup.sh b/virtualbox/pybox/config/labSetupFiles/lab_setup.sh similarity index 100% rename from virtualbox/pybox/configs/aio-sx/lab_setup.sh rename to virtualbox/pybox/config/labSetupFiles/lab_setup.sh diff --git a/virtualbox/pybox/configs/aio-sx/lab_setup1.sh b/virtualbox/pybox/config/labSetupFiles/lab_setup1.sh similarity index 100% rename from virtualbox/pybox/configs/aio-sx/lab_setup1.sh rename to virtualbox/pybox/config/labSetupFiles/lab_setup1.sh diff --git a/virtualbox/pybox/install_vbox.py b/virtualbox/pybox/install_vbox.py index edd7545..323a0ad 100755 --- a/virtualbox/pybox/install_vbox.py +++ b/virtualbox/pybox/install_vbox.py @@ -421,6 +421,12 @@ def create_lab(m_vboxoptions): if 'controller-0' in node: local_port = m_vboxoptions.nat_controller0_local_ssh_port ip_addr = m_vboxoptions.controller0_ip + + # Add port forward rule for StarlingX dashboard visualization at 8080 + rule_name = m_vboxoptions.labname + "-horizon-dashbord" + vboxmanage.vboxmanage_port_forward(rule_name, + m_vboxoptions.vboxnet_name, local_port=m_vboxoptions.horizon_dashboard_port, guest_port='8080', guest_ip=ip_addr) + elif 'controller-1' in node: local_port = m_vboxoptions.nat_controller1_local_ssh_port ip_addr = m_vboxoptions.controller1_ip @@ -940,7 +946,6 @@ def stage_config_controller(stream): # pylint: disable=too-many-locals #Update localhost.yml with system password new_config_ansible = override_ansible_become_pass() - #Send Ansible configuration file to VM LOG.info("Copying Ansible configuration file") destination_ansible = f'/home/{V_BOX_OPTIONS.username}/localhost.yml' @@ -1352,6 +1357,79 @@ def stage_unlock_workers(ssh_client): wait_for_hosts(ssh_client, hosts, 'available') +@connect_to_ssh +def stage_enable_kubernetes(ssh_client): + + ip_addr, port = get_ssh_ip_and_port() + + local_path = V_BOX_OPTIONS.kubernetes_config_files + send_dir( + { + "source": local_path, + "remote_host": ip_addr, + "remote_port": port, + "destination":'/home/' + V_BOX_OPTIONS.username + '/', + "username": V_BOX_OPTIONS.username, "password": V_BOX_OPTIONS.password + } + ) + LOG.info("###### Adding port-forward rule for kubernetes dashboard ######") + + # Add port forward rule for Kubernetes dashboard visualization at 32000 + ip_addr = V_BOX_OPTIONS.controller0_ip + rule_name = V_BOX_OPTIONS.labname + "-kubernetes-dasboard" + + vboxmanage.vboxmanage_port_forward(rule_name, V_BOX_OPTIONS.vboxnet_name, + local_port=V_BOX_OPTIONS.kubernetes_dashboard_port, + guest_port='32000', guest_ip=ip_addr) + + LOG.info("###### Installing Kubernetes dashboard ######") + + _, _, exitcode = run_ssh_cmd(ssh_client, + 'source /etc/platform/openrc && ' + 'source /etc/profile && ' + 'cp /etc/kubernetes/admin.conf ~/.kube/config && ' + 'helm repo update; helm repo add kubernetes-dashboard https://kubernetes.github.io/dashboard/ && ' + 'helm install kubernetes-dashboard kubernetes-dashboard/kubernetes-dashboard -f dashboard-values.yaml', timeout=60) + + if exitcode == 0: + LOG.info("###### Creating an admin-user service account with cluster-admin provileges ######") + + _, _, exitcode2 = run_ssh_cmd(ssh_client, + 'kubectl apply -f admin-login.yaml && kubectl -n kube-system ' + 'describe secret $(kubectl get secret | grep admin-user-sa-token | awk "{print $1}") | tee $HOME/token.txt', timeout=60) + if exitcode2 == 0: + send_token() + LOG.info('##### TOKEN CREATED AND FILE CONTAINING TOKEN SENT TO HOST AT /home/%s #####', getpass.getuser()) + + if exitcode != 0 or exitcode2 != 0: + msg = f'Installation of Kubernetes dashboard failed, expecting exit code of 0 but got {exitcode}.' + LOG.info(msg) + raise Exception(msg) + + +def send_token(): + LOG.info('###### Sending token.txt to /home/%s ######', getpass.getuser()) + ip_addr, port = get_ssh_ip_and_port() + username =V_BOX_OPTIONS.username + password = V_BOX_OPTIONS.password + source = f'/home/{username}/token.txt' + destination = f'/home/{getpass.getuser()}' + + # Send token file to HOME/Desktop using rsync + LOG.info("###### rsync command ######") + cmd = (f'rsync -avL --rsh="/usr/bin/sshpass -p {password} ' + f'ssh -p {port} -o StrictHostKeyChecking=no -l {username}" ' + f'{username}@{ip_addr}:{source}* {destination}') + LOG.info('CMD: %s', cmd) + + with subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) as process: + for line in iter(process.stdout.readline, b''): + LOG.info("%s", line.decode("utf-8").strip()) + process.wait() + if process.returncode: + raise Exception(f'Error in rsync, return code: {process.returncode}') + + def run_custom_script(script, timeout, console, mode): """ Run a custom script on the environment. @@ -1553,6 +1631,7 @@ STG_UNLOCK_STORAGES = "unlock-storages" STG_LAB_SETUP4 = "lab-setup4" STG_UNLOCK_WORKERS = "unlock-workers" STG_LAB_SETUP5 = "lab-setup5" +STG_ENABLE_KUBERNETES = "enable-kubernetes" STG_CUSTOM_SCRIPT1 = "custom-script1" STG_CUSTOM_SCRIPT2 = "custom-script2" STG_CUSTOM_SCRIPT3 = "custom-script3" @@ -1612,6 +1691,9 @@ STAGE_CALLBACKS = { STG_LAB_SETUP5: {CALLBACK: stage_lab_setup5, HELP: "Run lab_setup with one or more --lab-setup-conf files from controller-0."}, + STG_ENABLE_KUBERNETES: + {CALLBACK: stage_enable_kubernetes, + HELP: "Installation and configuration of Kubernetes dashboard"}, STG_CUSTOM_SCRIPT1: {CALLBACK: stage_custom_script1, HELP: "Run a custom script from /home/wrsroot, make sure you" \ @@ -1653,6 +1735,7 @@ AVAILABLE_STAGES = [STG_CREATE_LAB, STG_LAB_SETUP4, STG_UNLOCK_WORKERS, STG_LAB_SETUP5, + STG_ENABLE_KUBERNETES, STG_CUSTOM_SCRIPT1, STG_CUSTOM_SCRIPT2, STG_CUSTOM_SCRIPT3, @@ -1668,6 +1751,7 @@ AIO_SX_STAGES = [ STG_RSYNC_CONFIG, STG_LAB_SETUP1, STG_UNLOCK_CONTROLLER0, + STG_ENABLE_KUBERNETES, ] AIO_DX_STAGES = [ @@ -1681,6 +1765,7 @@ AIO_DX_STAGES = [ STG_LAB_SETUP2, STG_UNLOCK_CONTROLLER1, STG_LAB_SETUP3, + STG_ENABLE_KUBERNETES, ] STD_STAGES = [ @@ -1694,7 +1779,8 @@ STD_STAGES = [ STG_LAB_SETUP2, STG_UNLOCK_CONTROLLER1, STG_LAB_SETUP3, - STG_UNLOCK_WORKERS + STG_UNLOCK_WORKERS, + STG_ENABLE_KUBERNETES, ] STORAGE_STAGES = [ @@ -1711,7 +1797,8 @@ STORAGE_STAGES = [ STG_UNLOCK_STORAGES, STG_LAB_SETUP4, STG_UNLOCK_WORKERS, - STG_LAB_SETUP5 + STG_LAB_SETUP5, + STG_ENABLE_KUBERNETES, ] AIO_SX = 'AIO-SX'