Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Fix (Flux)HelmRelease cluster lookups
Browse files Browse the repository at this point in the history
The value obtained by `_, value := range slice` is overwritten on
every iteration. Thus, saving a pointer to `value` is dangerous
since its content will be overwritten in the next iteration.

From https://golang.org/ref/spec#For_range :

> For each entry it assigns iteration values to corresponding
> iteration variables if present and then executes the block.

But the iteration variables are the same on each iteration.

More explicitly, from [the gccgo source code](https://github.com/golang/gofrontend/blob/e387439bfd24d5e142874b8e68e7039f74c744d7/go/statements.cc#L5593-L5604):

```
  // The loop we generate:
  //   len_temp := len(range)
  //   range_temp := range
  //   for index_temp = 0; index_temp < len_temp; index_temp++ {
  //           value_temp = range_temp[index_temp]
  //           index = index_temp
  //           value = value_temp
  //           original body
  //   }
```
  • Loading branch information
2opremio committed May 7, 2019
1 parent 97022b6 commit 815b94b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cluster/kubernetes/resourcekinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ func (fhr *fluxHelmReleaseKind) getWorkloads(c *Cluster, namespace string) ([]wo
}

var workloads []workload
for _, f := range fluxHelmReleases.Items {
workloads = append(workloads, makeFluxHelmReleaseWorkload(&f))
for i, _ := range fluxHelmReleases.Items {
workloads = append(workloads, makeFluxHelmReleaseWorkload(&fluxHelmReleases.Items[i]))
}

return workloads, nil
Expand Down Expand Up @@ -459,8 +459,8 @@ func (hr *helmReleaseKind) getWorkloads(c *Cluster, namespace string) ([]workloa
}

var workloads []workload
for _, f := range helmReleases.Items {
workloads = append(workloads, makeHelmReleaseWorkload(&f))
for i, _ := range helmReleases.Items {
workloads = append(workloads, makeHelmReleaseWorkload(&helmReleases.Items[i]))
}

return workloads, nil
Expand Down

0 comments on commit 815b94b

Please sign in to comment.