---
title: Charmhub | Deploy Spark Integration Hub K8s using Charmhub - The Open Operator
  Collection
description: Deploy the latest version of Spark Integration Hub K8s as a Kubernetes
  Operator on any cloud.
url: https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account
---

# Spark Integration Hub K8s

[Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

* [Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

Platform:

3/stable 133

```
juju deploy spark-integration-hub-k8s --channel 3/stable
```

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

[Toggle side navigation](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#drawer)

## charms.spark\_integration\_hub\_k8s.v0.spark\_service\_account

* [*Docstrings*Docstrings](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account)
  [*Code*Source code](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.spark_integration_hub_k8s.v0.spark_service_account
    ```

    [Download spark\_service\_account.py](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account/download)
  + *Last updated* 12 Jun 2025
  + *Revision* Library version 0.2

Library for creating service accounts that are configured to run Spark jobs.

This library contains the SparkServiceAccountProvider and SparkServiceAccountRequirer
classes for handling the relation between charms that require Spark Service Account
to be created in order to function, and charms that create and provide them.

###### SparkServiceAccountRequirer

Following is an example of using the SparkServiceAccountRequirer class in the context
of the application charm code:

```
import json

from charms.spark_integration_hub_k8s.v0.spark_service_account import (
    SparkServiceAccountRequirer,
    ServiceAccountGrantedEvent,
    ServiceAccountPropertyChangedEvent,
    ServiceAccountGoneEvent
)
from ops.model import ActiveStatus, BlockedStatus

class RequirerCharm(CharmBase):

    def __init__(self, *args):
        super().__init__(*args)

        namespace, username = "default", "test"
        self.spark_service_account_requirer = SparkServiceAccountRequirer(self, relation_name="service-account", service_account=f"{namespace}:{username}", skip_creation=False)
        self.framework.observe(
            self.spark_service_account_requirer.on.account_granted, self._on_account_granted
        )
        self.framework.observe(
            self.spark_service_account_requirer.on.account_gone, self._on_account_gone
        )
        self.framework.observe(
            self.spark_service_account_requirer.on.properties_changed, self._on_spark_properties_changed
        )

    def _on_account_granted(self, event: ServiceAccountGrantedEvent):
        # Handle the account_granted event

        namespace, username = event.service_account.split(":")
        props_string = self.service_account_requirer.relation_data.fetch_relation_field(event.relation.id, "spark-properties")
        props = json.loads(props_string)

        resource_manifest = self.service_account_requirer.relation_data.fetch_relation_field(event.relation.id, "resource-manifest")

        # Create configuration file for app
        config_file = self._render_app_config_file(
            namespace=namespace,
            username=username,
            spark_properties=props,
            resource_manifest=resource_manifest
        )

        # Start application with rendered configuration
        self._start_application(config_file)

        # Set appropriate status
        self.unit.status = ActiveStatus("Received Spark service account")

    def _on_spark_properties_changed(self, event: ServiceAccountPropertyChangedEvent):
        # Handle the properties_changed event
        namespace, username = event.service_account.split(":")

        # Fetch the Spark properties from event data
        props_string = self.service_account_requirer.relation_data.fetch_relation_field(event.relation.id, "spark-properties")
        props = json.loads(props_string)

        resource_manifest = self.service_account_requirer.relation_data.fetch_relation_field(event.relation.id, "resource-manifest")

        # Create configuration file for app
        config_file = self._render_app_config_file(
            namespace=namespace,
            username=username,
            spark_properties=props,
            resource_manifest=resource_manifest
        )

        # Start application with rendered configuration
        self._start_application(config_file)

        # Set appropriate status
        self.unit.status = ActiveStatus("Spark service account properties changed")

    def _on_account_gone(self, event: ServiceAccountGoneEvent):
        # Handle the account_gone event

        # Create configuration file for app
        config_file = self._render_app_config_file(
            namespace=None,
            username=None,
            spark_properties=None,
            resource_manifest=None,
        )

        # Start application with rendered configuration
        self._start_application(config_file)

        # Set appropriate status
        self.unit.status = BlockedStatus("Missing spark service account")
```

###### SparkServiceAccountProvider

Following is an example of using the SparkServiceAccountProvider class in the context
of the application charm code:

```
from charms.spark_integration_hub_k8s.v0.spark_service_account import (
    SparkServiceAccountProvider,
    ServiceAccountRequestedEvent,
    ServiceAccountReleasedEvent,
)

class ProviderCharm(CharmBase):

    def __init__(self, *args):
        super().__init__(*args)

        self.spark_service_account_provider = SparkServiceAccountProvider(self, relation_name="service-account")
        self.framework.observe(self.sa.on.account_requested, self._on_service_account_requested)
        self.framework.observe(self.sa.on.account_released, self._on_service_account_released)

    def _on_service_account_requested(self, event: ServiceAccountRequestedEvent):
        # Handle the account_requested event

        namespace, username = event.service_account.split(":")
        skip_creation = event.skip_creation

        if not skip_creation:
            # Create the service account
            self.create_service_account(namespace, username)

        resource_manifest = self.generate_resource_manifest(namespace, username)
        spark_properties = self.generate_spark_properties(namespace, username)

        # Write the service account, Spark properties and resource manifest to relation data
        self.spark_service_account_provider.set_service_account(event.relation.id, f"{namespace}:{username}")
        self.spark_service_account_provider.set_spark_properties(event.relation.id, spark_properties)
        self.spark_service_account_provider.set_resource_manifest(event.relation.id, resource_manifest)

    def _on_service_account_released(self, event: ServiceAccountReleasedEvent):
        # Handle account_released event

        namespace, username = event.service_account.split(":")
        skip_creation = event.skip_creation

        if not skip_creation:
            # Delete the service account
            self.delete_service_account(namespace, username)
```

---

Index

* [class ServiceAccountEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountevent)
* + [def service\_account(
    self)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountevent-service_account)
* + [def spark\_properties(
    self)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountevent-spark_properties)
* + [def resource\_manifest(
    self)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountevent-resource_manifest)
* + [def skip\_creation(
    self)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountevent-skip_creation)
* [class ServiceAccountRequestedEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountrequestedevent)
* [class ServiceAccountReleasedEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountreleasedevent)
* [class SparkServiceAccountProviderEvents](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderevents)
* [class ServiceAccountGrantedEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountgrantedevent)
* [class ServiceAccountGoneEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountgoneevent)
* [class ServiceAccountPropertyChangedEvent](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#serviceaccountpropertychangedevent)
* [class SparkServiceAccountRequirerEvents](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirerevents)
* [class SparkServiceAccountProviderData](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderdata)
* + [def \_\_init\_\_(
    self,
    model,
    relation\_name)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderdata-__init__)
* + [def set\_service\_account(
    self,
    relation\_id,
    service\_account)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderdata-set_service_account)
* + [def set\_spark\_properties(
    self,
    relation\_id,
    spark\_properties)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderdata-set_spark_properties)
* + [def set\_resource\_manifest(
    self,
    relation\_id,
    resource\_manifest)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountproviderdata-set_resource_manifest)
* [class SparkServiceAccountProviderEventHandlers](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountprovidereventhandlers)
* + [def \_\_init\_\_(
    self,
    charm,
    relation\_data)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountprovidereventhandlers-__init__)
* [class SparkServiceAccountProvider](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountprovider)
* + [def \_\_init\_\_(
    self,
    charm,
    relation\_name)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountprovider-__init__)
* [class SparkServiceAccountRequirerData](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirerdata)
* + [def \_\_init\_\_(
    self,
    model,
    relation\_name,
    service\_account,
    skip\_creation,
    additional\_secret\_fields)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirerdata-__init__)
* + [def service\_account(
    self)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirerdata-service_account)
* + [def service\_account(
    self,
    value)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirerdata-service_account)
* [class SparkServiceAccountRequirerEventHandlers](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirereventhandlers)
* + [def \_\_init\_\_(
    self,
    charm,
    relation\_data)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirereventhandlers-__init__)
* [class SparkServiceAccountRequirer](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirer)
* + [def \_\_init\_\_(
    self,
    charm,
    relation\_name,
    service\_account,
    skip\_creation,
    additional\_secret\_fields)](https://charmhub.io/spark-integration-hub-k8s/libraries/spark_service_account#sparkserviceaccountrequirer-__init__)

#### class ServiceAccountEvent

Description

Base class for Service account events. None

Methods

ServiceAccountEvent.
service\_account(

*self*
)

Description

Returns the service account was requested. None

ServiceAccountEvent.
spark\_properties(

*self*
)

Description

Returns the Spark properties associated with service account. None

ServiceAccountEvent.
resource\_manifest(

*self*
)

Description

Returns the resource manifest associated with service account. None

ServiceAccountEvent.
skip\_creation(

*self*
)

Description

Returns the skip-creation flag associated with service account. None

#### class ServiceAccountRequestedEvent

Description

Event emitted when a set of service account is requested for use on this relation. None

#### class ServiceAccountReleasedEvent

Description

Event emitted when a set of service account is released. None

#### class SparkServiceAccountProviderEvents

Description

Event descriptor for events raised by ServiceAccountProvider. None

#### class ServiceAccountGrantedEvent

Description

Event emitted when service account are granted on this relation. None

#### class ServiceAccountGoneEvent

Description

Event emitted when service account are removed from this relation. None

#### class ServiceAccountPropertyChangedEvent

Description

Event emitted when Spark properties for the service account are changed in this relation. None

#### class SparkServiceAccountRequirerEvents

Description

Event descriptor for events raised by the Requirer. None

#### class SparkServiceAccountProviderData

Description

Implementation of ProviderData for the Spark Service Account relation. None

Methods

SparkServiceAccountProviderData.
\_\_init\_\_(

*self*

,
model: Model

,
relation\_name: str
)

SparkServiceAccountProviderData.
set\_service\_account(

*self*

,
relation\_id: int

,
service\_account: str
)

Set the service account name in the application relation databag.

Arguments

relation\_id

the identifier for a particular relation.

service\_account

the service account name.

SparkServiceAccountProviderData.
set\_spark\_properties(

*self*

,
relation\_id: int

,
spark\_properties: str
)

Set the Spark properties in the application relation databag.

Arguments

relation\_id

the identifier for a particular relation.

spark\_properties

the dictionary that contains key-value for Spark properties.

SparkServiceAccountProviderData.
set\_resource\_manifest(

*self*

,
relation\_id: int

,
resource\_manifest: str
)

Set the resource manifest in the application relation databag.

Arguments

relation\_id

the identifier for a particular relation.

resource\_manifest

the dictionary that contains key-value for resource manifest.

#### class SparkServiceAccountProviderEventHandlers

Description

Provider-side of the Spark Service Account relation. None

Methods

SparkServiceAccountProviderEventHandlers.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
relation\_data: SparkServiceAccountProviderData
)

#### class SparkServiceAccountProvider

Description

Provider-side of the Spark Service Account relation. None

Methods

SparkServiceAccountProvider.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
relation\_name: str
)

#### class SparkServiceAccountRequirerData

Description

Implementation of RequirerData for the Spark Service Account relation. None

Methods

SparkServiceAccountRequirerData.
\_\_init\_\_(

*self*

,
model: Model

,
relation\_name: str

,
service\_account: str

,
skip\_creation: bool

,
additional\_secret\_fields
)

Description

Manager of Spark Service Account relations. None

SparkServiceAccountRequirerData.
service\_account(

*self*
)

Description

Service account used for Spark. None

SparkServiceAccountRequirerData.
service\_account(

*self*

,
value
)

#### class SparkServiceAccountRequirerEventHandlers

Description

Requirer-side event handlers of the Spark Service Account relation. None

Methods

SparkServiceAccountRequirerEventHandlers.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
relation\_data: SparkServiceAccountRequirerData
)

#### class SparkServiceAccountRequirer

Description

Requirer side of the Spark Service Account relation. None

Methods

SparkServiceAccountRequirer.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
relation\_name: str

,
service\_account: str

,
skip\_creation: bool

,
additional\_secret\_fields
)
