@@ -19,6 +19,7 @@ import (
19
19
ctrl "sigs.k8s.io/controller-runtime"
20
20
logf "sigs.k8s.io/controller-runtime/pkg/log"
21
21
"sort"
22
+ "strconv"
22
23
"strings"
23
24
"time"
24
25
)
@@ -277,15 +278,65 @@ func checkURL(urlToGet *url.URL, fragments []string) int {
277
278
Timeout : time .Minute * 1 ,
278
279
}
279
280
280
- if resp , err = netClient .Get (urlToGet .String ()); err != nil {
281
+ urlStr := urlToGet .String ()
282
+
283
+ // Create a new request using http
284
+ req , err := http .NewRequest ("GET" , urlStr , nil )
285
+ if err != nil {
286
+ fmt .Printf (" FAILED error: %v\n " , err )
287
+ return 1
288
+ }
289
+
290
+ if strings .HasPrefix (urlStr , "https://github.com" ) {
291
+ if token , found := os .LookupEnv ("GH_TOKEN" ); found && token != "" {
292
+ // Create a Bearer string by appending string access token
293
+ var bearer = "Bearer " + token
294
+ // add authorization header to the req
295
+ req .Header .Add ("Authorization" , bearer )
296
+ fmt .Print (" (URL is GitHub, GH_TOKEN is set)" )
297
+ } else {
298
+ fmt .Print (" (URL is GitHub, but no auth token in GH_TOKEN)" )
299
+ }
300
+ }
301
+
302
+ if resp , err = netClient .Do (req ); err != nil {
281
303
fmt .Printf (" FAILED error: %v\n " , err )
282
304
return 1
283
305
}
306
+
307
+ retryCount := 10
308
+ if retryCountStr , found := os .LookupEnv ("LINK_CHECK_RETRY_COUNT" ); found {
309
+ if c , err := strconv .Atoi (retryCountStr ); err == nil {
310
+ retryCount = c
311
+ }
312
+ }
313
+
314
+ if resp .StatusCode == 429 {
315
+ for i := 0 ; i < retryCount ; i ++ {
316
+ _ = resp .Body .Close ()
317
+ fmt .Println (" received 429 status waiting for one minute" )
318
+ time .Sleep (1 * time .Minute )
319
+
320
+ if resp , err = netClient .Do (req ); err != nil {
321
+ fmt .Printf (" FAILED error: %v\n " , err )
322
+ return 1
323
+ }
324
+
325
+ if resp .StatusCode != 429 {
326
+ break
327
+ }
328
+ }
329
+ }
330
+
284
331
defer resp .Body .Close ()
285
332
286
- // Check if request was successful
333
+ // Check if the request was successful
287
334
if resp .StatusCode != 200 {
288
- fmt .Printf (" FAILED response: %d\n " , resp .StatusCode )
335
+ body := ""
336
+ if content , err = io .ReadAll (resp .Body ); err == nil {
337
+ body = string (content )
338
+ }
339
+ fmt .Printf (" FAILED response: %d\n %v\n %s\n " , resp .StatusCode , body , resp .Header )
289
340
return 1
290
341
}
291
342
0 commit comments