fix data race introduced by 1798e0f

This commit is contained in:
Nan Deng 2014-07-02 18:06:54 -07:00
parent 0a97e514b3
commit aa808a6505

View File

@ -689,11 +689,13 @@ func (kl *Kubelet) syncManifest(manifest *api.ContainerManifest, keepChannel cha
return nil return nil
} }
type empty struct{}
// Sync the configured list of containers (desired state) with the host current state // Sync the configured list of containers (desired state) with the host current state
func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error { func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
glog.Infof("Desired: %+v", config) glog.Infof("Desired: %+v", config)
var err error var err error
dockerIdsToKeep := map[DockerID]bool{} dockerIdsToKeep := map[DockerID]empty{}
keepChannel := make(chan DockerID) keepChannel := make(chan DockerID)
waitGroup := sync.WaitGroup{} waitGroup := sync.WaitGroup{}
@ -711,15 +713,18 @@ func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
} }
}(ix) }(ix)
} }
ch := make(chan bool)
go func() { go func() {
for id := range keepChannel { for id := range keepChannel {
dockerIdsToKeep[id] = true dockerIdsToKeep[id] = empty{}
} }
ch <- true
}() }()
if len(config) > 0 { if len(config) > 0 {
waitGroup.Wait() waitGroup.Wait()
close(keepChannel)
} }
close(keepChannel)
<-ch
// Kill any containers we don't need // Kill any containers we don't need
existingContainers, err := kl.getDockerContainers() existingContainers, err := kl.getDockerContainers()
@ -728,7 +733,7 @@ func (kl *Kubelet) SyncManifests(config []api.ContainerManifest) error {
return err return err
} }
for id, container := range existingContainers { for id, container := range existingContainers {
if !dockerIdsToKeep[id] { if _, ok := dockerIdsToKeep[id]; !ok {
glog.Infof("Killing: %s", id) glog.Infof("Killing: %s", id)
err = kl.killContainer(container) err = kl.killContainer(container)
if err != nil { if err != nil {