mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 18:24:07 +00:00
Fix race between syncPod and syncPods
This commit is contained in:
parent
099934d49f
commit
0a202354b9
@ -417,7 +417,7 @@ func (kl *Kubelet) killContainersInPod(pod *api.BoundPod, dockerContainers docke
|
|||||||
errs := make(chan error, len(pod.Spec.Containers))
|
errs := make(chan error, len(pod.Spec.Containers))
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
for _, container := range pod.Spec.Containers {
|
for _, container := range pod.Spec.Containers {
|
||||||
// TODO: Consider being more aggressive: kill all container with this pod UID, period.
|
// TODO: Consider being more aggressive: kill all containers with this pod UID, period.
|
||||||
if dockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, pod.UID, container.Name); found {
|
if dockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, pod.UID, container.Name); found {
|
||||||
count++
|
count++
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -456,7 +456,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke
|
|||||||
if netDockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uuid, networkContainerName); found {
|
if netDockerContainer, found, _ := dockerContainers.FindPodContainer(podFullName, uuid, networkContainerName); found {
|
||||||
netID = dockertools.DockerID(netDockerContainer.ID)
|
netID = dockertools.DockerID(netDockerContainer.ID)
|
||||||
} else {
|
} else {
|
||||||
glog.V(3).Infof("Network container doesn't exist for pod %q, creating", podFullName)
|
glog.V(3).Infof("Network container doesn't exist for pod %q, re-creating the pod", podFullName)
|
||||||
count, err := kl.killContainersInPod(pod, dockerContainers)
|
count, err := kl.killContainersInPod(pod, dockerContainers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -578,6 +578,7 @@ func (kl *Kubelet) syncPod(pod *api.BoundPod, dockerContainers dockertools.Docke
|
|||||||
_, keep := containersToKeep[id]
|
_, keep := containersToKeep[id]
|
||||||
_, killed := killedContainers[id]
|
_, killed := killedContainers[id]
|
||||||
if !keep && !killed {
|
if !keep && !killed {
|
||||||
|
glog.V(1).Infof("Killing unwanted container in pod %q: %+v", curUUID, container)
|
||||||
err = kl.killContainer(container)
|
err = kl.killContainer(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Error killing container: %v", err)
|
glog.Errorf("Error killing container: %v", err)
|
||||||
@ -633,6 +634,7 @@ func (kl *Kubelet) SyncPods(pods []api.BoundPod) error {
|
|||||||
glog.V(4).Infof("Desired: %#v", pods)
|
glog.V(4).Infof("Desired: %#v", pods)
|
||||||
var err error
|
var err error
|
||||||
desiredContainers := make(map[podContainer]empty)
|
desiredContainers := make(map[podContainer]empty)
|
||||||
|
desiredPods := make(map[string]empty)
|
||||||
|
|
||||||
dockerContainers, err := dockertools.GetKubeletDockerContainers(kl.dockerClient, false)
|
dockerContainers, err := dockertools.GetKubeletDockerContainers(kl.dockerClient, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -645,6 +647,7 @@ func (kl *Kubelet) SyncPods(pods []api.BoundPod) error {
|
|||||||
pod := &pods[ix]
|
pod := &pods[ix]
|
||||||
podFullName := GetPodFullName(pod)
|
podFullName := GetPodFullName(pod)
|
||||||
uuid := pod.UID
|
uuid := pod.UID
|
||||||
|
desiredPods[uuid] = empty{}
|
||||||
|
|
||||||
// Add all containers (including net) to the map.
|
// Add all containers (including net) to the map.
|
||||||
desiredContainers[podContainer{podFullName, uuid, networkContainerName}] = empty{}
|
desiredContainers[podContainer{podFullName, uuid, networkContainerName}] = empty{}
|
||||||
@ -665,6 +668,10 @@ func (kl *Kubelet) SyncPods(pods []api.BoundPod) error {
|
|||||||
for _, container := range dockerContainers {
|
for _, container := range dockerContainers {
|
||||||
// Don't kill containers that are in the desired pods.
|
// Don't kill containers that are in the desired pods.
|
||||||
podFullName, uuid, containerName, _ := dockertools.ParseDockerName(container.Names[0])
|
podFullName, uuid, containerName, _ := dockertools.ParseDockerName(container.Names[0])
|
||||||
|
if _, found := desiredPods[uuid]; found {
|
||||||
|
// syncPod() will handle this one.
|
||||||
|
continue
|
||||||
|
}
|
||||||
pc := podContainer{podFullName, uuid, containerName}
|
pc := podContainer{podFullName, uuid, containerName}
|
||||||
if _, ok := desiredContainers[pc]; !ok {
|
if _, ok := desiredContainers[pc]; !ok {
|
||||||
glog.V(1).Infof("Killing unwanted container %+v", pc)
|
glog.V(1).Infof("Killing unwanted container %+v", pc)
|
||||||
|
Loading…
Reference in New Issue
Block a user