@@ -69,6 +69,12 @@ func NewWithDefaults(name, ns string, client k8s.Interface) *Pod {
6969 }
7070}
7171
72+ // podResult holds the result of pod status check
73+ type podResult struct {
74+ pod * corev1.Pod
75+ err error
76+ }
77+
7278// Wait wait for the pod to get up and running
7379func (p * Pod ) Wait () (* corev1.Pod , error ) {
7480 // ensure pod exists before we actually check for it
@@ -77,66 +83,62 @@ func (p *Pod) Wait() (*corev1.Pod, error) {
7783 }
7884
7985 stopC := make (chan struct {})
80- eventC := make (chan interface {}, 10 )
8186 mu := sync.Mutex {}
82- defer func () {
83- mu .Lock ()
84- close (stopC )
85- close (eventC )
86- mu .Unlock ()
87- }()
8887
89- p . watcher ( stopC , eventC , & mu )
88+ var result podResult
9089
91- var pod * corev1.Pod
92- var err error
93- for e := range eventC {
94- pod , err = checkPodStatus (e )
95- if pod != nil || err != nil {
96- break
97- }
98- }
90+ // Start watcher in a goroutine
91+ go func () {
92+ p .watcher (stopC , & result , & mu )
93+ }()
9994
100- return pod , err
95+ // Wait for stopC
96+ <- stopC
97+ return result .pod , result .err
10198}
10299
103- func (p * Pod ) watcher (stopC <- chan struct {}, eventC chan <- interface {} , mu * sync.Mutex ) {
100+ func (p * Pod ) watcher (stopC chan struct {}, result * podResult , mu * sync.Mutex ) {
104101 factory := informers .NewSharedInformerFactoryWithOptions (
105102 p .Kc , time .Second * 10 ,
106103 informers .WithNamespace (p .Ns ),
107104 informers .WithTweakListOptions (podOpts (p .Name )))
108105
106+ updatePodStatus := func (obj interface {}) {
107+ mu .Lock ()
108+ defer mu .Unlock ()
109+
110+ pod , err := checkPodStatus (obj )
111+ if pod != nil || err != nil {
112+ result .pod = pod
113+ result .err = err
114+ close (stopC )
115+ }
116+ }
117+
109118 _ , err := factory .Core ().V1 ().Pods ().Informer ().AddEventHandler (
110119 cache.ResourceEventHandlerFuncs {
111120 AddFunc : func (obj interface {}) {
112- mu .Lock ()
113- defer mu .Unlock ()
114121 select {
115122 case <- stopC :
116123 return
117124 default :
118- // default is used to avoid pseudo-random selection of multiple matching cases
119- eventC <- obj
125+ updatePodStatus (obj )
120126 }
121127 },
122128 UpdateFunc : func (_ , newObj interface {}) {
123- mu .Lock ()
124- defer mu .Unlock ()
125129 select {
126130 case <- stopC :
127131 return
128132 default :
129- eventC <- newObj
133+ updatePodStatus ( newObj )
130134 }
131135 },
132136 DeleteFunc : func (obj interface {}) {
133- mu .Lock ()
134- defer mu .Unlock ()
135137 select {
136138 case <- stopC :
137139 return
138140 default :
139- eventC <- obj
141+ updatePodStatus ( obj )
140142 }
141143 },
142144 })
0 commit comments