|
| 1 | +# Template System Architecture |
| 2 | + |
| 3 | +Technical documentation for contributors working with the template rendering system. |
| 4 | + |
| 5 | +## 🏗️ System Overview |
| 6 | + |
| 7 | +The template system uses a **double indirection** approach to provide flexible infrastructure deployment while maintaining portability and customizability. |
| 8 | + |
| 9 | +## 📦 Double Indirection Pattern |
| 10 | + |
| 11 | +The system operates through two levels of indirection to balance portability with flexibility: |
| 12 | + |
| 13 | +### Level 1: Embedded → External Extraction |
| 14 | + |
| 15 | +1. **Source**: Templates are compiled into the binary as embedded resources |
| 16 | +2. **Extraction**: On first use, templates are extracted to an external directory (e.g., `data/templates`) |
| 17 | +3. **Benefit**: Enables single binary deployment while allowing runtime customization |
| 18 | + |
| 19 | +### Level 2: Template → Build Directory Rendering |
| 20 | + |
| 21 | +1. **Source**: Templates are read from the external directory |
| 22 | +2. **Processing**: Templates are processed (static copy or dynamic rendering with variables) |
| 23 | +3. **Output**: Final configuration files are written to the build directory |
| 24 | +4. **Benefit**: Separates template definitions from runtime-generated configurations |
| 25 | + |
| 26 | +## 🔄 Template Flow |
| 27 | + |
| 28 | +```text |
| 29 | +┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ |
| 30 | +│ Embedded │ │ External │ │ Build │ |
| 31 | +│ Templates │───▶│ Templates │───▶│ Directory │ |
| 32 | +│ (in binary) │ │ (data/templates) │ │ (build/) │ |
| 33 | +└─────────────────┘ └──────────────────┘ └─────────────────┘ |
| 34 | + │ │ │ |
| 35 | + Compile Time Runtime Extraction Runtime Rendering |
| 36 | +``` |
| 37 | + |
| 38 | +## 🎯 Template Types |
| 39 | + |
| 40 | +### Static Templates |
| 41 | + |
| 42 | +- **Processing**: Direct file copy from templates to build directory |
| 43 | +- **Examples**: Infrastructure definitions, playbooks |
| 44 | +- **Use Case**: Configuration files that don't need variable substitution |
| 45 | + |
| 46 | +### Dynamic Templates (Tera) |
| 47 | + |
| 48 | +- **Processing**: Variable substitution using Tera templating engine |
| 49 | +- **File Suffix**: `.tera` extension (e.g., `variables.tfvars.tera`) |
| 50 | +- **Use Case**: Configuration files requiring runtime parameters |
| 51 | + |
| 52 | +## 🔧 Key Components |
| 53 | + |
| 54 | +### Template Manager |
| 55 | + |
| 56 | +- Handles the embedded → external extraction process |
| 57 | +- Manages template source selection (embedded vs external directory) |
| 58 | +- Coordinates template availability and caching |
| 59 | + |
| 60 | +### Template Renderers |
| 61 | + |
| 62 | +- **OpenTofu Renderer**: Processes infrastructure templates |
| 63 | +- **Ansible Renderer**: Processes configuration management templates |
| 64 | +- Handle the template → build directory rendering process |
| 65 | + |
| 66 | +### Template Engine |
| 67 | + |
| 68 | +- Tera-based templating for dynamic content |
| 69 | +- Variable context resolution |
| 70 | +- Template syntax validation and error handling |
| 71 | + |
| 72 | +## ⚠️ Important Behaviors |
| 73 | + |
| 74 | +### Template Persistence |
| 75 | + |
| 76 | +- Once extracted, external templates persist between runs |
| 77 | +- Templates are **not** automatically refreshed from embedded sources |
| 78 | +- This enables template customization but can cause confusion during development |
| 79 | + |
| 80 | +### E2E Test Isolation |
| 81 | + |
| 82 | +- E2E tests clean the templates directory before each run |
| 83 | +- This ensures fresh embedded template extraction for consistent test results |
| 84 | +- Production deployments may use persistent template directories |
| 85 | + |
| 86 | +## 🎯 Design Goals |
| 87 | + |
| 88 | +### Portability |
| 89 | + |
| 90 | +- Single binary contains all necessary templates |
| 91 | +- No external dependencies for basic deployment |
| 92 | + |
| 93 | +### Flexibility |
| 94 | + |
| 95 | +- External templates can be customized without recompilation |
| 96 | +- Support for both static and dynamic template processing |
| 97 | +- CLI option to specify custom template directories |
| 98 | + |
| 99 | +### Test Isolation |
| 100 | + |
| 101 | +- Template cleanup ensures consistent test environments |
| 102 | +- Separation of template sources from generated configurations |
| 103 | + |
| 104 | +## 📋 Beta Status Notice |
| 105 | + |
| 106 | +This system is currently in beta. The implementation details, APIs, and internal structure may change significantly. This document focuses on the core architectural concept rather than specific implementation details that are likely to evolve. |
0 commit comments