Commit 545d87d5 authored by Daniel Smith's avatar Daniel Smith

Move clock to util

parent 5b8e9159
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
"time" "time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider" "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog" "github.com/golang/glog"
) )
...@@ -31,7 +32,7 @@ type ipCacheEntry struct { ...@@ -31,7 +32,7 @@ type ipCacheEntry struct {
} }
type ipCache struct { type ipCache struct {
clock Clock clock util.Clock
cloudProvider cloudprovider.Interface cloudProvider cloudprovider.Interface
cache map[string]ipCacheEntry cache map[string]ipCacheEntry
lock sync.Mutex lock sync.Mutex
...@@ -40,7 +41,7 @@ type ipCache struct { ...@@ -40,7 +41,7 @@ type ipCache struct {
// NewIPCache makes a new ip caching layer, which will get IP addresses from cp, // 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. // and use clock for deciding when to re-get an IP address.
// Thread-safe. // Thread-safe.
func NewIPCache(cp cloudprovider.Interface, clock Clock) *ipCache { func NewIPCache(cp cloudprovider.Interface, clock util.Clock) *ipCache {
return &ipCache{ return &ipCache{
clock: clock, clock: clock,
cloudProvider: cp, cloudProvider: cp,
...@@ -48,20 +49,6 @@ func NewIPCache(cp cloudprovider.Interface, clock Clock) *ipCache { ...@@ -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 // GetInstanceIP returns the IP address of host, from the cache
// if possible, otherwise it asks the cloud provider. // if possible, otherwise it asks the cloud provider.
func (c *ipCache) GetInstanceIP(host string) string { func (c *ipCache) GetInstanceIP(host string) string {
......
...@@ -21,19 +21,12 @@ import ( ...@@ -21,19 +21,12 @@ import (
"time" "time"
fake_cloud "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/fake" 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) { func TestCacheExpire(t *testing.T) {
fakeCloud := &fake_cloud.FakeCloud{} fakeCloud := &fake_cloud.FakeCloud{}
clock := &fakeClock{t: time.Now()} clock := &util.FakeClock{time.Now()}
c := NewIPCache(fakeCloud, clock) c := NewIPCache(fakeCloud, clock)
...@@ -41,7 +34,7 @@ func TestCacheExpire(t *testing.T) { ...@@ -41,7 +34,7 @@ func TestCacheExpire(t *testing.T) {
// This call should hit the cache, so we expect no additional calls to the cloud // This call should hit the cache, so we expect no additional calls to the cloud
_ = c.GetInstanceIP("foo") _ = c.GetInstanceIP("foo")
// Advance the clock, this call should miss the cache, so expect one more call. // 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") _ = c.GetInstanceIP("foo")
if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[1] != "ip-address" || fakeCloud.Calls[0] != "ip-address" { if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[1] != "ip-address" || fakeCloud.Calls[0] != "ip-address" {
......
...@@ -323,7 +323,7 @@ func (m *Master) init(c *Config) { ...@@ -323,7 +323,7 @@ func (m *Master) init(c *Config) {
var authenticator = c.Authenticator var authenticator = c.Authenticator
nodeRESTStorage := minion.NewREST(m.minionRegistry) nodeRESTStorage := minion.NewREST(m.minionRegistry)
ipCache := NewIPCache(c.Cloud, RealClock{}) ipCache := NewIPCache(c.Cloud, util.RealClock{})
podCache := NewPodCache( podCache := NewPodCache(
ipCache, ipCache,
c.KubeletClient, c.KubeletClient,
......
/*
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 util
import (
"time"
)
// Clock allows for injecting fake or real clocks into code that
// needs to do arbitrary things based on time.
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()
}
// FakeClock implements Clock, but returns an arbitary time.
type FakeClock struct {
Time time.Time
}
// Now returns f's time.
func (f *FakeClock) Now() time.Time {
return f.Time
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment