From 545d87d554e293b6bc11096ccaca0b4535146930 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Mon, 22 Dec 2014 11:54:32 -0800 Subject: [PATCH] Move clock to util --- pkg/master/ip_cache.go | 19 +++------------- pkg/master/ip_cache_test.go | 13 +++-------- pkg/master/master.go | 2 +- pkg/util/clock.go | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 pkg/util/clock.go diff --git a/pkg/master/ip_cache.go b/pkg/master/ip_cache.go index 02d7be71d10..35621384d71 100644 --- a/pkg/master/ip_cache.go +++ b/pkg/master/ip_cache.go @@ -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 { diff --git a/pkg/master/ip_cache_test.go b/pkg/master/ip_cache_test.go index dac9c33d86e..9b3cd487707 100644 --- a/pkg/master/ip_cache_test.go +++ b/pkg/master/ip_cache_test.go @@ -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" { diff --git a/pkg/master/master.go b/pkg/master/master.go index 1b18d12f7ad..b8c55b57248 100644 --- a/pkg/master/master.go +++ b/pkg/master/master.go @@ -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, diff --git a/pkg/util/clock.go b/pkg/util/clock.go new file mode 100644 index 00000000000..0ef99f99daf --- /dev/null +++ b/pkg/util/clock.go @@ -0,0 +1,45 @@ +/* +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 +}