The Service Layer

The infrastructure service layer: the platform’s only way to reach its infrastructure.

infrastructure gives the platform each service, whichever cloud provider implements it:

from smarter.apps.infrastructure.services import infrastructure

zone, created = infrastructure.dns.get_or_create_zone("example.com")
certificate_id, _ = infrastructure.certificates.get_or_create_certificate("example.com")
infrastructure.kubernetes.apply_manifest(manifest)
infrastructure.email.send_email(subject="Hello", body="...", to="user@example.com")
url = infrastructure.storage.presigned_url("data-exports/42/export.zip", expires_in=86400)
infrastructure.slack.post_message("smarter", "*Hello*")
  • dns: DNSService, implemented by the cloud provider, e.g. AWS Route53.

  • certificates: CertificateService, implemented by the cloud provider, e.g. AWS Certificate Manager.

  • storage: StorageService, private object storage, implemented by the cloud provider, e.g. AWS S3.

  • kubernetes: KubernetesService, implemented with kubectl, and the cloud provider’s kubeconfig.

  • email: EmailService, implemented with SMTP.

  • slack: SlackService, implemented with the Slack Web API.

  • logs: LogService, the pod logs, implemented with Loki.

  • provider: the CloudProvider of smarter_settings.cloud_provider.

Each service is resolved when it is used, not when this module is imported, so that tests can replace it, with configure_provider(), configure_kubernetes(), configure_email(), configure_slack() and configure_logs(), and so that importing the platform never reaches the network.

Tests of code that uses infrastructure can also patch it where it is used, e.g. patch("smarter.apps.llmclient.tasks.verify_custom_domain.infrastructure").

class smarter.apps.infrastructure.services.InfrastructureServices[source]

Bases: object

The platform’s infrastructure services.

Use the infrastructure instance.

property certificates: CertificateService

The cloud provider’s TLS certificates.

property dns: DNSService

The cloud provider’s DNS.

property email: EmailService

The platform’s outgoing email.

property kubernetes: KubernetesService

The platform’s Kubernetes cluster.

property logs: LogService

The platform’s pod logs, from the log store.

property provider: CloudProvider

The cloud provider, smarter_settings.cloud_provider, unless one is configured.

property ready: bool

Whether the cloud provider is authenticated.

property slack: SlackService

The platform’s messages to Slack.

property storage: StorageService

The cloud provider’s private object storage.

smarter.apps.infrastructure.services.infrastructure = <smarter.apps.infrastructure.services.InfrastructureServices object>

The platform’s infrastructure services.

The base class of every infrastructure service.

InfrastructureService gives each service, whichever provider implements it:

DiscoveredResource describes a resource that a service found, rather than created, for the inventory, smarter.apps.infrastructure.services.inventory.

class smarter.apps.infrastructure.services.base.DiscoveredResource(resource_type, resource_name, resource_id='', billable=False)[source]

Bases: object

A resource that exists in the platform’s infrastructure, whoever created it.

e.g. a Kubernetes node, or the Kubernetes cluster itself.

Parameters:
  • resource_type (str) – The kind of resource, e.g. kubernetes.node.

  • resource_name (str) – The resource’s name, e.g. ip-192-168-1-1.ec2.internal.

  • resource_id (str) – The provider’s id of the resource, if any, e.g. its ARN.

  • billable (bool) – Whether the provider bills for the resource.

__init__(resource_type, resource_name, resource_id='', billable=False)
billable: bool = False
resource_id: str = ''
resource_name: str
resource_type: str
class smarter.apps.infrastructure.services.base.InfrastructureService(provider_name, *args, **kwargs)[source]

Bases: ABC, SmarterHelperMixin

The base class of every infrastructure service.

Parameters:

provider_name (str) – The name of the cloud provider that implements the service, e.g. aws. Services that do not depend on a cloud, e.g. SMTP email, use their protocol’s name.

__init__(provider_name, *args, **kwargs)[source]
connection_state(connected, error=None)[source]

Record whether the service is connected to its backend.

Sends infrastructure_connected or infrastructure_connection_failed when the state changes, rather than on every check.

Parameters:
  • connected (bool) – Whether the service is connected.

  • error (str | None) – Why it is not connected.

Return type:

bool

Returns:

connected.

created_resource(resource, resource_id=None, reconciled=False)[source]

Send the signal that follows the creation of a resource.

Parameters:
  • resource (dict[str, Any]) – The resource’s signal arguments, from creating_resource().

  • resource_id (str | None) – The provider’s id of the resource.

  • reconciled (bool) – True when the ledger records a resource that already exists, e.g. a node that the cluster’s autoscaler added, rather than one that this service created.

Return type:

None

creating_resource(resource_type, resource_name, billable=False, **kwargs)[source]

Send the signal that precedes the creation of a resource.

Only billable resources have a “creating” signal, so that what costs money can be stopped or audited before it exists.

Return type:

dict[str, Any]

Returns:

The resource’s signal arguments, for created_resource().

destroyed_resource(resource, reconciled=False)[source]

Send the signal that follows the destruction of a resource.

Parameters:
  • resource (dict[str, Any]) – The resource’s signal arguments, from destroying_resource().

  • reconciled (bool) – True when the ledger records that a resource no longer exists, rather than that this service destroyed it.

Return type:

None

destroying_resource(resource_type, resource_name, resource_id=None, billable=False)[source]

Send the signal that precedes the destruction of a resource.

Return type:

dict[str, Any]

Returns:

The resource’s signal arguments, for destroyed_resource().

error_class

The exception that operation() raises for a provider’s errors.

alias of SmarterInfrastructureError

operation(name)[source]

Run one of the service’s operations, translating its errors.

An infrastructure error passes through. Any other error, e.g. a cloud SDK’s, is raised as the service’s error_class. Either way, infrastructure_operation_failed is sent.

with self.operation("create_zone"):
    response = self.client.create_hosted_zone(...)
Parameters:

name (str) – The operation’s name, e.g. create_zone.

Return type:

Iterator[None]

abstract property ready: bool

Whether the service is authenticated and connected, i.e. it can be used.

require_ready()[source]

Raise unless the service is ready.

Raises:

InfrastructureNotReadyError – If the service is not ready.

Return type:

None

send(signal, **kwargs)[source]

Send one of the infrastructure signals, with this service’s service and provider.

Return type:

None

service_name: str = 'service'

The name of the service in signals and logs, see InfrastructureServiceNames.

smarter.apps.infrastructure.services.base.refuse_in_unit_tests(what, allow_in_tests=False)[source]

Refuse to reach real infrastructure from the unit tests.

The Smarter containers’ cloud credentials and kubeconfig are real, and can create billable resources. A real service calls this before it reaches its backend. Tests install a fake, e.g. InMemoryProvider with configure_provider(), or pass allow_in_tests=True when they mock the backend themselves, or are tagged INFRASTRUCTURE.

Parameters:
  • what (str) – What is refused, for the error message.

  • allow_in_tests (bool) – True to allow it anyway.

Raises:

InfrastructureConfigurationError – In the unit tests, unless allowed.

Return type:

None