@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package util
17
+ package kapi
18
18
19
19
import (
20
20
"context"
@@ -29,15 +29,13 @@ import (
29
29
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
30
30
"k8s.io/apimachinery/pkg/fields"
31
31
"k8s.io/apimachinery/pkg/labels"
32
- "k8s.io/apimachinery/pkg/runtime"
33
32
"k8s.io/apimachinery/pkg/runtime/schema"
34
33
"k8s.io/apimachinery/pkg/util/wait"
35
34
"k8s.io/apimachinery/pkg/watch"
36
35
"k8s.io/client-go/kubernetes"
37
- "k8s.io/client-go/tools/cache"
38
36
"k8s.io/client-go/tools/clientcmd"
39
37
watchtools "k8s.io/client-go/tools/watch"
40
- "k8s.io/kubernetes/cmd/kubeadm/app/constants"
38
+ kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants"
41
39
"k8s.io/minikube/pkg/minikube/proxy"
42
40
)
43
41
48
46
ReasonableStartTime = time .Minute * 5
49
47
)
50
48
51
- // PodStore stores pods
52
- type PodStore struct {
53
- cache.Store
54
- stopCh chan struct {}
55
- Reflector * cache.Reflector
56
- }
57
-
58
- // List lists the pods
59
- func (s * PodStore ) List () []* core.Pod {
60
- objects := s .Store .List ()
61
- pods := make ([]* core.Pod , 0 )
62
- for _ , o := range objects {
63
- pods = append (pods , o .(* core.Pod ))
64
- }
65
- return pods
66
- }
67
-
68
- // Stop stops the pods
69
- func (s * PodStore ) Stop () {
70
- close (s .stopCh )
71
- }
72
-
73
- // GetClient gets the client from config
74
- func GetClient (kubectlContext ... string ) (kubernetes.Interface , error ) {
49
+ // Client gets the kuberentes client from default kubeconfig
50
+ func Client (kubectlContext ... string ) (kubernetes.Interface , error ) {
75
51
loadingRules := clientcmd .NewDefaultClientConfigLoadingRules ()
76
52
configOverrides := & clientcmd.ConfigOverrides {}
77
53
if kubectlContext != nil {
@@ -92,50 +68,16 @@ func GetClient(kubectlContext ...string) (kubernetes.Interface, error) {
92
68
return client , nil
93
69
}
94
70
95
- // NewPodStore creates a new PodStore
96
- func NewPodStore (c kubernetes.Interface , namespace string , label fmt.Stringer , field fmt.Stringer ) * PodStore {
97
- lw := & cache.ListWatch {
98
- ListFunc : func (options meta.ListOptions ) (runtime.Object , error ) {
99
- options .LabelSelector = label .String ()
100
- options .FieldSelector = field .String ()
101
- obj , err := c .CoreV1 ().Pods (namespace ).List (options )
102
- return runtime .Object (obj ), err
103
- },
104
- WatchFunc : func (options meta.ListOptions ) (watch.Interface , error ) {
105
- options .LabelSelector = label .String ()
106
- options .FieldSelector = field .String ()
107
- return c .CoreV1 ().Pods (namespace ).Watch (options )
108
- },
109
- }
110
- store := cache .NewStore (cache .MetaNamespaceKeyFunc )
111
- stopCh := make (chan struct {})
112
- reflector := cache .NewReflector (lw , & core.Pod {}, store , 0 )
113
- go reflector .Run (stopCh )
114
- return & PodStore {Store : store , stopCh : stopCh , Reflector : reflector }
115
- }
116
-
117
- // StartPods starts all pods
118
- func StartPods (c kubernetes.Interface , namespace string , pod core.Pod , waitForRunning bool ) error {
119
- pod .ObjectMeta .Labels ["name" ] = pod .Name
120
- if waitForRunning {
121
- label := labels .SelectorFromSet (labels .Set (map [string ]string {"name" : pod .Name }))
122
- err := WaitForPodsWithLabelRunning (c , namespace , label )
123
- if err != nil {
124
- return fmt .Errorf ("error waiting for pod %s to be running: %v" , pod .Name , err )
125
- }
126
- }
127
- return nil
128
- }
129
-
130
71
// WaitForPodsWithLabelRunning waits for all matching pods to become Running and at least one matching pod exists.
131
- func WaitForPodsWithLabelRunning (c kubernetes.Interface , ns string , label labels.Selector ) error {
72
+ func WaitForPodsWithLabelRunning (c kubernetes.Interface , ns string , label labels.Selector , timeOut ... time.Duration ) error {
73
+ start := time .Now ()
132
74
glog .Infof ("Waiting for pod with label %q in ns %q ..." , ns , label )
133
75
lastKnownPodNumber := - 1
134
- return wait . PollImmediate ( constants . APICallRetryInterval , ReasonableStartTime , func () (bool , error ) {
76
+ f := func () (bool , error ) {
135
77
listOpts := meta.ListOptions {LabelSelector : label .String ()}
136
78
pods , err := c .CoreV1 ().Pods (ns ).List (listOpts )
137
79
if err != nil {
138
- glog .Infof ("error getting Pods with label selector %q [%v]\n " , label .String (), err )
80
+ glog .Infof ("temporary error: getting Pods with label selector %q : [%v]\n " , label .String (), err )
139
81
return false , nil
140
82
}
141
83
@@ -150,45 +92,23 @@ func WaitForPodsWithLabelRunning(c kubernetes.Interface, ns string, label labels
150
92
151
93
for _ , pod := range pods .Items {
152
94
if pod .Status .Phase != core .PodRunning {
95
+ glog .Infof ("waiting for pod %q, current state: %s: [%v]\n " , label .String (), pod .Status .Phase , err )
153
96
return false , nil
154
97
}
155
98
}
156
99
157
100
return true , nil
158
- })
159
- }
160
-
161
- // WaitForPodDelete waits for a pod to be deleted
162
- func WaitForPodDelete (c kubernetes.Interface , ns string , label fmt.Stringer ) error {
163
- return wait .PollImmediate (constants .APICallRetryInterval , ReasonableMutateTime , func () (bool , error ) {
164
- listOpts := meta.ListOptions {LabelSelector : label .String ()}
165
- pods , err := c .CoreV1 ().Pods (ns ).List (listOpts )
166
- if err != nil {
167
- glog .Infof ("error getting Pods with label selector %q [%v]\n " , label .String (), err )
168
- return false , nil
169
- }
170
- return len (pods .Items ) == 0 , nil
171
- })
172
- }
173
-
174
- // WaitForEvent waits for the given event to appear
175
- func WaitForEvent (c kubernetes.Interface , ns string , reason string ) error {
176
- return wait .PollImmediate (constants .APICallRetryInterval , ReasonableMutateTime , func () (bool , error ) {
177
- events , err := c .EventsV1beta1 ().Events ("default" ).List (meta.ListOptions {})
178
- if err != nil {
179
- glog .Infof ("error getting events: %v" , err )
180
- return false , nil
181
- }
182
- for _ , e := range events .Items {
183
- if e .Reason == reason {
184
- return true , nil
185
- }
186
- }
187
- return false , nil
188
- })
101
+ }
102
+ t := ReasonableStartTime
103
+ if timeOut != nil {
104
+ t = timeOut [0 ]
105
+ }
106
+ err := wait .PollImmediate (kconst .APICallRetryInterval , t , f )
107
+ glog .Infof ("duration metric: took %s to wait for %s ..." , time .Since (start ), label )
108
+ return err
189
109
}
190
110
191
- // WaitForRCToStabilize waits till the RC has a matching generation/replica count between spec and status.
111
+ // WaitForRCToStabilize waits till the RC has a matching generation/replica count between spec and status. used by integration tests
192
112
func WaitForRCToStabilize (c kubernetes.Interface , ns , name string , timeout time.Duration ) error {
193
113
options := meta.ListOptions {FieldSelector : fields.Set {
194
114
"metadata.name" : name ,
@@ -222,7 +142,7 @@ func WaitForRCToStabilize(c kubernetes.Interface, ns, name string, timeout time.
222
142
return err
223
143
}
224
144
225
- // WaitForDeploymentToStabilize waits till the Deployment has a matching generation/replica count between spec and status.
145
+ // WaitForDeploymentToStabilize waits till the Deployment has a matching generation/replica count between spec and status. used by integrationt tests
226
146
func WaitForDeploymentToStabilize (c kubernetes.Interface , ns , name string , timeout time.Duration ) error {
227
147
options := meta.ListOptions {FieldSelector : fields.Set {
228
148
"metadata.name" : name ,
@@ -281,32 +201,6 @@ func WaitForService(c kubernetes.Interface, namespace, name string, exist bool,
281
201
return nil
282
202
}
283
203
284
- // WaitForServiceEndpointsNum waits until the amount of endpoints that implement service to expectNum.
285
- func WaitForServiceEndpointsNum (c kubernetes.Interface , namespace , serviceName string , expectNum int , interval , timeout time.Duration ) error {
286
- return wait .Poll (interval , timeout , func () (bool , error ) {
287
- glog .Infof ("Waiting for amount of service:%s endpoints to be %d" , serviceName , expectNum )
288
- list , err := c .CoreV1 ().Endpoints (namespace ).List (meta.ListOptions {})
289
- if err != nil {
290
- return false , err
291
- }
292
-
293
- for _ , e := range list .Items {
294
- if e .Name == serviceName && countEndpointsNum (& e ) == expectNum {
295
- return true , nil
296
- }
297
- }
298
- return false , nil
299
- })
300
- }
301
-
302
- func countEndpointsNum (e * core.Endpoints ) int {
303
- num := 0
304
- for _ , sub := range e .Subsets {
305
- num += len (sub .Addresses )
306
- }
307
- return num
308
- }
309
-
310
204
// IsRetryableAPIError returns if this error is retryable or not
311
205
func IsRetryableAPIError (err error ) bool {
312
206
return apierr .IsTimeout (err ) || apierr .IsServerTimeout (err ) || apierr .IsTooManyRequests (err ) || apierr .IsInternalError (err )
0 commit comments