Skip to content

Commit ab42db9

Browse files
committed
Enhancement: Add drift detection and automatic reconciliation
Proposal for drift detection feature.
1 parent eeb37af commit ab42db9

1 file changed

Lines changed: 229 additions & 0 deletions

File tree

enhancements/drift-detection.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# Enhancement: Drift Detection and Automatic Reconciliation
2+
3+
| Field | Value |
4+
|-------|-------|
5+
| **Status** | implementable |
6+
| **Author(s)** | @eshulman |
7+
| **Created** | 2026-02-03 |
8+
| **Last Updated** | 2026-02-03 |
9+
| **Tracking Issue** | TBD |
10+
11+
## Summary
12+
13+
This enhancement introduces drift detection and automatic reconciliation for ORC managed resources. The feature enables ORC to periodically check OpenStack resources for changes made outside of ORC (via CLI, dashboard, or other tools) and automatically restore them to match the desired state defined in the Kubernetes specification.
14+
15+
Additionally, managed resources that are deleted externally from OpenStack will be automatically recreated by ORC, ensuring the declared state is maintained.
16+
17+
## Motivation
18+
19+
In production environments, OpenStack resources may be modified outside of ORC through various means:
20+
21+
- Direct OpenStack CLI/SDK operations
22+
- OpenStack Horizon dashboard
23+
- Other automation tools or controllers
24+
- Manual emergency interventions
25+
- Third-party integrations
26+
27+
Without drift detection, these changes go unnoticed until they cause issues, leading to configuration drift between the declared Kubernetes state and the actual OpenStack state. This undermines the declarative model that ORC provides.
28+
29+
Similar Kubernetes controllers for cloud resources have implemented drift detection:
30+
31+
- **AWS Controllers for Kubernetes (ACK)**: Drift detection is **enabled by default** with a 10-hour resync period. Uses a detect-then-correct approach: periodically describes the AWS resource and only updates if drift is found. Configuration is set per-controller by authors, not configurable per-resource by users. No per-resource opt-out mechanism documented. ([ACK Drift Recovery docs](https://aws-controllers-k8s.github.io/community/docs/user-docs/drift-recovery/))
32+
33+
- **Azure Service Operator (ASO)**: Drift detection is **enabled by default** with a 1-hour resync period. Uses a PUT-on-every-reconcile approach rather than detect-then-correct. Provides **per-resource opt-out** via `reconcile-policy` annotation for adopted resources users don't want fully managed. **Global configuration** via `AZURE_SYNC_PERIOD` environment variable. Rate limiting via token-bucket algorithm and `MAX_CONCURRENT_RECONCILES` for parallelism control. ([ASO Controller Settings](https://azure.github.io/azure-service-operator/guide/aso-controller-settings-options/), [ASO Change Detection ADR](https://azure.github.io/azure-service-operator/design/adr-2022-11-change-detection/))
34+
35+
**Key design observations:**
36+
- Both projects enable drift detection by default
37+
- ASO provides more user-facing configuration options (global and per-resource)
38+
- Neither project documents behavior for externally-deleted resources
39+
40+
## Goals
41+
42+
- **Ensure state consistency**: Managed resources in OpenStack should match the desired state declared in Kubernetes
43+
- **Detect external modifications**: Identify when OpenStack resources are modified outside of ORC
44+
- **Automatic correction**: Restore drifted resources to their desired state without manual intervention
45+
- **Resource recreation**: Recreate managed resources that are deleted externally from OpenStack
46+
- **Configurable frequency**: Allow operators to tune the resync interval based on their requirements
47+
- **Hierarchical configuration**: Support configuration at ORC-wide, resource-type, and per-resource levels
48+
- **Minimal API impact**: Avoid excessive OpenStack API calls that could trigger rate limiting
49+
50+
## Non-Goals
51+
52+
- **Real-time drift detection**: Event-driven detection of changes (would require OpenStack webhooks or very short polling intervals)
53+
- **Drift reporting without correction**: Alerting on drift without taking corrective action. This applies to both mutable fields (which are corrected, not just reported) and immutable fields (which are ignored, not reported). May be considered as a future enhancement.
54+
- **Selective field reconciliation**: Allowing some fields to drift while correcting others
55+
- **Conflict resolution with merge semantics**: Merging external changes with desired state
56+
- **Drift correction for unmanaged resources**: Unmanaged resources are not modified by ORC; however, periodic resync will refresh their status to reflect the current OpenStack state
57+
58+
## Proposal
59+
60+
### Periodic Resync Mechanism
61+
62+
The drift detection mechanism works by periodically triggering reconciliation of resources. Unlike event-driven reconciles (triggered by Kubernetes spec/status changes), drift detection uses a time-based trigger to catch changes made directly in OpenStack. For managed resources, this includes drift correction; for unmanaged resources, this refreshes the status only.
63+
64+
1. **Trigger**: After a resource reaches a stable state (Progressing=False), ORC schedules a resync after `resyncPeriod` duration
65+
2. **Fetch**: On resync, ORC fetches the current state of the OpenStack resource
66+
3. **Compare**: The current state is compared against the desired state in the Kubernetes spec
67+
4. **Update**: If drift is detected, ORC updates the OpenStack resource to match the desired state
68+
5. **Reschedule**: After successful reconciliation, the next resync is scheduled
69+
70+
#### Implementation Details
71+
72+
At the end of a successful reconciliation (when no other reschedule is pending), the controller schedules the next resync:
73+
74+
```go
75+
// If periodic resync is enabled and we're not already rescheduling for
76+
// another reason, schedule the next resync to detect drift.
77+
if resyncPeriod > 0 {
78+
needsReschedule, _ := reconcileStatus.NeedsReschedule()
79+
if !needsReschedule {
80+
reconcileStatus = reconcileStatus.WithRequeue(resyncPeriod)
81+
}
82+
}
83+
```
84+
85+
This ensures the controller automatically triggers reconciliation after the configured period. Controller-runtime's work queue handles deduplication of reconcile requests, so no additional time-based checks are needed in `shouldReconcile`.
86+
87+
**Resources in terminal error are not resynced**: When a resource is in a terminal error state (e.g., invalid configuration, unrecoverable OpenStack error), periodic resync is not scheduled. Terminal errors indicate issues that cannot be resolved through automatic retry and require manual intervention to fix the underlying problem. This prevents wasted reconciliation cycles on resources that are known to be in an unrecoverable state.
88+
89+
### API Changes
90+
91+
A `resyncPeriod` field is added at the spec level, making it available to both managed and unmanaged resources:
92+
93+
```yaml
94+
apiVersion: openstack.k-orc.cloud/v1alpha1
95+
kind: Network
96+
metadata:
97+
name: critical-network
98+
spec:
99+
cloudCredentialsRef:
100+
secretName: openstack-clouds
101+
cloudName: openstack
102+
managementPolicy: managed
103+
resyncPeriod: 1h # Periodic resync every hour
104+
resource:
105+
description: Critical application network
106+
```
107+
108+
**Default**: Disabled (`0`). Set a positive duration like `10h` to enable.
109+
110+
### Behavior by Management Policy
111+
112+
The periodic resync behavior differs based on `managementPolicy`:
113+
114+
| Policy | On Resync |
115+
|--------|-----------|
116+
| `managed` | Fetch from OpenStack → correct drift → update status |
117+
| `unmanaged` | Fetch from OpenStack → update status only (no writes to OpenStack) |
118+
119+
This allows unmanaged/imported resources to keep their `status.resource` in sync with the actual OpenStack state without ORC modifying the resource.
120+
121+
### Configuration Hierarchy
122+
123+
Drift detection supports a two-level configuration hierarchy:
124+
125+
| Level | Scope | Configuration Location | Precedence |
126+
|-------|-------|----------------------|------------|
127+
| ORC-wide | All resources across all types | CLI flag | Lowest |
128+
| Per-resource | Individual resource instance | `spec.resyncPeriod` on the CR | Highest |
129+
130+
**Resolution order**: Per-resource → ORC-wide → Built-in default (disabled)
131+
132+
#### ORC-wide Configuration Options
133+
134+
A CLI flag sets the global default:
135+
136+
```
137+
--default-resync-period=10h
138+
```
139+
140+
For per-resource-type configuration, platform teams can use [kro (Kube Resource Orchestrator)](https://kro.run/) to wrap ORC resources with organizational defaults without changes to ORC itself.
141+
142+
### Resource Recreation on External Deletion
143+
144+
When a resource with `managementPolicy=managed` is deleted from OpenStack but the ORC object still exists:
145+
146+
1. On the next reconciliation, ORC attempts to fetch the resource by the ID stored in `status.id`
147+
2. If not found and the resource was originally created by ORC (not imported), ORC recreates it
148+
3. The new resource ID is stored in `status.id`
149+
150+
**Behavior when drift detection is disabled** (`resyncPeriod: 0`): The `shouldReconcile` function returns `false` when `Progressing=False` and generation is current, so no periodic resyncs occur. If the OpenStack resource is deleted externally and a reconciliation is triggered by other means (e.g., spec change, controller restart), it remains a terminal error as it is today. Resource recreation only occurs when drift detection is enabled and a periodic resync discovers the missing resource. No new conditionals are needed in the reconcile loop—the existing `shouldReconcile` check handles both cases.
151+
152+
For **imported resources** that are deleted externally, this is always a terminal error regardless of drift detection settings, because the resource was not created by ORC and recreating it would not restore the original resource.
153+
154+
**Note on dependent resources**: OpenStack enforces referential integrity for most resources (e.g., Networks cannot be deleted while Subnets exist). If resources are deleted through means that bypass these checks (direct database manipulation, OpenStack bugs), drift detection preserves ORC's existing reconciliation behavior:
155+
156+
- **Parent resource (e.g., Network)**: On next reconciliation, `GetOSResourceByID` returns 404 → terminal error ("resource has been deleted from OpenStack").
157+
- **Dependent resource update path (e.g., Subnet update)**: The controller doesn't check if its parent dependency is in terminal error. It fetches the resource by `status.id`, and if successful, proceeds with the update. The result depends on what OpenStack returns for that specific operation and would preserve the existing error handling behavior.
158+
- **Dependent resource create/recreate path**: The controller checks `IsAvailable(parent)` before proceeding. If the parent is in terminal error, the dependent waits on the dependency (not terminal, just waiting).
159+
160+
These behaviors exist regardless of drift detection—drift detection only changes scheduling, not reconciliation logic. Resolving such inconsistencies requires manual intervention.
161+
162+
### Field Coverage
163+
164+
Drift detection covers all **mutable fields** that ORC actuators implement update operations for. Before this feature is considered stable, all actuator implementations must be audited to ensure they cover all mutable fields.
165+
166+
## Risks and Edge Cases
167+
168+
### Split-Brain Scenarios
169+
170+
**Risk**: Multiple controllers or systems may be managing the same OpenStack resources, leading to conflicts where changes are repeatedly overwritten.
171+
172+
**Mitigation**:
173+
- Document that ORC should be the sole manager of resources it creates
174+
- Report conflicts in resource conditions for observability
175+
176+
### API Rate Limiting
177+
178+
**Risk**: Frequent resync across many resources could trigger OpenStack API rate limiting.
179+
180+
**Mitigation**:
181+
- Disabled by default; when enabled, recommend conservative intervals (e.g., 10 hours)
182+
- Add random jitter to resync times to avoid thundering herd: since reconciliation already uses "requeue after X duration", jitter simply adds a random offset (e.g., ±10%) to the resync period, spreading resyncs over time rather than having them fire simultaneously
183+
- Allow operators to disable or lengthen resync for stable resources
184+
185+
### Controller Resource Consumption
186+
187+
**Risk**: Frequent reconciliation increases CPU and memory usage on the ORC controller.
188+
189+
**Mitigation**:
190+
- Disabled by default; when enabled, conservative intervals limit reconciliation frequency
191+
192+
### Conflicts with External Systems
193+
194+
**Risk**: If resources are intentionally managed by external systems (e.g., autoscalers, other controllers), drift correction can cause unexpected behavior.
195+
196+
**Mitigation**:
197+
- Allow `resyncPeriod: 0` to disable drift detection
198+
- Use `managementPolicy: unmanaged` for externally managed resources
199+
- Document the implications clearly in the user guide
200+
201+
### Upgrade/Downgrade Considerations
202+
203+
**Risk**: Users upgrading to a version with drift detection may experience unexpected reconciliations.
204+
205+
**Mitigation**: Drift detection is disabled by default (opt-in), so users upgrading will not experience any behavior change unless they explicitly enable it. Document the new feature in release notes.
206+
207+
## Alternatives Considered
208+
209+
### Event-Driven Drift Detection
210+
211+
Use OpenStack notifications (Oslo messaging) to detect changes in real-time.
212+
213+
**Rejected because**: Requires OpenStack notification infrastructure, complex to implement, not all deployments have notifications enabled.
214+
215+
### Drift Detection Without Correction
216+
217+
Detect and report drift without automatically correcting it.
218+
219+
**Out of scope for this enhancement**: While drift notification has value for observability, it is better addressed as a separate alerting effort. This enhancement focuses on drift correction; reporting-only mode could be added as a future management policy option.
220+
221+
### Watch-Based Detection
222+
223+
Implement a watcher that periodically lists all resources from OpenStack and compares.
224+
225+
**Rejected because**: List operations can be expensive, harder to implement with proper filtering, and per-resource reconciliation integrates naturally with controller-runtime.
226+
227+
## Implementation History
228+
229+
- 2026-02-03: Enhancement proposed

0 commit comments

Comments
 (0)