@@ -21,7 +21,6 @@ import (
21
21
"errors"
22
22
"fmt"
23
23
"strings"
24
- "sync"
25
24
"testing"
26
25
"time"
27
26
@@ -31,9 +30,8 @@ import (
31
30
"k8s.io/client-go/rest"
32
31
)
33
32
34
- func WaitForReady (ctx context.Context , t * testing.T , cfg * rest.Config , keepMonitoring bool ) error {
35
- t .Logf ("waiting for readiness for server at %s" , cfg .Host )
36
-
33
+ // WaitForReady waits for /livez and then /readyz to return success.
34
+ func WaitForReady (ctx context.Context , cfg * rest.Config ) error {
37
35
cfg = rest .CopyConfig (cfg )
38
36
if cfg .NegotiatedSerializer == nil {
39
37
cfg .NegotiatedSerializer = kubernetesscheme .Codecs .WithoutConversion ()
@@ -44,45 +42,53 @@ func WaitForReady(ctx context.Context, t *testing.T, cfg *rest.Config, keepMonit
44
42
return fmt .Errorf ("failed to create unversioned client: %w" , err )
45
43
}
46
44
47
- wg := sync.WaitGroup {}
48
- wg .Add (2 )
49
- for _ , endpoint := range []string {"/livez" , "/readyz" } {
50
- go func (endpoint string ) {
51
- defer wg .Done ()
52
- waitForEndpoint (ctx , t , client , endpoint )
53
- }(endpoint )
45
+ if err := waitForEndpoint (ctx , client , "/livez" ); err != nil {
46
+ return fmt .Errorf ("server at %s didn't become ready: %w" , cfg .Host , err )
54
47
}
55
- wg .Wait ()
56
- t .Logf ("server at %s is ready" , cfg .Host )
57
-
58
- if keepMonitoring {
59
- for _ , endpoint := range []string {"/livez" , "/readyz" } {
60
- go func (endpoint string ) {
61
- monitorEndpoint (ctx , t , client , endpoint )
62
- }(endpoint )
63
- }
48
+ if err := waitForEndpoint (ctx , client , "/readyz" ); err != nil {
49
+ return fmt .Errorf ("server at %s didn't become ready: %w" , cfg .Host , err )
64
50
}
51
+
65
52
return nil
66
53
}
67
54
68
- func waitForEndpoint (ctx context.Context , t * testing. T , client * rest.RESTClient , endpoint string ) {
55
+ func waitForEndpoint (ctx context.Context , client * rest.RESTClient , endpoint string ) error {
69
56
var lastError error
70
57
if err := wait .PollUntilContextTimeout (ctx , 100 * time .Millisecond , time .Minute , true , func (ctx context.Context ) (bool , error ) {
71
58
req := rest .NewRequest (client ).RequestURI (endpoint )
72
- _ , err := req .Do (ctx ).Raw ()
73
- if err != nil {
59
+ if _ , err := req .Do (ctx ).Raw (); err != nil {
74
60
lastError = fmt .Errorf ("error contacting %s: failed components: %v" , req .URL (), unreadyComponentsFromError (err ))
75
61
return false , nil
76
62
}
77
-
78
- t .Logf ("success contacting %s" , req .URL ())
79
63
return true , nil
80
64
}); err != nil && lastError != nil {
81
- t .Error (lastError )
65
+ return lastError
66
+ }
67
+ return nil
68
+ }
69
+
70
+ // MonitorEndpoints keeps watching the given endpoints and fails t on error.
71
+ func MonitorEndpoints (t * testing.T , client * rest.Config , endpoints ... string ) {
72
+ ctx , cancel := context .WithCancel (context .Background ())
73
+ t .Cleanup (cancel )
74
+ for _ , endpoint := range endpoints {
75
+ go func (endpoint string ) {
76
+ monitorEndpoint (ctx , t , client , endpoint )
77
+ }(endpoint )
82
78
}
83
79
}
84
80
85
- func monitorEndpoint (ctx context.Context , t * testing.T , client * rest.RESTClient , endpoint string ) {
81
+ func monitorEndpoint (ctx context.Context , t * testing.T , cfg * rest.Config , endpoint string ) {
82
+ cfg = rest .CopyConfig (cfg )
83
+ if cfg .NegotiatedSerializer == nil {
84
+ cfg .NegotiatedSerializer = kubernetesscheme .Codecs .WithoutConversion ()
85
+ }
86
+ client , err := rest .UnversionedRESTClientFor (cfg )
87
+ if err != nil {
88
+ t .Errorf ("failed to create unversioned client: %v" , err )
89
+ return
90
+ }
91
+
86
92
// we need a shorter deadline than the server, or else:
87
93
// timeout.go:135] post-timeout activity - time-elapsed: 23.784917ms, GET "/livez" result: Header called after Handler finished
88
94
if deadline , ok := t .Deadline (); ok {
0 commit comments