|
| 1 | +# HAProxy Cookbook Development Instructions |
| 2 | + |
| 3 | +**ALWAYS follow these instructions first and fallback to additional search and context gathering only if the information here is incomplete or found to be in error.** |
| 4 | + |
| 5 | +This is a Chef cookbook for installing and configuring HAProxy load balancer. It provides custom Chef resources for managing HAProxy installations, configurations, and services across multiple platforms. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Bootstrap Environment and Dependencies |
| 10 | +```bash |
| 11 | +# Install Chef development tools (choose one method): |
| 12 | +# Method 1: Install Chef Workstation (recommended) |
| 13 | +wget https://packages.chef.io/files/stable/chef-workstation/24.12.1031/ubuntu/24.04/chef-workstation_24.12.1031-1_amd64.deb |
| 14 | +sudo dpkg -i chef-workstation_24.12.1031-1_amd64.deb |
| 15 | + |
| 16 | +# Method 2: Install individual gems if Chef Workstation fails |
| 17 | +sudo gem install cookstyle chefspec rspec berkshelf test-kitchen kitchen-dokken --no-document |
| 18 | +``` |
| 19 | + |
| 20 | +### Core Development Commands |
| 21 | +```bash |
| 22 | +# Install cookbook dependencies (if berkshelf is available) |
| 23 | +berks install |
| 24 | +# NOTE: If berkshelf is not installed, cookbook will still work for basic linting/testing |
| 25 | + |
| 26 | +# Lint Ruby code - takes 2-3 seconds - NEVER CANCEL |
| 27 | +cookstyle . |
| 28 | + |
| 29 | +# Fix auto-correctable linting issues |
| 30 | +cookstyle -a . |
| 31 | + |
| 32 | +# Run unit tests - takes 30 seconds to 2 minutes - NEVER CANCEL |
| 33 | +# NOTE: Requires full Chef installation with chefspec gem |
| 34 | +rspec spec/ |
| 35 | + |
| 36 | +# Run all unit tests with detailed output |
| 37 | +rspec spec/ --format documentation |
| 38 | +``` |
| 39 | + |
| 40 | +### Integration Testing (Advanced) |
| 41 | +**CRITICAL**: Integration tests require Docker and take 15-45 minutes per suite. NEVER CANCEL. |
| 42 | +**NOTE**: Requires test-kitchen and kitchen-dokken gems installed. |
| 43 | + |
| 44 | +```bash |
| 45 | +# List all available test suites (requires test-kitchen installation) |
| 46 | +kitchen list |
| 47 | + |
| 48 | +# Test a specific suite - NEVER CANCEL: Takes 15-45 minutes |
| 49 | +# Set timeout to 60+ minutes for any kitchen commands |
| 50 | +kitchen test package-ubuntu-2204 |
| 51 | + |
| 52 | +# Test source compilation (takes longest) - NEVER CANCEL: Takes 30-60 minutes |
| 53 | +kitchen test source-default-ubuntu-2204 |
| 54 | + |
| 55 | +# Destroy test instances after testing |
| 56 | +kitchen destroy |
| 57 | +``` |
| 58 | + |
| 59 | +### Build Validation Workflow |
| 60 | +**Minimal validation** (works in any environment with cookstyle): |
| 61 | +```bash |
| 62 | +# 1. Lint code (~2-3 seconds) - ALWAYS WORKS |
| 63 | +cookstyle . |
| 64 | + |
| 65 | +# 2. Fix linting issues automatically - ALWAYS WORKS |
| 66 | +cookstyle -a . |
| 67 | + |
| 68 | +# 3. Verify Ruby syntax - ALWAYS WORKS |
| 69 | +ruby -c resources/install.rb |
| 70 | +ruby -c resources/service.rb |
| 71 | +``` |
| 72 | + |
| 73 | +**Full validation** (requires complete Chef environment): |
| 74 | +```bash |
| 75 | +# 1. Lint code (~2-3 seconds) |
| 76 | +cookstyle . |
| 77 | + |
| 78 | +# 2. Run unit tests (~30 seconds - 2 minutes) - NEVER CANCEL |
| 79 | +rspec spec/ |
| 80 | + |
| 81 | +# 3. Test basic package installation (15-30 minutes) - NEVER CANCEL |
| 82 | +kitchen test package-ubuntu-2204 |
| 83 | + |
| 84 | +# 4. Clean up test instances |
| 85 | +kitchen destroy |
| 86 | +``` |
| 87 | + |
| 88 | +## Timing Expectations and Timeouts |
| 89 | +- **Linting (`cookstyle`)**: 2-3 seconds |
| 90 | +- **Unit tests (`rspec`)**: 30 seconds to 2 minutes - NEVER CANCEL |
| 91 | +- **Single integration test**: 15-45 minutes - NEVER CANCEL - Set timeout to 60+ minutes |
| 92 | +- **Source compilation tests**: 30-60 minutes - NEVER CANCEL - Set timeout to 90+ minutes |
| 93 | +- **Full CI matrix**: 1-2 hours across all platforms and suites |
| 94 | + |
| 95 | +**CRITICAL**: Always set timeouts of 60+ minutes for kitchen commands and 30+ minutes for unit tests. |
| 96 | + |
| 97 | +## Manual Validation Scenarios |
| 98 | + |
| 99 | +After making changes, ALWAYS test at least one complete scenario: |
| 100 | + |
| 101 | +### Package Installation Validation |
| 102 | +```bash |
| 103 | +# Test the most common installation method |
| 104 | +kitchen converge package-ubuntu-2204 |
| 105 | +kitchen verify package-ubuntu-2204 |
| 106 | +kitchen destroy package-ubuntu-2204 |
| 107 | +``` |
| 108 | + |
| 109 | +### Source Compilation Validation |
| 110 | +```bash |
| 111 | +# Test source compilation (most complex scenario) |
| 112 | +kitchen converge source-default-ubuntu-2204 |
| 113 | +kitchen verify source-default-ubuntu-2204 |
| 114 | +kitchen destroy source-default-ubuntu-2204 |
| 115 | +``` |
| 116 | + |
| 117 | +### Configuration Validation |
| 118 | +```bash |
| 119 | +# Test configuration management |
| 120 | +kitchen converge config-2-ubuntu-2204 |
| 121 | +kitchen verify config-2-ubuntu-2204 |
| 122 | +kitchen destroy config-2-ubuntu-2204 |
| 123 | +``` |
| 124 | + |
| 125 | +## Repository Structure and Navigation |
| 126 | + |
| 127 | +### Key Directories |
| 128 | +- `resources/` - Custom Chef resources (install.rb, service.rb, config_global.rb, etc.) |
| 129 | +- `libraries/` - Helper modules and shared code |
| 130 | +- `test/cookbooks/test/recipes/` - Test recipes for integration testing |
| 131 | +- `test/integration/` - InSpec integration test controls |
| 132 | +- `spec/unit/` - ChefSpec unit tests |
| 133 | +- `templates/` - ERB templates for configuration files |
| 134 | +- `documentation/` - Resource documentation |
| 135 | + |
| 136 | +### Important Files |
| 137 | +- `metadata.rb` - Cookbook metadata and dependencies |
| 138 | +- `Berksfile` - Cookbook dependency management |
| 139 | +- `kitchen.yml` - Vagrant-based integration testing (local development) |
| 140 | +- `kitchen.dokken.yml` - Docker-based integration testing (CI) |
| 141 | +- `.rubocop.yml` - Ruby linting configuration |
| 142 | +- `.github/workflows/ci.yml` - Continuous integration pipeline |
| 143 | + |
| 144 | +### Common Test Suites |
| 145 | +- `package` - Test package installation method |
| 146 | +- `source-default` - Test default source compilation |
| 147 | +- `source-24`, `source-26`, `source-28` - Test specific HAProxy versions |
| 148 | +- `config-2`, `config-acl`, `config-ssl-redirect` - Test various configurations |
| 149 | +- `source-lua` - Test Lua compilation support |
| 150 | + |
| 151 | +## Cookbook Architecture |
| 152 | + |
| 153 | +### Custom Resources Available |
| 154 | +- `haproxy_install` - Install HAProxy via package or source |
| 155 | +- `haproxy_service` - Manage HAProxy service |
| 156 | +- `haproxy_config_global` - Global configuration section |
| 157 | +- `haproxy_config_defaults` - Default configuration section |
| 158 | +- `haproxy_frontend` - Frontend configuration |
| 159 | +- `haproxy_backend` - Backend configuration |
| 160 | +- `haproxy_listen` - Listen section (combines frontend/backend) |
| 161 | +- `haproxy_acl` - Access Control Lists |
| 162 | +- `haproxy_userlist` - User authentication lists |
| 163 | + |
| 164 | +### Installation Methods |
| 165 | +1. **Package Installation**: Uses system packages (fastest, recommended for most users) |
| 166 | +2. **Source Compilation**: Compiles HAProxy from source (slower, more flexible options) |
| 167 | + |
| 168 | +## Development Workflow |
| 169 | + |
| 170 | +### Making Changes |
| 171 | +1. **Understand the resource**: Check `resources/` directory for the relevant resource file |
| 172 | +2. **Check existing tests**: Look in `test/integration/` and `spec/unit/` for related tests |
| 173 | +3. **Make minimal changes**: Modify only what's necessary |
| 174 | +4. **Test locally**: Run linting and unit tests first |
| 175 | +5. **Integration test**: Test with kitchen for the affected functionality |
| 176 | +6. **Validate**: Ensure the resource works as expected in a real scenario |
| 177 | + |
| 178 | +### Adding New Features |
| 179 | +1. **Check existing resources**: See if functionality exists in another resource |
| 180 | +2. **Follow patterns**: Use existing resources as templates for new ones |
| 181 | +3. **Add tests**: Create both unit tests (ChefSpec) and integration tests (InSpec) |
| 182 | +4. **Document**: Add or update documentation in `documentation/` directory |
| 183 | + |
| 184 | +### Debugging Issues |
| 185 | +- Use `kitchen diagnose` to check test kitchen configuration |
| 186 | +- Use `kitchen login` to access test instances for debugging |
| 187 | +- Check `/var/log/chef/` in test instances for Chef run logs |
| 188 | +- Use `haproxy -c -f /etc/haproxy/haproxy.cfg` to validate HAProxy config syntax |
| 189 | + |
| 190 | +## Platform Support |
| 191 | +- **Debian**: 11, 12 |
| 192 | +- **Ubuntu**: 20.04, 22.04 |
| 193 | +- **CentOS Stream**: 9 |
| 194 | +- **Amazon Linux**: 2023 |
| 195 | + |
| 196 | +Test changes across different platforms when modifying core installation or service logic. |
| 197 | + |
| 198 | +## CI Pipeline Understanding |
| 199 | +The GitHub Actions CI runs: |
| 200 | +1. **Lint-unit**: Cookstyle linting + ChefSpec unit tests (~5 minutes) |
| 201 | +2. **Integration**: Kitchen tests across platform matrix (~1-2 hours total) |
| 202 | +3. **Platform-specific**: Additional testing for Amazon Linux |
| 203 | + |
| 204 | +The pipeline tests all combinations of: |
| 205 | +- Multiple operating systems |
| 206 | +- Package vs source installation methods |
| 207 | +- Different HAProxy versions and configurations |
| 208 | +- Various use cases (ACL, SSL, Lua support, etc.) |
| 209 | + |
| 210 | +## Common Pitfalls to Avoid |
| 211 | +- **DO NOT** run integration tests without Docker properly configured |
| 212 | +- **DO NOT** cancel long-running builds or tests - they take time to compile HAProxy |
| 213 | +- **DO NOT** modify resource partials in `resources/partial/` without understanding impacts |
| 214 | +- **DO NOT** change service resource without testing service management functionality |
| 215 | +- **ALWAYS** test both package and source installation methods if changing install logic |
| 216 | +- **ALWAYS** validate HAProxy configuration syntax when changing config generation |
| 217 | + |
| 218 | +## Quick Reference Commands |
| 219 | + |
| 220 | +```bash |
| 221 | +# Common file listing |
| 222 | +ls -la # Repository root contents |
| 223 | +ls resources/ # Available Chef resources |
| 224 | +ls test/integration/ # Integration test suites |
| 225 | +kitchen list # Available test instances |
| 226 | + |
| 227 | +# Quick validation |
| 228 | +cookstyle . | head -20 # Show first linting issues |
| 229 | +rspec spec/ --fail-fast # Stop on first test failure |
| 230 | +kitchen diagnose | grep -A5 platforms # Show test platforms |
| 231 | +``` |
| 232 | + |
| 233 | +Use these instructions as your primary reference. Only search for additional information when encountering errors or missing details not covered here. |
0 commit comments