Skip to content

Commit eda8a6f

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

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

enhancements/drift-detection.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
#### Integration with `shouldReconcile`
71+
72+
The generic controller's `shouldReconcile` function determines whether to proceed with reconciliation. Currently it skips reconciliation when `Progressing=False` and `observedGeneration` matches the current generation. For drift detection, `shouldReconcile` is extended to accept the `resyncPeriod` and check if enough time has passed since the last sync:
73+
74+
```go
75+
// When Progressing is False and generation is current:
76+
if resyncPeriod > 0 {
77+
return time.Since(progressing.LastTransitionTime.Time) >= resyncPeriod
78+
}
79+
```
80+
81+
This reuses the existing `Progressing.LastTransitionTime` to track when the resource was last reconciled, avoiding the need for additional status fields
82+
83+
### API Changes
84+
85+
A `resyncPeriod` field is added at the spec level, making it available to both managed and unmanaged resources:
86+
87+
```yaml
88+
apiVersion: openstack.k-orc.cloud/v1alpha1
89+
kind: Network
90+
metadata:
91+
name: critical-network
92+
spec:
93+
cloudCredentialsRef:
94+
secretName: openstack-clouds
95+
cloudName: openstack
96+
managementPolicy: managed
97+
resyncPeriod: 1h # Periodic resync every hour
98+
resource:
99+
description: Critical application network
100+
```
101+
102+
**Default**: Disabled (`0`). Set a positive duration like `10h` to enable.
103+
104+
### Behavior by Management Policy
105+
106+
The periodic resync behavior differs based on `managementPolicy`:
107+
108+
| Policy | On Resync |
109+
|--------|-----------|
110+
| `managed` | Fetch from OpenStack → correct drift → update status |
111+
| `unmanaged` | Fetch from OpenStack → update status only (no writes to OpenStack) |
112+
113+
This allows unmanaged/imported resources to keep their `status.resource` in sync with the actual OpenStack state without ORC modifying the resource.
114+
115+
### Configuration Hierarchy
116+
117+
Drift detection supports a two-level configuration hierarchy:
118+
119+
| Level | Scope | Configuration Location | Precedence |
120+
|-------|-------|----------------------|------------|
121+
| ORC-wide | All resources across all types | CLI flag | Lowest |
122+
| Per-resource | Individual resource instance | `spec.resyncPeriod` on the CR | Highest |
123+
124+
**Resolution order**: Per-resource → ORC-wide → Built-in default (disabled)
125+
126+
#### ORC-wide Configuration Options
127+
128+
A CLI flag sets the global default:
129+
130+
```
131+
--default-resync-period=10h
132+
```
133+
134+
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.
135+
136+
### Resource Recreation on External Deletion
137+
138+
When a resource with `managementPolicy=managed` is deleted from OpenStack but the ORC object still exists:
139+
140+
1. On the next reconciliation, ORC attempts to fetch the resource by the ID stored in `status.id`
141+
2. If not found and the resource was originally created by ORC (not imported), ORC recreates it
142+
3. The new resource ID is stored in `status.id`
143+
144+
**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.
145+
146+
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.
147+
148+
**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:
149+
150+
- **Parent resource (e.g., Network)**: On next reconciliation, `GetOSResourceByID` returns 404 → terminal error ("resource has been deleted from OpenStack").
151+
- **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.
152+
- **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).
153+
154+
These behaviors exist regardless of drift detection—drift detection only changes scheduling, not reconciliation logic. Resolving such inconsistencies requires manual intervention.
155+
156+
### Field Coverage
157+
158+
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.
159+
160+
## Risks and Edge Cases
161+
162+
### Split-Brain Scenarios
163+
164+
**Risk**: Multiple controllers or systems may be managing the same OpenStack resources, leading to conflicts where changes are repeatedly overwritten.
165+
166+
**Mitigation**:
167+
- Document that ORC should be the sole manager of resources it creates
168+
- Report conflicts in resource conditions for observability
169+
170+
### API Rate Limiting
171+
172+
**Risk**: Frequent resync across many resources could trigger OpenStack API rate limiting.
173+
174+
**Mitigation**:
175+
- Disabled by default; when enabled, recommend conservative intervals (e.g., 10 hours)
176+
- 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
177+
- Allow operators to disable or lengthen resync for stable resources
178+
179+
### Controller Resource Consumption
180+
181+
**Risk**: Frequent reconciliation increases CPU and memory usage on the ORC controller.
182+
183+
**Mitigation**:
184+
- Disabled by default; when enabled, conservative intervals limit reconciliation frequency
185+
186+
### Conflicts with External Systems
187+
188+
**Risk**: If resources are intentionally managed by external systems (e.g., autoscalers, other controllers), drift correction can cause unexpected behavior.
189+
190+
**Mitigation**:
191+
- Allow `resyncPeriod: 0` to disable drift detection
192+
- Use `managementPolicy: unmanaged` for externally managed resources
193+
- Document the implications clearly in the user guide
194+
195+
### Upgrade/Downgrade Considerations
196+
197+
**Risk**: Users upgrading to a version with drift detection may experience unexpected reconciliations.
198+
199+
**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.
200+
201+
## Alternatives Considered
202+
203+
### Event-Driven Drift Detection
204+
205+
Use OpenStack notifications (Oslo messaging) to detect changes in real-time.
206+
207+
**Rejected because**: Requires OpenStack notification infrastructure, complex to implement, not all deployments have notifications enabled.
208+
209+
### Drift Detection Without Correction
210+
211+
Detect and report drift without automatically correcting it.
212+
213+
**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.
214+
215+
### Watch-Based Detection
216+
217+
Implement a watcher that periodically lists all resources from OpenStack and compares.
218+
219+
**Rejected because**: List operations can be expensive, harder to implement with proper filtering, and per-resource reconciliation integrates naturally with controller-runtime.
220+
221+
## Implementation History
222+
223+
- 2026-02-03: Enhancement proposed

0 commit comments

Comments
 (0)