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
d7896159
Unverified
Commit
d7896159
authored
May 27, 2017
by
Mikhail Mazurskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Shared Informer Run blocks until all goroutines finish
Fixes #45454
parent
e09a3539
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
52 additions
and
42 deletions
+52
-42
apiserver.go
pkg/kubelet/config/apiserver.go
+3
-1
kubelet.go
pkg/kubelet/kubelet.go
+4
-2
wait.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
+10
-0
controller.go
staging/src/k8s.io/client-go/tools/cache/controller.go
+4
-1
mutation_detector.go
...ing/src/k8s.io/client-go/tools/cache/mutation_detector.go
+8
-10
reflector.go
staging/src/k8s.io/client-go/tools/cache/reflector.go
+3
-14
reflector_test.go
staging/src/k8s.io/client-go/tools/cache/reflector_test.go
+1
-1
shared_informer.go
staging/src/k8s.io/client-go/tools/cache/shared_informer.go
+18
-12
pod_store.go
test/utils/pod_store.go
+1
-1
No files found.
pkg/kubelet/config/apiserver.go
View file @
d7896159
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
clientset
"k8s.io/client-go/kubernetes"
clientset
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/cache"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api"
...
@@ -43,5 +44,6 @@ func newSourceApiserverFromLW(lw cache.ListerWatcher, updates chan<- interface{}
...
@@ -43,5 +44,6 @@ func newSourceApiserverFromLW(lw cache.ListerWatcher, updates chan<- interface{}
}
}
updates
<-
kubetypes
.
PodUpdate
{
Pods
:
pods
,
Op
:
kubetypes
.
SET
,
Source
:
kubetypes
.
ApiserverSource
}
updates
<-
kubetypes
.
PodUpdate
{
Pods
:
pods
,
Op
:
kubetypes
.
SET
,
Source
:
kubetypes
.
ApiserverSource
}
}
}
cache
.
NewReflector
(
lw
,
&
v1
.
Pod
{},
cache
.
NewUndeltaStore
(
send
,
cache
.
MetaNamespaceKeyFunc
),
0
)
.
Run
()
r
:=
cache
.
NewReflector
(
lw
,
&
v1
.
Pod
{},
cache
.
NewUndeltaStore
(
send
,
cache
.
MetaNamespaceKeyFunc
),
0
)
go
r
.
Run
(
wait
.
NeverStop
)
}
}
pkg/kubelet/kubelet.go
View file @
d7896159
...
@@ -392,7 +392,8 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub
...
@@ -392,7 +392,8 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub
serviceIndexer
:=
cache
.
NewIndexer
(
cache
.
MetaNamespaceKeyFunc
,
cache
.
Indexers
{
cache
.
NamespaceIndex
:
cache
.
MetaNamespaceIndexFunc
})
serviceIndexer
:=
cache
.
NewIndexer
(
cache
.
MetaNamespaceKeyFunc
,
cache
.
Indexers
{
cache
.
NamespaceIndex
:
cache
.
MetaNamespaceIndexFunc
})
if
kubeDeps
.
KubeClient
!=
nil
{
if
kubeDeps
.
KubeClient
!=
nil
{
serviceLW
:=
cache
.
NewListWatchFromClient
(
kubeDeps
.
KubeClient
.
Core
()
.
RESTClient
(),
"services"
,
metav1
.
NamespaceAll
,
fields
.
Everything
())
serviceLW
:=
cache
.
NewListWatchFromClient
(
kubeDeps
.
KubeClient
.
Core
()
.
RESTClient
(),
"services"
,
metav1
.
NamespaceAll
,
fields
.
Everything
())
cache
.
NewReflector
(
serviceLW
,
&
v1
.
Service
{},
serviceIndexer
,
0
)
.
Run
()
r
:=
cache
.
NewReflector
(
serviceLW
,
&
v1
.
Service
{},
serviceIndexer
,
0
)
go
r
.
Run
(
wait
.
NeverStop
)
}
}
serviceLister
:=
corelisters
.
NewServiceLister
(
serviceIndexer
)
serviceLister
:=
corelisters
.
NewServiceLister
(
serviceIndexer
)
...
@@ -400,7 +401,8 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub
...
@@ -400,7 +401,8 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub
if
kubeDeps
.
KubeClient
!=
nil
{
if
kubeDeps
.
KubeClient
!=
nil
{
fieldSelector
:=
fields
.
Set
{
api
.
ObjectNameField
:
string
(
nodeName
)}
.
AsSelector
()
fieldSelector
:=
fields
.
Set
{
api
.
ObjectNameField
:
string
(
nodeName
)}
.
AsSelector
()
nodeLW
:=
cache
.
NewListWatchFromClient
(
kubeDeps
.
KubeClient
.
Core
()
.
RESTClient
(),
"nodes"
,
metav1
.
NamespaceAll
,
fieldSelector
)
nodeLW
:=
cache
.
NewListWatchFromClient
(
kubeDeps
.
KubeClient
.
Core
()
.
RESTClient
(),
"nodes"
,
metav1
.
NamespaceAll
,
fieldSelector
)
cache
.
NewReflector
(
nodeLW
,
&
v1
.
Node
{},
nodeIndexer
,
0
)
.
Run
()
r
:=
cache
.
NewReflector
(
nodeLW
,
&
v1
.
Node
{},
nodeIndexer
,
0
)
go
r
.
Run
(
wait
.
NeverStop
)
}
}
nodeInfo
:=
&
predicates
.
CachedNodeInfo
{
NodeLister
:
corelisters
.
NewNodeLister
(
nodeIndexer
)}
nodeInfo
:=
&
predicates
.
CachedNodeInfo
{
NodeLister
:
corelisters
.
NewNodeLister
(
nodeIndexer
)}
...
...
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
View file @
d7896159
...
@@ -19,6 +19,7 @@ package wait
...
@@ -19,6 +19,7 @@ package wait
import
(
import
(
"errors"
"errors"
"math/rand"
"math/rand"
"sync"
"time"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/runtime"
...
@@ -36,6 +37,15 @@ var ForeverTestTimeout = time.Second * 30
...
@@ -36,6 +37,15 @@ var ForeverTestTimeout = time.Second * 30
// NeverStop may be passed to Until to make it never stop.
// NeverStop may be passed to Until to make it never stop.
var
NeverStop
<-
chan
struct
{}
=
make
(
chan
struct
{})
var
NeverStop
<-
chan
struct
{}
=
make
(
chan
struct
{})
// StartUntil starts f in a new goroutine and calls done once f has finished.
func
StartUntil
(
stopCh
<-
chan
struct
{},
wg
*
sync
.
WaitGroup
,
f
func
(
stopCh
<-
chan
struct
{}))
{
wg
.
Add
(
1
)
go
func
()
{
defer
wg
.
Done
()
f
(
stopCh
)
}()
}
// Forever calls f every period for ever.
// Forever calls f every period for ever.
//
//
// Forever is syntactic sugar on top of Until.
// Forever is syntactic sugar on top of Until.
...
...
staging/src/k8s.io/client-go/tools/cache/controller.go
View file @
d7896159
...
@@ -116,7 +116,10 @@ func (c *controller) Run(stopCh <-chan struct{}) {
...
@@ -116,7 +116,10 @@ func (c *controller) Run(stopCh <-chan struct{}) {
c
.
reflector
=
r
c
.
reflector
=
r
c
.
reflectorMutex
.
Unlock
()
c
.
reflectorMutex
.
Unlock
()
r
.
RunUntil
(
stopCh
)
var
wg
sync
.
WaitGroup
defer
wg
.
Wait
()
wait
.
StartUntil
(
stopCh
,
&
wg
,
r
.
Run
)
wait
.
Until
(
c
.
processLoop
,
time
.
Second
,
stopCh
)
wait
.
Until
(
c
.
processLoop
,
time
.
Second
,
stopCh
)
}
}
...
...
staging/src/k8s.io/client-go/tools/cache/mutation_detector.go
View file @
d7896159
...
@@ -79,17 +79,15 @@ type cacheObj struct {
...
@@ -79,17 +79,15 @@ type cacheObj struct {
func
(
d
*
defaultCacheMutationDetector
)
Run
(
stopCh
<-
chan
struct
{})
{
func
(
d
*
defaultCacheMutationDetector
)
Run
(
stopCh
<-
chan
struct
{})
{
// we DON'T want protection from panics. If we're running this code, we want to die
// we DON'T want protection from panics. If we're running this code, we want to die
go
func
()
{
for
{
for
{
d
.
CompareObjects
()
d
.
CompareObjects
()
select
{
select
{
case
<-
stopCh
:
case
<-
stopCh
:
return
return
case
<-
time
.
After
(
d
.
period
)
:
case
<-
time
.
After
(
d
.
period
)
:
}
}
}
}
()
}
}
}
// AddObject makes a deep copy of the object for later comparison. It only works on runtime.Object
// AddObject makes a deep copy of the object for later comparison. It only works on runtime.Object
...
...
staging/src/k8s.io/client-go/tools/cache/reflector.go
View file @
d7896159
...
@@ -182,21 +182,10 @@ func extractStackCreator() (string, int, bool) {
...
@@ -182,21 +182,10 @@ func extractStackCreator() (string, int, bool) {
}
}
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
// Run starts a watch and handles watch events. Will restart the watch if it is closed.
// Run
starts a goroutine and returns immediately
.
// Run
will exit when stopCh is closed
.
func
(
r
*
Reflector
)
Run
()
{
func
(
r
*
Reflector
)
Run
(
stopCh
<-
chan
struct
{}
)
{
glog
.
V
(
3
)
.
Infof
(
"Starting reflector %v (%s) from %s"
,
r
.
expectedType
,
r
.
resyncPeriod
,
r
.
name
)
glog
.
V
(
3
)
.
Infof
(
"Starting reflector %v (%s) from %s"
,
r
.
expectedType
,
r
.
resyncPeriod
,
r
.
name
)
go
wait
.
Until
(
func
()
{
wait
.
Until
(
func
()
{
if
err
:=
r
.
ListAndWatch
(
wait
.
NeverStop
);
err
!=
nil
{
utilruntime
.
HandleError
(
err
)
}
},
r
.
period
,
wait
.
NeverStop
)
}
// RunUntil starts a watch and handles watch events. Will restart the watch if it is closed.
// RunUntil starts a goroutine and returns immediately. It will exit when stopCh is closed.
func
(
r
*
Reflector
)
RunUntil
(
stopCh
<-
chan
struct
{})
{
glog
.
V
(
3
)
.
Infof
(
"Starting reflector %v (%s) from %s"
,
r
.
expectedType
,
r
.
resyncPeriod
,
r
.
name
)
go
wait
.
Until
(
func
()
{
if
err
:=
r
.
ListAndWatch
(
stopCh
);
err
!=
nil
{
if
err
:=
r
.
ListAndWatch
(
stopCh
);
err
!=
nil
{
utilruntime
.
HandleError
(
err
)
utilruntime
.
HandleError
(
err
)
}
}
...
...
staging/src/k8s.io/client-go/tools/cache/reflector_test.go
View file @
d7896159
...
@@ -83,7 +83,7 @@ func TestRunUntil(t *testing.T) {
...
@@ -83,7 +83,7 @@ func TestRunUntil(t *testing.T) {
return
&
v1
.
PodList
{
ListMeta
:
metav1
.
ListMeta
{
ResourceVersion
:
"1"
}},
nil
return
&
v1
.
PodList
{
ListMeta
:
metav1
.
ListMeta
{
ResourceVersion
:
"1"
}},
nil
},
},
}
}
r
.
RunUntil
(
stopCh
)
go
r
.
Run
(
stopCh
)
// Synchronously add a dummy pod into the watch channel so we
// Synchronously add a dummy pod into the watch channel so we
// know the RunUntil go routine is in the watch handler.
// know the RunUntil go routine is in the watch handler.
fw
.
Add
(
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"bar"
}})
fw
.
Add
(
&
v1
.
Pod
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"bar"
}})
...
...
staging/src/k8s.io/client-go/tools/cache/shared_informer.go
View file @
d7896159
...
@@ -147,6 +147,7 @@ type sharedIndexInformer struct {
...
@@ -147,6 +147,7 @@ type sharedIndexInformer struct {
// stopCh is the channel used to stop the main Run process. We have to track it so that
// stopCh is the channel used to stop the main Run process. We have to track it so that
// late joiners can have a proper stop
// late joiners can have a proper stop
stopCh
<-
chan
struct
{}
stopCh
<-
chan
struct
{}
wg
sync
.
WaitGroup
}
}
// dummyController hides the fact that a SharedInformer is different from a dedicated one
// dummyController hides the fact that a SharedInformer is different from a dedicated one
...
@@ -204,12 +205,14 @@ func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) {
...
@@ -204,12 +205,14 @@ func (s *sharedIndexInformer) Run(stopCh <-chan struct{}) {
s
.
controller
=
New
(
cfg
)
s
.
controller
=
New
(
cfg
)
s
.
controller
.
(
*
controller
)
.
clock
=
s
.
clock
s
.
controller
.
(
*
controller
)
.
clock
=
s
.
clock
s
.
stopCh
=
stopCh
s
.
started
=
true
s
.
started
=
true
}()
}()
s
.
stopCh
=
stopCh
defer
s
.
wg
.
Wait
()
s
.
cacheMutationDetector
.
Run
(
stopCh
)
s
.
processor
.
run
(
stopCh
)
wait
.
StartUntil
(
stopCh
,
&
s
.
wg
,
s
.
cacheMutationDetector
.
Run
)
wait
.
StartUntil
(
stopCh
,
&
s
.
wg
,
s
.
processor
.
run
)
s
.
controller
.
Run
(
stopCh
)
s
.
controller
.
Run
(
stopCh
)
}
}
...
@@ -324,8 +327,8 @@ func (s *sharedIndexInformer) AddEventHandlerWithResyncPeriod(handler ResourceEv
...
@@ -324,8 +327,8 @@ func (s *sharedIndexInformer) AddEventHandlerWithResyncPeriod(handler ResourceEv
s
.
processor
.
addListener
(
listener
)
s
.
processor
.
addListener
(
listener
)
go
listener
.
run
(
s
.
stopCh
)
wait
.
StartUntil
(
s
.
stopCh
,
&
s
.
wg
,
listener
.
run
)
go
listener
.
pop
(
s
.
stopCh
)
wait
.
StartUntil
(
s
.
stopCh
,
&
s
.
wg
,
listener
.
pop
)
items
:=
s
.
indexer
.
List
()
items
:=
s
.
indexer
.
List
()
for
i
:=
range
items
{
for
i
:=
range
items
{
...
@@ -395,13 +398,16 @@ func (p *sharedProcessor) distribute(obj interface{}, sync bool) {
...
@@ -395,13 +398,16 @@ func (p *sharedProcessor) distribute(obj interface{}, sync bool) {
}
}
func
(
p
*
sharedProcessor
)
run
(
stopCh
<-
chan
struct
{})
{
func
(
p
*
sharedProcessor
)
run
(
stopCh
<-
chan
struct
{})
{
p
.
listenersLock
.
RLock
()
var
wg
sync
.
WaitGroup
defer
p
.
listenersLock
.
RUnlock
()
func
()
{
p
.
listenersLock
.
RLock
()
for
_
,
listener
:=
range
p
.
listeners
{
defer
p
.
listenersLock
.
RUnlock
()
go
listener
.
run
(
stopCh
)
for
_
,
listener
:=
range
p
.
listeners
{
go
listener
.
pop
(
stopCh
)
wait
.
StartUntil
(
stopCh
,
&
wg
,
listener
.
run
)
}
wait
.
StartUntil
(
stopCh
,
&
wg
,
listener
.
pop
)
}
}()
wg
.
Wait
()
}
}
// shouldResync queries every listener to determine if any of them need a resync, based on each
// shouldResync queries every listener to determine if any of them need a resync, based on each
...
...
test/utils/pod_store.go
View file @
d7896159
...
@@ -51,7 +51,7 @@ func NewPodStore(c clientset.Interface, namespace string, label labels.Selector,
...
@@ -51,7 +51,7 @@ func NewPodStore(c clientset.Interface, namespace string, label labels.Selector,
store
:=
cache
.
NewStore
(
cache
.
MetaNamespaceKeyFunc
)
store
:=
cache
.
NewStore
(
cache
.
MetaNamespaceKeyFunc
)
stopCh
:=
make
(
chan
struct
{})
stopCh
:=
make
(
chan
struct
{})
reflector
:=
cache
.
NewReflector
(
lw
,
&
v1
.
Pod
{},
store
,
0
)
reflector
:=
cache
.
NewReflector
(
lw
,
&
v1
.
Pod
{},
store
,
0
)
reflector
.
RunUntil
(
stopCh
)
go
reflector
.
Run
(
stopCh
)
return
&
PodStore
{
Store
:
store
,
stopCh
:
stopCh
,
Reflector
:
reflector
}
return
&
PodStore
{
Store
:
store
,
stopCh
:
stopCh
,
Reflector
:
reflector
}
}
}
...
...
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