|
| 1 | +# ChaosBlade Build Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +ChaosBlade is a powerful chaos engineering platform that supports compiling various project components on Mac, Linux, or Windows platforms. This document provides detailed instructions on how to build the ChaosBlade project. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +### System Requirements |
| 10 | +- **Git Version**: >= 1.8.5 |
| 11 | +- **Go Version**: Supports Go modules |
| 12 | +- **Operating System**: macOS (Darwin), Linux, Windows |
| 13 | + |
| 14 | +### Dependencies |
| 15 | +- Go compiler |
| 16 | +- Git |
| 17 | +- Make |
| 18 | +- Optional: Docker or Podman (for containerized builds) |
| 19 | + |
| 20 | +## Version Management |
| 21 | + |
| 22 | +### Automatic Version Detection |
| 23 | +ChaosBlade supports automatically obtaining version numbers from Git Tags: |
| 24 | +- If Git Tag exists, the version number will be automatically extracted (removing the v prefix) |
| 25 | +- If no Git Tag exists, the default version 1.7.4 or environment variable `BLADE_VERSION` will be used |
| 26 | + |
| 27 | +### Manual Version Setting |
| 28 | +```bash |
| 29 | +export BLADE_VERSION=1.8.0 |
| 30 | +make build |
| 31 | +``` |
| 32 | + |
| 33 | +## Build Targets |
| 34 | + |
| 35 | +### Basic Build |
| 36 | + |
| 37 | +#### 1. Current Platform Build |
| 38 | +```bash |
| 39 | +# Build CLI tool |
| 40 | +make build |
| 41 | + |
| 42 | +# Build all components |
| 43 | +make build_all |
| 44 | +``` |
| 45 | + |
| 46 | +#### 2. Specific Platform Build |
| 47 | +```bash |
| 48 | +# Build all components for specific platform |
| 49 | +make darwin_amd64 |
| 50 | +make darwin_arm64 |
| 51 | +make linux_amd64 |
| 52 | +make linux_arm64 |
| 53 | +make windows_amd64 |
| 54 | + |
| 55 | +# Build specific components for specific platform |
| 56 | +make linux_amd64 MODULES=cli |
| 57 | +make linux_amd64 MODULES=cli,os,java |
| 58 | +make linux_amd64 MODULES=all |
| 59 | +``` |
| 60 | + |
| 61 | +### Component List |
| 62 | + |
| 63 | +| Component | Description | Notes | |
| 64 | +|-----------|-------------|-------| |
| 65 | +| `cli` | Command line tool | Core CLI tool | |
| 66 | +| `os` | Operating system experiment scenarios | Basic resource experiment scenarios | |
| 67 | +| `cloud` | Cloud platform experiment scenarios | Cloud service experiment scenarios | |
| 68 | +| `middleware` | Middleware experiment scenarios | Middleware service experiment scenarios | |
| 69 | +| `java` | Java experiment scenarios | JVM-related experiment scenarios | |
| 70 | +| `cplus` | C/C++ experiment scenarios | C/C++ application experiment scenarios | |
| 71 | +| `cri` | Container runtime experiment scenarios | CRI-related experiment scenarios | |
| 72 | +| `kubernetes` | Kubernetes experiment scenarios | K8s-related experiment scenarios | |
| 73 | +| `nsexec` | Namespace executor | Linux only, supports cross-platform compilation | |
| 74 | +| `check_yaml` | Check specification files | Downloads check specification YAML files | |
| 75 | + |
| 76 | +## Build Process |
| 77 | + |
| 78 | +### 1. Pre-build Preparation |
| 79 | +```bash |
| 80 | +make pre_build |
| 81 | +``` |
| 82 | +- Generate version information |
| 83 | +- Clean and create build directories |
| 84 | +- Set platform-specific environment variables |
| 85 | + |
| 86 | +### 2. Component Build |
| 87 | +Each component has an independent build process, including: |
| 88 | +- Clone or update source code repositories |
| 89 | +- Execute platform-specific build commands |
| 90 | +- Copy build artifacts to target directories |
| 91 | + |
| 92 | +### 3. Packaging |
| 93 | +After build completion, platform-specific compressed packages are automatically generated: |
| 94 | +- `chaosblade-{version}-{platform}_{arch}.tar.gz` |
| 95 | + |
| 96 | +## Platform-Specific Build |
| 97 | + |
| 98 | +### macOS (Darwin) |
| 99 | +```bash |
| 100 | +# AMD64 architecture |
| 101 | +make darwin_amd64 |
| 102 | + |
| 103 | +# ARM64 architecture (Apple Silicon) |
| 104 | +make darwin_arm64 |
| 105 | +``` |
| 106 | + |
| 107 | +### Linux |
| 108 | +```bash |
| 109 | +# AMD64 architecture |
| 110 | +make linux_amd64 |
| 111 | + |
| 112 | +# ARM64 architecture |
| 113 | +make linux_arm64 |
| 114 | +``` |
| 115 | + |
| 116 | +### Windows |
| 117 | +```bash |
| 118 | +# AMD64 architecture |
| 119 | +make windows_amd64 |
| 120 | +``` |
| 121 | + |
| 122 | +## Containerized Build |
| 123 | + |
| 124 | +### Docker Image Build |
| 125 | +```bash |
| 126 | +# Build Linux AMD64 image |
| 127 | +make build_linux_amd64_image |
| 128 | + |
| 129 | +# Build Linux ARM64 image |
| 130 | +make build_linux_arm64_image |
| 131 | +``` |
| 132 | + |
| 133 | +### Image Push |
| 134 | +```bash |
| 135 | +# Push to container image registry |
| 136 | +make push_image |
| 137 | +``` |
| 138 | + |
| 139 | +## Cross-Platform Compilation |
| 140 | + |
| 141 | +### nsexec Cross-Platform Compilation |
| 142 | +The nsexec component supports cross-compilation from macOS to Linux: |
| 143 | + |
| 144 | +#### Automatic Compiler Detection |
| 145 | +The system automatically detects available cross-compilation toolchains: |
| 146 | +- `musl-gcc` |
| 147 | +- `/usr/local/musl/bin/musl-gcc` |
| 148 | +- `x86_64-linux-musl-gcc` |
| 149 | +- `aarch64-linux-musl-gcc` |
| 150 | +- `gcc` |
| 151 | +- `aarch64-linux-gnu-gcc` |
| 152 | + |
| 153 | +#### Containerized Compilation |
| 154 | +If no suitable cross-compilation toolchain is available, containers can be used for compilation: |
| 155 | +```bash |
| 156 | +# Using Docker |
| 157 | +make nsexec CONTAINER_RUNTIME=docker |
| 158 | + |
| 159 | +# Using Podman |
| 160 | +make nsexec CONTAINER_RUNTIME=podman |
| 161 | +``` |
| 162 | + |
| 163 | +## Build Configuration |
| 164 | + |
| 165 | +### Environment Variables |
| 166 | +- `GOOS`: Target operating system |
| 167 | +- `GOARCH`: Target architecture |
| 168 | +- `BLADE_VERSION`: Version number |
| 169 | +- `CONTAINER_RUNTIME`: Container runtime (docker/podman) |
| 170 | +- `MODULES`: List of components to build |
| 171 | + |
| 172 | +### Build Flags |
| 173 | +- `CGO_ENABLED=0`: Disable CGO |
| 174 | +- `GO111MODULE=on`: Enable Go modules |
| 175 | +- Static linking flags: `-ldflags="-s -w"` |
| 176 | + |
| 177 | +## Build Artifacts |
| 178 | + |
| 179 | +### Directory Structure |
| 180 | +``` |
| 181 | +target/ |
| 182 | +└── chaosblade-{version}-{platform}_{arch}/ |
| 183 | + ├── bin/ # Executable files |
| 184 | + ├── lib/ # Library files |
| 185 | + └── yaml/ # Configuration files |
| 186 | +``` |
| 187 | + |
| 188 | +### File Naming |
| 189 | +- Executable files: `blade` (Linux/macOS) or `blade.exe` (Windows) |
| 190 | +- Compressed packages: `chaosblade-{version}-{platform}_{arch}.tar.gz` |
| 191 | + |
| 192 | +## Common Command Examples |
| 193 | + |
| 194 | +### Quick Start |
| 195 | +```bash |
| 196 | +# Build for current platform |
| 197 | +make build |
| 198 | + |
| 199 | +# Build all components |
| 200 | +make build_all |
| 201 | +``` |
| 202 | + |
| 203 | +### Specific Platform Build |
| 204 | +```bash |
| 205 | +# Build for Linux AMD64 platform |
| 206 | +make linux_amd64 |
| 207 | + |
| 208 | +# Build for macOS ARM64 platform |
| 209 | +make darwin_arm64 |
| 210 | +``` |
| 211 | + |
| 212 | +### Component Selection Build |
| 213 | +```bash |
| 214 | +# Only build CLI and OS components |
| 215 | +make linux_amd64 MODULES=cli,os |
| 216 | + |
| 217 | +# Build all components |
| 218 | +make linux_amd64 MODULES=all |
| 219 | +``` |
| 220 | + |
| 221 | +### Container Image Build |
| 222 | +```bash |
| 223 | +# Build and push images |
| 224 | +make build_linux_amd64_image |
| 225 | +make build_linux_arm64_image |
| 226 | +make push_image |
| 227 | +``` |
| 228 | + |
| 229 | +## Troubleshooting |
| 230 | + |
| 231 | +### Common Issues |
| 232 | + |
| 233 | +#### 1. Git Version Too Low |
| 234 | +```bash |
| 235 | +# Error message |
| 236 | +ALERTMSG="please update git to >= 1.8.5" |
| 237 | + |
| 238 | +# Solution |
| 239 | +# Ubuntu/Debian |
| 240 | +sudo apt-get update && sudo apt-get install git |
| 241 | + |
| 242 | +# macOS |
| 243 | +brew install git |
| 244 | +``` |
| 245 | + |
| 246 | +#### 2. Missing Cross-Compilation Toolchain |
| 247 | +```bash |
| 248 | +# Ubuntu/Debian |
| 249 | +sudo apt-get install musl-tools gcc-aarch64-linux-gnu |
| 250 | + |
| 251 | +# macOS |
| 252 | +brew install FiloSottile/musl-cross/musl-cross |
| 253 | +``` |
| 254 | + |
| 255 | +#### 3. Container Runtime Issues |
| 256 | +```bash |
| 257 | +# Check Docker status |
| 258 | +docker info |
| 259 | + |
| 260 | +# Check Podman status |
| 261 | +podman info |
| 262 | + |
| 263 | +# Manually specify container runtime |
| 264 | +make nsexec CONTAINER_RUNTIME=docker |
| 265 | +``` |
| 266 | + |
| 267 | +### Clean Build Artifacts |
| 268 | +```bash |
| 269 | +# Clean all build artifacts |
| 270 | +make clean |
| 271 | +``` |
| 272 | + |
| 273 | +## Testing |
| 274 | + |
| 275 | +### Run Tests |
| 276 | +```bash |
| 277 | +# Run all tests |
| 278 | +make test |
| 279 | +``` |
| 280 | + |
| 281 | +### Test Coverage |
| 282 | +Tests generate coverage reports: |
| 283 | +- File: `coverage.txt` |
| 284 | +- Mode: `atomic` |
| 285 | + |
| 286 | +## Help Information |
| 287 | + |
| 288 | +### View Help |
| 289 | +```bash |
| 290 | +make help |
| 291 | +``` |
| 292 | + |
| 293 | +### Available Targets |
| 294 | +```bash |
| 295 | +# View all available make targets |
| 296 | +make -n help |
| 297 | +``` |
| 298 | + |
| 299 | +## Related Links |
| 300 | + |
| 301 | +- [Project Homepage](https://github.com/chaosblade-io/chaosblade) |
| 302 | +- [Contributing Guide](CONTRIBUTING.md) |
| 303 | +- [Code Style Guide](docs/code_styles.md) |
| 304 | +- [Release Process](docs/release_process.md) |
| 305 | + |
0 commit comments