Kubernetes Charm Libraries
- Canonical Telco
Channel | Revision | Published | Runs on |
---|---|---|---|
latest/edge | 55 | 28 Oct 2024 |
juju deploy kubernetes-charm-libraries --channel edge
Deploy universal operators easily with Juju, the Universal Operator Lifecycle Manager.
Platform:
charms.kubernetes_charm_libraries.v0.multus
-
- Last updated 09 Sep 2024
- Revision Library version 0.16
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
Description
Object to represent Kubernetes Multus NetworkAttachmentDefinition. None
Methods
NetworkAttachmentDefinition. __eq__( self , other )
Description
Validates equality between two NetworkAttachmentDefinitions object. None
class NetworkAnnotation
Description
NetworkAnnotation. None
Methods
NetworkAnnotation. dict( self )
Returns a NetworkAnnotation in the form of a dictionary.
Returns
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 )
Deleting given pod.
Arguments
Pod name
KubernetesClient. pod_is_ready( self , pod_name: str )
Returns whether pod has the requisite network annotation and NET_ADMIN capability.
Arguments
Pod name
List of network annotations
Container name
Container requires NET_ADMIN capability
Container requires privileged security context
Returns
Whether pod is ready.
KubernetesClient. network_attachment_definition_is_created( self , network_attachment_definition: NetworkAttachmentDefinition )
Returns whether a NetworkAttachmentDefinition is created.
Arguments
NetworkAttachmentDefinition
Returns
Whether the NetworkAttachmentDefinition is created
KubernetesClient. create_network_attachment_definition( self , network_attachment_definition: GenericNamespacedResource )
Creates a NetworkAttachmentDefinition.
Arguments
NetworkAttachmentDefinition object
KubernetesClient. list_network_attachment_definitions( self )
Lists NetworkAttachmentDefinitions in a given namespace.
Returns
List of NetworkAttachmentDefinitions
KubernetesClient. delete_network_attachment_definition( self , name: str )
Deletes network attachment definition based on name.
Arguments
NetworkAttachmentDefinition name
KubernetesClient. patch_statefulset( self , name: str , network_annotations , container_name: str , cap_net_admin: bool , privileged: bool )
Patches a statefulset with Multus annotation and NET_ADMIN capability.
Arguments
Statefulset name
List of network annotations
Container name
Container requires NET_ADMIN capability
Container requires privileged security context
KubernetesClient. unpatch_statefulset( self , name: str , container_name: str )
Removes annotations, security privilege and NET_ADMIN capability from stateful set.
Arguments
Statefulset name
Container name
KubernetesClient. statefulset_is_patched( self , name: str , network_annotations , container_name: str , cap_net_admin: bool , privileged: bool )
Returns whether the statefulset has the expected multus annotation.
Arguments
Statefulset name.
list of network annotations
Container name
Container requires NET_ADMIN capability
Container requires privileged security context
Returns
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
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 )
Constructor for the KubernetesMultusCharmLib.
Arguments
list of NetworkAttachmentDefinition
to be created.
List of NetworkAnnotation
to be added to the container.
Kubernetes namespace
Statefulset name
Pod name
Container name
Container requires NET_ADMIN capability
Container requires privileged security context
KubernetesMultusCharmLib. configure( self )
Description
Creates network attachment definitions and patches statefulset. None
KubernetesMultusCharmLib. is_ready( self )
Returns whether Multus is ready.
Returns
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
Deletes 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
Whether Multus is enabled