github-vet / github-vet/rangeloop-pointer-findings
kubeedge/examples: kubeedge-counter-demo/vendor/github.com/kubeedge/kubeedge/edge/pkg/edged/containers/container_manager.go; 71 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [kubeedge/examples](https://www.github.com/kubeedge/examples) at [kubeedge-counter-demo/vendor/github.com/kubeedge/kubeedge/edge/pkg/edged/containers/container_manager.go](https://github.com/kubeedge/examples/blob/cc38a1a06c71de7390a77b445ae13e2f71dffa42/kubeedge-counter-demo/vendor/github.com/kubeedge/kubeedge/edge/pkg/edged/containers/container_manager.go#L312-L382)
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 which takes a reference to container at line 326 may start a goroutine
[Click here to see the code in its original context.](https://github.com/kubeedge/examples/blob/cc38a1a06c71de7390a77b445ae13e2f71dffa42/kubeedge-counter-demo/vendor/github.com/kubeedge/kubeedge/edge/pkg/edged/containers/container_manager.go#L312-L382)
Click here to show the 71 line(s) of Go which triggered the analyzer.
```go
for idx, container := range pod.Spec.Containers {
innerContainer, ok := cm.getContainerFromMap(pod.UID)
if !ok {
restartCount = 0
log.LOGGER.Infof("no container id for pod %s, need start container", pod.Name)
changes.ContainersToStart = append(changes.ContainersToStart, idx)
continue
}
status, err := cm.runtimeService.ContainerStatus(innerContainer.ID)
if err != nil {
log.LOGGER.Errorf("get container status for pod %s failed: %v", pod.Name, err)
}
if status == nil || status.State != cri.StatusRUNNING {
if shouldContainerBeRestarted(&container, pod, status) {
message := fmt.Sprintf("Container %+v is dead, but RestartPolicy says that we should restart it.", container)
log.LOGGER.Infof(message)
changes.ContainersToStart = append(changes.ContainersToStart, idx)
}
continue
}
labels, err := cm.getPodContainerLabels(pod)
if err != nil {
log.LOGGER.Errorf("get container labels for pod %s failed: %v", pod.Name, err)
continue
}
if labels[containerHashLabel] == "" {
log.LOGGER.Errorf("container hash is empty for pod %s failed: %v", pod.Name, err)
continue
}
hash, err := strconv.ParseUint(labels[containerHashLabel], 16, 64)
if err != nil {
log.LOGGER.Errorf("container hash is empty for pod %s failed: %v", pod.Name, err)
continue
}
// The container is running, but kill the container if any of the following condition is met.
reason := ""
restart := shouldRestartOnFailure(pod)
containerID := kubecontainer.BuildContainerID("docker", status.ID.ID)
expectedHash, actualHash, changed := containerChanged(&container, uint64(hash))
if changed {
reason = fmt.Sprintf("Container spec hash changed (%d vs %d).", actualHash, expectedHash)
// Restart regardless of the restart policy because the container
// spec changed.
restart = true
} else if liveness, found := cm.livenessManager.Get(containerID); found && liveness == proberesults.Failure {
reason = "Container failed liveness probe."
} else {
continue
}
message := reason
if restart {
message = fmt.Sprintf("%s. Container will be killed and recreated.", message)
changes.ContainersToStart = append(changes.ContainersToStart, idx)
}
// We need to kill the container, but if we also want to restart the
// container afterwards, make the intent clear in the message. Also do
// not kill the entire pod since we expect container to be running eventually.
changes.ContainersToKill[types.UID(status.ID.ID)] = containerToKillInfo{
name: container.Name,
container: &pod.Spec.Containers[idx],
message: message,
}
log.LOGGER.Infof("Container %q (%q) of pod %s: %s", container.Name, status.ID.ID, pod.Name, message)
}
```
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: cc38a1a06c71de7390a77b445ae13e2f71dffa42
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.