Skip to content

Commit

Permalink
fix bug (#4338)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-town authored Feb 28, 2024
1 parent fb55027 commit 26185fc
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions api/server/handlers/release/update_image_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,25 @@ func (c *UpdateImageBatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
return
}

printReleases := func(releases []*models.Release) string {
var names []string
for _, release := range releases {
names = append(names, fmt.Sprintf("%s-%s", release.Name, release.Namespace))
}
return strings.Join(names, ",")
}

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "releases", Value: printReleases(releases)})

var namespaceScopedReleases []*models.Release
for _, release := range releases {
if release.Namespace == helmAgent.Namespace() {
namespaceScopedReleases = append(namespaceScopedReleases, release)
}
}

telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "releases-in-namespace", Value: printReleases(namespaceScopedReleases)})

registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
if err != nil {
err = telemetry.Error(ctx, span, err, "error listing registries by project id")
Expand All @@ -89,15 +101,22 @@ func (c *UpdateImageBatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
mu := &sync.Mutex{}
errs := make([]string, 0)

for i := range namespaceScopedReleases {
for i, rel := range namespaceScopedReleases {
wg.Add(1)

go func(index int) {
go func(release *models.Release, i int) {
defer wg.Done()

ctx, span := telemetry.NewSpan(ctx, "update-image-batch")
defer span.End()
defer wg.Done()

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "release-name", Value: release.Name},
telemetry.AttributeKV{Key: "release-index", Value: i},
)

// read release via agent
rel, err := helmAgent.GetRelease(ctx, namespaceScopedReleases[index].Name, 0, false)
rel, err := helmAgent.GetRelease(ctx, release.Name, 0, false)
if err != nil {
err = telemetry.Error(ctx, span, err, "error getting release")
// if this is a release not found error, just return - the release has likely been deleted from the underlying
Expand All @@ -107,20 +126,20 @@ func (c *UpdateImageBatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
}

mu.Lock()
errs = append(errs, fmt.Sprintf("Error for %s, index %d: %s", namespaceScopedReleases[index].Name, index, err.Error()))
errs = append(errs, fmt.Sprintf("Error for %s, index %d: %s", release.Name, i, err.Error()))
mu.Unlock()
return
}

if rel.Chart.Name() == "job" {
image := map[string]interface{}{}
image["repository"] = namespaceScopedReleases[index].ImageRepoURI
image["repository"] = release.ImageRepoURI
image["tag"] = request.Tag
rel.Config["image"] = image
rel.Config["paused"] = true

conf := &helm.UpgradeReleaseConfig{
Name: namespaceScopedReleases[index].Name,
Name: release.Name,
Cluster: cluster,
Repo: c.Repo(),
Registries: registries,
Expand All @@ -137,11 +156,11 @@ func (c *UpdateImageBatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
}

mu.Lock()
errs = append(errs, fmt.Sprintf("Error for %s, index %d: %s", namespaceScopedReleases[index].Name, index, err.Error()))
errs = append(errs, fmt.Sprintf("Error for %s, index %d: %s", release.Name, i, err.Error()))
mu.Unlock()
}
}
}(i)
}(rel, i)
}

wg.Wait()
Expand Down

0 comments on commit 26185fc

Please sign in to comment.