Architecture Overview
This operator is built following the principles of Domain-Driven Design (DDD). The goal is to separate the core business logic from the implementation details of the surrounding infrastructure (like Kubernetes). This makes the code easier to test, maintain, and understand.
The code is organized into distinct layers, each with a specific responsibility. The fundamental rule is that dependencies only point inwards: Adapter -> Application -> DomainService -> Domain.
graph TD
A[Adapter Layer] --> B[Application Layer];
B --> C[Domain Service Layer];
C --> D[Domain Layer];Layers
pkg/domain
This is the heart of the operator. It contains the core business logic, rules, and data structures (Aggregates and Value Objects) that represent the problem space.
- Content: Pure data structures (
BlueprintSpec,StateDiff,Dogu) and the logic to maintain their consistency. - Key Rule: This layer has zero dependencies on any other layer in the project. It knows nothing about Kubernetes, repositories, or use cases.
pkg/domainservice
This layer contains domain logic that doesn't naturally fit within a single domain aggregate. It often orchestrates actions between multiple domain objects.
- Content: Services that perform complex validations or calculations, e.g.,
ValidateDependenciesDomainUseCasewhich needs to check dependencies across multiple dogus. - Key Rule: Depends only on the
domainlayer.
pkg/application
This layer defines the specific use cases of the operator. It acts as an entrypoint for external actors (like the Kubernetes controller) to interact with the application.
- Content: Use cases like
BlueprintSpecChangeUseCasewhich orchestrates the entire process of receiving a blueprint change, preparing it, and applying it. - Key Rule: It directs the flow of data, using
domainservices anddomainobjects to perform its tasks. It depends on interfaces for repositories but not their concrete implementations.
pkg/adapter
This is the outermost layer, responsible for interacting with the outside world. It adapts external technologies for use by the application layer.
- Content:
- Kubernetes Controller: The
BlueprintReconcilerlistens for changes to CRDs. - Repositories: Concrete implementations of the repository interfaces defined in the
domainservicelayer (e.g.,blueprintSpecCRRepositorywhich saves/loadsBlueprintSpecdomain objects to/from Kubernetes). - Serializers: Code that translates between
domainobjects and their Kubernetes CRD representations.
- Kubernetes Controller: The
- Key Rule: This layer is the "glue" that connects the application to Kubernetes. It depends on all inner layers.
pkg/config
This package is responsible for all operator-level configuration, primarily reading and parsing environment variables.
Development Principles
To maintain the integrity of this architecture, please adhere to the following principles when adding or modifying code.
-
The Dependency Rule is a hard rule. Code in an inner layer must not import from or reference code in an outer layer.
domainknows nothing aboutapplicationoradapter.applicationknows nothing aboutadapter.
-
Domain objects protect their own invariants. All changes to a domain object's state should be made through methods on that object (the "Aggregate Root"). Do not reach into a domain object from an outer layer to change its fields directly. This ensures the object is always in a consistent state.
-
Repositories belong to the
adapterlayer. Thedomainservicelayer defines the interface for a repository (what the application needs), but the concrete implementation (how it's done, e.g., with a Kubernetes client) lives in theadapterlayer. -
Use cases drive the application. Any new feature should be expressed as a use case in the
applicationlayer. The controller in theadapterlayer should be kept "thin" and primarily delegate to these use cases. -
Keep external dependencies in the
adapterlayer. If you need to interact with a new external service or library, the code for that interaction belongs in an adapter. This isolates external dependencies and makes them easier to replace or mock for tests.
Last updated on 10/10/2025