Files
kata-containers/virtcontainers/podlist.go
Peng Tao 1d291fc105 pod: add a in-memory pod list
It tracks all existing pods in the current runtime. If the runtime
calls multiple APIs, it can reuse existing pod data structure instead
of re-construct it in every API call.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
2018-04-10 13:17:19 +08:00

61 lines
1.3 KiB
Go

//
// Copyright (c) 2018 HyperHQ Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package virtcontainers
import (
"fmt"
"sync"
)
type podList struct {
lock sync.RWMutex
pods map[string]*Pod
}
// globalPodList tracks pods globally
var globalPodList = &podList{pods: make(map[string]*Pod)}
func (p *podList) addPod(pod *Pod) (err error) {
if pod == nil {
return nil
}
p.lock.Lock()
defer p.lock.Unlock()
if p.pods[pod.id] == nil {
p.pods[pod.id] = pod
} else {
err = fmt.Errorf("pod %s exists", pod.id)
}
return err
}
func (p *podList) removePod(id string) {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.pods, id)
}
func (p *podList) lookupPod(id string) (*Pod, error) {
p.lock.RLock()
defer p.lock.RUnlock()
if p.pods[id] != nil {
return p.pods[id], nil
}
return nil, fmt.Errorf("pod %s does not exist", id)
}