Selectors
Selectors are Ctrlplane’s powerful filtering system for targeting resources, environments, and deployments. They allow dynamic, rules-based matching instead of static assignments.Why Selectors?
Traditional deployment tools require manually assigning resources to environments. Ctrlplane uses selectors for dynamic matching:Without Selectors (Traditional)
With Selectors (Ctrlplane)
Selector Types
Ctrlplane supports several types of selectors:- Metadata Selector - Match on resource metadata
- Kind Selector - Match on resource kind
- Identifier Selector - Match on resource identifier
- Name Selector - Match on resource/environment name
- CEL Selector - Complex expressions using CEL (Common Expression Language)
- Composite Selectors - Combine selectors with AND/OR logic
Metadata Selector
Match resources based on metadata key-value pairs.Simple Equality
metadata.environment === "production".
Supported Operators
equals
Exact match:not_equals
Does not match:contains
Value contains substring:prod-cluster-1, k8s-prod, production-server.
matches (Regex)
Regular expression match:prod-cluster-1, prod-server-42.
exists
Key exists with any value:team metadata key.
not_exists
Key does not exist:in
Value is in a list:not_in
Value is not in a list:Kind Selector
Match resources by theirkind field.
- Kubernetes-only deployments:
kind == "kubernetes-cluster" - VM-only deployments:
kind == "vm" - Exclude certain kinds:
kind != "deprecated-resource"
Identifier Selector
Match resources by their identifier.- Match specific naming patterns
- Filter by identifier prefix/suffix
- Regex matching on identifiers
Name Selector
Match by the resource or environment name field.- Policy selectors targeting specific environments
- Matching resources by name patterns
Composite Selectors (AND/OR)
Combine multiple conditions with boolean logic.AND Logic
All conditions must match:environment=production AND region=us-east-1.
OR Logic
Any condition can match:us-east-1 OR us-west-2.
Nested Logic
Combine AND and OR:environment=production AND (region=us-east-1 OR region=us-west-2).
CEL Selector
For complex logic, use CEL (Common Expression Language):CEL Basics
Field Access:metadata.environment- Access metadata fieldkind- Access kind fieldname- Access name fieldidentifier- Access identifier field
==,!=- Equality&&,||- Logical AND/OR!- Logical NOT<,>,<=,>=- Comparisonin- List membershipmatches- Regex match
When to Use CEL
Use CEL when:- ✅ You need complex nested logic
- ✅ You need mathematical operations
- ✅ Standard selectors are too verbose
- ✅ You’re comfortable with expression languages
- ❌ Simple metadata matching is sufficient
- ❌ You want UI-friendly selector editing
- ❌ Team is unfamiliar with CEL syntax
Selector Use Cases
Environment Resource Selectors
Define which resources belong to an environment:Deployment Resource Selectors
Limit which resources a deployment can target:Policy Selectors
Target policies to specific deployments/environments/resources:Testing Selectors
Via API
Test a selector against your resources:Via Web UI
Most selector configuration screens have a “Test Selector” or “Preview Resources” button that shows which resources match.Common Selector Patterns
Pattern 1: Environment-Based
Pattern 2: Region-Based
Pattern 3: Team-Based
Pattern 4: Tier-Based
Pattern 5: Canary
Pattern 6: Percentage-Based (using identifier)
Best Practices
Metadata Schema Design
Design your resource metadata with selectors in mind: Good Metadata Schema:- Consistent key names
- Enumerated values (not free-form)
- Multiple dimensions for targeting
- Clear, hierarchical organization
Selector Simplicity
Prefer Simple Selectors:- Easier to understand
- Easier to edit in UI
- Less error-prone
- More performant
Document Complex Selectors
If you must use complex selectors, document them:Test Before Applying
Always test selectors before using them:- Use the query API to see matched resources
- Verify count matches expectations
- Check for unexpected matches
- Test in lower environment first
Avoid Over-Matching
Be specific to avoid accidentally matching wrong resources: Too Broad:prod-cluster, product-server, reproduction-env.
Better:
prod-.
Troubleshooting
Selector matches too many resources
- Make selector more specific (add conditions)
- Use AND instead of OR
- Add negation conditions
- Check resource metadata for unexpected values
Selector matches too few resources
- Check metadata key names match exactly (case-sensitive)
- Verify resources have the expected metadata
- Use OR logic if resources vary
- Test with simpler selector first
Selector matches nothing
- Verify selector syntax is correct
- Check resources exist with expected metadata
- Test each condition independently
- Review resource metadata in UI
CEL expression errors
- Check syntax (use CEL validator)
- Verify field names are correct
- Test expression step-by-step
- Consult CEL documentation
Advanced Topics
Selector Evaluation Order
When multiple selectors apply (e.g., environment + deployment):- Environment selector filters resources
- Deployment selector (if present) filters further
- Final set is intersection of both
Selector Performance
Selectors are evaluated:- When resources are created/updated
- When environments/deployments are created/updated
- When querying resources
- Simple selectors are faster than complex ones
- Metadata equality checks are very fast
- Regex matching is slower
- CEL expressions have some overhead
Selector Caching
Ctrlplane caches selector evaluation results:- Cache invalidated on resource/metadata changes
- Improves performance for repeated queries
- No manual cache management needed
Next Steps
- Environments - Use selectors in environments
- Deployments - Filter deployment targets
- Resources - Design resource metadata
- Policies - Target policies with selectors