Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
545d87d5
Commit
545d87d5
authored
Dec 22, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move clock to util
parent
5b8e9159
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
27 deletions
+52
-27
ip_cache.go
pkg/master/ip_cache.go
+3
-16
ip_cache_test.go
pkg/master/ip_cache_test.go
+3
-10
master.go
pkg/master/master.go
+1
-1
clock.go
pkg/util/clock.go
+45
-0
No files found.
pkg/master/ip_cache.go
View file @
545d87d5
...
@@ -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
{
...
...
pkg/master/ip_cache_test.go
View file @
545d87d5
...
@@ -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"
{
...
...
pkg/master/master.go
View file @
545d87d5
...
@@ -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
,
...
...
pkg/util/clock.go
0 → 100644
View file @
545d87d5
/*
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment