Controller

This library makes use of something called controllers. A controller is a class that controls one resource. The controllers can (and should) be used to create, get and update resources.

Available controllers are:

In addition to this:

Abstract controllers (internal only):

Note

Please read the class documentations carefully.

User Controller

class lab_orchestrator_lib.controller.controller.UserController(adapter)

Bases: object

User controller.

This is the only controller that has no create, delete or save methods because the lab orchestrator lib doesn’t create users. It’s part of the program that uses this library to manage the users. That means you can create, update and delete users by your own without using this controller.

Methods

__init__(adapter)

Initializes a user controller.

Parameters

adapter (UserAdapterInterface) – User adapter that should be used.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier (Union[str, int]) – The identifier of the object.

Return type

User

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[User]

Returns

A list of all objects of the adapter.

Namespace Controller

class lab_orchestrator_lib.controller.controller.NamespaceController(registry, template_engine=None)

Bases: lab_orchestrator_lib.controller.kubernetes_controller.NotNamespacedController

Controller of Kubernetes Namespaces.

When you need to create, delete or get namespaces in your program you need to use this controller. Usually you don’t need to create, get or delete namespaces by your own.

Methods

__init__(registry, template_engine=None)

Initializes a KubernetesController.

Parameters
  • registry (APIRegistry) – The APIRegistry that should be used.

  • template_engine (Optional[TemplateEngine]) – Optional template engine that is used to read the templates. If set to None the default is lab_orchestrator_lib.template_engine.TemplateEngine.

create(namespace)

Creates a new namespace.

Parameters

namespace – The name of the namespace.

Returns

YAML str of the namespace.

delete(identifier)

Deletes a specific object.

Parameters

identifier – Identifier of the object.

Returns

A string that contains the deletion status.

get(identifier)

Gives a specific object.

Parameters

identifier – Identifier of the object.

Returns

A YAML string that contains the object.

get_list()

Gives a list of all objects.

Returns

A YAML string that contains all objects.

Network Policy Controller

class lab_orchestrator_lib.controller.controller.NetworkPolicyController(registry, template_engine=None)

Bases: lab_orchestrator_lib.controller.kubernetes_controller.NamespacedController

Controller of Kubernetes Namespaces.

When you need to create, delete or get network policies in your program you need to use this controller. Usually you don’t need to create, get or delete network policies by you own.

Methods

__init__(registry, template_engine=None)

Initializes a network policy controller.

Parameters
  • registry (APIRegistry) – The APIRegistry that should be used.

  • template_engine (Optional[TemplateEngine]) – The template engine that should be used. If none: a default one is used.

create(namespace)

Creates a new network policy.

Parameters

namespace – The name of the namespace where the network policy should be created.

Returns

YAML str of the network policy.

delete(namespace, identifier)

Deletes a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A string that contains the deletion status.

get(namespace, identifier)

Gives a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A YAML string that contains the object.

get_list(namespace)

Gives a list of all objects in the namespace.

Parameters

namespace – Namespace where to get the objects from.

Return type

str

Returns

A YAML string that contains all objects.

Docker Image Controller

class lab_orchestrator_lib.controller.controller.DockerImageController(adapter)

Bases: Generic[lab_orchestrator_lib.controller.adapter_controller.Adapter, lab_orchestrator_lib.controller.adapter_controller.LibModelType]

Docker image controller.

This controller is used by the library to get access to docker images. When you want to add new docker images, get or delete old ones you can do it directly without using this controller.

Methods

__init__(adapter)

Initializes a docker image controller.

Parameters

adapter (DockerImageAdapterInterface) – The docker image adapter that is used to connect to the database.

create(name, description, url)

Creates a new docker image.

Parameters
  • name – Name of the docker image.

  • description – Description of the docker image.

  • url – Url of the docker image.

Return type

DockerImage

Returns

The created docker image.

delete(identifier)

Deletes a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

None

Returns

None

filter(**kwargs)

Filters the objects of the adapter and returns all objects that matches the filter criteria.

The database should be filtered by the attributes and belonging values that are given in the kwargs dictionary.

Parameters

kwargs – A dictionary with filters.

Return type

List[~LibModelType]

Returns

All objects that matches the filters.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

~LibModelType

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[~LibModelType]

Returns

A list of all objects of the adapter.

save(obj)

Saves changes of the object to the database.

Parameters

obj (~LibModelType) – The object object that contains changes.

Return type

~LibModelType

Returns

The object.

Lab Docker Image Controller

class lab_orchestrator_lib.controller.controller.LabDockerImageController(adapter)

Bases: Generic[lab_orchestrator_lib.controller.adapter_controller.Adapter, lab_orchestrator_lib.controller.adapter_controller.LibModelType]

Lab docker image controller.

This controller is used by the library to get access to lab docker images. When you want to add new lab docker images, get or delete old ones you can do it directly without using this controller.

Methods

__init__(adapter)

Initializes a lab docker image controller.

Parameters

adapter (LabDockerImageAdapterInterface) – The lab docker image adapter that is used to connect to the database.

create(lab_id, docker_image_id, docker_image_name)

Creates a new lab docker image.

Parameters
  • lab_id (Union[str, int]) – Id of the lab.

  • docker_image_id (Union[str, int]) – Id of the docker image.

  • docker_image_name (str) – Name of the VM.

Return type

LabDockerImage

Returns

The created lab docker image.

delete(identifier)

Deletes a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

None

Returns

None

filter(**kwargs)

Filters the objects of the adapter and returns all objects that matches the filter criteria.

The database should be filtered by the attributes and belonging values that are given in the kwargs dictionary.

Parameters

kwargs – A dictionary with filters.

Return type

List[~LibModelType]

Returns

All objects that matches the filters.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

~LibModelType

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[~LibModelType]

Returns

A list of all objects of the adapter.

save(obj)

Saves changes of the object to the database.

Parameters

obj (~LibModelType) – The object object that contains changes.

Return type

~LibModelType

Returns

The object.

Lab Controller

class lab_orchestrator_lib.controller.controller.LabController(adapter)

Bases: Generic[lab_orchestrator_lib.controller.adapter_controller.Adapter, lab_orchestrator_lib.controller.adapter_controller.LibModelType]

Lab controller.

This controller is used by the library to get access to labs. When you want to add new labs, get or delete old ones you can do it directly without using this controller.

Methods

__init__(adapter)

Initializes a lab controller.

Parameters

adapter (LabAdapterInterface) – The lab adapter that is used to connect to the database.

create(name, namespace_prefix, description)

Creates a new lab.

Parameters
  • name (str) – The name of the lab.

  • namespace_prefix (str) – The namespace prefix of the lab.

  • description (str) – The description of the lab

Return type

Lab

Returns

The created docker image.

delete(identifier)

Deletes a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

None

Returns

None

filter(**kwargs)

Filters the objects of the adapter and returns all objects that matches the filter criteria.

The database should be filtered by the attributes and belonging values that are given in the kwargs dictionary.

Parameters

kwargs – A dictionary with filters.

Return type

List[~LibModelType]

Returns

All objects that matches the filters.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

~LibModelType

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[~LibModelType]

Returns

A list of all objects of the adapter.

save(obj)

Saves changes of the object to the database.

Parameters

obj (~LibModelType) – The object object that contains changes.

Return type

~LibModelType

Returns

The object.

Virtual Machine Instance Controller

class lab_orchestrator_lib.controller.controller.VirtualMachineInstanceController(registry, namespace_ctrl, docker_image_ctrl, lab_docker_image_ctrl, template_engine=None)

Bases: lab_orchestrator_lib.controller.kubernetes_controller.NamespacedController

Controller of KubeVirts VMIs.

When you need to create, delete or get VMIs in your program you need to use this controller. Usually you don’t need to create or delete VMIs by your own. Starting a VMIs is done automatically when you create a lab instance.

Methods

__init__(registry, namespace_ctrl, docker_image_ctrl, lab_docker_image_ctrl, template_engine=None)

Initializes a virtual machine instance controller.

Parameters
  • registry (APIRegistry) – APIRegistry that should be used.

  • namespace_ctrl (NamespaceController) – Namespace controller that should be used.

  • docker_image_ctrl (DockerImageController) – Docker image controller that should be used.

  • lab_docker_image_ctrl (LabDockerImageController) – Lab docker image controller that should be used.

  • template_engine (Optional[TemplateEngine]) – The template engine that should be used. If none: a default one is used.

create(namespace, lab_docker_image)

Creates a new virtual machine instance.

Parameters
  • namespace – Namespace of the virtual machine instance.

  • lab_docker_image (LabDockerImage) – Lab docker image that should be started.

Returns

YAML str of the created virtual machine instance.

delete(namespace, identifier)

Deletes a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A string that contains the deletion status.

get(namespace, identifier)

Gives a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A YAML string that contains the object.

get_list(namespace)

Gives a list of all objects in the namespace.

Parameters

namespace – Namespace where to get the objects from.

Return type

str

Returns

A YAML string that contains all objects.

get_list_of_lab_instance(lab_instance, lab_ctrl)

Gives a list of virtual machine instances that belongs to a specific lab instance.

Parameters
  • lab_instance (LabInstance) – The lab instance.

  • lab_ctrl (LabController) – The lab controller that is used to get the namespace.

Returns

A list of VMIs that belong to this lab instance.

get_of_lab_instance(lab_instance, virtual_machine_instance_id, lab_ctrl)

Gives a specific of virtual machine instance that belongs to a specific lab instance.

Parameters
  • lab_instance (LabInstance) – The lab instance.

  • virtual_machine_instance_id – The id of the vmi.

  • lab_ctrl (LabController) – The lab controller that is used to get the namespace.

Returns

The specific VMI.

Lab Instance Controller

class lab_orchestrator_lib.controller.controller.LabInstanceController(adapter, virtual_machine_instance_ctrl, lab_docker_image_ctrl, namespace_ctrl, lab_ctrl, network_policy_ctrl, user_ctrl, secret_key)

Bases: Generic[lab_orchestrator_lib.controller.adapter_controller.Adapter, lab_orchestrator_lib.controller.adapter_controller.LibModelType]

Controller of lab instances.

When you need to create, get or delete VMIs in your program you need to use this controller. Creating a new lab instance will automatically create a namespace, a network policy and start all VMIs of the referred lab. You should not delete a lab instance by your own, because then the namespace, network policy and the VMIs won’t stop running in Kubernetes.

Methods

__init__(adapter, virtual_machine_instance_ctrl, lab_docker_image_ctrl, namespace_ctrl, lab_ctrl, network_policy_ctrl, user_ctrl, secret_key)

Initializes a lab instance controller.

Parameters
create(lab_id, user_id)

Creates a lab instance.

Creating a lab instance is equivalent to starting a lab for a user. This contains creating a namespace, a network policy in the namespace and one or more virtual machine instances. In addition to this a JWT token will be created with that the user is able to connect to the virtual machine instances through the LabVNC.

Parameters
Return type

LabInstanceKubernetes

Returns

Returns a lab instance kubernetes object.

Raises

Exception – if parameters are invalid.

delete(lab_instance)

Deletes a lab instance.

This also deletes the created namespace with all resources that are contained in this namespace.

Parameters

lab_instance (LabInstance) – The lab instance that should be deleted.

Return type

None

Returns

None

filter(**kwargs)

Filters the objects of the adapter and returns all objects that matches the filter criteria.

The database should be filtered by the attributes and belonging values that are given in the kwargs dictionary.

Parameters

kwargs – A dictionary with filters.

Return type

List[~LibModelType]

Returns

All objects that matches the filters.

static gen_namespace_name(lab, user_id, lab_instance_id)

Generates the namespace name where the resources of a lab instances are created.

The namespace name is generated by a combination of the labs namespace prefix, the user id and the lab instance id. This namespace name is unique for every lab instance.

Parameters
  • lab (Lab) – The lab that is started.

  • user_id – The user that starts this lab.

  • lab_instance_id – The id of the lab instance.

Return type

str

Returns

The name of the namespace.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

~LibModelType

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[~LibModelType]

Returns

A list of all objects of the adapter.

get_list_of_user(user)

Gives a list of lab instances that belong to a specific user.

Parameters

user (User) – The user that belongs to the lab instances.

Return type

List[LabInstance]

Returns

A list of lab instances that belongs to the user.

static get_namespace_name(lab_instance, lab_ctrl)

Returns the namespace name where the resources of a lab instances are created.

The namespace name is generated by a combination of the labs namespace prefix, the user id and the lab instance id. This namespace name is unique for every lab instance.

Parameters
  • lab_instance (LabInstance) – The lab instance from which you want the namespace name.

  • lab_ctrl (LabController) – The lab controller that should be used.

Return type

str

Returns

The name of the namespace.

save(obj)

Removes the inherited save method, because lab instances can’t be changed.

Raises

Exception – Always.

Return type

LabInstance

Controller Collection

A controller collection is a collection of all controllers. You can create one with the lab_orchestrator_lib.controllers.controller_collection.create_controller_collection(...) function. This function takes all adapters, one api registry and a secret key for creating JWT tokens as parameter. The api registry is needed for the Kubernetes controllers and the adapters are injected into the database controllers.

class lab_orchestrator_lib.controller.controller_collection.ControllerCollection(user_ctrl, namespace_ctrl, network_policy_ctrl, docker_image_ctrl, lab_docker_image_ctrl, virtual_machine_instance_ctrl, lab_ctrl, lab_instance_ctrl)

Bases: object

Contains all controllers.

__init__(user_ctrl, namespace_ctrl, network_policy_ctrl, docker_image_ctrl, lab_docker_image_ctrl, virtual_machine_instance_ctrl, lab_ctrl, lab_instance_ctrl)
docker_image_ctrl: lab_orchestrator_lib.controller.controller.DockerImageController
lab_ctrl: lab_orchestrator_lib.controller.controller.LabController
lab_docker_image_ctrl: lab_orchestrator_lib.controller.controller.LabDockerImageController
lab_instance_ctrl: lab_orchestrator_lib.controller.controller.LabInstanceController
namespace_ctrl: lab_orchestrator_lib.controller.controller.NamespaceController
network_policy_ctrl: lab_orchestrator_lib.controller.controller.NetworkPolicyController
user_ctrl: lab_orchestrator_lib.controller.controller.UserController
virtual_machine_instance_ctrl: lab_orchestrator_lib.controller.controller.VirtualMachineInstanceController

Create Controller Collection

This method could be used to create all controllers at once.

lab_orchestrator_lib.controller.controller_collection.create_controller_collection(registry, user_adapter, docker_image_adapter, lab_docker_image_adapter, lab_adapter, lab_instance_adapter, secret_key)

Initializes all controllers.

Parameters
  • registry (APIRegistry) – APIRegistry that should be injected into Kubernetes controllers.

  • user_adapter (UserAdapterInterface) – User adapter that should be injected into the controllers.

  • docker_image_adapter (DockerImageAdapterInterface) – Docker image adapter that should be injected into the controllers.

  • lab_docker_image_adapter (LabDockerImageAdapterInterface) – Lab docker image adapter that should be injected into the controllers.

  • lab_adapter (LabAdapterInterface) – Lab adapter that should be injected into the controllers.

  • lab_instance_adapter (LabInstanceAdapterInterface) – Lab instance adapter that should be injected into the controllers.

  • secret_key (str) – Secret key that should be used to create JWT tokens.

Returns

A controller collection with initialized controllers.

Adapter Controller

class lab_orchestrator_lib.controller.adapter_controller.AdapterController(adapter)

Bases: Generic[lab_orchestrator_lib.controller.adapter_controller.Adapter, lab_orchestrator_lib.controller.adapter_controller.LibModelType]

Generic controller for adapter classes.

This generic class implements some of the methods that are needed for a adapter controller. Whats missing is the create method and some specific methods that can’t be abstracted.

__init__(adapter)

Initializes an adapter controller.

Parameters

adapter (~Adapter) – The adapter of the controller.

delete(identifier)

Deletes a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

None

Returns

None

filter(**kwargs)

Filters the objects of the adapter and returns all objects that matches the filter criteria.

The database should be filtered by the attributes and belonging values that are given in the kwargs dictionary.

Parameters

kwargs – A dictionary with filters.

Return type

List[~LibModelType]

Returns

All objects that matches the filters.

get(identifier)

Gives a specific object of the adapter.

Parameters

identifier – The identifier of the object.

Return type

~LibModelType

Returns

The specific object.

get_all()

Gives all objects of the adapter.

Return type

List[~LibModelType]

Returns

A list of all objects of the adapter.

save(obj)

Saves changes of the object to the database.

Parameters

obj (~LibModelType) – The object object that contains changes.

Return type

~LibModelType

Returns

The object.

Kubernetes Controller

class lab_orchestrator_lib.controller.kubernetes_controller.KubernetesController(registry, template_engine=None)

Bases: object

Base class for Kubernetes controllers.

All Kubernetes controllers should inherit from KubernetesController. This class adds a method to get templates that are used for the resources.

Parameters

template_file – This file is a class attribute and should be overwritten by the class. It should have the filename of a template in lab_orchestrator_lib/templates.

__init__(registry, template_engine=None)

Initializes a KubernetesController.

Parameters
  • registry (APIRegistry) – The APIRegistry that should be used.

  • template_engine (Optional[TemplateEngine]) – Optional template engine that is used to read the templates. If set to None the default is lab_orchestrator_lib.template_engine.TemplateEngine.

template_file = None

Namespaced Controller

class lab_orchestrator_lib.controller.kubernetes_controller.NamespacedController(registry, template_engine=None)

Bases: lab_orchestrator_lib.controller.kubernetes_controller.KubernetesController

Abstract base controller for namespaced resources.

All Kubernetes controllers that uses namespaced api resources should inherit from this class.

__init__(registry, template_engine=None)

Initializes a KubernetesController.

Parameters
  • registry (APIRegistry) – The APIRegistry that should be used.

  • template_engine (Optional[TemplateEngine]) – Optional template engine that is used to read the templates. If set to None the default is lab_orchestrator_lib.template_engine.TemplateEngine.

delete(namespace, identifier)

Deletes a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A string that contains the deletion status.

get(namespace, identifier)

Gives a specific object in the namespace.

Parameters
  • namespace – Namespace of the object.

  • identifier – Identifier of the object.

Return type

str

Returns

A YAML string that contains the object.

get_list(namespace)

Gives a list of all objects in the namespace.

Parameters

namespace – Namespace where to get the objects from.

Return type

str

Returns

A YAML string that contains all objects.

template_file = None

Not Namespaced Controller

class lab_orchestrator_lib.controller.kubernetes_controller.NotNamespacedController(registry, template_engine=None)

Bases: lab_orchestrator_lib.controller.kubernetes_controller.KubernetesController

Abstract base controller for not namespaced resources.

All Kubernetes controllers that uses not namespaced api resources should inherit from this class.

__init__(registry, template_engine=None)

Initializes a KubernetesController.

Parameters
  • registry (APIRegistry) – The APIRegistry that should be used.

  • template_engine (Optional[TemplateEngine]) – Optional template engine that is used to read the templates. If set to None the default is lab_orchestrator_lib.template_engine.TemplateEngine.

delete(identifier)

Deletes a specific object.

Parameters

identifier – Identifier of the object.

Returns

A string that contains the deletion status.

get(identifier)

Gives a specific object.

Parameters

identifier – Identifier of the object.

Returns

A YAML string that contains the object.

get_list()

Gives a list of all objects.

Returns

A YAML string that contains all objects.

template_file = None