Skip to content

Commit

Permalink
VSS: Ensure all resource updates are synced.
Browse files Browse the repository at this point in the history
Previously, whenever HMACSecretData was set to true and no data drift
was detected, the reconciler would skip destination updates, like
annotations, labels etc. This change adds tracking of a resource's
generation to determine when the reconciler is handling a resource
update.
  • Loading branch information
benashz committed Dec 4, 2023
1 parent f68311c commit a76b030
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/v1beta1/vaultstaticsecret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type VaultStaticSecretSpec struct {

// VaultStaticSecretStatus defines the observed state of VaultStaticSecret
type VaultStaticSecretStatus struct {
// LastGeneration is the Generation of the last reconciled resource.
LastGeneration int64 `json:"lastGeneration"`
// SecretMAC used when deciding whether new Vault secret data should be synced.
//
// The controller will compare the "new" Vault secret data to this value using HMAC,
Expand Down
7 changes: 7 additions & 0 deletions chart/crds/secrets.hashicorp.com_vaultstaticsecrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ spec:
status:
description: VaultStaticSecretStatus defines the observed state of VaultStaticSecret
properties:
lastGeneration:
description: LastGeneration is the Generation of the last reconciled
resource.
format: int64
type: integer
secretMAC:
description: "SecretMAC used when deciding whether new Vault secret
data should be synced. \n The controller will compare the \"new\"
Expand All @@ -159,6 +164,8 @@ spec:
is also used to detect drift in the Destination Secret's Data. If
drift is detected the data will be synced to the Destination."
type: string
required:
- lastGeneration
type: object
type: object
served: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ spec:
status:
description: VaultStaticSecretStatus defines the observed state of VaultStaticSecret
properties:
lastGeneration:
description: LastGeneration is the Generation of the last reconciled
resource.
format: int64
type: integer
secretMAC:
description: "SecretMAC used when deciding whether new Vault secret
data should be synced. \n The controller will compare the \"new\"
Expand All @@ -159,6 +164,8 @@ spec:
is also used to detect drift in the Destination Secret's Data. If
drift is detected the data will be synced to the Destination."
type: string
required:
- lastGeneration
type: object
type: object
served: true
Expand Down
11 changes: 8 additions & 3 deletions controllers/vaultstaticsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
}

var doRolloutRestart bool
syncSecret := true
doSync := true
if o.Spec.HMACSecretData {
// we want to ensure that requeueAfter is set so that we can perform the proper drift detection during each reconciliation.
// setting up a watcher on the Secret is also possibility, but polling seems to be the simplest approach for now.
Expand All @@ -117,7 +117,11 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
return ctrl.Result{}, err
}

syncSecret = !macsEqual
// skip the next sync if the data has not changed since the last sync, and the
// resource has not been updated.
if o.Status.LastGeneration == o.GetGeneration() {
doSync = !macsEqual
}

o.Status.SecretMAC = base64.StdEncoding.EncodeToString(messageMAC)
} else if len(o.Spec.RolloutRestartTargets) > 0 {
Expand All @@ -126,7 +130,7 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
"targets", o.Spec.RolloutRestartTargets)
}

if syncSecret {
if doSync {
if err := helpers.SyncSecret(ctx, r.Client, o, data); err != nil {
r.Recorder.Eventf(o, corev1.EventTypeWarning, consts.ReasonSecretSyncError,
"Failed to update k8s secret: %s", err)
Expand All @@ -144,6 +148,7 @@ func (r *VaultStaticSecretReconciler) Reconcile(ctx context.Context, req ctrl.Re
r.Recorder.Event(o, corev1.EventTypeNormal, consts.ReasonSecretSync, "Secret sync not required")
}

o.Status.LastGeneration = o.GetGeneration()
if err := r.Status().Update(ctx, o); err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit a76b030

Please sign in to comment.