Workload

This script defines the Workload and WorkloadBuilder classes for creating and managing workloads.

Classes

  • Workload:

    Represents a workload with various attributes and methods to update them.

  • WorkloadBuilder:

    A builder class to create a Workload object with a fluent interface.

Usage

  • Create a workload using the WorkloadBuilder:
    workload = Workload.builder() \
        .workload_name("nginx") \
        .agent_name("agent_A") \
        .runtime("podman") \
        .restart_policy("NEVER") \
        .runtime_config("image: docker.io/library/nginx\n"
                        + "commandOptions: [\"-p\", \"8080:80\"]") \
        .add_dependency("other_workload", "ADD_COND_RUNNING") \
        .add_tag("key1", "value1") \
        .add_tag("key2", "value2") \
        .build()
    
  • Update fields of the workload:
    workload.update_agent_name("agent_B")
    
  • Update dependencies:
    deps = workload.get_dependencies()
    deps["other_workload"] = "ADD_COND_SUCCEEDED"
    workload.update_dependencies(deps)
    
  • Update tags:
    tags = workload.get_tags()
    tags.pop("key1")
    workload.update_tags(tags)
    
  • Print the updated workload:
    print(workload)
    

Workload Class

class ankaios_sdk._components.workload.Workload(name)[source]

Bases: object

A class to represent a workload.

name

The workload name.

Type:

str

__init__(name)[source]

Initialize a Workload object.

The Workload object should be created using the Workload.builder() method.

Parameters:

name (str) – The workload name.

__str__()[source]

Return a string representation of the Workload object.

Returns:

String representation of the Workload object.

Return type:

str

static builder()[source]

Return a WorkloadBuilder object.

Returns:

A builder object to create a Workload.

Return type:

WorkloadBuilder

update_workload_name(name)[source]

Set the workload name.

Parameters:

name (str) – The workload name to update.

Return type:

None

update_agent_name(agent_name)[source]

Set the agent name for the workload.

Parameters:

agent_name (str) – The agent name to update.

Return type:

None

update_runtime(runtime)[source]

Set the runtime for the workload.

Parameters:

runtime (str) – The runtime to update.

Return type:

None

update_runtime_config(config)[source]

Set the runtime-specific configuration for the workload.

Parameters:

config (str) – The runtime configuration to update.

Return type:

None

update_runtime_config_from_file(config_file)[source]

Set the runtime-specific configuration for the workload from a file.

Parameters:

config_file (str) – The path to the configuration file.

Return type:

None

update_restart_policy(policy)[source]

Set the restart policy for the workload. Supported values: NEVER, ON_FAILURE, ALWAYS.

Parameters:

policy (str) – The restart policy to update.

Raises:

WorkloadFieldException – If an invalid restart policy is provided.

Return type:

None

get_dependencies()[source]

Return the dependencies of the workload.

Returns:

A dictionary of dependencies with workload names as keys and conditions as values.

Return type:

dict

update_dependencies(dependencies)[source]

Update the dependencies of the workload. Supported conditions: ADD_COND_RUNNING, ADD_COND_SUCCEEDED, ADD_COND_FAILED.

Parameters:

dependencies (dict) – A dictionary of dependencies with workload names and condition as values.

Raises:

WorkloadFieldException – If an invalid condition is provided.

Return type:

None

add_tag(key, value)[source]

Add a tag to the workload.

Parameters:
  • key (str) – The key of the tag.

  • value (str) – The value of the tag.

Return type:

None

get_tags()[source]

Return the tags of the workload.

Returns:

A list of tuples containing tag keys and values.

Return type:

list

update_tags(tags)[source]

Update the tags of the workload.

Parameters:

tags (list) – A list of tuples containing tag keys and values.

Return type:

None

get_allow_rules()[source]

Return the allow rules of the workload.

Returns:

A list of tuples containing operation and filter masks.

Return type:

list

update_allow_rules(rules)[source]

Update the allow rules of the workload. Supported values: Nothing, Write, Read, ReadWrite.

Parameters:

rules (list) – A list of tuples containing operation and filter masks.

Raises:

WorkloadFieldException – If an invalid operation is provided

Return type:

None

get_deny_rules()[source]

Return the deny rules of the workload.

Returns:

A list of tuples containing operation and filter masks.

Return type:

list

update_deny_rules(rules)[source]

Update the deny rules of the workload. Supported values: Nothing, Write, Read, ReadWrite.

Parameters:

rules (list) – A list of tuples containing operation and filter masks.

Raises:

WorkloadFieldException – If an invalid operation is provided

Return type:

None

add_config(alias, name)[source]

Link a configuration to the workload.

Parameters:
  • alias (str) – The alias of the configuration.

  • name (str) – The name of the configuration.

Return type:

None

get_configs()[source]

Return the configurations linked to the workload.

Returns:

A dict containing the alias as key and name of the

configuration as value.

Return type:

dict[str, str]

update_configs(configs)[source]

Update the configurations linked to the workload.

Parameters:

configs (dict[str, str]) – A tuple containing the alias and name of the configurations.

Return type:

None

to_dict()[source]

Convert the Workload object to a dictionary.

Returns:

The dictionary representation of the Workload object.

Return type:

dict

WorkloadBuilder Class

class ankaios_sdk._components.workload.WorkloadBuilder[source]

Bases: object

A builder class to create a Workload object.

wl_name

The workload name.

Type:

str

wl_agent_name

The agent name.

Type:

str

wl_runtime

The runtime.

Type:

str

wl_runtime_config

The runtime configuration.

Type:

str

wl_restart_policy

The restart policy.

Type:

str

dependencies

The dependencies.

Type:

dict

tags

The tags.

Type:

list

__init__()[source]

Initialize a WorkloadBuilder object.

workload_name(workload_name)[source]

Set the workload name.

Parameters:

workload_name (str) – The workload name to set.

Returns:

The builder object.

Return type:

WorkloadBuilder

agent_name(agent_name)[source]

Set the agent name.

Parameters:

agent_name (str) – The agent name to set.

Returns:

The builder object.

Return type:

WorkloadBuilder

runtime(runtime)[source]

Set the runtime.

Parameters:

runtime (str) – The runtime to set.

Returns:

The builder object.

Return type:

WorkloadBuilder

runtime_config(runtime_config)[source]

Set the runtime configuration.

Parameters:

runtime_config (str) – The runtime configuration to set.

Returns:

The builder object.

Return type:

WorkloadBuilder

runtime_config_from_file(runtime_config_path)[source]

Set the runtime configuration using a file.

Parameters:

runtime_config_path (str) – The path to the configuration file.

Returns:

The builder object.

Return type:

WorkloadBuilder

restart_policy(restart_policy)[source]

Set the restart policy.

Parameters:

restart_policy (str) – The restart policy to set.

Returns:

The builder object.

Return type:

WorkloadBuilder

add_dependency(workload_name, condition)[source]

Add a dependency.

Parameters:
  • workload_name (str) – The name of the dependent workload.

  • condition (str) – The condition for the dependency.

Returns:

The builder object.

Return type:

WorkloadBuilder

add_tag(key, value)[source]

Add a tag.

Parameters:
  • key (str) – The key of the tag.

  • value (str) – The value of the tag.

Returns:

The builder object.

Return type:

WorkloadBuilder

add_allow_rule(operation, filter_masks)[source]

Add an allow rule to the workload.

Parameters:
  • operation (str) – The operation the rule allows.

  • filter_masks (list) – The list of filter masks.

Returns:

The builder object.

Return type:

WorkloadBuilder

add_deny_rule(operation, filter_masks)[source]

Add a deny rule to the workload.

Parameters:
  • operation (str) – The operation the rule denies.

  • filter_masks (list) – The list of filter masks.

Returns:

The builder object.

Return type:

WorkloadBuilder

add_config(alias, name)[source]

Link a configuration to the workload.

Parameters:
  • alias (str) – The alias of the configuration.

  • name (str) – The name of the configuration.

Return type:

WorkloadBuilder

build()[source]

Build the Workload object. Required fields: workload name, agent name, runtime and runtime configuration.

Returns:

The built Workload object.

Return type:

Workload

Raises:

WorkloadBuilderException – If required fields are not set.