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: theCloudProviderofsmarter_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:
objectThe platform’s infrastructure services.
Use the
infrastructureinstance.- 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 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:
its identity in signals and logs:
service_nameandprovider_name.the signals of
smarter.apps.infrastructure.signals, sent withsend(), and the lifecycle helpers that send them in the right order.operation(), which translates a provider’s SDK errors into the service’s own exception, so that the platform never catches a provider’s exceptions.the unit test guard:
refuse_in_unit_tests().
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:
objectA resource that exists in the platform’s infrastructure, whoever created it.
e.g. a Kubernetes node, or the Kubernetes cluster itself.
- Parameters:
- __init__(resource_type, resource_name, resource_id='', billable=False)
- class smarter.apps.infrastructure.services.base.InfrastructureService(provider_name, *args, **kwargs)[source]
Bases:
ABC,SmarterHelperMixinThe 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.
- connection_state(connected, error=None)[source]
Record whether the service is connected to its backend.
Sends
infrastructure_connectedorinfrastructure_connection_failedwhen the state changes, rather than on every check.
- 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, fromcreating_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:
- 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:
- 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.
- destroying_resource(resource_type, resource_name, resource_id=None, billable=False)[source]
Send the signal that precedes the destruction of a resource.
- Return type:
- 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_failedis sent.with self.operation("create_zone"): response = self.client.create_hosted_zone(...)
- 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:
- send(signal, **kwargs)[source]
Send one of the infrastructure signals, with this service’s
serviceandprovider.- Return type:
- 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.
InMemoryProviderwithconfigure_provider(), or passallow_in_tests=Truewhen they mock the backend themselves, or are taggedINFRASTRUCTURE.- Parameters:
- Raises:
InfrastructureConfigurationError – In the unit tests, unless allowed.
- Return type: