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
6cb344e7
Commit
6cb344e7
authored
Jun 28, 2018
by
fisherxu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
resource version parsing should all be in one place
parent
05f073dc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
22 deletions
+16
-22
cacher.go
staging/src/k8s.io/apiserver/pkg/storage/cacher.go
+1
-1
watch_cache.go
staging/src/k8s.io/apiserver/pkg/storage/watch_cache.go
+12
-20
watch_cache_test.go
staging/src/k8s.io/apiserver/pkg/storage/watch_cache_test.go
+3
-1
No files found.
staging/src/k8s.io/apiserver/pkg/storage/cacher.go
View file @
6cb344e7
...
...
@@ -191,7 +191,7 @@ type Cacher struct {
// its internal cache and updating its cache in the background based on the
// given configuration.
func
NewCacherFromConfig
(
config
CacherConfig
)
*
Cacher
{
watchCache
:=
newWatchCache
(
config
.
CacheCapacity
,
config
.
KeyFunc
,
config
.
GetAttrsFunc
)
watchCache
:=
newWatchCache
(
config
.
CacheCapacity
,
config
.
KeyFunc
,
config
.
GetAttrsFunc
,
config
.
Versioner
)
listerWatcher
:=
newCacherListerWatcher
(
config
.
Storage
,
config
.
ResourcePrefix
,
config
.
NewListFunc
)
reflectorName
:=
"storage/cacher.go:"
+
config
.
ResourcePrefix
...
...
staging/src/k8s.io/apiserver/pkg/storage/watch_cache.go
View file @
6cb344e7
...
...
@@ -24,7 +24,6 @@ import (
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
...
...
@@ -137,12 +136,16 @@ type watchCache struct {
// for testing timeouts.
clock
clock
.
Clock
// An underlying storage.Versioner.
versioner
Versioner
}
func
newWatchCache
(
capacity
int
,
keyFunc
func
(
runtime
.
Object
)
(
string
,
error
),
getAttrsFunc
func
(
runtime
.
Object
)
(
labels
.
Set
,
fields
.
Set
,
bool
,
error
))
*
watchCache
{
getAttrsFunc
func
(
runtime
.
Object
)
(
labels
.
Set
,
fields
.
Set
,
bool
,
error
),
versioner
Versioner
)
*
watchCache
{
wc
:=
&
watchCache
{
capacity
:
capacity
,
keyFunc
:
keyFunc
,
...
...
@@ -153,6 +156,7 @@ func newWatchCache(
store
:
cache
.
NewStore
(
storeElementKey
),
resourceVersion
:
0
,
clock
:
clock
.
RealClock
{},
versioner
:
versioner
,
}
wc
.
cond
=
sync
.
NewCond
(
wc
.
RLocker
())
return
wc
...
...
@@ -160,7 +164,7 @@ func newWatchCache(
// Add takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Add
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
w
.
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
return
err
}
...
...
@@ -172,7 +176,7 @@ func (w *watchCache) Add(obj interface{}) error {
// Update takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Update
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
w
.
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
return
err
}
...
...
@@ -184,7 +188,7 @@ func (w *watchCache) Update(obj interface{}) error {
// Delete takes runtime.Object as an argument.
func
(
w
*
watchCache
)
Delete
(
obj
interface
{})
error
{
object
,
resourceVersion
,
err
:=
objectToVersionedRuntimeObject
(
obj
)
object
,
resourceVersion
,
err
:=
w
.
objectToVersionedRuntimeObject
(
obj
)
if
err
!=
nil
{
return
err
}
...
...
@@ -194,30 +198,18 @@ func (w *watchCache) Delete(obj interface{}) error {
return
w
.
processEvent
(
event
,
resourceVersion
,
f
)
}
func
objectToVersionedRuntimeObject
(
obj
interface
{})
(
runtime
.
Object
,
uint64
,
error
)
{
func
(
w
*
watchCache
)
objectToVersionedRuntimeObject
(
obj
interface
{})
(
runtime
.
Object
,
uint64
,
error
)
{
object
,
ok
:=
obj
.
(
runtime
.
Object
)
if
!
ok
{
return
nil
,
0
,
fmt
.
Errorf
(
"obj does not implement runtime.Object interface: %v"
,
obj
)
}
meta
,
err
:=
meta
.
Accessor
(
object
)
if
err
!=
nil
{
return
nil
,
0
,
err
}
resourceVersion
,
err
:=
parseResourceVersion
(
meta
.
GetResourceVersion
())
resourceVersion
,
err
:=
w
.
versioner
.
ObjectResourceVersion
(
object
)
if
err
!=
nil
{
return
nil
,
0
,
err
}
return
object
,
resourceVersion
,
nil
}
func
parseResourceVersion
(
resourceVersion
string
)
(
uint64
,
error
)
{
if
resourceVersion
==
""
{
return
0
,
nil
}
// Use bitsize being the size of int on the machine.
return
strconv
.
ParseUint
(
resourceVersion
,
10
,
0
)
}
func
(
w
*
watchCache
)
processEvent
(
event
watch
.
Event
,
resourceVersion
uint64
,
updateFunc
func
(
*
storeElement
)
error
)
error
{
key
,
err
:=
w
.
keyFunc
(
event
.
Object
)
if
err
!=
nil
{
...
...
@@ -362,7 +354,7 @@ func (w *watchCache) GetByKey(key string) (interface{}, bool, error) {
// Replace takes slice of runtime.Object as a parameter.
func
(
w
*
watchCache
)
Replace
(
objs
[]
interface
{},
resourceVersion
string
)
error
{
version
,
err
:=
p
arseResourceVersion
(
resourceVersion
)
version
,
err
:=
w
.
versioner
.
P
arseResourceVersion
(
resourceVersion
)
if
err
!=
nil
{
return
err
}
...
...
staging/src/k8s.io/apiserver/pkg/storage/watch_cache_test.go
View file @
6cb344e7
...
...
@@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
etcdstorage
"k8s.io/apiserver/pkg/storage/etcd"
"k8s.io/client-go/tools/cache"
)
...
...
@@ -73,7 +74,8 @@ func newTestWatchCache(capacity int) *watchCache {
}
return
labels
.
Set
(
pod
.
Labels
),
fields
.
Set
{
"spec.nodeName"
:
pod
.
Spec
.
NodeName
},
false
,
nil
}
wc
:=
newWatchCache
(
capacity
,
keyFunc
,
getAttrsFunc
)
versioner
:=
etcdstorage
.
APIObjectVersioner
{}
wc
:=
newWatchCache
(
capacity
,
keyFunc
,
getAttrsFunc
,
versioner
)
wc
.
clock
=
clock
.
NewFakeClock
(
time
.
Now
())
return
wc
}
...
...
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