Move clock to util

This commit is contained in:
Daniel Smith
2014-12-22 11:54:32 -08:00
parent 5b8e91595a
commit 545d87d554
4 changed files with 52 additions and 27 deletions

View File

@@ -21,6 +21,7 @@ import (
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
)
@@ -31,7 +32,7 @@ type ipCacheEntry struct {
}
type ipCache struct {
clock Clock
clock util.Clock
cloudProvider cloudprovider.Interface
cache map[string]ipCacheEntry
lock sync.Mutex
@@ -40,7 +41,7 @@ type ipCache struct {
// NewIPCache makes a new ip caching layer, which will get IP addresses from cp,
// and use clock for deciding when to re-get an IP address.
// Thread-safe.
func NewIPCache(cp cloudprovider.Interface, clock Clock) *ipCache {
func NewIPCache(cp cloudprovider.Interface, clock util.Clock) *ipCache {
return &ipCache{
clock: clock,
cloudProvider: cp,
@@ -48,20 +49,6 @@ func NewIPCache(cp cloudprovider.Interface, clock Clock) *ipCache {
}
}
// Clock allows for injecting fake or real clocks into
// the cache.
type Clock interface {
Now() time.Time
}
// RealClock really calls time.Now()
type RealClock struct{}
// Now returns the current time.
func (r RealClock) Now() time.Time {
return time.Now()
}
// GetInstanceIP returns the IP address of host, from the cache
// if possible, otherwise it asks the cloud provider.
func (c *ipCache) GetInstanceIP(host string) string {

View File

@@ -21,19 +21,12 @@ import (
"time"
fake_cloud "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/fake"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
)
type fakeClock struct {
t time.Time
}
func (f *fakeClock) Now() time.Time {
return f.t
}
func TestCacheExpire(t *testing.T) {
fakeCloud := &fake_cloud.FakeCloud{}
clock := &fakeClock{t: time.Now()}
clock := &util.FakeClock{time.Now()}
c := NewIPCache(fakeCloud, clock)
@@ -41,7 +34,7 @@ func TestCacheExpire(t *testing.T) {
// This call should hit the cache, so we expect no additional calls to the cloud
_ = c.GetInstanceIP("foo")
// Advance the clock, this call should miss the cache, so expect one more call.
clock.t = clock.t.Add(60 * time.Second)
clock.Time = clock.Time.Add(60 * time.Second)
_ = c.GetInstanceIP("foo")
if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[1] != "ip-address" || fakeCloud.Calls[0] != "ip-address" {

View File

@@ -323,7 +323,7 @@ func (m *Master) init(c *Config) {
var authenticator = c.Authenticator
nodeRESTStorage := minion.NewREST(m.minionRegistry)
ipCache := NewIPCache(c.Cloud, RealClock{})
ipCache := NewIPCache(c.Cloud, util.RealClock{})
podCache := NewPodCache(
ipCache,
c.KubeletClient,