|
| 1 | +# DRBD Build Server |
| 2 | + |
| 3 | +A high-performance HTTP server that builds DRBD kernel modules on-demand for various kernel versions. The server accepts kernel headers, builds DRBD modules asynchronously, and caches the results for efficient reuse. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The DRBD Build Server is designed to provide a centralized service for building DRBD kernel modules. It eliminates the need to build modules on each node by providing a shared build service that can be accessed across a Kubernetes cluster. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **On-Demand Module Building**: Builds DRBD kernel modules for any kernel version |
| 12 | +- **Intelligent Caching**: Caches built modules using SHA256 hash of kernel version and headers |
| 13 | +- **Asynchronous Builds**: Non-blocking build process with job status tracking |
| 14 | +- **Multiple DRBD Source Options**: |
| 15 | + - Use pre-existing DRBD source directory |
| 16 | + - Clone from Git repository with version specification |
| 17 | +- **RESTful API**: Simple HTTP API for build requests and status checks |
| 18 | +- **SPAAS Integration**: Integrates with DRBD's SPAAS build service |
| 19 | +- **TLS Support**: Optional TLS encryption for secure communication |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +The server consists of several components: |
| 24 | + |
| 25 | +- **HTTP Server**: Handles incoming build requests and serves cached modules |
| 26 | +- **Build Service**: Manages build jobs and coordinates the build process |
| 27 | +- **Job Repository**: Tracks build job status and metadata |
| 28 | +- **Cache System**: Stores built modules in compressed tar.gz format |
| 29 | + |
| 30 | +## API Endpoints |
| 31 | + |
| 32 | +### POST `/api/v1/build` |
| 33 | + |
| 34 | +Initiates a build request for DRBD kernel modules. |
| 35 | + |
| 36 | +**Request:** |
| 37 | +- **Body**: Kernel headers as `tar.gz` archive |
| 38 | +- **Headers**: |
| 39 | + - `X-Kernel-Version` (optional): Kernel version (e.g., `5.15.0-86-generic`) |
| 40 | +- **Query Parameters**: |
| 41 | + - `kernel_version` (optional): Kernel version (alternative to header) |
| 42 | + |
| 43 | +**Response:** |
| 44 | +- `202 Accepted`: Build job created or already in progress |
| 45 | +- `200 OK`: Cached module found, returns module file immediately |
| 46 | + |
| 47 | +**Response Body (JSON):** |
| 48 | +```json |
| 49 | +{ |
| 50 | + "status": "building", |
| 51 | + "job_id": "abc123..." |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### GET `/api/v1/status/{job_id}` |
| 56 | + |
| 57 | +Retrieves the status of a build job. |
| 58 | + |
| 59 | +**Response:** |
| 60 | +- `200 OK`: Job completed or failed |
| 61 | +- `202 Accepted`: Job still in progress |
| 62 | +- `404 Not Found`: Job not found |
| 63 | + |
| 64 | +**Response Body (JSON):** |
| 65 | +```json |
| 66 | +{ |
| 67 | + "status": "completed", |
| 68 | + "job_id": "abc123...", |
| 69 | + "kernel_version": "5.15.0-86-generic", |
| 70 | + "created_at": "2024-01-01T12:00:00Z", |
| 71 | + "completed_at": "2024-01-01T12:05:00Z", |
| 72 | + "duration": "5m0s", |
| 73 | + "cache_path": "/var/cache/drbd-build-server/abc123....tar.gz", |
| 74 | + "download_url": "/api/v1/download/abc123..." |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### GET `/api/v1/download/{job_id}` |
| 79 | + |
| 80 | +Downloads the built DRBD kernel modules. |
| 81 | + |
| 82 | +**Response:** |
| 83 | +- `200 OK`: Returns `application/gzip` file with modules |
| 84 | +- `400 Bad Request`: Job not completed yet |
| 85 | +- `404 Not Found`: Job or cache file not found |
| 86 | + |
| 87 | +### GET `/api/v1/hello` |
| 88 | + |
| 89 | +Health check endpoint. |
| 90 | + |
| 91 | +**Response:** |
| 92 | +- `200 OK`: Server is running |
| 93 | + |
| 94 | +## Configuration |
| 95 | + |
| 96 | +### Command-Line Flags |
| 97 | + |
| 98 | +| Flag | Default | Description | |
| 99 | +|------|---------|-------------| |
| 100 | +| `--addr` | `:2021` | Server address and port | |
| 101 | +| `--drbd-dir` | `/drbd` | Path to DRBD source directory | |
| 102 | +| `--drbd-version` | `` | DRBD version to clone (e.g., `9.2.12`) | |
| 103 | +| `--drbd-repo` | `https://github.com/LINBIT/drbd.git` | DRBD repository URL | |
| 104 | +| `--cache-dir` | `/var/cache/drbd-build-server` | Path to cache directory | |
| 105 | +| `--maxbytesbody` | `104857600` | Maximum request body size (100MB) | |
| 106 | +| `--keeptmpdir` | `false` | Keep temporary directories for debugging | |
| 107 | +| `--certfile` | `` | Path to TLS certificate file | |
| 108 | +| `--keyfile` | `` | Path to TLS key file | |
| 109 | +| `--log-level` | `info` | Log level: `debug`, `info`, `warn`, `error` | |
| 110 | +| `--version` | - | Print version and exit | |
| 111 | + |
| 112 | +### Environment Variables |
| 113 | + |
| 114 | +| Variable | Description | |
| 115 | +|----------|-------------| |
| 116 | +| `SPAAS_URL` | SPAAS service URL (default: `https://spaas.drbd.io`) | |
| 117 | +| `DRBD_VERSION` | DRBD version to clone (overrides `--drbd-version`) | |
| 118 | +| `DRBD_REPO` | DRBD repository URL (overrides `--drbd-repo`) | |
| 119 | + |
| 120 | +## Build Process |
| 121 | + |
| 122 | +1. **Request Reception**: Server receives kernel headers archive and kernel version |
| 123 | +2. **Cache Check**: Generates cache key (SHA256 of kernel version + headers) and checks cache |
| 124 | +3. **Job Creation**: If not cached, creates a new build job |
| 125 | +4. **DRBD Source Preparation**: |
| 126 | + - If `--drbd-dir` exists: Copies DRBD source to isolated build directory |
| 127 | + - If `--drbd-version` specified: Clones DRBD repository for the specified version |
| 128 | +5. **Kernel Headers Extraction**: Extracts kernel headers from request body |
| 129 | +6. **Build Execution**: Runs `make module` with kernel build directory |
| 130 | +7. **Module Collection**: Collects all `.ko` files from build output |
| 131 | +8. **Archive Creation**: Creates `tar.gz` archive with modules |
| 132 | +9. **Cache Storage**: Saves archive to cache directory |
| 133 | +10. **Status Update**: Updates job status to `completed` |
| 134 | + |
| 135 | +## Caching |
| 136 | + |
| 137 | +The server uses an intelligent caching mechanism: |
| 138 | + |
| 139 | +- **Cache Key**: SHA256 hash of kernel version + kernel headers data |
| 140 | +- **Cache Location**: `/var/cache/drbd-build-server/{cache_key}.tar.gz` |
| 141 | +- **Cache Hit**: If cache exists and is valid, module is served immediately |
| 142 | +- **Cache Miss**: New build job is created |
| 143 | + |
| 144 | +This ensures that identical kernel configurations result in cache hits, significantly reducing build time. |
| 145 | + |
| 146 | +## Job Status |
| 147 | + |
| 148 | +Build jobs can have the following statuses: |
| 149 | + |
| 150 | +- `notExist`: Job does not exist |
| 151 | +- `pending`: Job created but not yet started |
| 152 | +- `building`: Build in progress |
| 153 | +- `completed`: Build completed successfully |
| 154 | +- `failed`: Build failed with error |
| 155 | + |
| 156 | +## Usage Examples |
| 157 | + |
| 158 | +### Build Request |
| 159 | + |
| 160 | +```bash |
| 161 | +# Send kernel headers and request build |
| 162 | +curl -X POST \ |
| 163 | + -H "X-Kernel-Version: 5.15.0-86-generic" \ |
| 164 | + --data-binary @kernel-headers.tar.gz \ |
| 165 | + http://drbd-build-server:2021/api/v1/build |
| 166 | +``` |
| 167 | + |
| 168 | +### Check Build Status |
| 169 | + |
| 170 | +```bash |
| 171 | +# Check job status |
| 172 | +curl http://drbd-build-server:2021/api/v1/status/abc123... |
| 173 | +``` |
| 174 | + |
| 175 | +### Download Built Modules |
| 176 | + |
| 177 | +```bash |
| 178 | +# Download completed build |
| 179 | +curl -O http://drbd-build-server:2021/api/v1/download/abc123... |
| 180 | +``` |
| 181 | + |
| 182 | +### Using Query Parameter for Kernel Version |
| 183 | + |
| 184 | +```bash |
| 185 | +curl -X POST \ |
| 186 | + "http://drbd-build-server:2021/api/v1/build?kernel_version=5.15.0-86-generic" \ |
| 187 | + --data-binary @kernel-headers.tar.gz |
| 188 | +``` |
| 189 | + |
| 190 | +## Deployment |
| 191 | + |
| 192 | +The server is designed to run in Kubernetes and is typically deployed as a Deployment with: |
| 193 | + |
| 194 | +- **Persistent Cache Volume**: For module cache persistence |
| 195 | +- **DRBD Source Volume**: Optional, if using pre-existing DRBD source |
| 196 | +- **Resource Limits**: CPU and memory limits based on build requirements |
| 197 | +- **Health Checks**: Liveness, readiness, and startup probes on `/api/v1/hello` |
| 198 | + |
| 199 | +### Resource Requirements |
| 200 | + |
| 201 | +- **Minimum**: 100m CPU, 512Mi memory |
| 202 | +- **Maximum**: 2000m CPU, 4Gi memory (with VPA) |
| 203 | +- **Storage**: Depends on cache size and number of kernel versions |
| 204 | + |
| 205 | +## Kernel Version Format |
| 206 | + |
| 207 | +Kernel versions must match the format: |
| 208 | +``` |
| 209 | +X.Y.Z[-flavor] or X.Y.Z[-flavor]-build |
| 210 | +``` |
| 211 | + |
| 212 | +Examples: |
| 213 | +- `5.15.0` |
| 214 | +- `5.15.0-generic` |
| 215 | +- `5.15.0-86-generic` |
| 216 | + |
| 217 | +## DRBD Source Configuration |
| 218 | + |
| 219 | +The server supports two modes for DRBD source: |
| 220 | + |
| 221 | +1. **Pre-existing Directory**: Mount DRBD source at `--drbd-dir` (default: `/drbd`) |
| 222 | +2. **Git Clone**: Specify `--drbd-version` to clone from repository |
| 223 | + |
| 224 | +When cloning, the server: |
| 225 | +- Clones the branch `drbd-{version}` (e.g., `drbd-9.2.12`) |
| 226 | +- Initializes and updates submodules |
| 227 | +- Creates `.drbd_git_revision` file with git hash |
| 228 | +- Removes `.git` directory after cloning |
| 229 | + |
| 230 | +## Security Considerations |
| 231 | + |
| 232 | +- **TLS**: Use `--certfile` and `--keyfile` for encrypted communication |
| 233 | +- **Request Size Limits**: `--maxbytesbody` limits request body size (default: 100MB) |
| 234 | +- **Path Traversal Protection**: Archive extraction validates paths to prevent directory traversal |
| 235 | +- **Isolated Builds**: Each build uses an isolated temporary directory |
| 236 | + |
| 237 | +## Troubleshooting |
| 238 | + |
| 239 | +### Enable Debug Logging |
| 240 | + |
| 241 | +```bash |
| 242 | +--log-level=debug |
| 243 | +``` |
| 244 | + |
| 245 | +### Keep Temporary Directories |
| 246 | + |
| 247 | +```bash |
| 248 | +--keeptmpdir=true |
| 249 | +``` |
| 250 | + |
| 251 | +This allows inspection of build artifacts for debugging. |
| 252 | + |
| 253 | +### Check Build Logs |
| 254 | + |
| 255 | +The server logs all build operations with structured logging. Look for: |
| 256 | +- `job_id`: Unique identifier for each build |
| 257 | +- `kernel_version`: Target kernel version |
| 258 | +- `cache_key`: Cache key for the build |
| 259 | +- `status`: Current build status |
| 260 | + |
| 261 | +## Development |
| 262 | + |
| 263 | +### Building |
| 264 | + |
| 265 | +```bash |
| 266 | +go build -o drbd-build-server ./cmd/main.go |
| 267 | +``` |
| 268 | + |
| 269 | +### Running Locally |
| 270 | + |
| 271 | +```bash |
| 272 | +./drbd-build-server \ |
| 273 | + --addr=:2021 \ |
| 274 | + --drbd-version=9.2.12 \ |
| 275 | + --cache-dir=./cache \ |
| 276 | + --log-level=debug |
| 277 | +``` |
| 278 | + |
| 279 | +## License |
| 280 | + |
| 281 | +See the main project LICENSE file. |
| 282 | + |
0 commit comments