How haproxytech/dataplaneapi works
Main components
• API Server: This is the central HTTP/HTTPS server (listening on configurable ports, with TLS options available) that receives and handles requests from clients (see “cmd/dataplaneapi/main.go” and “handlers” packages).
• Handlers and Operations Layer: A significant portion of the code (under “handlers” and “operations”) implements business logic. Each “operation” (for example, for ACLs, backends, frontends, map updates, etc.) is responsible for interpreting API commands, validating configuration changes, and constructing the new HAProxy configuration.
• Configuration Manager & FS Interaction: The “configuration” and “storage” directories indicate components that manage reading/writing HAProxy’s configuration files, map files, SSL certificates, and other auxiliary resources. They ensure persistence and allow for backups, reload retention, as well as migration between configuration versions.
• HAProxy Integration & Command Execution: The project wraps around the HAProxy binary and external commands. It invokes reload, restart, and validation commands against the running HAProxy process. The “haproxy” directory and parts of the runtime logic (e.g., “runtime/serve.go”) encapsulate this integration.
• Service Discovery: The “discovery” directory provides support for integrating with external service registries (like AWS and Consul) to automatically update HAProxy configuration.
• Auxiliary and Debug Components: There are command line tools (see “client-native”, “runtime/commands”) and a command socket for debugging and system introspection.
• Testing Framework: Extensive end-to-end tests (inside “e2e”) and unit tests in “test” reflect robust automated testing practices.
Relationships and Interactions
• Client requests enter the system through the API Server. Requests are routed to appropriate handler components.
• The handlers call into the operations layer which enforces business rules and interacts with the configuration manager.
• Updated configuration is written to the file system and/or communicated via shells/command calls to trigger HAProxy reloads or restarts.
• A background monitoring or runtime layer manages inotify watchers and handles transactions, ensuring changes are consistent across stored configurations.
• Service discovery components run in parallel to update configuration based on dynamic changes from external services.
Architectural Patterns and Design Principles
• Layered Architecture: The clear separation between API serving, business logic (handlers/operations), and integration with external systems (HAProxy, file systems, service discovery) emphasizes separation of concerns.
• Command Pattern: The operations packages act as builders and invokers for specific commands (e.g., creating, editing, and deleting HAProxy configuration fragments).
• Modular Design: Having separate directories for each configuration domain (ACL, backend, frontend, log, spoe, etc.) allows independent evolution and testing.
• Transaction and State Management: There is careful handling of configuration transactions, backup retention and migration (see “operations”, “transactions” directories), ensuring HAProxy’s high availability.
Relevant Technologies and Frameworks
• The project is built in Go (note the numerous “*.go” files and use of go modules).
• Integration with HAProxy is a core requirement, and the API wraps around HAProxy’s configurations and reload logic.
• External service integrations (e.g., AWS, Consul) for service discovery are highlighted.
• Testing frameworks: Use of BATS for end-to-end testing can be indicated, as seen in the “e2e” directory.