Skip to content

Commit

Permalink
update code to satisfy comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jaireddjawed committed Dec 19, 2024
1 parent 857159f commit dee2d4d
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions controllers/hcpvaultsecretsapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ func (r *HCPVaultSecretsAppReconciler) Reconcile(ctx context.Context, req ctrl.R
return ctrl.Result{}, r.handleDeletion(ctx, o)
}

if !orphanedShadowSecretCleanupInitialized {
go r.startShadowSecretCleanupRoutine(ctx)
// prevent the shadow secret cleanup goroutine from being started more than once
orphanedShadowSecretCleanupInitialized = true
}

var requeueAfter time.Duration
if o.Spec.RefreshAfter != "" {
d, err := parseDurationString(o.Spec.RefreshAfter, ".spec.refreshAfter", r.MinRefreshAfter)
Expand Down Expand Up @@ -305,23 +299,23 @@ func (r *HCPVaultSecretsAppReconciler) startShadowSecretCleanupRoutine(ctx conte
ticker := time.NewTicker(1 * time.Hour)

// this process is expected to run indefinitely
for {
select {
case <-ticker.C:
err := r.cleanupOrphanedShadowSecrets(ctx)
if err != nil {
logger.Error(err, "Failed to cleanup orphaned shadow secrets")
}
for range ticker.C {
err := r.cleanupOrphanedShadowSecrets(ctx)
if err != nil {
logger.Error(err, "Failed to cleanup orphaned shadow secrets")
}
}
}

func (r *HCPVaultSecretsAppReconciler) cleanupOrphanedShadowSecrets(ctx context.Context) error {
logger := log.FromContext(ctx)

// filtering only for dynamic secrets
dynamicSecretLabelSelector := client.MatchingLabels{"app.kubernetes.io/component": "hvs-dynamic-secret-cache"}

// get all secrets in the operator namespace
secrets := corev1.SecretList{}
if err := r.List(ctx, &secrets, client.InNamespace(common.OperatorNamespace)); err != nil {
if err := r.List(ctx, &secrets, client.InNamespace(common.OperatorNamespace), dynamicSecretLabelSelector); err != nil {
logger.Error(err, "Failed to list secrets")
}

Expand All @@ -333,13 +327,13 @@ func (r *HCPVaultSecretsAppReconciler) cleanupOrphanedShadowSecrets(ctx context.

// get the HCPVaultSecretsApp instance that that the shadow secret belongs to (if applicable)
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, o)
if err != nil {
if err != nil && !apierrors.IsNotFound(err) {
logger.Error(err, "Error getting resource from k8s", "secret", secret.Name)
continue
}

// delete the HCPVaultSecretsApp if it no longer exists
if o.GetDeletionTimestamp() != nil {
if apierrors.IsNotFound(err) || o.GetDeletionTimestamp() != nil {
if err := r.handleDeletion(ctx, o); err != nil {
return err
}
Expand Down Expand Up @@ -369,6 +363,12 @@ func (r *HCPVaultSecretsAppReconciler) SetupWithManager(mgr ctrl.Manager, opts c
r.BackOffRegistry = NewBackOffRegistry()
}

if !orphanedShadowSecretCleanupInitialized {
go r.startShadowSecretCleanupRoutine(context.Background())
// prevent the shadow secret cleanup goroutine from being started more than once
orphanedShadowSecretCleanupInitialized = true
}

return ctrl.NewControllerManagedBy(mgr).
For(&secretsv1beta1.HCPVaultSecretsApp{}).
WithEventFilter(syncableSecretPredicate(nil)).
Expand Down

0 comments on commit dee2d4d

Please sign in to comment.