Building Rancher from Source
This guide covers building Rancher from source code, including development setup, build configuration, and testing procedures.Prerequisites
Required Tools
- Go 1.22+: Installation guide
- Docker: For building container images
- Docker Buildx: For cross-platform builds
- kubectl: Kubernetes CLI tool
- Helm 3: For deploying Rancher
- Make: Build automation
- Git: Version control
Optional Tools
- k3d: For local Kubernetes clusters (Installation)
- Dapper: Build environment tool
Build Configuration
Build Variables File
Location:build.yaml
All build variables are defined in a single source of truth:
docs/build.md
This file contains:
- Component versions
- Dependency versions
- Build-time constants
- Environment variable defaults
Updating Build Variables
-
Edit build.yaml:
-
Regenerate Go constants:
-
Rebuild:
Adding New Build Variables
-
Add to build.yaml:
-
Update export-config script:
-
Run go generate:
-
Use in code:
scripts/build-serverscripts/build-agentpackage/Dockerfilepackage/Dockerfile.agentpkg/settings/setting.go
Building Rancher
Quick Build (Local Development)
Generate a local container image for testing:amd64, arm64
Source: docs/development.md
Full Build
Build all components:- Generate code from build.yaml
- Build server binary
- Build agent binary
- Run code generators
- Compile assets
Package for Distribution
Create distributable packages:- Docker images
- Helm charts
- Release artifacts
Build Targets
Available targets (fromMakefile):
Development Workflow
Local Development Setup
-
Clone the repository:
-
Install dependencies:
-
Set up local Kubernetes cluster (k3d):
-
Build and deploy:
Iterative Development
- Make code changes
-
Build incremental image:
-
Update deployment:
Hot Reload (Server Development)
For faster iteration during server development:-
Build binary:
-
Run locally (against remote cluster):
Deploying Custom Images
Using Helm
Deploy custom Rancher image:Using kubectl
Patch existing deployment:Testing
Unit Tests
Run unit tests:Integration Tests
Location:tests/v2/integration/
Requirements (from tests/README.md):
- Running Rancher instance
- Admin access token
- Go 1.22+
- k3d for local clusters
cattle-config.yaml:
Validation Tests
Location:tests/v2/validation/
Validation tests may require:
- Cloud provider credentials
- External service access
- Specific configuration files
Controller Tests
Location:tests/controllers/
End-to-End Tests
Location:pkg/controllers/management/test/e2e/
Code Generation
Generate All
Regenerate all generated code:- Build config constants
- Kubernetes client code
- Deep copy methods
- API schemas
Specific Generators
Build config:Using Dapper
Dapper provides a consistent build environment:Install Dapper
Build with Dapper
Build Optimization
Parallel Builds
Speed up Go builds:Build Cache
Leverage Go build cache:Docker BuildKit
Enable BuildKit for faster Docker builds:Troubleshooting
Build Failures
Dependency issues:Runtime Issues
Check logs:Performance Issues
Profile CPU:Continuous Integration
Rancher uses GitHub Actions for CI: Workflows location:.github/workflows/
Local CI testing:
Best Practices
- Always run
go generateafter changing build.yaml - Use Dapper for consistent builds across environments
- Test locally before pushing
- Keep dependencies updated:
go get -u ./... - Run tests before committing:
go test ./... - Follow Go conventions: Run
gofmtandgolint - Use feature branches for development
- Clean up Docker images regularly
- Document new build variables in comments
- Tag dev images with timestamps for traceability
Related Resources
- Build Configuration:
docs/build.md - Development Guide:
docs/development.md - Makefile:
Makefile - Build Variables:
build.yaml - Build Config Package:
pkg/buildconfig/ - Test Framework:
tests/README.md - Contributing: See Contributing Guide