Skip to content

Commit 59017cd

Browse files
crenshaw-devMangaal
authored andcommitted
feat(hydrator): parallelize repo-server calls (argoproj#24451) (argoproj#24436)
Signed-off-by: Michael Crenshaw <[email protected]> Signed-off-by: Mangaal <[email protected]>
1 parent c5b9d88 commit 59017cd

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

controller/hydrator/hydrator.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"sync"
78
"time"
89

10+
"golang.org/x/sync/errgroup"
11+
912
log "github.com/sirupsen/logrus"
1013
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1114
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -279,17 +282,32 @@ func (h *Hydrator) hydrate(logCtx *log.Entry, apps []*appv1.Application, project
279282
syncBranch := apps[0].Spec.SourceHydrator.SyncSource.TargetBranch
280283
targetBranch := apps[0].Spec.GetHydrateToSource().TargetRevision
281284

282-
var paths []*commitclient.PathDetails
283-
var targetRevision string
284-
var err error
285-
// TODO: parallelize this loop
286-
for _, app := range apps {
287-
var pathDetails *commitclient.PathDetails
288-
targetRevision, pathDetails, err = h.getManifests(context.Background(), app, targetRevision, projects[app.Spec.Project])
289-
if err != nil {
290-
return "", "", fmt.Errorf("failed to get manifests for app %q: %w", app.QualifiedName(), err)
291-
}
292-
paths = append(paths, pathDetails)
285+
// Get a static SHA revision from the first app so that all apps are hydrated from the same revision.
286+
targetRevision, pathDetails, err := h.getManifests(context.Background(), apps[0], "", projects[apps[0].Spec.Project])
287+
if err != nil {
288+
return "", "", fmt.Errorf("failed to get manifests for app %q: %w", apps[0].QualifiedName(), err)
289+
}
290+
paths := []*commitclient.PathDetails{pathDetails}
291+
292+
eg, ctx := errgroup.WithContext(context.Background())
293+
var mu sync.Mutex
294+
295+
for _, app := range apps[1:] {
296+
app := app
297+
eg.Go(func() error {
298+
_, pathDetails, err = h.getManifests(ctx, app, targetRevision, projects[app.Spec.Project])
299+
if err != nil {
300+
return fmt.Errorf("failed to get manifests for app %q: %w", app.QualifiedName(), err)
301+
}
302+
mu.Lock()
303+
paths = append(paths, pathDetails)
304+
mu.Unlock()
305+
return nil
306+
})
307+
}
308+
err = eg.Wait()
309+
if err != nil {
310+
return "", "", fmt.Errorf("failed to get manifests for apps: %w", err)
293311
}
294312

295313
// If all the apps are under the same project, use that project. Otherwise, use an empty string to indicate that we

0 commit comments

Comments
 (0)