Template Variables Reference
Template variables enable dynamic configuration based on deployment context. They are essential for creating flexible, reusable m2p configurations.
Available Variables
| Variable | Description | Example Output |
|---|---|---|
@{ obj.key }@ |
Unique package key | myapp-abc123 |
@{ obj.fqdn_hostname }@ |
Main FQDN | myapp.example.com |
@{ package_release.name }@ |
Package name | myapp |
@{ package_release.main_fqdn }@ |
Full main FQDN | myapp.example.com |
Service Naming Convention
Muppy creates services with the pattern: {key}-{part_code}-svc
parts:
api: # Creates: {key}-api-svc
database: # Creates: {key}-database-svc
cache: # Creates: {key}-cache-svc
Usage Rules
1. Template variables ONLY work inside quoted strings
2. Use for ALL inter-service communication
- Database connections
- API endpoints
- Cache connections
- Message queues
3. Special Cases
- MongoDB replica set members
- Redis sentinel configurations
- Kubernetes probes with service references
Common Patterns
Database Connections
# PostgreSQL
- name: DATABASE_URL
value: "postgresql://user:pass@@{ obj.key }@-postgres-svc:5432/dbname"
# MongoDB
- name: MONGO_URI
value: "mongodb://@{ obj.key }@-mongo-svc:27017/dbname"
# MongoDB with replica set
- name: MONGO_URI
value: "mongodb://@{ obj.key }@-mongo-svc:27017/dbname?replicaSet=rs0"
# Redis
- name: REDIS_URL
value: "redis://@{ obj.key }@-redis-svc:6379/0"
API Communications
# Internal API communication
- name: INTERNAL_API_URL
value: "http://@{ obj.key }@-api-svc:8080"
# External/public URL
- name: PUBLIC_URL
value: "https://@{ obj.fqdn_hostname }@"
# Frontend to backend
- name: BACKEND_URL
value: "http://@{ obj.key }@-backend-svc:3000/api"
Microservices Architecture
# Service-to-service communication
- name: AUTH_SERVICE_URL
value: "http://@{ obj.key }@-auth-svc:4000"
- name: NOTIFICATION_SERVICE_URL
value: "http://@{ obj.key }@-notifier-svc:5000"
- name: PAYMENT_SERVICE_URL
value: "http://@{ obj.key }@-payment-svc:6000"
Special Cases
MongoDB Replica Set Initialization
When using MongoDB with replica sets, the probe commands also need template variables:
probes:
readiness:
enabled: true
type: exec
command:
- bash
- "-c"
- |
echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'@{ obj.key }@-mongodb-svc:27017'}]}) }" | mongosh --port 27017 --quiet
Redis with Sentinel
- name: REDIS_SENTINEL_URL
value: "redis-sentinel://@{ obj.key }@-redis-sentinel-svc:26379/mymaster"
Kubernetes Health Checks
probes:
readiness:
enabled: true
type: httpGet
path: /health
port: 8080
httpHeaders:
- name: Host
value: "@{ obj.fqdn_hostname }@"
Troubleshooting
Common Errors
Error: Pods start but can't connect to other services
Cause: Hardcoded service names without template variables
Solution: Replace mongodb://mongodb:27017 with "mongodb://@{ obj.key }@-mongodb-svc:27017"
Error: Template variable not working Cause: Missing quotes around the value Solution: Always wrap values containing template variables in quotes
Error: Wrong service name format
Cause: Incorrect template variable usage
Solution: Use exact pattern @{ obj.key }@-{part_name}-svc
Debugging Tips
- Check rendered values: Download the final Helm values from Muppy to see how variables were resolved
- Verify service names: Use
kubectl get services -n {namespace}to see actual service names - Test connections: Use debug mode to shell into containers and test connectivity manually
Best Practices
- Always use template variables for service discovery
- Quote all template variable values to prevent YAML parsing issues
- Test locally with
helm template --debugbefore deploying - Use descriptive part names as they become part of service names
- Keep part codes ≤ 8 characters to avoid long service names
Migration from Hardcoded Names
If you have existing configurations with hardcoded service names:
Step 1: Identify hardcoded references
Look for patterns like:
- mongodb://mongodb:
- redis://redis:
- http://backend:
- postgres://db:
Step 2: Replace with template variables
# Before
- name: DB_URL
value: postgresql://postgres:5432/myapp
# After
- name: DB_URL
value: "postgresql://@{ obj.key }@-postgres-svc:5432/myapp"
Step 3: Update and test
- Update Parts Config in Muppy
- Click "Sync 'Parts Config.'"
- Check pod logs for successful connections
💡 Remember: Template variables are what make m2p deployments truly portable and enable multiple instances in the same namespace!