mlops-libs

MLOps Libs

Channel Revision Published Runs on
latest/edge 1 07 Feb 2024
Ubuntu 22.04
juju deploy mlops-libs --channel edge
Show information

Platform:

Ubuntu
22.04

charms.mlops_libs.v0.k8s_service_info

Library for sharing Kubernetes Services information.

This library offers a Python API for providing and requesting information about any Kubernetes Service resource. The default relation name is k8s-svc-info and it's recommended to use that name, though if changed, you must ensure to pass the correct name when instantiating the provider and requirer classes, as well as in metadata.yaml.

Getting Started
Fetching the library with charmcraft

Using charmcraft you can:

charmcraft fetch-lib charms.mlops_libs.v0.k8s_service_info
Using the library as requirer
Add relation to metadata.yaml
requires:
  k8s-svc-info:
    interface: k8s-service
    limit: 1
Instantiate the KubernetesServiceInfoRequirer class in charm.py
from ops.charm import CharmBase
from charms.mlops_libs.v0.kubernetes_service_info import KubernetesServiceInfoRequirer, KubernetesServiceInfoRelationError

class RequirerCharm(CharmBase):
    def __init__(self, *args):
        self._k8s_svc_info_requirer = KubernetesServiceInfoRequirer(self)
        self.framework.observe(self.on.some_event_emitted, self.some_event_function)

    def some_event_function():
        # use the getter function wherever the info is needed
        try:
            k8s_svc_info_data = self._k8s_svc_info_requirer.get_data()
        except KubernetesServiceInfoRelationError as error:
            "your error handler goes here"
Using the library as provider
Add relation to metadata.yaml
provides:
  k8s-svc-info:
    interface: k8s-service
Instantiate the KubernetesServiceInfoProvider class in charm.py
from ops.charm import CharmBase
from charms.mlops_libs.v0.kubernetes_service_info import KubernetesServiceInfoProvider, KubernetesServiceInfoRelationError

class ProviderCharm(CharmBase):
    def __init__(self, *args, **kwargs):
        ...
        self._k8s_svc_info_provider = KubernetesServiceInfoProvider(self)
        self.observe(self.on.some_event, self._some_event_handler)
    def _some_event_handler(self, ...):
        # This will update the relation data bag with the Service name and port
        try:
            self._k8s_svc_info_provider.send_data(name, port)
        except KubernetesServiceInfoRelationError as error:
            "your error handler goes here"
Relation data

The data shared by this library is:

  • name: the name of the Kubernetes Service as it appears in the resource metadata, e.g. "metadata-grpc-service".
  • port: the port of the Kubernetes Service

class KubernetesServiceInfoRelationError

Description

Base exception class for any relation error handled by this library. None

class KubernetesServiceInfoRelationMissingError

Description

Exception to raise when the relation is missing on either end. None

Methods

KubernetesServiceInfoRelationMissingError. __init__( self )

class KubernetesServiceInfoRelationDataMissingError

Description

Exception to raise when there is missing data in the relation data bag. None

Methods

KubernetesServiceInfoRelationDataMissingError. __init__( self , message )

class KubernetesServiceInfoUpdatedEvent

Description

Indicates the Kubernetes Service Info data was updated. None

class KubernetesServiceInfoEvents

Description

Events for the Kubernetes Service Info library. None

class KubernetesServiceInfoObject

Representation of a Kubernetes Service info object.

Arguments

name

The name of the Service

port

The port of the Service

class KubernetesServiceInfoRequirer

Implement the Requirer end of the Kubernetes Service Info relation.

Arguments

charm (CharmBase)

the provider application

refresh_event

(list, optional): list of BoundEvents that this manager should handle. Use this to update the data sent on this relation on demand.

relation_name (str)

the name of the relation

Attributes

charm (CharmBase)
variable for storing the requirer application
relation_name (str)
variable for storing the name of the relation

Description

Observes the relation events and get data of a related application.

This library emits:

  • KubernetesServiceInfoUpdatedEvent: when data received on the relation is updated.

Methods

KubernetesServiceInfoRequirer. __init__( self , charm: CharmBase , refresh_event , relation_name )

KubernetesServiceInfoRequirer. get_data( self )

Description

Return a KubernetesServiceInfoObject. None

class KubernetesServiceInfoRequirerWrapper

Wrapper for the relation data getting logic.

Arguments

charm (CharmBase)

the requirer application

relation_name (str)

the name of the relation

Attributes

relation_name (str)
variable for storing the name of the relation

Methods

KubernetesServiceInfoRequirerWrapper. __init__( self , charm , relation_name )

KubernetesServiceInfoRequirerWrapper. get_data( self )

Return a KubernetesServiceInfoObject containing Kubernetes Service information.

class KubernetesServiceInfoProvider

Implement the Provider end of the Kubernetes Service Info relation.

Arguments

charm (CharmBase)

the provider application

name (str)

the name of the Kubernetes Service the provider knows about

port (str)

the port number of the Kubernetes Service the provider knows about

refresh_event

(list, optional): list of BoundEvents that this manager should handle. Use this to update the data sent on this relation on demand.

relation_name (str)

the name of the relation

Attributes

charm (CharmBase)
variable for storing the provider application
relation_name (str)
variable for storing the name of the relation

Description

Observes relation events to send data to related applications.

Methods

KubernetesServiceInfoProvider. __init__( self , charm: CharmBase , name: str , port: str , refresh_event , relation_name )

class KubernetesServiceInfoProviderWrapper

Wrapper for the relation data sending logic.

Arguments

charm (CharmBase)

the provider application

relation_name (str)

the name of the relation

Attributes

charm (CharmBase)
variable for storing the provider application
relation_name (str)
variable for storing the name of the relation

Methods

KubernetesServiceInfoProviderWrapper. __init__( self , charm: CharmBase , relation_name )

KubernetesServiceInfoProviderWrapper. send_data( self , name: str , port: str )

Update the relation data bag with data from a Kubernetes Service.

Arguments

name (str)

the name of the Kubernetes Service the provider knows about

port (str)

the port number of the Kubernetes Service the provider knows about

Description

This method will complete successfully even if there are no related applications.