2014-06-18 22:18:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 master
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2014-07-01 22:35:56 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-18 22:18:38 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/pod"
|
|
|
|
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-18 22:18:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PodCache contains both a cache of container information, as well as the mechanism for keeping
|
|
|
|
// that cache up to date.
|
|
|
|
type PodCache struct {
|
2014-07-01 22:35:56 +00:00
|
|
|
containerInfo client.PodInfoGetter
|
2014-08-11 07:34:59 +00:00
|
|
|
pods pod.Registry
|
2014-07-01 22:35:56 +00:00
|
|
|
// This is a map of pod id to a map of container name to the
|
|
|
|
podInfo map[string]api.PodInfo
|
|
|
|
podLock sync.Mutex
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 11:54:05 +00:00
|
|
|
// NewPodCache returns a new PodCache which watches container information registered in the given PodRegistry.
|
2014-08-20 00:33:54 +00:00
|
|
|
func NewPodCache(info client.PodInfoGetter, pods pod.Registry) *PodCache {
|
2014-06-18 22:18:38 +00:00
|
|
|
return &PodCache{
|
|
|
|
containerInfo: info,
|
|
|
|
pods: pods,
|
2014-07-01 22:35:56 +00:00
|
|
|
podInfo: map[string]api.PodInfo{},
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 21:39:49 +00:00
|
|
|
// makePodCacheKey constructs a key for use in a map to address a pod with specified namespace and id
|
|
|
|
func makePodCacheKey(podNamespace, podID string) string {
|
|
|
|
return podNamespace + "." + podID
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// GetPodInfo implements the PodInfoGetter.GetPodInfo.
|
2014-07-01 22:35:56 +00:00
|
|
|
// The returned value should be treated as read-only.
|
2014-08-21 21:30:49 +00:00
|
|
|
// TODO: Remove the host from this call, it's totally unnecessary.
|
2014-10-09 21:39:49 +00:00
|
|
|
func (p *PodCache) GetPodInfo(host, podNamespace, podID string) (api.PodInfo, error) {
|
2014-06-18 22:18:38 +00:00
|
|
|
p.podLock.Lock()
|
|
|
|
defer p.podLock.Unlock()
|
2014-10-09 21:39:49 +00:00
|
|
|
value, ok := p.podInfo[makePodCacheKey(podNamespace, podID)]
|
2014-06-18 22:18:38 +00:00
|
|
|
if !ok {
|
2014-07-18 14:54:43 +00:00
|
|
|
return nil, client.ErrPodInfoNotAvailable
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
2014-07-11 10:10:24 +00:00
|
|
|
return value, nil
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 21:39:49 +00:00
|
|
|
func (p *PodCache) updatePodInfo(host, podNamespace, podID string) error {
|
|
|
|
info, err := p.containerInfo.GetPodInfo(host, podNamespace, podID)
|
2014-06-18 22:18:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.podLock.Lock()
|
|
|
|
defer p.podLock.Unlock()
|
2014-10-09 21:39:49 +00:00
|
|
|
p.podInfo[makePodCacheKey(podNamespace, podID)] = info
|
2014-06-18 22:18:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-11 10:10:24 +00:00
|
|
|
// UpdateAllContainers updates information about all containers. Either called by Loop() below, or one-off.
|
2014-06-18 22:18:38 +00:00
|
|
|
func (p *PodCache) UpdateAllContainers() {
|
2014-10-09 21:39:49 +00:00
|
|
|
ctx := api.NewContext()
|
2014-09-25 18:34:01 +00:00
|
|
|
pods, err := p.pods.ListPods(ctx, labels.Everything())
|
2014-06-18 22:18:38 +00:00
|
|
|
if err != nil {
|
2014-07-22 04:26:03 +00:00
|
|
|
glog.Errorf("Error synchronizing container list: %v", err)
|
2014-06-18 22:18:38 +00:00
|
|
|
return
|
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
for _, pod := range pods.Items {
|
2014-11-13 15:52:13 +00:00
|
|
|
if pod.Status.Host == "" {
|
2014-09-29 04:29:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-11-13 15:52:13 +00:00
|
|
|
err := p.updatePodInfo(pod.Status.Host, pod.Namespace, pod.Name)
|
2014-07-18 14:54:43 +00:00
|
|
|
if err != nil && err != client.ErrPodInfoNotAvailable {
|
2014-07-22 04:26:03 +00:00
|
|
|
glog.Errorf("Error synchronizing container: %v", err)
|
2014-06-18 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|