mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 11:13:48 +00:00
Move clock to util
This commit is contained in:
parent
5b8e91595a
commit
545d87d554
@ -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 {
|
||||
|
@ -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" {
|
||||
|
@ -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,
|
||||
|
45
pkg/util/clock.go
Normal file
45
pkg/util/clock.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user