github-vet / github-vet/rangeloop-pointer-findings

improbable-eng/etcd-cluster-operator: controllers/etcdrestore_controller.go; 55 LoC

Open
#6,485 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [improbable-eng/etcd-cluster-operator](https://www.github.com/improbable-eng/etcd-cluster-operator) at [controllers/etcdrestore_controller.go](https://github.com/improbable-eng/etcd-cluster-operator/blob/f84abc6561735814debd67d45bb62d2d2ed8cf4a/controllers/etcdrestore_controller.go#L148-L202)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> function call at line 154 may store a reference to expectedPVC

[Click here to see the code in its original context.](https://github.com/improbable-eng/etcd-cluster-operator/blob/f84abc6561735814debd67d45bb62d2d2ed8cf4a/controllers/etcdrestore_controller.go#L148-L202)

Click here to show the 55 line(s) of Go which triggered the analyzer.

```go
for _, expectedPVC := range expectedPVCs {
var pvc corev1.PersistentVolumeClaim
err := r.Get(ctx, name(&expectedPVC), &pvc)

if apierrors.IsNotFound(err) {
// No PVC. Make it!
log.Info("Creating PVC", "pvc-name", name(&expectedPVC))
err := r.Create(ctx, &expectedPVC)
if err != nil {
return ctrl.Result{}, err
}
r.Recorder.Eventf(&restore,
reconcilerevent.K8sEventTypeNormal,
pvcCreatedEventReason,
"Created PVC '%s/%s'",
pvc.Namespace,
pvc.Name)
return ctrl.Result{}, nil
} else if err != nil {
// There was some other, non-notfound error. Exit as we can't handle this case.
log.Info("Encountered error while finding PVC")
return ctrl.Result{}, err
}
log.Info("PVC already exists. continuing", "pvc-name", name(&pvc))
// The PVC is there already. Continue.

// Check to make sure we're expecting to restore into this PVC
if !IsOurPVC(restore, pvc) {
// It's unsafe to take any further action. We don't control the PVC.
log.Info("PVC is not marked as our PVC. Failing", "pvc-name", name(&pvc))
// Construct error reason string
var reason string
if restoredFrom, found := pvc.Labels[restoredFromLabel]; found {
reason = fmt.Sprintf("Label %s indicates a different restore %s made this PVC.",
restoredFromLabel,
restoredFrom)
} else {
reason = fmt.Sprintf("Label %s not set, indicating this is a pre-existing PVC.",
restoredFromLabel)
}
r.Recorder.Eventf(&restore,
reconcilerevent.K8sEventTypeWarning,
pvcInvalidEventReason,
"Found a PVC '%s/%s' as expected, but it wasn't labeled as ours: %s",
pvc.Namespace,
pvc.Name,
reason)
restore.Status.Phase = etcdv1alpha1.EtcdRestorePhaseFailed
err := r.Client.Status().Update(ctx, &restore)
return ctrl.Result{}, err
}
log.Info("PVC correctly marked as ours", "pvc-name", name(&pvc))
// Great, this PVC is marked as *our* restore (and not any random PVC, or one for an existing cluster, or
// a conflicting restore).
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: f84abc6561735814debd67d45bb62d2d2ed8cf4a

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.