Skip to content

Commit

Permalink
Remove the use of error channel from go routine
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Jan 17, 2023
1 parent 40696e4 commit 74c0dcd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
26 changes: 5 additions & 21 deletions k8-operator/controllers/auto_redeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,24 @@ func (r *InfisicalSecretReconciler) ReconcileDeploymentsWithManagedSecrets(ctx c
return 0, fmt.Errorf("unable to fetch Kubernetes secret to update deployment: %v", err)
}

// Create a channel to receive errors from goroutines
errChan := make(chan error, len(listOfDeployments.Items))

wg := sync.WaitGroup{}
wg.Add(len(listOfDeployments.Items))
go func() {
wg.Wait()
close(errChan)
}()

var wg sync.WaitGroup
// Iterate over the deployments and check if they use the managed secret
for _, deployment := range listOfDeployments.Items {
if deployment.Annotations[AUTO_RELOAD_DEPLOYMENT_ANNOTATION] == "true" && r.IsDeploymentUsingManagedSecret(deployment, infisicalSecret) {
// Start a goroutine to reconcile the deployment
wg.Add(1)
go func(d v1.Deployment, s corev1.Secret) {
defer wg.Done()
if err := r.ReconcileDeployment(ctx, d, s); err != nil {
errChan <- err
fmt.Printf("unable to reconcile deployment with [name=%v]. Will try next requeue", deployment.ObjectMeta.Name)
}
}(deployment, *managedKubeSecret)
}
}

// Collect any errors that were sent through the channel
var errs []error
for err := range errChan {
errs = append(errs, err)
}

if len(errs) > 0 {
return 0, fmt.Errorf("unable to reconcile some deployments: %v", errs)
}
wg.Wait()

return len(listOfDeployments.Items), nil
return 0, nil
}

// Check if the deployment uses managed secrets
Expand Down
1 change: 1 addition & 0 deletions k8-operator/controllers/infisicalsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (r *InfisicalSecretReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// Sync again after the specified time
fmt.Printf("Operator will requeue after [%v] \n", requeueTime)
return ctrl.Result{
RequeueAfter: requeueTime,
}, nil
Expand Down

0 comments on commit 74c0dcd

Please sign in to comment.