|
| 1 | +# Credential rotation consumer finalizer guard |
| 2 | + |
| 3 | +OpenStack service operators that consume rotating credential secrets |
| 4 | +(transport URL, application credentials, or similar) attach a consumer |
| 5 | +finalizer to the old secret so it is not deleted until every sub-service |
| 6 | +has rolled out with the new credential. |
| 7 | + |
| 8 | +## Shared helpers |
| 9 | + |
| 10 | +Use helpers from these packages instead of duplicating guard logic in each |
| 11 | +operator: |
| 12 | + |
| 13 | +- `object.ManageSecretConsumerFinalizer` — add finalizer to current secret |
| 14 | +- `object.RemoveSecretConsumerFinalizer` — remove finalizer from old secret |
| 15 | +- `object.FinalizeSecretRotation` — rotation guard: hold old finalizer |
| 16 | + until all sub-services are ready, then release |
| 17 | +- `condition.CredentialRotationGuardReady` — compute `guardReady` for the |
| 18 | + rotation guard |
| 19 | +- `helper.EnsureFresh` — bypass informer cache during rotation so the |
| 20 | + parent reads accurate sub-CR conditions from the API server |
| 21 | +- `helper.SetAPIReader` — configure the cache-bypassing reader |
| 22 | +- `statefulset.IsReady` / `deployment.IsReady` — check UpdatedReplicas, |
| 23 | + CurrentRevision, and ObservedGeneration so DeploymentReady is only |
| 24 | + True when all pods have rolled to the new config |
| 25 | + |
| 26 | +## Parent controller integration |
| 27 | + |
| 28 | +### 1. Wire up APIReader |
| 29 | + |
| 30 | +Add `APIReader client.Reader` to the reconciler struct and pass |
| 31 | +`mgr.GetAPIReader()` from `cmd/main.go`. In `Reconcile`, call |
| 32 | +`helper.SetAPIReader(r.APIReader)` after creating the helper. |
| 33 | + |
| 34 | +### 2. Pass transport URL directly — never through Status |
| 35 | + |
| 36 | +Pass `transportURL.Status.SecretName` as a parameter to sub-CR creation |
| 37 | +functions and config generation. Never read from |
| 38 | +`instance.Status.TransportURLSecret` when building sub-CR specs — the |
| 39 | +status field is only used as the "old" value for `FinalizeSecretRotation`. |
| 40 | + |
| 41 | +Set `instance.Status.TransportURLSecret` early only for first-time setup: |
| 42 | + |
| 43 | +```go |
| 44 | +if instance.Status.TransportURLSecret == "" || |
| 45 | + instance.Status.TransportURLSecret == transportURL.Status.SecretName { |
| 46 | + instance.Status.TransportURLSecret = transportURL.Status.SecretName |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +During rotation (old != current), the status is updated solely by |
| 51 | +`FinalizeSecretRotation` at the end of reconcile. |
| 52 | + |
| 53 | +### 3. Track stability and call EnsureFresh |
| 54 | + |
| 55 | +Use a simple boolean instead of StabilityTracker: |
| 56 | + |
| 57 | +```go |
| 58 | +allSubCRsStable := true |
| 59 | +rotationInProgress := instance.Status.TransportURLSecret != "" && |
| 60 | + instance.Status.TransportURLSecret != transportURL.Status.SecretName |
| 61 | + |
| 62 | +// After each sub-CR CreateOrPatch: |
| 63 | +subCR, op, err := r.subCRCreateOrUpdate(ctx, instance, transportURL.Status.SecretName) |
| 64 | +if err != nil { return ctrl.Result{}, err } |
| 65 | +if err := helper.EnsureFresh(ctx, op, subCR, rotationInProgress); err != nil { |
| 66 | + return ctrl.Result{}, err |
| 67 | +} |
| 68 | +if op != controllerutil.OperationResultNone { |
| 69 | + allSubCRsStable = false |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +`EnsureFresh` re-reads the sub-CR directly from the API server when |
| 74 | +`op == None` and rotation is in progress. Without this, the informer |
| 75 | +cache may return stale data where `Generation == ObservedGeneration` |
| 76 | +from before the spec change, causing the guard to pass prematurely. |
| 77 | + |
| 78 | +### 4. Transport URL annotation (operators without TransportURLSecret in sub-CR spec) |
| 79 | + |
| 80 | +For operators whose sub-CR specs do not include a `TransportURLSecret` |
| 81 | +field (e.g. nova, watcher, glance, designate, octavia, telemetry), add |
| 82 | +an annotation inside the `CreateOrPatch` mutate function to force a |
| 83 | +spec change when the transport URL rotates: |
| 84 | + |
| 85 | +```go |
| 86 | +op, err := controllerutil.CreateOrPatch(ctx, r.Client, subCR, func() error { |
| 87 | + subCR.Spec = spec |
| 88 | + if subCR.Annotations == nil { |
| 89 | + subCR.Annotations = map[string]string{} |
| 90 | + } |
| 91 | + subCR.Annotations["openstack.org/transport-url-secret"] = transportURLSecretName |
| 92 | + return controllerutil.SetControllerReference(instance, subCR, r.Scheme) |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +This is not needed for operators that pass `TransportURLSecret` directly |
| 97 | +in the sub-CR spec (cinder, manila, ironic, barbican, heat) — the spec |
| 98 | +field change already makes `CreateOrPatch` return `"updated"`. |
| 99 | + |
| 100 | +### 5. Compute the rotation guard and finalize |
| 101 | + |
| 102 | +```go |
| 103 | +guardReady := condition.CredentialRotationGuardReady( |
| 104 | + allSubCRsStable, |
| 105 | + &instance.Status.Conditions, |
| 106 | +) |
| 107 | + |
| 108 | +instance.Status.TransportURLSecret, err = object.FinalizeSecretRotation( |
| 109 | + ctx, helper, instance.Namespace, |
| 110 | + instance.Status.TransportURLSecret, |
| 111 | + transportURL.Status.SecretName, |
| 112 | + myTransportConsumerFinalizer, |
| 113 | + guardReady, |
| 114 | +) |
| 115 | +``` |
| 116 | + |
| 117 | +The same pattern works for application credential secrets: |
| 118 | + |
| 119 | +```go |
| 120 | +instance.Status.ApplicationCredentialSecret, err = object.FinalizeSecretRotation( |
| 121 | + ctx, helper, instance.Namespace, |
| 122 | + instance.Status.ApplicationCredentialSecret, |
| 123 | + instance.Spec.Auth.ApplicationCredentialSecret, |
| 124 | + myACConsumerFinalizer, |
| 125 | + guardReady, |
| 126 | +) |
| 127 | +``` |
| 128 | + |
| 129 | +## Sub-CR integration |
| 130 | + |
| 131 | +Sub-CR controllers should use `statefulset.IsReady()` or |
| 132 | +`deployment.IsReady()` for the `DeploymentReadyCondition` check: |
| 133 | + |
| 134 | +```go |
| 135 | +if statefulset.IsReady(ssData) { |
| 136 | + instance.Status.Conditions.MarkTrue( |
| 137 | + condition.DeploymentReadyCondition, |
| 138 | + condition.DeploymentReadyMessage) |
| 139 | +} else if *instance.Spec.Replicas > 0 { |
| 140 | + instance.Status.Conditions.Set(condition.FalseCondition( |
| 141 | + condition.DeploymentReadyCondition, |
| 142 | + condition.RequestedReason, |
| 143 | + condition.SeverityInfo, |
| 144 | + condition.DeploymentReadyRunningMessage)) |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +These helpers check `*Replicas == ReadyReplicas`, `*Replicas == UpdatedReplicas`, |
| 149 | +`Generation == ObservedGeneration`, and `CurrentRevision == UpdateRevision`, |
| 150 | +ensuring DeploymentReady is only True when all pods have rolled to the |
| 151 | +new config. |
0 commit comments