2014-08-11 07:34:59 +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 registrytest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-03-06 23:29:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
2014-08-11 07:34:59 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-08-11 19:58:19 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2014-08-11 07:34:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PodRegistry struct {
|
2014-08-19 23:41:51 +00:00
|
|
|
Err error
|
|
|
|
Pod *api.Pod
|
2014-08-29 00:48:07 +00:00
|
|
|
Pods *api.PodList
|
2014-08-11 07:34:59 +00:00
|
|
|
sync.Mutex
|
2014-08-14 21:14:31 +00:00
|
|
|
|
2014-12-04 08:30:51 +00:00
|
|
|
broadcaster *watch.Broadcaster
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 00:48:07 +00:00
|
|
|
func NewPodRegistry(pods *api.PodList) *PodRegistry {
|
2014-08-11 07:34:59 +00:00
|
|
|
return &PodRegistry{
|
2014-12-04 08:30:51 +00:00
|
|
|
Pods: pods,
|
2015-01-13 01:40:17 +00:00
|
|
|
broadcaster: watch.NewBroadcaster(0, watch.WaitIfChannelFull),
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 04:11:29 +00:00
|
|
|
func (r *PodRegistry) SetError(err error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
r.Err = err
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
func (r *PodRegistry) ListPodsPredicate(ctx api.Context, filter func(*api.Pod) bool) (*api.PodList, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
if r.Err != nil {
|
2014-08-29 00:48:07 +00:00
|
|
|
return nil, r.Err
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
var filtered []api.Pod
|
2014-08-29 00:48:07 +00:00
|
|
|
for _, pod := range r.Pods.Items {
|
2014-09-16 23:15:40 +00:00
|
|
|
if filter(&pod) {
|
2014-08-11 07:34:59 +00:00
|
|
|
filtered = append(filtered, pod)
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 00:48:07 +00:00
|
|
|
pods := *r.Pods
|
|
|
|
pods.Items = filtered
|
|
|
|
return &pods, nil
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 15:46:04 +00:00
|
|
|
func (r *PodRegistry) ListPods(ctx api.Context, selector labels.Selector) (*api.PodList, error) {
|
2014-09-26 19:19:22 +00:00
|
|
|
return r.ListPodsPredicate(ctx, func(pod *api.Pod) bool {
|
2014-09-16 23:15:40 +00:00
|
|
|
return selector.Matches(labels.Set(pod.Labels))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-06 23:29:03 +00:00
|
|
|
func (r *PodRegistry) WatchPods(ctx api.Context, label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) {
|
2014-12-04 08:30:51 +00:00
|
|
|
return r.broadcaster.Watch(), nil
|
2014-08-11 19:58:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
func (r *PodRegistry) GetPod(ctx api.Context, podId string) (*api.Pod, error) {
|
2014-08-11 07:34:59 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
return r.Pod, r.Err
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
func (r *PodRegistry) CreatePod(ctx api.Context, pod *api.Pod) error {
|
2014-08-11 07:34:59 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2014-09-08 04:14:18 +00:00
|
|
|
r.Pod = pod
|
2014-12-04 08:30:51 +00:00
|
|
|
r.broadcaster.Action(watch.Added, pod)
|
2014-08-11 07:34:59 +00:00
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
func (r *PodRegistry) UpdatePod(ctx api.Context, pod *api.Pod) error {
|
2014-08-11 07:34:59 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2014-09-08 04:14:18 +00:00
|
|
|
r.Pod = pod
|
2014-12-04 08:30:51 +00:00
|
|
|
r.broadcaster.Action(watch.Modified, pod)
|
2014-08-11 07:34:59 +00:00
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
2014-09-26 19:19:22 +00:00
|
|
|
func (r *PodRegistry) DeletePod(ctx api.Context, podId string) error {
|
2014-08-11 07:34:59 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2014-12-04 08:30:51 +00:00
|
|
|
r.broadcaster.Action(watch.Deleted, r.Pod)
|
2014-08-11 07:34:59 +00:00
|
|
|
return r.Err
|
|
|
|
}
|