// Pods method for runningK8sData
func (rkd *runningK8sData) Pods() ([]*models.K8sResource, error) {
pods, err := getPods(rkd.podLister)
if err != nil {
return nil, err
}
ret := make([]*models.K8sResource, len(pods))
for i, p := range pods {
pod := &models.K8sResource{
Data: &p,
}
ret[i] = pod
}
return ret, nil
}
The usage of &p in the for i, p :- range pods iteration is being shadowed over each loop iteration, resulting in a ret slice where all items are duplicates pointing to the final member of the pods slice.
There are several instances of this, and each should be remediated. @arschles figured out that by making a copy of the enumeration variable, and then assigning a pointer to it, we'll solve this.
E.g.,
The usage of
&p
in thefor i, p :- range pods
iteration is being shadowed over each loop iteration, resulting in aret
slice where all items are duplicates pointing to the final member of thepods
slice.There are several instances of this, and each should be remediated. @arschles figured out that by making a copy of the enumeration variable, and then assigning a pointer to it, we'll solve this.