mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 14:14:39 +00:00
35
pkg/client/cache/fifo.go
vendored
35
pkg/client/cache/fifo.go
vendored
@@ -18,6 +18,8 @@ package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
// FIFO receives adds and updates from a Reflector, and puts them in a queue for
|
||||
@@ -33,30 +35,30 @@ type FIFO struct {
|
||||
}
|
||||
|
||||
// Add inserts an item, and puts it in the queue.
|
||||
func (f *FIFO) Add(ID string, obj interface{}) {
|
||||
func (f *FIFO) Add(id string, obj interface{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.items[ID] = obj
|
||||
f.queue = append(f.queue, ID)
|
||||
f.items[id] = obj
|
||||
f.queue = append(f.queue, id)
|
||||
f.cond.Broadcast()
|
||||
}
|
||||
|
||||
// Update updates an item, and adds it to the queue.
|
||||
func (f *FIFO) Update(ID string, obj interface{}) {
|
||||
func (f *FIFO) Update(id string, obj interface{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.items[ID] = obj
|
||||
f.queue = append(f.queue, ID)
|
||||
f.items[id] = obj
|
||||
f.queue = append(f.queue, id)
|
||||
f.cond.Broadcast()
|
||||
}
|
||||
|
||||
// Delete removes an item. It doesn't add it to the queue, because
|
||||
// this implementation assumes the consumer only cares about the objects,
|
||||
// not the order in which they were created/added.
|
||||
func (f *FIFO) Delete(ID string, obj interface{}) {
|
||||
func (f *FIFO) Delete(id string) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
delete(f.items, ID)
|
||||
delete(f.items, id)
|
||||
}
|
||||
|
||||
// List returns a list of all the items.
|
||||
@@ -70,11 +72,24 @@ func (f *FIFO) List() []interface{} {
|
||||
return list
|
||||
}
|
||||
|
||||
// Contains returns a util.StringSet containing all IDs of stored the items.
|
||||
// This is a snapshot of a moment in time, and one should keep in mind that
|
||||
// other go routines can add or remove items after you call this.
|
||||
func (c *FIFO) Contains() util.StringSet {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
set := util.StringSet{}
|
||||
for id := range c.items {
|
||||
set.Insert(id)
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// Get returns the requested item, or sets exists=false.
|
||||
func (f *FIFO) Get(ID string) (item interface{}, exists bool) {
|
||||
func (f *FIFO) Get(id string) (item interface{}, exists bool) {
|
||||
f.lock.RLock()
|
||||
defer f.lock.RUnlock()
|
||||
item, exists = f.items[ID]
|
||||
item, exists = f.items[id]
|
||||
return item, exists
|
||||
}
|
||||
|
||||
|
81
pkg/client/cache/poller.go
vendored
Normal file
81
pkg/client/cache/poller.go
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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 cache
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Enumerator should be able to return the list of objects to be synced with
|
||||
// one object at a time.
|
||||
type Enumerator interface {
|
||||
Len() int
|
||||
Get(index int) (ID string, object interface{})
|
||||
}
|
||||
|
||||
// GetFunc should return an enumerator that you wish the Poller to proccess.
|
||||
type GetFunc func() (Enumerator, error)
|
||||
|
||||
// Poller is like Reflector, but it periodically polls instead of watching.
|
||||
// This is intended to be a workaround for api objects that don't yet support
|
||||
// watching.
|
||||
type Poller struct {
|
||||
getFunc GetFunc
|
||||
period time.Duration
|
||||
store Store
|
||||
}
|
||||
|
||||
// NewPoller constructs a new poller. Note that polling probably doesn't make much
|
||||
// sense to use along with the FIFO queue. The returned Poller will call getFunc and
|
||||
// sync the objects in 'store' with the returned Enumerator, waiting 'period' between
|
||||
// each call. It probably only makes sense to use a poller if you're treating the
|
||||
// store as read-only.
|
||||
func NewPoller(getFunc GetFunc, period time.Duration, store Store) *Poller {
|
||||
return &Poller{
|
||||
getFunc: getFunc,
|
||||
period: period,
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
// Run begins polling. It starts a goroutine and returns immediately.
|
||||
func (p *Poller) Run() {
|
||||
go util.Forever(func() {
|
||||
e, err := p.getFunc()
|
||||
if err != nil {
|
||||
glog.Errorf("failed to list: %v", err)
|
||||
return
|
||||
}
|
||||
p.sync(e)
|
||||
}, p.period)
|
||||
}
|
||||
|
||||
func (p *Poller) sync(e Enumerator) {
|
||||
current := p.store.Contains()
|
||||
for i := 0; i < e.Len(); i++ {
|
||||
id, object := e.Get(i)
|
||||
p.store.Update(id, object)
|
||||
current.Delete(id)
|
||||
}
|
||||
// Delete all the objects not found.
|
||||
for id := range current {
|
||||
p.store.Delete(id)
|
||||
}
|
||||
}
|
119
pkg/client/cache/poller_test.go
vendored
Normal file
119
pkg/client/cache/poller_test.go
vendored
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
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 cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testPair struct {
|
||||
id string
|
||||
obj interface{}
|
||||
}
|
||||
type testEnumerator []testPair
|
||||
|
||||
func (t testEnumerator) Len() int { return len(t) }
|
||||
func (t testEnumerator) Get(i int) (string, interface{}) {
|
||||
return t[i].id, t[i].obj
|
||||
}
|
||||
|
||||
func TestPoller_sync(t *testing.T) {
|
||||
table := []struct {
|
||||
// each step simulates the list that a getFunc would receive.
|
||||
steps [][]testPair
|
||||
}{
|
||||
{
|
||||
steps: [][]testPair{
|
||||
{
|
||||
{"foo", "foo1"},
|
||||
{"bar", "bar1"},
|
||||
{"baz", "baz1"},
|
||||
{"qux", "qux1"},
|
||||
}, {
|
||||
{"foo", "foo2"},
|
||||
{"bar", "bar2"},
|
||||
{"qux", "qux2"},
|
||||
}, {
|
||||
{"bar", "bar3"},
|
||||
{"baz", "baz2"},
|
||||
{"qux", "qux3"},
|
||||
}, {
|
||||
{"qux", "qux4"},
|
||||
}, {
|
||||
{"foo", "foo3"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for testCase, item := range table {
|
||||
s := NewStore()
|
||||
// This is a unit test for the sync function, hence the nil getFunc.
|
||||
p := NewPoller(nil, 0, s)
|
||||
for line, pairs := range item.steps {
|
||||
p.sync(testEnumerator(pairs))
|
||||
|
||||
ids := s.Contains()
|
||||
for _, pair := range pairs {
|
||||
if !ids.Has(pair.id) {
|
||||
t.Errorf("%v, %v: expected to find entry for %v, but did not.", testCase, line, pair.id)
|
||||
continue
|
||||
}
|
||||
found, ok := s.Get(pair.id)
|
||||
if !ok {
|
||||
t.Errorf("%v, %v: unexpected absent entry for %v", testCase, line, pair.id)
|
||||
continue
|
||||
}
|
||||
if e, a := pair.obj, found; !reflect.DeepEqual(e, a) {
|
||||
t.Errorf("%v, %v: expected %v, got %v for %v", testCase, line, e, a, pair.id)
|
||||
}
|
||||
}
|
||||
if e, a := len(pairs), len(ids); e != a {
|
||||
t.Errorf("%v, %v: expected len %v, got %v", testCase, line, e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoller_Run(t *testing.T) {
|
||||
s := NewStore()
|
||||
const count = 10
|
||||
var called = 0
|
||||
done := make(chan struct{})
|
||||
NewPoller(func() (Enumerator, error) {
|
||||
called++
|
||||
if called == count {
|
||||
close(done)
|
||||
}
|
||||
// test both error and regular returns.
|
||||
if called&1 == 0 {
|
||||
return testEnumerator{}, nil
|
||||
}
|
||||
return nil, errors.New("transient error")
|
||||
}, time.Millisecond, s).Run()
|
||||
|
||||
// The test here is that we get called at least count times.
|
||||
<-done
|
||||
|
||||
// We never added anything, verify that.
|
||||
if e, a := 0, len(s.Contains()); e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
58
pkg/client/cache/reflector.go
vendored
58
pkg/client/cache/reflector.go
vendored
@@ -21,62 +21,56 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// Store is a generic object storage interface. Reflector knows how to watch a server
|
||||
// and update a store. A generic store is provided, which allows Reflector to be used
|
||||
// as a local caching system, and an LRU store, which allows Reflector to work like a
|
||||
// queue of items yet to be processed.
|
||||
type Store interface {
|
||||
Add(ID string, obj interface{})
|
||||
Update(ID string, obj interface{})
|
||||
Delete(ID string, obj interface{})
|
||||
List() []interface{}
|
||||
Get(ID string) (item interface{}, exists bool)
|
||||
}
|
||||
|
||||
// Reflector watches a specified resource and causes all changes to be reflected in the given store.
|
||||
type Reflector struct {
|
||||
kubeClient *client.Client
|
||||
resource string
|
||||
// The type of object we expect to place in the store.
|
||||
expectedType reflect.Type
|
||||
store Store
|
||||
// The destination to sync up with the watch source
|
||||
store Store
|
||||
// watchFactory is called to initiate watches.
|
||||
watchFactory WatchFactory
|
||||
// period controls timing between one watch ending and
|
||||
// the beginning of the next one.
|
||||
period time.Duration
|
||||
}
|
||||
|
||||
// WatchFactory should begin a watch at the specified version.
|
||||
type WatchFactory func(resourceVersion uint64) (watch.Interface, error)
|
||||
|
||||
// NewReflector makes a new Reflector object which will keep the given store up to
|
||||
// date with the server's contents for the given resource. Reflector promises to
|
||||
// only put things in the store that have the type of expectedType.
|
||||
// TODO: define a query so you only locally cache a subset of items.
|
||||
func NewReflector(resource string, kubeClient *client.Client, expectedType interface{}, store Store) *Reflector {
|
||||
func NewReflector(watchFactory WatchFactory, expectedType interface{}, store Store) *Reflector {
|
||||
gc := &Reflector{
|
||||
resource: resource,
|
||||
kubeClient: kubeClient,
|
||||
watchFactory: watchFactory,
|
||||
store: store,
|
||||
expectedType: reflect.TypeOf(expectedType),
|
||||
period: time.Second,
|
||||
}
|
||||
return gc
|
||||
}
|
||||
|
||||
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
|
||||
// Run starts a goroutine and returns immediately.
|
||||
func (gc *Reflector) Run() {
|
||||
var resourceVersion uint64
|
||||
go util.Forever(func() {
|
||||
w, err := gc.startWatch()
|
||||
w, err := gc.watchFactory(resourceVersion)
|
||||
if err != nil {
|
||||
glog.Errorf("failed to watch %v: %v", gc.resource, err)
|
||||
glog.Errorf("failed to watch %v: %v", gc.expectedType, err)
|
||||
return
|
||||
}
|
||||
gc.watchHandler(w)
|
||||
}, 5*time.Second)
|
||||
gc.watchHandler(w, &resourceVersion)
|
||||
}, gc.period)
|
||||
}
|
||||
|
||||
func (gc *Reflector) startWatch() (watch.Interface, error) {
|
||||
return gc.kubeClient.Get().Path(gc.resource).Path("watch").Watch()
|
||||
}
|
||||
|
||||
func (gc *Reflector) watchHandler(w watch.Interface) {
|
||||
// watchHandler watches w and keeps *resourceVersion up to date.
|
||||
func (gc *Reflector) watchHandler(w watch.Interface, resourceVersion *uint64) {
|
||||
for {
|
||||
event, ok := <-w.ResultChan()
|
||||
if !ok {
|
||||
@@ -98,9 +92,13 @@ func (gc *Reflector) watchHandler(w watch.Interface) {
|
||||
case watch.Modified:
|
||||
gc.store.Update(jsonBase.ID(), event.Object)
|
||||
case watch.Deleted:
|
||||
gc.store.Delete(jsonBase.ID(), event.Object)
|
||||
// TODO: Will any consumers need access to the "last known
|
||||
// state", which is passed in event.Object? If so, may need
|
||||
// to change this.
|
||||
gc.store.Delete(jsonBase.ID())
|
||||
default:
|
||||
glog.Errorf("unable to understand watch event %#v", event)
|
||||
}
|
||||
*resourceVersion = jsonBase.ResourceVersion() + 1
|
||||
}
|
||||
}
|
||||
|
92
pkg/client/cache/reflector_test.go
vendored
92
pkg/client/cache/reflector_test.go
vendored
@@ -17,29 +17,27 @@ limitations under the License.
|
||||
package cache
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
||||
)
|
||||
|
||||
func TestReflector_watchHandler(t *testing.T) {
|
||||
s := NewStore()
|
||||
g := NewReflector("foo", nil, &api.Pod{}, s)
|
||||
g := NewReflector(nil, &api.Pod{}, s)
|
||||
fw := watch.NewFake()
|
||||
s.Add("foo", &api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
|
||||
s.Add("bar", &api.Pod{JSONBase: api.JSONBase{ID: "bar"}})
|
||||
go func() {
|
||||
fw.Modify(&api.Pod{JSONBase: api.JSONBase{ID: "bar", ResourceVersion: 55}})
|
||||
fw.Add(&api.Pod{JSONBase: api.JSONBase{ID: "baz"}})
|
||||
fw.Add(&api.Service{JSONBase: api.JSONBase{ID: "rejected"}})
|
||||
fw.Delete(&api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
|
||||
fw.Modify(&api.Pod{JSONBase: api.JSONBase{ID: "bar", ResourceVersion: 55}})
|
||||
fw.Add(&api.Pod{JSONBase: api.JSONBase{ID: "baz", ResourceVersion: 32}})
|
||||
fw.Stop()
|
||||
}()
|
||||
g.watchHandler(fw)
|
||||
var resumeRV uint64
|
||||
g.watchHandler(fw, &resumeRV)
|
||||
|
||||
table := []struct {
|
||||
ID string
|
||||
@@ -49,7 +47,7 @@ func TestReflector_watchHandler(t *testing.T) {
|
||||
{"foo", 0, false},
|
||||
{"rejected", 0, false},
|
||||
{"bar", 55, true},
|
||||
{"baz", 0, true},
|
||||
{"baz", 32, true},
|
||||
}
|
||||
for _, item := range table {
|
||||
obj, exists := s.Get(item.ID)
|
||||
@@ -63,32 +61,62 @@ func TestReflector_watchHandler(t *testing.T) {
|
||||
t.Errorf("%v: expected %v, got %v", item.ID, e, a)
|
||||
}
|
||||
}
|
||||
|
||||
// RV should stay 1 higher than the last id we see.
|
||||
if e, a := uint64(33), resumeRV; e != a {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflector_startWatch(t *testing.T) {
|
||||
table := []struct{ resource, path string }{
|
||||
{"pods", "/api/v1beta1/pods/watch"},
|
||||
{"services", "/api/v1beta1/services/watch"},
|
||||
}
|
||||
for _, testItem := range table {
|
||||
got := make(chan struct{})
|
||||
srv := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, req *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
if req.URL.Path == testItem.path {
|
||||
close(got)
|
||||
return
|
||||
}
|
||||
t.Errorf("unexpected path %v", req.URL.Path)
|
||||
}))
|
||||
s := NewStore()
|
||||
c := client.New(srv.URL, nil)
|
||||
g := NewReflector(testItem.resource, c, &api.Pod{}, s)
|
||||
_, err := g.startWatch()
|
||||
// We're just checking that it watches the right path.
|
||||
if err == nil {
|
||||
t.Errorf("unexpected non-error")
|
||||
func TestReflector_Run(t *testing.T) {
|
||||
createdFakes := make(chan *watch.FakeWatcher)
|
||||
|
||||
// Expect our starter to get called at the beginning of the watch with 0, and again with 3 when we
|
||||
// inject an error at 2.
|
||||
expectedRVs := []uint64{0, 3}
|
||||
watchStarter := func(rv uint64) (watch.Interface, error) {
|
||||
fw := watch.NewFake()
|
||||
if e, a := expectedRVs[0], rv; e != a {
|
||||
t.Errorf("Expected rv %v, but got %v", e, a)
|
||||
}
|
||||
<-got
|
||||
expectedRVs = expectedRVs[1:]
|
||||
// channel is not buffered because the for loop below needs to block. But
|
||||
// we don't want to block here, so report the new fake via a go routine.
|
||||
go func() { createdFakes <- fw }()
|
||||
return fw, nil
|
||||
}
|
||||
s := NewFIFO()
|
||||
r := NewReflector(watchStarter, &api.Pod{}, s)
|
||||
r.period = 0
|
||||
r.Run()
|
||||
|
||||
ids := []string{"foo", "bar", "baz", "qux", "zoo"}
|
||||
var fw *watch.FakeWatcher
|
||||
for i, id := range ids {
|
||||
if fw == nil {
|
||||
fw = <-createdFakes
|
||||
}
|
||||
sendingRV := uint64(i + 1)
|
||||
fw.Add(&api.Pod{JSONBase: api.JSONBase{ID: id, ResourceVersion: sendingRV}})
|
||||
if sendingRV == 2 {
|
||||
// Inject a failure.
|
||||
fw.Stop()
|
||||
fw = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Verify we received the right ids with the right resource versions.
|
||||
for i, id := range ids {
|
||||
pod := s.Pop().(*api.Pod)
|
||||
if e, a := id, pod.ID; e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
if e, a := uint64(i+1), pod.ResourceVersion; e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", i, e, a)
|
||||
}
|
||||
}
|
||||
|
||||
if len(expectedRVs) != 0 {
|
||||
t.Error("called watchStarter an unexpected number of times")
|
||||
}
|
||||
}
|
||||
|
48
pkg/client/cache/store.go
vendored
48
pkg/client/cache/store.go
vendored
@@ -18,32 +18,47 @@ package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
// Store is a generic object storage interface. Reflector knows how to watch a server
|
||||
// and update a store. A generic store is provided, which allows Reflector to be used
|
||||
// as a local caching system, and an LRU store, which allows Reflector to work like a
|
||||
// queue of items yet to be processed.
|
||||
type Store interface {
|
||||
Add(id string, obj interface{})
|
||||
Update(id string, obj interface{})
|
||||
Delete(id string)
|
||||
List() []interface{}
|
||||
Contains() util.StringSet
|
||||
Get(id string) (item interface{}, exists bool)
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
lock sync.RWMutex
|
||||
items map[string]interface{}
|
||||
}
|
||||
|
||||
// Add inserts an item into the cache.
|
||||
func (c *cache) Add(ID string, obj interface{}) {
|
||||
func (c *cache) Add(id string, obj interface{}) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.items[ID] = obj
|
||||
c.items[id] = obj
|
||||
}
|
||||
|
||||
// Update sets an item in the cache to its updated state.
|
||||
func (c *cache) Update(ID string, obj interface{}) {
|
||||
func (c *cache) Update(id string, obj interface{}) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.items[ID] = obj
|
||||
c.items[id] = obj
|
||||
}
|
||||
|
||||
// Delete removes an item from the cache.
|
||||
func (c *cache) Delete(ID string, obj interface{}) {
|
||||
func (c *cache) Delete(id string) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
delete(c.items, ID)
|
||||
delete(c.items, id)
|
||||
}
|
||||
|
||||
// List returns a list of all the items.
|
||||
@@ -58,12 +73,25 @@ func (c *cache) List() []interface{} {
|
||||
return list
|
||||
}
|
||||
|
||||
// Get returns the requested item, or sets exists=false.
|
||||
// Get is completely threadsafe as long as you treat all items as immutable.
|
||||
func (c *cache) Get(ID string) (item interface{}, exists bool) {
|
||||
// Contains returns a util.StringSet containing all IDs of stored the items.
|
||||
// This is a snapshot of a moment in time, and one should keep in mind that
|
||||
// other go routines can add or remove items after you call this.
|
||||
func (c *cache) Contains() util.StringSet {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
item, exists = c.items[ID]
|
||||
set := util.StringSet{}
|
||||
for id := range c.items {
|
||||
set.Insert(id)
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// Get returns the requested item, or sets exists=false.
|
||||
// Get is completely threadsafe as long as you treat all items as immutable.
|
||||
func (c *cache) Get(id string) (item interface{}, exists bool) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
item, exists = c.items[id]
|
||||
return item, exists
|
||||
}
|
||||
|
||||
|
13
pkg/client/cache/store_test.go
vendored
13
pkg/client/cache/store_test.go
vendored
@@ -40,10 +40,12 @@ func doTestStore(t *testing.T, store Store) {
|
||||
t.Errorf("expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
store.Delete("foo", "qux")
|
||||
store.Delete("foo")
|
||||
if _, ok := store.Get("foo"); ok {
|
||||
t.Errorf("found deleted item??")
|
||||
}
|
||||
|
||||
// Test List
|
||||
store.Add("a", "b")
|
||||
store.Add("c", "d")
|
||||
store.Add("e", "e")
|
||||
@@ -57,6 +59,15 @@ func doTestStore(t *testing.T, store Store) {
|
||||
if len(found) != 3 {
|
||||
t.Errorf("extra items")
|
||||
}
|
||||
|
||||
// Check that ID list is correct.
|
||||
ids := store.Contains()
|
||||
if !ids.HasAll("a", "c", "e") {
|
||||
t.Errorf("missing items")
|
||||
}
|
||||
if len(ids) != 3 {
|
||||
t.Errorf("extra items")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
|
@@ -343,13 +343,17 @@ func (c *testClient) Validate(t *testing.T, received interface{}, err error) {
|
||||
}
|
||||
|
||||
requestBody := body(c.Request.Body, c.Request.RawBody)
|
||||
actualQuery := c.handler.RequestReceived.URL.Query()
|
||||
// We check the query manually, so blank it out so that FakeHandler.ValidateRequest
|
||||
// won't check it.
|
||||
c.handler.RequestReceived.URL.RawQuery = ""
|
||||
c.handler.ValidateRequest(t, makeURL(c.Request.Path), c.Request.Method, requestBody)
|
||||
for key, values := range c.Request.Query {
|
||||
validator, ok := c.QueryValidator[key]
|
||||
if !ok {
|
||||
validator = func(a, b string) bool { return a == b }
|
||||
}
|
||||
observed := c.handler.RequestReceived.URL.Query().Get(key)
|
||||
observed := actualQuery.Get(key)
|
||||
if !validator(values[0], observed) {
|
||||
t.Errorf("Unexpected query arg for key: %s. Expected %s, Received %s", key, values[0], observed)
|
||||
}
|
||||
|
@@ -62,10 +62,7 @@ func TestDoRequestNewWay(t *testing.T) {
|
||||
} else if !reflect.DeepEqual(obj, expectedObj) {
|
||||
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz", "POST", &reqBody)
|
||||
if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo" {
|
||||
t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &reqBody)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
||||
}
|
||||
@@ -88,7 +85,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
|
||||
Path("foo/bar").
|
||||
Path("baz").
|
||||
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
|
||||
Sync(false).
|
||||
Sync(true).
|
||||
Timeout(time.Second).
|
||||
Body(bytes.NewBuffer(reqBodyExpected)).
|
||||
Do().Get()
|
||||
@@ -102,10 +99,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
|
||||
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
|
||||
}
|
||||
tmpStr := string(reqBodyExpected)
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo" {
|
||||
t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&sync=true&timeout=1s", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
||||
}
|
||||
@@ -141,10 +135,7 @@ func TestDoRequestNewWayObj(t *testing.T) {
|
||||
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
|
||||
}
|
||||
tmpStr := string(reqBodyExpected)
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo" {
|
||||
t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
||||
}
|
||||
@@ -194,10 +185,7 @@ func TestDoRequestNewWayFile(t *testing.T) {
|
||||
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
|
||||
}
|
||||
tmpStr := string(reqBodyExpected)
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.URL.RawQuery != "labels=name%3Dfoo" {
|
||||
t.Errorf("Unexpected query: %v", fakeHandler.RequestReceived.URL.RawQuery)
|
||||
}
|
||||
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
|
||||
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
|
||||
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
|
||||
}
|
||||
|
Reference in New Issue
Block a user