OpenStack:Magnum

From Define Wiki
Revision as of 17:41, 20 December 2018 by Mariusz (talk | contribs) (Fix copy-paste typo)
Jump to navigation Jump to search

Adding custom driver

How to create and test a custom Magnum driver - based on the work done on k8_ubuntu_v1.

Build the driver

Clone Magnum's repo from https://github.com/openstack/magnum. Checkout a stable branch that's the same as the version of deployed OpenStack (you should be good with just using master as templates don't change much). Copy an existing driver from either magnum/drivers or contrib/drivers.

Copy one of the *template_def.py available in magnum/drivers/heat/, rename it so it starts with your driver name and replace all references to the old driver. For example:

drivers/heat/k8s_ubuntu_template_def.py

from magnum.drivers.heat import k8s_template_def
...
class UbuntuK8sTemplateDefinition(k8s_template_def.K8sTemplateDefinition):
    """Kubernetes template for an Ubuntu."""

    def __init__(self):
        super(UbuntuK8sTemplateDefinition, self).__init__()
...

In the driver's driver.py script replace references to the old driver, for example this one is for the Ubuntu driver:

drivers/k8s_ubuntu_v1/driver.py
...
from magnum.drivers.k8s_ubuntu_v1 import template_def
...
    @property
    def provides(self):
        return [
            {'server_type': 'vm',
             'os': 'ubuntu',
             'coe': 'kubernetes'},
        ]

    def get_template_definition(self):
        return template_def.UbuntuK8sTemplateDefinition()
...

There will also be references to the old driver in template_def.py, for example:

drivers/k8s_ubuntu_v1/template_def.py
...
class UbuntuK8sTemplateDefinition(kctd.UbuntuK8sTemplateDefinition):
    """Kubernetes template for an Ubuntu VM."""
...

Add your driver to the list of magnum.drivers in setup.cfg

setup.cfg
...
magnum.drivers =
...
    k8s_ubuntu_v1 = magnum.drivers.k8s_ubuntu_v1.driver:Driver
...

Now for Heat templates in the templates directory of the driver: we want to make them as simple as possible for now. There shouldn't be a need to kubecluster.yaml, at least not initially. In kubemaster.yaml remove all OS::Heat::SoftwareConfig objects from the software configs section of the template and only leave write_heat_params and wc_notify. Same for kubeminion.yaml. Remove scripts from templates/fragments directory - all but fragments/write-heat-params-master.yaml and fragments/write-heat-params-minion.yaml.

Your minimal wc_notify should look like this one:

  wc_notify:
    type: OS::Heat::SoftwareConfig
    properties:
      group: ungrouped
      config:
        str_replace:
          template: |
            #!/bin/bash -v
            wc_notify --data-binary '{"status": "SUCCESS"}'
          params:
            wc_notify: {get_attr: [master_wait_handle, curl_cli]}

Make sure that under software configs there is this MultipartMime:

  kube_master_init:
    type: OS::Heat::MultipartMime
    properties:
      parts:
        - config: {get_resource: write_heat_params}
        - config: {get_resource: wc_notify}

(this is for masters, use kube_minion_init for the minions)

Test the driver

You can build a whole Magnum from the git repo by creating a virutalenv and running python setup.py install. And then run magnum-api and magnum-conductor that you get from the installation.

If you already have a working OpenStack cluster with Magnum deployed with Kolla, it's easier to use it as a test environment by following these steps:

  • (do this once) Add your driver to the [magnum.drivers] list in package entry points:
docker cp magnum_api:/usr/lib/python2.7/site-packages/magnum-6.2.0-py2.7.egg-info/entry_points.txt ~/
vim ~/entry_points.txt
# Add your driver to [magnum.drivers] then proceed
[root@vscaler-vgpu ~]# docker cp ~/entry_points.txt magnum_conductor:/usr/lib/python2.7/site-packages/magnum-6.2.0-py2.7.egg-info/entry_points.txt
[root@vscaler-vgpu ~]# docker cp ~/entry_points.txt magnum_api:/usr/lib/python2.7/site-packages/magnum-6.2.0-py2.7.egg-info/entry_points.txt

  • If you've changed heat/*template_def.py, copy it to both containers:
docker cp ~/magnum/magnum/drivers/heat/k8s_ubuntu_template_def.py magnum_conductor:/usr/lib/python2.7/site-packages/magnum/drivers/heat/
docker cp ~/magnum/magnum/drivers/heat/k8s_ubuntu_template_def.py magnum_api:/usr/lib/python2.7/site-packages/magnum/drivers/heat/

  • Copy your driver to /usr/lib/python2.7/site-packages/magnum/drivers/ for both API and conductor containers like so:
docker cp ~/magnum/magnum/drivers/k8s_ubuntu_v1 magnum_conductor:/usr/lib/python2.7/site-packages/magnum/drivers/
docker cp ~/magnum/magnum/drivers/k8s_ubuntu_v1 magnum_api:/usr/lib/python2.7/site-packages/magnum/drivers/

  • Restart both Magnum containers:
docker restart magnum_api
docker restart magnum_conductor

The 2 last steps should be repeated every time you make changes to the driver.

To test the driver works with Magnum, add appropriate os_distro property to the image you want to use, for example

openstack image set --property os_distro=ubuntu ubuntu-software-config-dgx

then create a simple Magnum cluster template and launch a cluster specifying the COE and the other parameters.

Going further

Send data from host to Heat

You can send back a JSON with the "data" key containing a string parameter, for example a variable:

wc_notify --data-binary '{"status": "SUCCESS", "id": "token", "data": "'$TOKEN'"}'

This variable can then be put in outputs of the template like so:

outputs:
...
  kubeadm_token:
    value: { 'Fn::Select': ['token', {get_attr: [master_wait_condition, data] }] }

and used in the kubecluster.yaml like this:

kubeadm_token: {get_attr: [kube_masters, resource.0.kubeadm_token]}

This allows you to pass data from a master to minions.

Resources

  1. https://docs.openstack.org/heat/queens/template_guide/software_deployment.html


Baking heat components into the image

TODO