Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Commit

Permalink
MGMT-1318 - add annotations to BMH CRs that include data from bm-inve… (
Browse files Browse the repository at this point in the history
#12)

* MGMT-1318 - add annotations to BMH CRs that include data from bm-inventory (IPs etc')
  • Loading branch information
yevgeny-shnaidman committed Jul 27, 2020
1 parent 9e1c697 commit cdc5fb8
Show file tree
Hide file tree
Showing 15 changed files with 1,169 additions and 38 deletions.
22 changes: 17 additions & 5 deletions Dockerfile.ignition-manifests-and-kubeconfig-generate
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# [TODO] - remove this line, once we are ready to use openshift-installer from the release
FROM quay.io/yshnaidm/openshift-installer
FROM quay.io/ocpmetal/openshift-installer
# [TODO] - add someway to get oc client in order to use it to extract openshift-baremetal-install executable
# FROM quay.io/yshnaidm/oc-image:latest
FROM quay.io/ocpmetal/bm-inventory:latest AS inventory


FROM fedora:31
RUN dnf install -y libvirt-libs pip python && \
FROM centos:8
RUN dnf install -y libvirt-libs python3 findutils && \
yum clean all && \
rm -rf /var/cache/yum


COPY requirements.txt /tmp/requirements.txt
RUN pip install -r /tmp/requirements.txt
COPY --from=inventory /clients/bm-inventory-client-*.tar.gz /build/pip/
RUN pip3 install -r /tmp/requirements.txt
RUN pip3 install ipython
RUN find /build/pip/ -name 'setup.py' -exec dirname {} \; | xargs pip3 install

RUN rm /tmp/*requirements.txt

Expand All @@ -18,11 +24,17 @@ ARG WORK_DIR=/data
RUN mkdir $WORK_DIR
RUN chmod 777 $WORK_DIR

RUN mkdir /root/.docker

# [TODO] - change this line to use openshift-installer from the release, once we are ready
COPY --from=0 /root/installer/openshift-install $WORK_DIR
#COPY --from=0 /root/oc/oc $WORK_DIR
COPY ./render_files.py $WORK_DIR
COPY ./utils.py $WORK_DIR
COPY ./bmh_utils.py $WORK_DIR
COPY ./test_utils.py $WORK_DIR

ENV WORK_DIR=$WORK_DIR

ENV EXEC_PATH=$WORK_DIR/render_files.py
ENTRYPOINT python $EXEC_PATH
ENTRYPOINT python3 $EXEC_PATH
5 changes: 5 additions & 0 deletions Dockerfile.oc-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#This dockerfile require an openshift installer binary in CWD
FROM alpine:latest
RUN mkdir /root/oc
COPY ./oc /root/oc

25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ Building:

Testing:
---------------
You can test generation of files (not the uploading) locally on your laptop
After coping the install-config.yaml.platform to installer-config.yaml and updating installer-config.yaml file template run this image with the directory containing the installer-config.yaml file mounted
for example:
Testing can be done in 2 stages:

1) test generation of the ignition files , locally on your laptop.
a) copy install-config.yaml.baremetal to installer-config.yaml in installer_dir.
b) run ignition-manifests-and-kubeconfig-generate immage that you previously created.

docker run -v $(pwd)/installer_dir:/data/installer_dir -it ignition-manifests-and-kubeconfig-generate:ad6939c67c115cef7877ab7d06d72f2d06cebe0

if no error is printed, then the ignition files are generated in the nstaller_dir

2) test specific manipulations on generated ignition. Currently only BMH annotations generations is checked. This stage must be run only after first stage
a) change permissions of the file generated in the first stage. from installer_dir run:
sudo chmod -R 777 auth bootstrap.ign master.ign metadata.json worker.ign .openshift_install.log .openshift_install_state.json
b) run:
skipper run python3 test_bmh_annotations.py



```
docker run -v $(pwd)/installer_dir:/data/installer_dir -it quay.io/oscohen/ignition-manifests-and-kubeconfig-generate:latest
```
in the mounted dir the ignition files and the kubeconfig will be generated.
you will also be able to see the list of the files that would have been uploaded to s3 in case of an actual run

Usage:
```
Expand Down
77 changes: 77 additions & 0 deletions bmh_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import re
import base64
import yaml
import json


BMH_CR_FILE_PATTERN = 'openshift-cluster-api_hosts'


def is_bmh_cr_file(path):
if BMH_CR_FILE_PATTERN in path:
return True
return False


def get_bmh_dict_from_file(file_data):
source_string = file_data['contents']['source']
base64_string = re.split("base64,", source_string)[1]
decoded_string = base64.b64decode(base64_string).decode()
return yaml.safe_load(decoded_string)


def set_new_bmh_dict_in_file(file_data, bmh_dict):
decoded_string = yaml.dump(bmh_dict)
base64_string = base64.b64encode(decoded_string.encode())
source_string = 'data:text/plain;charset=utf-8;' + 'base64,' + base64_string.decode()
file_data['contents']['source'] = source_string


def is_master_bmh(bmh_dict):
if "-master-" in bmh_dict['metadata']['name']:
return True
return False


def update_credentials_name(bmh_dict):
bmh_dict['spec']['bmc']['credentialsName'] = ''


def set_baremtal_annotation_in_bmh_dict(bmh_dict, annot_dict):
bmh_dict['metadata']['annotations'] = annot_dict


def find_available_inventory_host(hosts_list, is_master):
role = 'master' if is_master else 'worker'
for host in hosts_list:
if host.is_role(role):
return host
return None


def prepare_bmh_annotation_dict(status_dict, hosts_list, is_master):
inventory_host = find_available_inventory_host(hosts_list, is_master)
if inventory_host is None:
return None

annot_dict = dict.copy(status_dict)
nics = inventory_host.get_inventory_host_nics_data()
cpu = inventory_host.get_inventory_host_cpu_data()
storage = inventory_host.get_inventory_host_storage_data()
ram = inventory_host.get_inventory_host_memory()
hostname = inventory_host.get_inventory_host_name()
system_vendor = inventory_host.get_inventory_host_system_vendor()
hardware = {'nics': nics, 'cpu': cpu, 'storage': storage, 'ramMebibytes': ram, 'hostname': hostname, 'systemVendor': system_vendor}
annot_dict['hardware'] = hardware
hosts_list.remove(inventory_host)
return {'baremetalhost.metal3.io/status': json.dumps(annot_dict)}


def update_bmh_cr_file(file_data, hosts_list):
bmh_dict = get_bmh_dict_from_file(file_data)
annot_dict = prepare_bmh_annotation_dict(bmh_dict['status'], hosts_list, is_master_bmh(bmh_dict))
if annot_dict is not None:
# [TODO] - make sure that Kiren fix to openshift-installer is working before removing this fix in 4.6
# update_credentials_name(bmh_dict)
set_baremtal_annotation_in_bmh_dict(bmh_dict, annot_dict)
set_new_bmh_dict_in_file(file_data, bmh_dict)
Loading

0 comments on commit cdc5fb8

Please sign in to comment.