Skip to main content

Kubernetes Service Binding Library for Python Applications

Project description

pyservicebinding

PyPI - Downloads Release Supported Python Versions CI

Kubernetes Service Binding Library for Python Applications

The Service Binding Specification for Kubernetes standardizes exposing backing service secrets to applications. This project provides a Python module to consume the bindings projected into the container. The Application Projection section of the spec describes how the bindings are projected into the application. The primary mechanism of projection is through files mounted at a specific directory. The bindings directory path can be discovered through SERVICE_BINDING_ROOT environment variable. The operator must have injected SERVICE_BINDING_ROOT environment to all the containers where bindings are created.

Within this service binding root directory, there could be multiple bindings projected from different Service Bindings. For example, suppose an application requires to connect to a database and cache server. In that case, one Service Binding can provide the database, and the other Service Binding can offer bindings to the cache server.

Let's take a look at the example given in the spec:

$SERVICE_BINDING_ROOT
├── account-database
│   ├── type
│   ├── provider
│   ├── uri
│   ├── username
│   └── password
└── transaction-event-stream
    ├── type
    ├── connection-count
    ├── uri
    ├── certificates
    └── private-key

In the above example, there are two bindings under the SERVICE_BINDING_ROOT directory. The account-database and transaction-event-system are the names of the bindings. Files within each bindings directory has a special file named type, and you can rely on the value of that file to identify the type of the binding projected into that directory. In certain directories, you can also see another file named provider. The provider is an additional identifier to narrow down the type further. This module use the type field and, optionally, provider field to look up the bindings.

Installation

You can install this package using pip:

pip install pyservicebinding

Usage

The ServiceBinding object can be instantiated like this:

from pyservicebinding import binding
try:
    sb = binding.ServiceBinding()
except binding.ServiceBindingRootMissingError as msg:
    # log the error message and retry/exit
    print("SERVICE_BINDING_ROOT env var not set")

To get bindings for a specific type, say postgresql:

bindings_list = sb.bindings("postgresql")

To get bindings for a specific type, say mysql, and provider, say mariadb:

bindings_list = sb.bindings("mysql", "mariadb")

To get all bindings irrespective of the type and provider:

bindings_list = sb.all_bindings()

This is the complete API of the module:

class ServiceBindingRootMissingError(Exception):
    pass


class ServiceBinding:

    def __init__(self):
        """
        - raise ServiceBindingRootMissingError if SERVICE_BINDING_ROOT env var not set
        """

    def all_bindings(self) -> list[dict[str, str]]:
        """Get all bindings as a list of dictionaries

        - return empty list if no bindings found
        """

    def bindings(self, _type: str, provider: typing.Optional[str] = None) -> list[dict[str, str]]:
        """Get filtered bindings as a list of dictionaries

        - return empty dictionary if no binding found
        - filter the result with the given _type input
        - if provider input is given, filter bindings using the given type and provider
        """

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyservicebinding-0.2.4.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyservicebinding-0.2.4-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pyservicebinding-0.2.4.tar.gz.

File metadata

  • Download URL: pyservicebinding-0.2.4.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for pyservicebinding-0.2.4.tar.gz
Algorithm Hash digest
SHA256 899a3b81f243fa4766f4692393383eb5d9871e3bd888e2befae64ff7c37a2362
MD5 c2d66cb71b56e021063eb89931377ae7
BLAKE2b-256 4e9637687567592f58d7734bbfdaaa53078c3640d03fa2c0f180379274a275b8

See more details on using hashes here.

File details

Details for the file pyservicebinding-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: pyservicebinding-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for pyservicebinding-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b0bf1f1324c67b44fc8b877b9cdf6a799bad47315bccdc7cc8262c12832cf980
MD5 8af9d6bb35ec6054679a0ae8fb7e8397
BLAKE2b-256 f89cc8adfc9ab7727e2610ebec9d9bc9df55fa9350195cc8fedfd7fff4b3cebb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page