---
title: Charmhub | Deploy Kubernetes Charm Libraries using Charmhub - The Open Operator
  Collection
description: Deploy the latest version of Kubernetes Charm Libraries on any cloud.
url: https://charmhub.io/kubernetes-charm-libraries/libraries/multus
---

# Kubernetes Charm Libraries

[Canonical Telco](https://charmhub.io/publisher/telco-charmers "View all packages from Canonical Telco")

* [Canonical Telco](https://charmhub.io/publisher/telco-charmers "View all packages from Canonical Telco")

Platform:

22.04

edge 65

```
juju deploy kubernetes-charm-libraries --channel edge
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#drawer)

## charms.kubernetes\_charm\_libraries.v0.multus

* [*Docstrings*Docstrings](https://charmhub.io/kubernetes-charm-libraries/libraries/multus)
  [*Code*Source code](https://charmhub.io/kubernetes-charm-libraries/libraries/multus/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.kubernetes_charm_libraries.v0.multus
    ```

    [Download multus.py](https://charmhub.io/kubernetes-charm-libraries/libraries/multus/download)
  + *Last updated* 10 Dec 2024
  + *Revision* Library version 0.17

Charm Library used to leverage the Multus Kubernetes CNI in charms.

##### Usage

```
from typing import List

from charms.kubernetes_charm_libraries.v0.multus import (
    KubernetesMultusCharmLib,
    NetworkAnnotation,
    NetworkAttachmentDefinition,
)
from ops import RemoveEvent
from ops.charm import CharmBase
from ops.framework import EventBase
from ops.main import main

class YourCharm(CharmBase):

    def __init__(self, *args):
        super().__init__(*args)
        self._kubernetes_multus = KubernetesMultusCharmLib(
            cap_net_admin=True,
            namespace=self.model.name,
            statefulset_name=self.model.app.name,
            pod_name="-".join(self.model.unit.name.rsplit("/", 1)),
            container_name=self._bessd_container_name,
            network_annotations=self._generate_network_annotations(),
            network_attachment_definitions=self._network_attachment_definitions_from_config(),
            privileged=True,
        )

        self.framework.observe(self.on.update_status, self._on_update_status)

    def _on_update_status(self, event: EventBase):
        self._kubernetes_multus.configure()

    def _on_remove(self, _: RemoveEvent) -> None:
        self._kubernetes_multus.remove()

    def _generate_network_annotations(self) -> List[NetworkAnnotation]:
        return [
            NetworkAnnotation(
                name=ACCESS_NETWORK_ATTACHMENT_DEFINITION_NAME,
                interface_name=ACCESS_INTERFACE_NAME,
                bridge_name=ACCESS_INTERFACE_BRIDGE_NAME,
            ),
            NetworkAnnotation(
                name=CORE_NETWORK_ATTACHMENT_DEFINITION_NAME,
                interface_name=CORE_INTERFACE_NAME,
                bridge_name=CORE_INTERFACE_BRIDGE_NAME,
            ),
        ]

    def _network_attachment_definitions_from_config(self) -> List[NetworkAttachmentDefinition]:
        return [
            NetworkAttachmentDefinition(
                name=ACCESS_NETWORK_ATTACHMENT_DEFINITION_NAME,
                cni_type="macvlan",
                network_name=self.config["access_network_name"],
            ),
            NetworkAttachmentDefinition(
                name=CORE_NETWORK_ATTACHMENT_DEFINITION_NAME,
                cni_type="macvlan",
                network_name=self.config["core_network_name"],
            ),
        ]
```

---

Index

* [class NetworkAttachmentDefinition](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#networkattachmentdefinition)
* + [def \_\_eq\_\_(
    self,
    other)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#networkattachmentdefinition-__eq__)
* [class NetworkAnnotation](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#networkannotation)
* + [def dict(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#networkannotation-dict)
* [class KubernetesMultusError](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuserror)
* + [def \_\_init\_\_(
    self,
    message)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuserror-__init__)
* [class KubernetesClient](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient)
* + [def \_\_init\_\_(
    self,
    namespace)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-__init__)
* + [def delete\_pod(
    self,
    pod\_name)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-delete_pod)
* + [def pod\_is\_ready(
    self,
    pod\_name)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-pod_is_ready)
* + [def network\_attachment\_definition\_is\_created(
    self,
    network\_attachment\_definition)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-network_attachment_definition_is_created)
* + [def create\_network\_attachment\_definition(
    self,
    network\_attachment\_definition)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-create_network_attachment_definition)
* + [def list\_network\_attachment\_definitions(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-list_network_attachment_definitions)
* + [def delete\_network\_attachment\_definition(
    self,
    name)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-delete_network_attachment_definition)
* + [def patch\_statefulset(
    self,
    name,
    network\_annotations,
    container\_name,
    cap\_net\_admin,
    privileged)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-patch_statefulset)
* + [def unpatch\_statefulset(
    self,
    name,
    container\_name)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-unpatch_statefulset)
* + [def statefulset\_is\_patched(
    self,
    name,
    network\_annotations,
    container\_name,
    cap\_net\_admin,
    privileged)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-statefulset_is_patched)
* + [def multus\_is\_available(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesclient-multus_is_available)
* [class KubernetesMultusCharmLib](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib)
* + [def \_\_init\_\_(
    self,
    network\_attachment\_definitions,
    network\_annotations,
    namespace,
    statefulset\_name,
    pod\_name,
    container\_name,
    cap\_net\_admin,
    privileged)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-__init__)
* + [def configure(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-configure)
* + [def is\_ready(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-is_ready)
* + [def remove(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-remove)
* + [def delete\_pod(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-delete_pod)
* + [def multus\_is\_available(
    self)](https://charmhub.io/kubernetes-charm-libraries/libraries/multus#kubernetesmultuscharmlib-multus_is_available)

#### class NetworkAttachmentDefinition

Description

Object to represent Kubernetes Multus NetworkAttachmentDefinition. None

Methods

NetworkAttachmentDefinition.
\_\_eq\_\_(

*self*

,
other
)

Description

Validate equality between two NetworkAttachmentDefinitions object. None

#### class NetworkAnnotation

Description

NetworkAnnotation. None

Methods

NetworkAnnotation.
dict(

*self*
)

Return a NetworkAnnotation in the form of a dictionary.

Returns

dict

Dictionary representation of the NetworkAnnotation

#### class KubernetesMultusError

Description

KubernetesMultusError. None

Methods

KubernetesMultusError.
\_\_init\_\_(

*self*

,
message: str
)

#### class KubernetesClient

Description

Class containing all the Kubernetes specific calls. None

Methods

KubernetesClient.
\_\_init\_\_(

*self*

,
namespace: str
)

KubernetesClient.
delete\_pod(

*self*

,
pod\_name: str
)

Delete given pod.

Arguments

pod\_name
(str)

Pod name

KubernetesClient.
pod\_is\_ready(

*self*

,
pod\_name: str
)

Return whether pod has the requisite network annotation and NET\_ADMIN capability.

Arguments

pod\_name

Pod name

network\_annotations

List of network annotations

container\_name

Container name

cap\_net\_admin

Container requires NET\_ADMIN capability

privileged

Container requires privileged security context

Returns

bool

Whether pod is ready.

KubernetesClient.
network\_attachment\_definition\_is\_created(

*self*

,
network\_attachment\_definition: NetworkAttachmentDefinition
)

Return whether a NetworkAttachmentDefinition is created.

Arguments

network\_attachment\_definition

NetworkAttachmentDefinition

Returns

bool

Whether the NetworkAttachmentDefinition is created

KubernetesClient.
create\_network\_attachment\_definition(

*self*

,
network\_attachment\_definition: GenericNamespacedResource
)

Create a NetworkAttachmentDefinition.

Arguments

network\_attachment\_definition

NetworkAttachmentDefinition object

KubernetesClient.
list\_network\_attachment\_definitions(

*self*
)

List NetworkAttachmentDefinitions in a given namespace.

Returns

list[NetworkAttachmentDefinition]

List of NetworkAttachmentDefinitions

KubernetesClient.
delete\_network\_attachment\_definition(

*self*

,
name: str
)

Delete network attachment definition based on name.

Arguments

name

NetworkAttachmentDefinition name

KubernetesClient.
patch\_statefulset(

*self*

,
name: str

,
network\_annotations

,
container\_name: str

,
cap\_net\_admin: bool

,
privileged: bool
)

Patch a statefulset with Multus annotation and NET\_ADMIN capability.

Arguments

name

Statefulset name

network\_annotations

List of network annotations

container\_name

Container name

cap\_net\_admin

Container requires NET\_ADMIN capability

privileged

Container requires privileged security context

KubernetesClient.
unpatch\_statefulset(

*self*

,
name: str

,
container\_name: str
)

Remove annotations, security privilege and NET\_ADMIN capability from stateful set.

Arguments

name

Statefulset name

container\_name

Container name

KubernetesClient.
statefulset\_is\_patched(

*self*

,
name: str

,
network\_annotations

,
container\_name: str

,
cap\_net\_admin: bool

,
privileged: bool
)

Return whether the statefulset has the expected multus annotation.

Arguments

name

Statefulset name.

network\_annotations

list of network annotations

container\_name

Container name

cap\_net\_admin

Container requires NET\_ADMIN capability

privileged

Container requires privileged security context

Returns

bool

Whether the statefulset has the expected multus annotation.

KubernetesClient.
multus\_is\_available(

*self*
)

Check whether Multus is enabled leveraging existence of NAD custom resource.

Returns

bool

Whether Multus is enabled

#### class KubernetesMultusCharmLib

Description

Class to be instantiated by charms requiring Multus networking. None

Methods

KubernetesMultusCharmLib.
\_\_init\_\_(

*self*

,
network\_attachment\_definitions

,
network\_annotations

,
namespace: str

,
statefulset\_name: str

,
pod\_name: str

,
container\_name: str

,
cap\_net\_admin: bool

,
privileged: bool
)

Create instance of the KubernetesMultusCharmLib.

Arguments

network\_attachment\_definitions

list of `NetworkAttachmentDefinition` to be created.

network\_annotations

List of `NetworkAnnotation` to be added to the container.

namespace

Kubernetes namespace

statefulset\_name

Statefulset name

pod\_name

Pod name

container\_name

Container name

cap\_net\_admin

Container requires NET\_ADMIN capability

privileged

Container requires privileged security context

KubernetesMultusCharmLib.
configure(

*self*
)

Description

Create network attachment definitions and patches statefulset. None

KubernetesMultusCharmLib.
is\_ready(

*self*
)

Return whether Multus is ready.

Returns

bool

Whether Multus is ready

Description

Validates that the network attachment definitions are created, that the statefulset is
patched with the appropriate Multus annotations and capabilities and that the pod
also contains the same annotations and capabilities.

KubernetesMultusCharmLib.
remove(

*self*
)

Description

Delete network attachment definitions and removes patch. None

KubernetesMultusCharmLib.
delete\_pod(

*self*
)

Description

Delete the pod. None

KubernetesMultusCharmLib.
multus\_is\_available(

*self*
)

Check whether Multus is enabled leveraging existence of NAD custom resource.

Returns

bool

Whether Multus is enabled
