2014-10-09 20:56:30 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2014-10-09 20:56:30 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
|
|
|
)
|
|
|
|
|
2015-02-18 18:02:22 +00:00
|
|
|
// GenericRegistry knows how to store & list any runtime.Object.
|
2014-10-09 20:56:30 +00:00
|
|
|
type GenericRegistry struct {
|
|
|
|
Err error
|
|
|
|
Object runtime.Object
|
|
|
|
ObjectList runtime.Object
|
|
|
|
sync.Mutex
|
|
|
|
|
2014-12-04 08:30:51 +00:00
|
|
|
Broadcaster *watch.Broadcaster
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGeneric(list runtime.Object) *GenericRegistry {
|
|
|
|
return &GenericRegistry{
|
2014-12-04 08:30:51 +00:00
|
|
|
ObjectList: list,
|
2015-01-13 01:40:17 +00:00
|
|
|
Broadcaster: watch.NewBroadcaster(0, watch.WaitIfChannelFull),
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:14:15 +00:00
|
|
|
func (r *GenericRegistry) ListPredicate(ctx api.Context, m generic.Matcher) (runtime.Object, error) {
|
2014-10-09 20:56:30 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
2015-02-11 23:35:05 +00:00
|
|
|
return generic.FilterList(r.ObjectList, m, nil)
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 02:14:15 +00:00
|
|
|
func (r *GenericRegistry) WatchPredicate(ctx api.Context, m generic.Matcher, resourceVersion string) (watch.Interface, error) {
|
2014-10-09 20:56:30 +00:00
|
|
|
// TODO: wire filter down into the mux; it needs access to current and previous state :(
|
2014-12-04 08:30:51 +00:00
|
|
|
return r.Broadcaster.Watch(), nil
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *GenericRegistry) Get(ctx api.Context, id string) (runtime.Object, error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2015-03-07 10:00:45 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
if r.Object != nil {
|
|
|
|
return r.Object, nil
|
|
|
|
}
|
|
|
|
panic("generic registry should either have an object or an error for Get")
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 23:35:05 +00:00
|
|
|
func (r *GenericRegistry) CreateWithName(ctx api.Context, id string, obj runtime.Object) error {
|
2014-10-09 20:56:30 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
r.Object = obj
|
2014-12-04 08:30:51 +00:00
|
|
|
r.Broadcaster.Action(watch.Added, obj)
|
2014-10-09 20:56:30 +00:00
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:35:05 +00:00
|
|
|
func (r *GenericRegistry) UpdateWithName(ctx api.Context, id string, obj runtime.Object) error {
|
2014-10-09 20:56:30 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
r.Object = obj
|
2014-12-04 08:30:51 +00:00
|
|
|
r.Broadcaster.Action(watch.Modified, obj)
|
2014-10-09 20:56:30 +00:00
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
2015-03-05 03:34:31 +00:00
|
|
|
func (r *GenericRegistry) Delete(ctx api.Context, id string, options *api.DeleteOptions) (runtime.Object, error) {
|
2014-10-09 20:56:30 +00:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2014-12-04 08:30:51 +00:00
|
|
|
r.Broadcaster.Action(watch.Deleted, r.Object)
|
2015-02-11 23:35:05 +00:00
|
|
|
return &api.Status{Status: api.StatusSuccess}, r.Err
|
2014-10-09 20:56:30 +00:00
|
|
|
}
|